@@ -1912,7 +1912,8 @@ public static ParseTree compile(TokenStream stream, Environment environment,
19121912 Stack <List <Procedure >> procs = new Stack <>();
19131913 procs .add (new ArrayList <>());
19141914 processKeywords (tree , environment , compilerErrors );
1915- addSelfStatements (tree , environment , envs , compilerErrors );
1915+ addSelfStatements (rootNode , environment , envs , compilerErrors ); // Pass rootNode since this might rewrite 'tree'.
1916+ tree = rootNode .getChildAt (0 );
19161917 rewriteAutoconcats (tree , environment , envs , compilerErrors , true );
19171918 processLateKeywords (tree , environment , compilerErrors );
19181919 checkLinearComponents (tree , environment , compilerErrors );
@@ -2075,10 +2076,19 @@ private static void moveNodeModifiersOffSyntheticNodes(ParseTree node) {
20752076 }
20762077 }
20772078
2078- private static void addSelfStatements (ParseTree root , Environment env ,
2079+ /**
2080+ * Adds semicolon AST nodes after self statements within children of the given AST node (following
2081+ * {@link Function#isSelfStatement(Target, Environment, List, Set)}).
2082+ * When the self statement is not yet within an {@link __autoconcat__}, it is wrapped into one.
2083+ * @param ast - The abstract syntax tree.
2084+ * @param env - The environment.
2085+ * @param envs - The set of expected environment classes at runtime.
2086+ * @param compilerErrors - A set to put compile errors in.
2087+ */
2088+ private static void addSelfStatements (ParseTree ast , Environment env ,
20792089 Set <Class <? extends Environment .EnvironmentImpl >> envs , Set <ConfigCompileException > compilerErrors ) {
2080- for (int i = 0 ; i < root .numberOfChildren (); i ++) {
2081- ParseTree node = root .getChildAt (i );
2090+ for (int i = 0 ; i < ast .numberOfChildren (); i ++) {
2091+ ParseTree node = ast .getChildAt (i );
20822092 boolean isSelfStatement ;
20832093 try {
20842094 isSelfStatement = node .getData () instanceof CFunction cf
@@ -2090,16 +2100,21 @@ private static void addSelfStatements(ParseTree root, Environment env,
20902100 return ;
20912101 }
20922102 if (isSelfStatement ) {
2093- int offset = i + 1 ;
2094- if (!(root .getData () instanceof CFunction cf && cf .val ().equals (Compiler .__autoconcat__ .NAME ))) {
2095- // We need to create an autoconcat node first, and put this and the semicolon in that
2096- ParseTree newNode = new ParseTree (new CFunction (Compiler .__autoconcat__ .NAME , Target .UNKNOWN ), root .getFileOptions (), true );
2097- newNode .addChild (root );
2098- root = newNode ;
2099- offset = 1 ;
2100- }
2101- root .getChildren ().add (offset , new ParseTree (new CSemicolon (Target .UNKNOWN ),
2102- node .getFileOptions (), true ));
2103+ if (ast .getData () instanceof CFunction cf && cf .val ().equals (Compiler .__autoconcat__ .NAME )) {
2104+
2105+ // Add semicolon to existing autoconcat.
2106+ ast .getChildren ().add (i + 1 , new ParseTree (
2107+ new CSemicolon (Target .UNKNOWN ), node .getFileOptions (), true ));
2108+ i ++; // Skip added semicolon.
2109+ } else {
2110+
2111+ // Replace node with an autoconcat that contains the node and a semicolon.
2112+ ParseTree newNode = new ParseTree (new CFunction (
2113+ Compiler .__autoconcat__ .NAME , Target .UNKNOWN ), ast .getFileOptions (), true );
2114+ newNode .addChild (node );
2115+ newNode .addChild (new ParseTree (new CSemicolon (Target .UNKNOWN ), node .getFileOptions (), true ));
2116+ ast .getChildren ().set (i , newNode );
2117+ }
21032118 }
21042119 addSelfStatements (node , env , envs , compilerErrors );
21052120 }
0 commit comments