1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
package com.sun.tools.javafx.antlr; |
6 | |
|
7 | |
import com.sun.tools.javac.tree.JCTree.*; |
8 | |
|
9 | |
import org.antlr.runtime.*; |
10 | |
import org.antlr.runtime.tree.*; |
11 | |
import com.sun.tools.javac.util.*; |
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
public class JavafxSyntacticAnalysis { |
18 | |
|
19 | |
|
20 | 12 | protected static final Context.Key<JavafxSyntacticAnalysis> syntaxKey = |
21 | |
new Context.Key<JavafxSyntacticAnalysis>(); |
22 | |
|
23 | |
protected Context context; |
24 | |
|
25 | |
|
26 | |
protected Options options; |
27 | |
|
28 | |
|
29 | |
protected Name.Table names; |
30 | |
|
31 | |
|
32 | |
|
33 | |
public Log log; |
34 | |
|
35 | |
public static JavafxSyntacticAnalysis instance(Context context) { |
36 | 399 | JavafxSyntacticAnalysis instance = context.get(syntaxKey); |
37 | 399 | if (instance == null) |
38 | 399 | instance = new JavafxSyntacticAnalysis(context); |
39 | 399 | return instance; |
40 | |
} |
41 | |
|
42 | 399 | protected JavafxSyntacticAnalysis(final Context context) { |
43 | 399 | this.context = context; |
44 | 399 | context.put(syntaxKey, this); |
45 | |
|
46 | 399 | names = Name.Table.instance(context); |
47 | 399 | options = Options.instance(context); |
48 | 399 | log = Log.instance(context); |
49 | 399 | } |
50 | |
|
51 | |
public JCCompilationUnit parse(CharSequence content) { |
52 | 407 | JCCompilationUnit unit = null; |
53 | 407 | String parserChoice = options.get("parser"); |
54 | 407 | if (parserChoice == null) { |
55 | 407 | parserChoice = "v3"; |
56 | |
} |
57 | |
{ |
58 | |
try { |
59 | |
|
60 | 407 | ANTLRStringStream input = new ANTLRStringStream(content.toString()); |
61 | |
|
62 | 407 | v3Lexer lexer = new v3Lexer(context, input); |
63 | |
|
64 | 407 | CommonTokenStream tokens = new CommonTokenStream(lexer); |
65 | |
|
66 | 407 | v3Parser parser = new v3Parser(tokens); |
67 | |
|
68 | 407 | parser.initialize(context); |
69 | |
|
70 | 407 | v3Parser.module_return comReturn = parser.module(); |
71 | 407 | CommonTree comTree = (CommonTree) comReturn.getTree(); |
72 | 407 | if (errorCount() == 0) { |
73 | |
|
74 | 397 | CommonTreeNodeStream nodes = new CommonTreeNodeStream(comTree); |
75 | |
|
76 | 397 | nodes.setTokenStream(tokens); |
77 | |
|
78 | 397 | v3Walker walker = new v3Walker(nodes); |
79 | |
|
80 | 397 | walker.initialize(context); |
81 | |
|
82 | 397 | unit = walker.module(); |
83 | |
} |
84 | 407 | String treeChoice = options.get("tree"); |
85 | 407 | if (treeChoice != null) { |
86 | 0 | printTree(comTree, "---"); |
87 | |
} |
88 | 0 | } catch (Throwable thr) { |
89 | 0 | System.err.println("Error in syntactic analysis:"); |
90 | 0 | thr.printStackTrace(System.err); |
91 | 407 | } |
92 | |
} |
93 | 407 | return unit; |
94 | |
} |
95 | |
|
96 | |
|
97 | |
|
98 | |
public int errorCount() { |
99 | 407 | return log.nerrors; |
100 | |
} |
101 | |
|
102 | |
private void printTree(Tree tree, String prefix) { |
103 | 0 | CommonToken token = (CommonToken)((CommonTree)tree).getToken(); |
104 | 0 | System.out.println(prefix + tree + " (" + token.getStartIndex() + ")" + token.getLine()); |
105 | 0 | int n = tree.getChildCount(); |
106 | 0 | String nextPrefix = prefix + " "; |
107 | 0 | for (int i = 0; i < n; ++i) { |
108 | 0 | printTree(tree.getChild(i), nextPrefix); |
109 | |
} |
110 | 0 | } |
111 | |
} |
112 | |
|