@@ -2075,10 +2075,19 @@ private static void moveNodeModifiersOffSyntheticNodes(ParseTree node) {
20752075 }
20762076 }
20772077
2078- private static void addSelfStatements (ParseTree root , Environment env ,
2078+ /**
2079+ * Adds semicolon AST nodes after self statements within children of the given AST node (following
2080+ * {@link Function#isSelfStatement(Target, Environment, List, Set)}).
2081+ * When the self statement is not yet within an {@link __autoconcat__}, it is wrapped into one.
2082+ * @param ast - The abstract syntax tree.
2083+ * @param env - The environment.
2084+ * @param envs - The set of expected environment classes at runtime.
2085+ * @param compilerErrors - A set to put compile errors in.
2086+ */
2087+ private static void addSelfStatements (ParseTree ast , Environment env ,
20792088 Set <Class <? extends Environment .EnvironmentImpl >> envs , Set <ConfigCompileException > compilerErrors ) {
2080- for (int i = 0 ; i < root .numberOfChildren (); i ++) {
2081- ParseTree node = root .getChildAt (i );
2089+ for (int i = 0 ; i < ast .numberOfChildren (); i ++) {
2090+ ParseTree node = ast .getChildAt (i );
20822091 boolean isSelfStatement ;
20832092 try {
20842093 isSelfStatement = node .getData () instanceof CFunction cf
@@ -2090,16 +2099,21 @@ private static void addSelfStatements(ParseTree root, Environment env,
20902099 return ;
20912100 }
20922101 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 ));
2102+ if (ast .getData () instanceof CFunction cf && cf .val ().equals (Compiler .__autoconcat__ .NAME )) {
2103+
2104+ // Add semicolon to existing autoconcat.
2105+ ast .getChildren ().add (i + 1 , new ParseTree (
2106+ new CSemicolon (Target .UNKNOWN ), node .getFileOptions (), true ));
2107+ i ++; // Skip added semicolon.
2108+ } else {
2109+
2110+ // Replace node with an autoconcat that contains the node and a semicolon.
2111+ ParseTree newNode = new ParseTree (new CFunction (
2112+ Compiler .__autoconcat__ .NAME , Target .UNKNOWN ), ast .getFileOptions (), true );
2113+ newNode .addChild (node );
2114+ newNode .addChild (new ParseTree (new CSemicolon (Target .UNKNOWN ), node .getFileOptions (), true ));
2115+ ast .getChildren ().set (i , newNode );
2116+ }
21032117 }
21042118 addSelfStatements (node , env , envs , compilerErrors );
21052119 }
0 commit comments