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 | |
package com.sun.tools.javafx.antlr; |
27 | |
|
28 | |
import com.sun.tools.javafx.tree.JavafxTreeMaker; |
29 | |
import com.sun.tools.javac.tree.JCTree; |
30 | |
import com.sun.tools.javac.tree.JCTree.*; |
31 | |
|
32 | |
import com.sun.tools.javac.code.*; |
33 | |
import com.sun.tools.javac.util.*; |
34 | |
import com.sun.tools.javafx.tree.JFXInterpolateValue; |
35 | |
import com.sun.tools.javafx.util.MsgSym; |
36 | |
import java.util.HashMap; |
37 | |
import javax.tools.DiagnosticListener; |
38 | |
import static com.sun.tools.javac.util.ListBuffer.lb; |
39 | |
|
40 | |
import org.antlr.runtime.*; |
41 | |
import org.antlr.runtime.tree.*; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public abstract class AbstractGeneratedTreeParser extends TreeParser { |
49 | |
|
50 | |
|
51 | |
|
52 | |
protected JavafxTreeMaker F; |
53 | |
|
54 | |
|
55 | |
|
56 | |
protected Log log; |
57 | |
|
58 | |
|
59 | |
protected Source source; |
60 | |
|
61 | |
|
62 | |
protected Name.Table names; |
63 | |
|
64 | |
|
65 | |
protected boolean genEndPos; |
66 | |
|
67 | |
|
68 | |
HashMap<JCTree,Integer> endPositions; |
69 | |
|
70 | |
|
71 | |
HashMap<JCTree,String> docComments; |
72 | |
|
73 | |
|
74 | |
protected int whiteSpaceToken; |
75 | |
|
76 | |
|
77 | |
|
78 | |
protected JCErroneous errorTree; |
79 | |
|
80 | |
|
81 | |
protected void initialize(Context context) { |
82 | 397 | this.F = (JavafxTreeMaker)JavafxTreeMaker.instance(context); |
83 | 397 | this.log = Log.instance(context); |
84 | 397 | this.names = Name.Table.instance(context); |
85 | 397 | this.source = Source.instance(context); |
86 | 397 | Options options = Options.instance(context); |
87 | 397 | this.genEndPos = options.get("-Xjcov") != null || |
88 | |
context.get(DiagnosticListener.class) != null || |
89 | |
Boolean.getBoolean("JavafxModuleBuilder.debugBadPositions"); |
90 | 397 | } |
91 | |
|
92 | |
protected AbstractGeneratedTreeParser(TreeNodeStream input) { |
93 | 0 | super(input); |
94 | 0 | } |
95 | |
|
96 | |
protected AbstractGeneratedTreeParser(TreeNodeStream input, RecognizerSharedState state) { |
97 | 397 | super(input, state); |
98 | 397 | } |
99 | |
|
100 | |
public String getErrorMessage(RecognitionException e, String[] tokenNames) { |
101 | |
|
102 | 0 | String msg = null; |
103 | 0 | if (e instanceof NoViableAltException) { |
104 | 0 | NoViableAltException nvae = (NoViableAltException) e; |
105 | 0 | msg = "Trying to understand your program, I'm confused at " + getRuleInvocationStack(e, this.getClass().getName()); |
106 | |
|
107 | |
|
108 | |
|
109 | 0 | } else { |
110 | 0 | msg = super.getErrorMessage(e, tokenNames); |
111 | |
} |
112 | 0 | return msg; |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
@Override |
123 | |
public void displayRecognitionError(String[] tokenNames, RecognitionException e) { |
124 | 0 | int pos = ((CommonToken)(e.token)).getStartIndex(); |
125 | 0 | String msg = getErrorMessage(e, tokenNames); |
126 | |
|
127 | 0 | log.error(pos, MsgSym.MESSAGE_JAVAFX_GENERALERROR, msg); |
128 | 0 | } |
129 | |
|
130 | |
protected int pos(CommonTree tree) { |
131 | |
|
132 | 51550 | return ((CommonToken)tree.getToken()).getStartIndex(); |
133 | |
} |
134 | |
|
135 | |
protected List noJCTrees() { |
136 | 0 | return List.<JCTree>nil(); |
137 | |
} |
138 | |
|
139 | |
protected List<JCAnnotation> noJCAnnotations() { |
140 | 397 | return List.<JCAnnotation>nil(); |
141 | |
} |
142 | |
|
143 | |
void setDocComment(JCTree tree, CommonTree comment) { |
144 | 3216 | if (comment != null) { |
145 | 140 | if (docComments == null) |
146 | 84 | docComments = new HashMap<JCTree,String>(); |
147 | 140 | docComments.put(tree, comment.getText()); |
148 | |
} |
149 | 3216 | } |
150 | |
|
151 | |
void endPos(JCTree tree, CommonTree node) { |
152 | 36073 | int endIndex = node.getTokenStopIndex(); |
153 | 36073 | if (genEndPos && endIndex != -1) { |
154 | 706 | TokenStream src = input.getTokenStream(); |
155 | 706 | CommonToken endToken = (CommonToken)src.get(endIndex); |
156 | |
|
157 | 717 | while (endToken.getType() == whiteSpaceToken || endToken.getCharPositionInLine() == -1) { |
158 | 11 | if (--endIndex < 0) |
159 | 0 | return; |
160 | 11 | endToken = (CommonToken)src.get(endIndex); |
161 | |
} |
162 | 706 | int endPos = endToken.getStopIndex(); |
163 | 706 | endPos(tree, endPos+1); |
164 | |
} |
165 | 36073 | } |
166 | |
|
167 | |
void endPos(JCTree tree, com.sun.tools.javac.util.List<JFXInterpolateValue> list) { |
168 | 0 | if (genEndPos) { |
169 | 0 | int endLast = endPositions.get(list.last()); |
170 | 0 | endPositions.put(tree, endLast); |
171 | |
} |
172 | 0 | } |
173 | |
|
174 | |
void endPos(JCTree tree, int end) { |
175 | 13555 | if (genEndPos) |
176 | 878 | endPositions.put(tree, end); |
177 | 13555 | } |
178 | |
} |