/
TypeAndScopeScanner.java
138 lines (104 loc) · 3.52 KB
/
TypeAndScopeScanner.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
package play;
import org.antlr.v4.runtime.ParserRuleContext;
import play.PlayScriptParser.*;
import java.util.Stack;
/**
* 第一遍扫描:识别出所有类型(包括类和函数),以及Scope。(但函数的参数信息要等到下一个阶段才会添加进去。)
*/
public class TypeAndScopeScanner extends PlayScriptBaseListener {
private AnnotatedTree at = null;
private Stack<Scope> scopeStack = new Stack<Scope>();
public TypeAndScopeScanner(AnnotatedTree at) {
this.at = at;
}
private Scope pushScope(Scope scope, ParserRuleContext ctx) {
at.node2Scope.put(ctx, scope);
scope.ctx = ctx;
scopeStack.push(scope);
return scope;
}
//从栈中弹出scope
private void popScope() {
scopeStack.pop();
}
//在遍历树的过程中,当前的Scope
private Scope currentScope() {
if (scopeStack.size() > 0) {
return scopeStack.peek();
} else {
return null;
}
}
@Override
public void enterProg(ProgContext ctx) {
NameSpace scope = new NameSpace("", currentScope(), ctx);
at.nameSpace = scope; //scope的根
pushScope(scope, ctx);
}
@Override
public void exitProg(ProgContext ctx) {
popScope();
}
@Override
public void enterBlock(BlockContext ctx) {
//对于函数,不需要再额外建一个scope
if (!(ctx.parent instanceof FunctionBodyContext)){
BlockScope scope = new BlockScope(currentScope(), ctx);
currentScope().addSymbol(scope);
pushScope(scope, ctx);
}
}
@Override
public void exitBlock(BlockContext ctx) {
if (!(ctx.parent instanceof FunctionBodyContext)) {
popScope();
}
}
@Override
public void enterStatement(StatementContext ctx) {
//为for建立额外的Scope
if (ctx.FOR() != null) {
BlockScope scope = new BlockScope(currentScope(), ctx);
currentScope().addSymbol(scope);
pushScope(scope, ctx);
}
}
@Override
public void exitStatement(StatementContext ctx) {
//释放for语句的外层scope
if (ctx.FOR() != null) {
popScope();
}
}
@Override
public void enterFunctionDeclaration(FunctionDeclarationContext ctx) {
String idName = ctx.IDENTIFIER().getText();
//注意:目前funtion的信息并不完整,参数要等到TypeResolver.java中去确定。
Function function = new Function(idName, currentScope(), ctx);
at.types.add(function);
currentScope().addSymbol(function);
// 创建一个新的scope
pushScope(function, ctx);
}
@Override
public void exitFunctionDeclaration(FunctionDeclarationContext ctx) {
popScope();
}
@Override
public void enterClassDeclaration(ClassDeclarationContext ctx) {
// 把类的签名存到符号表中,不能跟已有符号名称冲突
String idName = ctx.IDENTIFIER().getText();
Class theClass = new Class(idName, ctx);
at.types.add(theClass);
if (at.lookupClass(currentScope(), idName) != null) {
at.log("duplicate class name:" + idName, ctx); // 只是报警,但仍然继续解析
}
currentScope().addSymbol(theClass);
// 创建一个新的scope
pushScope(theClass, ctx);
}
@Override
public void exitClassDeclaration(ClassDeclarationContext ctx) {
popScope();
}
}