author | jlahoda |
Thu, 28 Mar 2019 11:31:30 +0100 | |
branch | jlahoda-tree-builder |
changeset 57295 | 5497ee9d40f4 |
child 57296 | 464cc8d22d94 |
permissions | -rw-r--r-- |
57295
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
1 |
/* |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
4 |
* |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
10 |
* |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
15 |
* accompanied this code). |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
16 |
* |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
20 |
* |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
23 |
* questions. |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
24 |
*/ |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
25 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
26 |
import com.sun.source.tree.BinaryTree; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
27 |
import com.sun.source.tree.ClassTree; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
28 |
import com.sun.source.tree.CompilationUnitTree; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
29 |
import com.sun.source.tree.IdentifierTree; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
30 |
import com.sun.source.tree.LiteralTree; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
31 |
import com.sun.source.tree.PrimitiveTypeTree; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
32 |
import com.sun.source.tree.Tree; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
33 |
import com.sun.source.tree.VariableTree; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
34 |
import com.sun.source.util.TreeScanner; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
35 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
36 |
import java.util.HashSet; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
37 |
import java.util.Locale; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
38 |
import java.util.Set; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
39 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
40 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
41 |
public class CodeBuilder { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
42 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
43 |
public static String buildCodeToGenerate(Tree t, String builder) { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
44 |
StringBuilder result = new StringBuilder(); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
45 |
new TreeScanner<Void, Void>() { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
46 |
private final Set<String> usedNames = new HashSet<>(); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
47 |
private String currentBuilder = builder; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
48 |
@Override |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
49 |
public Void visitClass(ClassTree node, Void p) { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
50 |
result.append(currentBuilder() + "._class(\"" + node.getSimpleName() + "\", "); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
51 |
doScan("C", () -> super.visitClass(node, p)); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
52 |
result.append(")"); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
53 |
return null; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
54 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
55 |
@Override |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
56 |
public Void visitCompilationUnit(CompilationUnitTree node, Void p) { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
57 |
result.append(currentBuilder() + ".createCompilationUnitTree("); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
58 |
doScan("U", () -> super.visitCompilationUnit(node, p)); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
59 |
result.append(")"); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
60 |
return null; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
61 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
62 |
@Override |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
63 |
public Void visitVariable(VariableTree node, Void p) { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
64 |
result.append(currentBuilder() + ".field(\"" + node.getName() + "\", "); //XXX: field/vs local variable! |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
65 |
doScan("T", node.getType()); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
66 |
result.append(", "); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
67 |
doScan("F", () -> { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
68 |
//TODO: modifiers.... |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
69 |
if (node.getInitializer() != null) { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
70 |
result.append(currentBuilder() + ".init("); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
71 |
doScan("E", node.getInitializer()); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
72 |
result.append(")"); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
73 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
74 |
}); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
75 |
result.append(")"); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
76 |
return null; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
77 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
78 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
79 |
@Override |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
80 |
public Void visitPrimitiveType(PrimitiveTypeTree node, Void p) { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
81 |
result.append(currentBuilder() + "._" + node.getPrimitiveTypeKind().name().toLowerCase(Locale.ROOT) + "()"); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
82 |
return null; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
83 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
84 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
85 |
@Override |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
86 |
public Void visitLiteral(LiteralTree node, Void p) { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
87 |
result.append(currentBuilder() + ".literal(" + node.getValue() + ")"); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
88 |
return null; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
89 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
90 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
91 |
@Override |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
92 |
public Void visitBinary(BinaryTree node, Void p) { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
93 |
switch (node.getKind()) { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
94 |
case PLUS: |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
95 |
result.append(currentBuilder() + ".plus("); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
96 |
doScan("E", node.getLeftOperand()); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
97 |
result.append(", "); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
98 |
doScan("E", node.getRightOperand()); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
99 |
result.append(")"); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
100 |
break; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
101 |
default: throw new IllegalStateException("Not handled: " + node.getKind()); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
102 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
103 |
return null; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
104 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
105 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
106 |
@Override |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
107 |
public Void visitIdentifier(IdentifierTree node, Void p) { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
108 |
result.append(currentBuilder() + ".ident(\"" + node.getName() + "\")"); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
109 |
return null; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
110 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
111 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
112 |
private String currentBuilder() { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
113 |
if (currentBuilder != null) { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
114 |
String res = currentBuilder; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
115 |
currentBuilder = null; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
116 |
return res; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
117 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
118 |
return ""; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
119 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
120 |
private void doScan(String preferredBuilderName, Tree... subTreesToScan) { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
121 |
doScan(preferredBuilderName, () -> { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
122 |
for (Tree tree : subTreesToScan) { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
123 |
scan(tree, null); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
124 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
125 |
}); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
126 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
127 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
128 |
private void doScan(String preferredBuilderName, Runnable scan) { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
129 |
String prevBuilder = currentBuilder; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
130 |
String builder = inferBuilderName(preferredBuilderName); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
131 |
try { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
132 |
result.append(builder + " -> "); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
133 |
int len = result.length(); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
134 |
currentBuilder = builder; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
135 |
scan.run(); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
136 |
if (result.length() == len) { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
137 |
result.append("{}"); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
138 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
139 |
} finally { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
140 |
currentBuilder = prevBuilder; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
141 |
usedNames.remove(builder); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
142 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
143 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
144 |
private String inferBuilderName(String preferredBuilderName) { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
145 |
Integer idx = null; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
146 |
while (true) { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
147 |
String thisBuilder = preferredBuilderName + (idx != null ? idx : ""); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
148 |
if (usedNames.add(thisBuilder)) { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
149 |
return thisBuilder; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
150 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
151 |
idx = idx != null ? idx + 1 : 1; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
152 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
153 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
154 |
}.scan(t, null); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
155 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
156 |
return result.toString(); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
157 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
158 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
159 |
} |