我猜你正在使用this grammar。
虽然该语法表明创建了正确的 AST,但事实并非如此。它使用一些内联运算符从解析树中排除某些标记,但它从不为树创建任何根,从而产生一个完全平坦的解析树。由此,您无法以合理的方式获取所有全局变量。
你需要稍微调整一下语法:
在语法文件顶部的options { ... }下添加以下内容:
tokens
{
VARIABLE;
FUNCTION;
}
现在将以下规则替换为:functionDeclaration、functionExpression 和 variableDeclaration:
functionDeclaration
: 'function' LT* Identifier LT* formalParameterList LT* functionBody
-> ^(FUNCTION Identifier formalParameterList functionBody)
;
functionExpression
: 'function' LT* Identifier? LT* formalParameterList LT* functionBody
-> ^(FUNCTION Identifier? formalParameterList functionBody)
;
variableDeclaration
: Identifier LT* initialiser?
-> ^(VARIABLE Identifier initialiser?)
;
现在生成了更合适的树。如果你现在解析源:
var a = 1; function foo() { var b = 2; } var c = 3;
生成以下树:
您现在所要做的就是遍历树根的子节点,当您偶然发现VARIABLE 令牌时,您就知道它是“全局”的,因为所有其他变量都将位于FUNCTION 节点下。
以下是如何做到这一点:
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
public class Main {
public static void main(String[] args) throws Exception {
String source = "var a = 1; function foo() { var b = 2; } var c = 3;";
ANTLRStringStream in = new ANTLRStringStream(source);
JavaScriptLexer lexer = new JavaScriptLexer(in);
CommonTokenStream tokens = new CommonTokenStream(lexer);
JavaScriptParser parser = new JavaScriptParser(tokens);
JavaScriptParser.program_return returnValue = parser.program();
CommonTree tree = (CommonTree)returnValue.getTree();
for(Object o : tree.getChildren()) {
CommonTree child = (CommonTree)o;
if(child.getType() == JavaScriptParser.VARIABLE) {
System.out.println("Found a global var: "+child.getChild(0));
}
}
}
}
产生以下输出:
Found a global var: a
Found a global var: c