author | jlahoda |
Tue, 02 Apr 2019 16:51:25 +0200 | |
branch | jlahoda-tree-builder |
changeset 57300 | c79e191854e4 |
parent 57297 | ad0be596956b |
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 |
|
57297 | 40 |
import com.sun.source.tree.BlockTree; |
41 |
import com.sun.source.tree.IfTree; |
|
57296 | 42 |
import com.sun.source.tree.MemberSelectTree; |
57297 | 43 |
import com.sun.source.tree.MethodTree; |
57296 | 44 |
import com.sun.source.tree.ParameterizedTypeTree; |
57297 | 45 |
import com.sun.source.tree.ReturnTree; |
46 |
import com.sun.source.tree.StatementTree; |
|
57295
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
47 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
48 |
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
|
49 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
50 |
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
|
51 |
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
|
52 |
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
|
53 |
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
|
54 |
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
|
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 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
|
57 |
result.append(currentBuilder() + "._class(\"" + node.getSimpleName() + "\", "); |
57296 | 58 |
doScan("C", () -> { |
59 |
// R r = scan(node.getModifiers(), p); |
|
60 |
// r = scanAndReduce(node.getTypeParameters(), p, r); |
|
61 |
if (node.getExtendsClause() != null) { |
|
62 |
result.append(currentBuilder() + ".superclass("); |
|
63 |
handleDeclaredType(node.getExtendsClause()); |
|
64 |
result.append(")"); |
|
65 |
} |
|
66 |
for (Tree impl : node.getImplementsClause()) { |
|
67 |
result.append(currentBuilder() + ".superinterface("); |
|
68 |
handleDeclaredType(impl); |
|
69 |
result.append(")"); |
|
70 |
} |
|
71 |
scan(node.getMembers(), p); |
|
72 |
}); |
|
57295
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
73 |
result.append(")"); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
74 |
return null; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
75 |
} |
57297 | 76 |
|
57295
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
77 |
@Override |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
78 |
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
|
79 |
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
|
80 |
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
|
81 |
result.append(")"); |
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 |
} |
57297 | 84 |
|
57295
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 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
|
87 |
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
|
88 |
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
|
89 |
result.append(", "); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
90 |
doScan("F", () -> { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
91 |
//TODO: modifiers.... |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
92 |
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
|
93 |
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
|
94 |
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
|
95 |
result.append(")"); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
96 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
97 |
}); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
98 |
result.append(")"); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
99 |
return null; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
100 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
101 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
102 |
@Override |
57297 | 103 |
public Void visitMethod(MethodTree node, Void p) { |
104 |
result.append(currentBuilder() + ".method(\"" + node.getName() + "\", "); |
|
105 |
doScan("T", node.getReturnType()); |
|
106 |
result.append(", "); |
|
107 |
doScan("M", () -> { |
|
108 |
//TODO: other attributes! |
|
109 |
for (VariableTree param : node.getParameters()) { |
|
110 |
result.append(currentBuilder() + ".parameter("); |
|
111 |
doScan("T", param.getType()); |
|
112 |
result.append(", "); |
|
113 |
doScan("P", () -> { |
|
114 |
result.append(currentBuilder() + ".name(\"" + param.getName() + "\")"); |
|
115 |
}); |
|
116 |
//TODO: other attributes! |
|
117 |
result.append(")"); |
|
118 |
} |
|
119 |
if (node.getBody() != null) {//TODO: test no/null body! |
|
120 |
result.append(currentBuilder() + ".body("); |
|
121 |
doScan("B", node.getBody()); |
|
122 |
result.append(")"); |
|
123 |
} |
|
124 |
}); |
|
125 |
result.append(")"); |
|
126 |
return null; |
|
127 |
} |
|
128 |
||
129 |
@Override |
|
57295
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
130 |
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
|
131 |
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
|
132 |
return null; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
133 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
134 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
135 |
@Override |
57296 | 136 |
public Void visitParameterizedType(ParameterizedTypeTree node, Void p) { |
137 |
scan(node.getType(), null); |
|
138 |
result.append(", "); |
|
139 |
doScan("A", () -> { |
|
140 |
for (Tree ta : node.getTypeArguments()) { |
|
141 |
result.append(currentBuilder() + ".type("); |
|
142 |
handleDeclaredType(ta); |
|
143 |
result.append(")"); |
|
144 |
} |
|
145 |
}); |
|
146 |
return null; |
|
147 |
} |
|
148 |
||
149 |
@Override |
|
57295
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
150 |
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
|
151 |
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
|
152 |
return null; |
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 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
155 |
@Override |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
156 |
public Void visitBinary(BinaryTree node, Void p) { |
57297 | 157 |
String methodName; |
57295
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
158 |
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
|
159 |
case PLUS: |
57297 | 160 |
methodName = "plus"; break; |
161 |
case EQUAL_TO: |
|
162 |
methodName = "equal_to"; break; |
|
57295
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
163 |
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
|
164 |
} |
57297 | 165 |
result.append(currentBuilder() + "." + methodName + "("); |
166 |
doScan("E", node.getLeftOperand()); |
|
167 |
result.append(", "); |
|
168 |
doScan("E", node.getRightOperand()); |
|
169 |
result.append(")"); |
|
57295
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
170 |
return null; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
171 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
172 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
173 |
@Override |
57296 | 174 |
public Void visitMemberSelect(MemberSelectTree node, Void p) { |
175 |
//TODO: use ident(String...) for "flat" selects |
|
176 |
result.append(currentBuilder() + ".select("); |
|
177 |
doScan("E", node.getExpression()); |
|
178 |
result.append(", \""); |
|
179 |
result.append(node.getIdentifier()); |
|
180 |
result.append("\")"); |
|
181 |
return null; |
|
182 |
} |
|
183 |
||
184 |
@Override |
|
57295
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
185 |
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
|
186 |
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
|
187 |
return null; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
188 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
189 |
|
57297 | 190 |
@Override |
191 |
public Void visitIf(IfTree node, Void p) { |
|
192 |
result.append(currentBuilder() + "._if("); |
|
193 |
doScan("E", node.getCondition()); |
|
194 |
result.append(", "); |
|
195 |
doScan("S", node.getThenStatement()); |
|
196 |
if (node.getElseStatement() != null) { |
|
197 |
result.append(", "); |
|
198 |
doScan("S", node.getElseStatement()); |
|
199 |
} |
|
200 |
result.append(")"); |
|
201 |
return null; |
|
202 |
} |
|
203 |
||
204 |
@Override |
|
205 |
public Void visitReturn(ReturnTree node, Void p) { |
|
206 |
result.append(currentBuilder() + "._return("); |
|
207 |
if (node.getExpression()!= null) { |
|
208 |
doScan("E", node.getExpression()); |
|
209 |
} |
|
210 |
result.append(")"); |
|
211 |
return null; |
|
212 |
} |
|
213 |
||
57296 | 214 |
private void handleDeclaredType(Tree t) { |
215 |
doScan("T", () -> { |
|
216 |
result.append(currentBuilder() + "._class("); |
|
217 |
doScan("Q", t); |
|
218 |
result.append(")"); |
|
219 |
}); |
|
220 |
} |
|
221 |
||
57295
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
222 |
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
|
223 |
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
|
224 |
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
|
225 |
currentBuilder = null; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
226 |
return res; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
227 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
228 |
return ""; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
229 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
230 |
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
|
231 |
doScan(preferredBuilderName, () -> { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
232 |
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
|
233 |
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
|
234 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
235 |
}); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
236 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
237 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
238 |
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
|
239 |
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
|
240 |
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
|
241 |
try { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
242 |
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
|
243 |
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
|
244 |
currentBuilder = builder; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
245 |
scan.run(); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
246 |
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
|
247 |
result.append("{}"); |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
248 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
249 |
} finally { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
250 |
currentBuilder = prevBuilder; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
251 |
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
|
252 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
253 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
254 |
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
|
255 |
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
|
256 |
while (true) { |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
257 |
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
|
258 |
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
|
259 |
return thisBuilder; |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
260 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
261 |
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
|
262 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
263 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
264 |
}.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
|
265 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
266 |
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
|
267 |
} |
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
268 |
|
5497ee9d40f4
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff
changeset
|
269 |
} |