diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index 23971947e..31a8111c5 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -21,6 +21,7 @@ import net.minecraft.core.BlockPos; import net.minecraft.world.entity.Entity; +import javax.annotation.Nullable; import java.util.List; public class CarpetExpression @@ -45,7 +46,7 @@ public BlockPos getOrigin() return origin; } - public CarpetExpression(Module module, String expression, CommandSourceStack source, BlockPos origin) + public CarpetExpression(@Nullable Module module, String expression, CommandSourceStack source, BlockPos origin) { this.origin = origin; this.source = source; diff --git a/src/main/java/carpet/script/Expression.java b/src/main/java/carpet/script/Expression.java index dcf4f419c..2854b87e7 100644 --- a/src/main/java/carpet/script/Expression.java +++ b/src/main/java/carpet/script/Expression.java @@ -70,6 +70,7 @@ String getCodeString() private boolean allowNewlineSubstitutions = true; private boolean allowComments = false; + @Nullable public Module module = null; public String getModuleName() @@ -83,7 +84,7 @@ public void asATextSource() allowComments = true; } - public void asAModule(Module mi) + public void asAModule(@Nullable Module mi) { module = mi; } @@ -91,6 +92,7 @@ public void asAModule(Module mi) /** * Cached AST (Abstract Syntax Tree) (root) of the expression */ + @Nullable private LazyValue ast = null; /** diff --git a/src/main/java/carpet/script/ScriptCommand.java b/src/main/java/carpet/script/ScriptCommand.java index 109e9d68d..3161f99cd 100644 --- a/src/main/java/carpet/script/ScriptCommand.java +++ b/src/main/java/carpet/script/ScriptCommand.java @@ -19,9 +19,11 @@ import org.apache.commons.lang3.StringUtils; import java.io.IOException; +import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Deque; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -561,34 +563,43 @@ private static int explain(CommandContext context, @Nullable }); } - private static void prettyPrintTokens(CommandSourceStack source, List tokens) { - //Collections.sort(tokens); - // indentations per line + private static void prettyPrintTokens(CommandSourceStack source, List tokens) + { Map indents = new HashMap<>(); for (Token token : tokens) { indents.put(token.lineno, Math.min(indents.getOrDefault(token.lineno, token.linepos), token.linepos)); } + List lines = new ArrayList<>(indents.keySet()); + Collections.sort(lines); + Deque indentsStack = new ArrayDeque<>(lines); - - int lastLine = 0; - int lastPos = 0; List elements = new ArrayList<>(); - for (Token token : tokens) + for (int i = 0; i < tokens.size(); i++) { - if (token.lineno != lastLine) + Token token = tokens.get(i); + if (!indentsStack.isEmpty() && token.lineno == indentsStack.peekFirst()) { flushElements(source, elements); - lastLine = token.lineno; - lastPos = indents.getOrDefault(token.lineno, 0); + indentsStack.pollFirst(); + // remove all elements of intendStack that don't have any more tokens + outer: while (!indentsStack.isEmpty()) + { + int nextLineNo = indentsStack.peekFirst(); + for (int j = i + 1; j < tokens.size(); j++) + { + if (tokens.get(j).lineno == nextLineNo) + { + break outer; + } + } + indentsStack.pollFirst(); + } + + int lastPos = indents.getOrDefault(token.lineno, 0); elements.add(Carpet.Messenger_compose("w " + " ".repeat(lastPos))); } - //else if (token.linepos > lastPos) - //{ - // lastPos = token.linepos; - //} elements.add(tokenToComponent(token)); - lastPos += token.surface.length() + 1; } flushElements(source, elements); } @@ -600,7 +611,8 @@ private static Component tokenToComponent(Token token) { surface = token.display; } - if (token.comment.isEmpty()) { + if (token.comment.isEmpty()) + { return Carpet.Messenger_compose(styleForToken(token) + " " + surface); } return Carpet.Messenger_compose(styleForToken(token)+"br " + surface, "^gi " + token.comment); @@ -610,21 +622,21 @@ private static String styleForToken(Token token) { return switch (token.type) { case Token.TokenType.LITERAL -> "l"; - case Token.TokenType.HEX_LITERAL -> "e"; + case Token.TokenType.HEX_LITERAL -> "m"; case Token.TokenType.CONSTANT -> "q"; case Token.TokenType.OPEN_PAREN, Token.TokenType.CLOSE_PAREN, Token.TokenType.COMMA, Token.TokenType.OPERATOR, Token.TokenType.UNARY_OPERATOR -> "w"; - case Token.TokenType.FUNCTION -> "y"; + case Token.TokenType.FUNCTION -> "d"; case Token.TokenType.VARIABLE -> "y"; case Token.TokenType.STRINGPARAM -> "c"; default -> // marker - "w"; + "f"; }; } private static void flushElements(CommandSourceStack source, List elements) { - if (elements.size() > 0) + if (!elements.isEmpty()) { Carpet.Messenger_message(source, elements.toArray(new Component[0])); elements.clear();