-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathAnalyzingParser.java
More file actions
71 lines (63 loc) · 2.31 KB
/
AnalyzingParser.java
File metadata and controls
71 lines (63 loc) · 2.31 KB
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
/**
* Copyright 2009, Google Inc. All rights reserved.
* Licensed to PSF under a Contributor Agreement.
*/
package org.python.antlr;
import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;
import org.python.antlr.ast.Name;
import org.python.antlr.base.mod;
import java.util.List;
/**
* Parser used by the indexer.
*/
public class AnalyzingParser extends BaseParser {
public static class AnalyzerTreeAdaptor extends PythonTreeAdaptor {
/**
* Make sure a parenthesized {@link Name} expr has its start/stop bounds
* set to the bounds of the identifier.
*/
@Override
public void setTokenBoundaries(Object t, Token startToken, Token stopToken) {
//XXX: should do this for all expr types, and have a prop list on Expr
//that records all enclosing paren locations for IDE use cases.
if (!(t instanceof Name)
|| startToken == null
|| stopToken == null
|| startToken.getType() != PythonParser.LPAREN
|| stopToken.getType() != PythonParser.RPAREN) {
super.setTokenBoundaries(t, startToken, stopToken);
}
}
}
public AnalyzingParser(CharStream stream, String filename, String encoding) {
super(stream, filename, encoding);
errorHandler = new RecordingErrorHandler();
}
public List<RecognitionException> getRecognitionErrors() {
return ((RecordingErrorHandler)errorHandler).errs;
}
@Override
protected PythonParser setupParser(boolean single) {
PythonParser parser = super.setupParser(single);
parser.setTreeAdaptor(new AnalyzerTreeAdaptor());
return parser;
}
public static void main(String[] args) {
CharStream in = null;
try {
in = new ANTLRFileStream(args[0]);
} catch (Exception x) {
x.printStackTrace();
}
AnalyzingParser p = new AnalyzingParser(in, args[0], "ascii");
mod ast = p.parseModule();
if (ast != null) {
System.out.println("parse result: \n" + ast.toStringTree());
} else {
System.out.println("failure: \n" + p.getRecognitionErrors());
}
}
}