-
Notifications
You must be signed in to change notification settings - Fork 160
/
SyntaxAnalyzer.java
487 lines (392 loc) · 15.1 KB
/
SyntaxAnalyzer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package org.develnext.jphp.core.syntax;
import org.develnext.jphp.core.syntax.generators.*;
import org.develnext.jphp.core.syntax.generators.manually.BodyGenerator;
import org.develnext.jphp.core.syntax.generators.manually.SimpleExprGenerator;
import org.develnext.jphp.core.tokenizer.Tokenizer;
import org.develnext.jphp.core.tokenizer.token.CommentToken;
import org.develnext.jphp.core.tokenizer.token.Token;
import org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken;
import org.develnext.jphp.core.tokenizer.token.expr.value.ClosureStmtToken;
import org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken;
import org.develnext.jphp.core.tokenizer.token.expr.value.NameToken;
import org.develnext.jphp.core.tokenizer.token.stmt.*;
import php.runtime.env.Context;
import php.runtime.env.Environment;
import php.runtime.exceptions.ParseException;
import php.runtime.exceptions.support.ErrorType;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import static org.develnext.jphp.core.tokenizer.token.stmt.NamespaceUseStmtToken.UseType.CONSTANT;
public class SyntaxAnalyzer {
private Tokenizer tokenizer;
private List<Token> tokens;
private List<Token> tree;
private List<Generator> generators;
private final Map<Class<? extends Generator>, Generator> map = new HashMap<Class<? extends Generator>, Generator>();
private NamespaceStmtToken namespace = NamespaceStmtToken.getDefault();
private ClassStmtToken clazz;
private FunctionStmtToken function;
private Stack<FunctionStmtToken> closureStack;
private Stack<Scope> scopeStack;
private Stack<Scope> rootScopeStack = new Stack<Scope>();
//private Stack<Set<VariableExprToken>> localStack;
//private Stack<Set<VariableExprToken>> rootLocalStack = new Stack<Set<VariableExprToken>>();
private Map<String, ClassStmtToken> classes;
private List<FunctionStmtToken> functions;
private List<ConstStmtToken> constants;
private List<ClosureStmtToken> closures;
private Environment environment;
private int generatorSize = 0;
public SyntaxAnalyzer(Environment environment, Tokenizer tokenizer){
this(environment, tokenizer, null);
}
public void reset(Environment environment, Tokenizer tokenizer){
removeScope();
//removeLocalScope();
generatorSize = 0;
tokenizer.reset();
this.environment = environment;
this.tokenizer = tokenizer;
this.function = null;
classes.clear();
functions.clear();
constants.clear();
closures.clear();
closureStack.clear();
tokens.clear();
tree.clear();
//localStack.clear();
scopeStack.clear();
addScope(true);
//addLocalScope(true);
process();
}
public SyntaxAnalyzer(Environment environment, Tokenizer tokenizer, FunctionStmtToken function) {
if (tokenizer != null)
tokenizer.reset();
this.environment = environment;
this.tokenizer = tokenizer;
this.function = function;
classes = new LinkedHashMap<String, ClassStmtToken>();
functions = new ArrayList<FunctionStmtToken>();
constants = new ArrayList<ConstStmtToken>();
closures = new ArrayList<ClosureStmtToken>();
closureStack = new Stack<FunctionStmtToken>();
tokens = new LinkedList<Token>();
tree = new ArrayList<Token>();
//localStack = new Stack<Set<VariableExprToken>>();
scopeStack = new Stack<Scope>();
generators = new ArrayList<Generator>(50);
generators.add(new NamespaceGenerator(this));
generators.add(new UseGenerator(this));
generators.add(new DeclareGenerator(this));
generators.add(new ClassGenerator(this));
generators.add(new ConstGenerator(this));
generators.add(new FunctionGenerator(this));
generators.add(new LambdaGenerator(this));
generators.add(new TryCatchGenerator(this));
generators.add(new ThrowGenerator(this));
generators.add(new NameGenerator(this));
// non-automatic
generators.add(new SimpleExprGenerator(this));
generators.add(new BodyGenerator(this));
generators.add(new ExprGenerator(this));
for (Generator generator : generators)
map.put(generator.getClass(), generator);
addScope(true);
//addLocalScope(true);
if (tokenizer != null)
process();
}
public Environment getEnvironment() {
return environment;
}
public void registerClass(ClassStmtToken clazz){
classes.put(clazz.getFulledName().toLowerCase(), clazz);
for (MethodStmtToken method : clazz.getMethods()) {
if (method.isGenerator()) {
method.setGeneratorId(generatorSize++);
}
}
}
public void registerFunction(FunctionStmtToken function){
if (function.getId() <= functions.size()){
function.setId(functions.size());
if (function.isGenerator()) {
function.setGeneratorId(generatorSize++);
}
functions.add(function);
}
}
public void registerConstant(ConstStmtToken constant){
constants.add(constant);
}
public void registerClosure(ClosureStmtToken closure){
closure.setId(closures.size());
if (closure.getFunction().isGenerator()) {
closure.getFunction().setGeneratorId(generatorSize++);
}
closures.add(closure);
}
public Collection<ClassStmtToken> getClasses() {
return classes.values();
}
public Collection<FunctionStmtToken> getFunctions() {
return functions;
}
public Collection<ConstStmtToken> getConstants(){
return constants;
}
public Collection<ClosureStmtToken> getClosures(){
return closures;
}
public ClassStmtToken findClass(String name){
return classes.get(name.toLowerCase());
}
protected void process(){
tokenizer.reset();
tree.clear();
Token token;
try {
while ((token = tokenizer.nextToken()) != null) {
if (token instanceof CommentToken) {
if (((CommentToken) token).getKind() != CommentToken.Kind.DOCTYPE)
continue;
}
tokens.add(token);
}
} catch (ParseException e) {
this.getEnvironment().error(e.getTraceInfo(), ErrorType.E_PARSE, e.getMessage());
}
/*if (tokenizer.hasDirective("mode")){
Directive mode = tokenizer.getDirective("mode");
try {
this.langMode = LangMode.valueOf(mode.value.toUpperCase());
} catch (IllegalArgumentException e){
environment.warning(
mode.trace, "Invalid value '%s' for directive 'mode'", mode.value
);
}
}*/
ListIterator<Token> iterator = tokens.listIterator();
tree = process(iterator);
}
protected void registerToken(Token token){
if (token instanceof ClassStmtToken)
registerClass((ClassStmtToken)token);
else if (token instanceof FunctionStmtToken)
registerFunction((FunctionStmtToken)token);
else if (token instanceof ConstStmtToken)
registerConstant((ConstStmtToken)token);
}
public List<Token> process(ListIterator<Token> iterator){
List<Token> result = new ArrayList<Token>();
while (iterator.hasNext()){
Token gen = processNext(iterator);
if (gen instanceof NamespaceStmtToken) {
List<Token> tree = ((NamespaceStmtToken) gen).getTree();
((NamespaceStmtToken) gen).setTree(null);
result.add(gen);
registerToken(gen);
result.addAll(tree);
if (!((NamespaceStmtToken) gen).isTokenRegistered()) {
for (Token el : tree) {
registerToken(el);
}
}
} else {
result.add(gen);
registerToken(gen);
}
}
return result;
}
public Token processNext(ListIterator<Token> iterator){
Token current = iterator.next();
Token gen = generateToken(current, iterator);
return (gen == null ? current : gen);
}
public Scope addScope(boolean isRoot) {
Scope scope = new Scope(scopeStack.empty() ? null : scopeStack.peek());
scopeStack.push(scope);
if (isRoot)
rootScopeStack.push(scope);
return scope;
}
public Scope addScope(){
return addScope(false);
}
public Scope removeScope() {
Scope scope = getScope();
if (rootScopeStack.peek() == scope)
rootScopeStack.pop();
else
rootScopeStack.peek().appendScope(scope);
return scopeStack.pop();
}
public Scope getScope() {
return scopeStack.peek();
}
/*
public Set<VariableExprToken> addLocalScope(){
return addLocalScope(false);
}
public Set<VariableExprToken> addLocalScope(boolean isRoot){
Set<VariableExprToken> local = new HashSet<VariableExprToken>();
localStack.push(local);
if (isRoot)
rootLocalStack.push(local);
return local;
}
public Set<VariableExprToken> removeLocalScope(){
Set<VariableExprToken> scope = getLocalScope();
if (rootLocalStack.peek() == scope){
rootLocalStack.pop();
} else
rootLocalStack.peek().addAll(getLocalScope());
return localStack.pop();
}
public Set<VariableExprToken> getLocalScope(){
return localStack.peek();
} */
@SuppressWarnings("unchecked")
public <T extends Generator> T generator(Class<T> clazz){
Generator generator = map.get(clazz);
if (generator == null)
throw new AssertionError("Generator '"+clazz.getName()+"' not found");
if (generator.isSingleton())
return (T) generator;
Constructor<T> constructor = null;
try {
constructor = clazz.getConstructor(SyntaxAnalyzer.class);
return constructor.newInstance(this);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public Token generateToken(Token current, ListIterator<Token> iterator){
Token gen = null;
for(Generator generator : generators){
if (!generator.isAutomatic())
continue;
gen = generator.getToken(current, iterator);
if (gen != null)
break;
}
return gen;
}
public List<Token> getTree(){
return tree;
}
public Context getContext(){
return tokenizer.getContext();
}
public NamespaceStmtToken getNamespace() {
return namespace;
}
public void setNamespace(NamespaceStmtToken namespace) {
this.namespace = namespace;
}
public ClassStmtToken getClazz() {
return clazz;
}
public void setClazz(ClassStmtToken clazz) {
this.clazz = clazz;
}
public FunctionStmtToken getFunction(boolean absolute) {
if (!absolute){
FunctionStmtToken func = peekClosure();
if (func != null)
return func;
}
return function;
}
public FunctionStmtToken getFunction() {
return getFunction(false);
}
public void setFunction(FunctionStmtToken function) {
this.function = function;
}
public void pushClosure(FunctionStmtToken closure) {
closureStack.push(closure);
}
public FunctionStmtToken peekClosure() {
if (closureStack.empty())
return null;
else
return closureStack.peek();
}
public FunctionStmtToken popClosure(){
return closureStack.pop();
}
public ValueExprToken getRealName(ValueExprToken value, NamespaceUseStmtToken.UseType useType){
if (value == null)
return null;
if (value instanceof NameToken)
return getRealName((NameToken)value, useType);
else
return value;
}
public FulledNameToken getRealName(NameToken what, NamespaceUseStmtToken.UseType useType) {
return getRealName(what, namespace, useType);
}
public FulledNameToken getRealName(NameToken what) {
return getRealName(what, NamespaceUseStmtToken.UseType.CLASS);
}
public static FulledNameToken getRealName(NameToken what, NamespaceStmtToken namespace) {
return getRealName(what, namespace, NamespaceUseStmtToken.UseType.CLASS);
}
public static FulledNameToken getRealName(NameToken what, NamespaceStmtToken namespace, NamespaceUseStmtToken.UseType useType) {
if (what instanceof FulledNameToken) {
FulledNameToken fulledNameToken = (FulledNameToken) what;
if (fulledNameToken.isProcessed(useType)) {
return fulledNameToken;
} else {
if (fulledNameToken.isAnyProcessed()) {
what = (fulledNameToken).getLastName();
}
}
}
String name = what.getName();
// check name in uses
if (namespace != null) {
for (NamespaceUseStmtToken use : namespace.getUses()) {
if (use.getUseType() != useType) {
continue;
}
if (use.getAs() == null) {
String string = use.getName().getLastName().getName();
if ((useType == CONSTANT && name.equals(string)) || (useType != CONSTANT && name.equalsIgnoreCase(string))) {
FulledNameToken t = new FulledNameToken(use.getName());
t.setProcessed(useType);
return t;
}
} else {
String string = use.getAs().getName();
if ((useType == CONSTANT && name.equals(string)) || (useType != CONSTANT && name.equalsIgnoreCase(string))) {
FulledNameToken t = new FulledNameToken(use.getName());
t.setProcessed(useType);
return t;
}
}
}
}
List<NameToken> names = namespace == null || namespace.getName() == null
? new ArrayList<NameToken>()
: new ArrayList<NameToken>(namespace.getName().getNames());
if (what instanceof FulledNameToken)
names.addAll(((FulledNameToken) what).getNames());
else
names.add(what);
FulledNameToken t = new FulledNameToken(what.getMeta(), names);
t.setProcessed(useType);
return t;
}
}