Skip to content
Browse files
Fix post parse AST rewriting for single-function scripts
Fixes post parse AST rewrites not using the rewritten AST for scripts with a single root function that is rewritten.
Fixes #1219.
  • Loading branch information
Pieter12345 committed Jul 15, 2020
1 parent 7df115a commit b752e1bb70870e819da3a152d867eba174501368
Showing 1 changed file with 13 additions and 6 deletions.
@@ -1287,8 +1287,9 @@ public static ParseTree compile(TokenStream stream, Environment environment,
// Get the file options.
final FileOptions fileOptions = stream.getFileOptions();

ParseTree tree = new ParseTree(fileOptions);
final ParseTree rootNode = new ParseTree(fileOptions);
ParseTree tree = rootNode;
Stack<ParseTree> parents = new Stack<>();
* constructCount is used to determine if we need to use autoconcat when reaching a FUNC_END. The previous
@@ -1815,12 +1816,20 @@ public static ParseTree compile(TokenStream stream, Environment environment,
throw new ConfigCompileException("Mismatched curly braces", target);

// Assert that the parents stack does not have unexpected unhandled elements remaining.
assert parents.size() == 2 : "Expected exactly the root and autoconcat nodes on parents stack.";
assert parents.pop() == tree : "Mismatching stack element.";
assert parents.pop() == rootNode : "Expected the last element of the stack to be the root node.";
assert rootNode.getChildAt(0) == tree : "Expected tree to be the first child of the root node.";

// Process the AST.
Stack<List<Procedure>> procs = new Stack<>();
procs.add(new ArrayList<>());
processKeywords(tree, compilerErrors);
rewriteAutoconcats(tree, environment, envs, compilerErrors);
checkLinearComponents(tree, environment, compilerErrors);
tree = postParseRewrite(tree, environment, envs, compilerErrors);
postParseRewrite(rootNode, environment, envs, compilerErrors); // Pass rootNode since this might rewrite 'tree'.
tree = rootNode.getChildAt(0);
if(staticAnalysis != null) {
staticAnalysis.analyze(tree, environment, envs, compilerErrors);
@@ -1838,9 +1847,7 @@ public static ParseTree compile(TokenStream stream, Environment environment,
eliminateDeadCode(tree, environment, envs);
tree = parents.pop();
return tree;
return rootNode;

private static void checkLinearComponents(ParseTree tree, Environment env,

0 comments on commit b752e1b

Please sign in to comment.