@@ -2075,10 +2075,19 @@ private static void moveNodeModifiersOffSyntheticNodes(ParseTree node) {
2075
2075
}
2076
2076
}
2077
2077
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 ,
2079
2088
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 );
2082
2091
boolean isSelfStatement ;
2083
2092
try {
2084
2093
isSelfStatement = node .getData () instanceof CFunction cf
@@ -2090,16 +2099,21 @@ private static void addSelfStatements(ParseTree root, Environment env,
2090
2099
return ;
2091
2100
}
2092
2101
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
+ }
2103
2117
}
2104
2118
addSelfStatements (node , env , envs , compilerErrors );
2105
2119
}
0 commit comments