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 static com.sun.tools.javac.util.ListBuffer.lb; |
35 | |
import com.sun.tools.javac.util.Position; |
36 | |
|
37 | |
import com.sun.tools.javafx.util.MsgSym; |
38 | |
|
39 | |
import org.antlr.runtime.*; |
40 | |
|
41 | |
import java.util.List; |
42 | |
import java.util.ArrayList; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 141604 | public abstract class Lexer extends org.antlr.runtime.Lexer { |
51 | |
|
52 | |
|
53 | |
|
54 | |
protected Log log; |
55 | 407 | protected int previousTokenType = -1; |
56 | 407 | List<Token> tokens = new ArrayList<Token>(); |
57 | 407 | private final BraceQuoteTracker NULL_BQT = new BraceQuoteTracker(null, '\'', false); |
58 | 407 | private BraceQuoteTracker quoteStack = NULL_BQT; |
59 | |
|
60 | 0 | protected Lexer(){}; |
61 | |
|
62 | |
protected Lexer (CharStream input) { |
63 | 0 | super(input); |
64 | 0 | } |
65 | |
|
66 | |
protected Lexer (CharStream input, RecognizerSharedState state) { |
67 | 407 | super(input, state); |
68 | 407 | } |
69 | |
|
70 | |
|
71 | |
|
72 | |
public void emit(Token token) { |
73 | 138897 | int ttype = token.getType(); |
74 | |
|
75 | 138897 | if (verifyPreviousType(ttype, previousTokenType)) { |
76 | 1755 | Token syntheticSemi = new CommonToken(token); |
77 | 1755 | syntheticSemi.setType(getSyntheticSemiType()); |
78 | 1755 | syntheticSemi.setText("beginning of new statement"); |
79 | 1755 | state.token = syntheticSemi; |
80 | 1755 | tokens.add(syntheticSemi); |
81 | |
|
82 | 1755 | } else { |
83 | 137142 | state.token = token; |
84 | |
} |
85 | 138897 | tokens.add(token); |
86 | |
|
87 | 138897 | if (verifyCurrentType(ttype)) { |
88 | 78910 | previousTokenType = ttype; |
89 | |
} |
90 | 138897 | } |
91 | |
|
92 | |
protected abstract int getSyntheticSemiType(); |
93 | |
|
94 | |
protected abstract boolean verifyCurrentType(int ttype); |
95 | |
|
96 | |
protected abstract boolean verifyPreviousType(int ttype, int previousTokenType); |
97 | |
|
98 | |
public Token nextToken() { |
99 | 140652 | if ( tokens.size() > 0 ) { |
100 | 2002 | return tokens.remove(0); |
101 | |
} |
102 | 138650 | super.nextToken(); |
103 | 138650 | if ( tokens.size()==0 ) { |
104 | 407 | emit(Token.EOF_TOKEN); |
105 | |
} |
106 | 138650 | return tokens.remove(0); |
107 | |
} |
108 | |
|
109 | |
void processString() { |
110 | 3470 | setText(StringLiteralProcessor.convert( log, getCharIndex(), getText() )); |
111 | 3470 | } |
112 | |
|
113 | |
void processFormatString() { |
114 | |
|
115 | 40 | StringBuilder sb = new StringBuilder(); |
116 | 40 | sb.append('"').append(getText()).append('"'); |
117 | 40 | setText(StringLiteralProcessor.convert(log, getCharIndex() + 1, sb.toString())); |
118 | 40 | } |
119 | |
|
120 | |
void processTranslationKey() { |
121 | 28 | String text = getText().substring(2); |
122 | 28 | if (text.length() > 0) { |
123 | 4 | text = StringLiteralProcessor.convert( log, getCharIndex(), text ); |
124 | |
} |
125 | 28 | setText(text); |
126 | 28 | } |
127 | |
|
128 | |
|
129 | |
protected void enterBrace(int quote, boolean nextIsPercent) { |
130 | 3798 | quoteStack.enterBrace(quote, nextIsPercent); |
131 | 3798 | } |
132 | |
|
133 | |
protected void leaveQuote() { |
134 | 724 | quoteStack.leaveQuote(); |
135 | 724 | } |
136 | |
|
137 | |
protected boolean rightBraceLikeQuote(int quote) { |
138 | 37624 | return quoteStack.rightBraceLikeQuote(quote); |
139 | |
} |
140 | |
|
141 | |
protected void leaveBrace() { |
142 | 3795 | quoteStack.leaveBrace(); |
143 | 3795 | } |
144 | |
|
145 | |
protected boolean percentIsFormat() { |
146 | 112 | return quoteStack.percentIsFormat(); |
147 | |
} |
148 | |
|
149 | |
protected void resetPercentIsFormat() { |
150 | 40 | quoteStack.resetPercentIsFormat(); |
151 | 40 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
public String getErrorMessage(RecognitionException e, String[] tokenNames) { |
156 | |
|
157 | 4 | StringBuffer mb = new StringBuffer(); |
158 | 4 | if (e instanceof NoViableAltException) { |
159 | 3 | NoViableAltException nvae = (NoViableAltException) e; |
160 | 3 | if (e.c == Token.EOF) { |
161 | 2 | mb.append("Sorry, I reached to the end of file. "); |
162 | 2 | mb.append("Perhaps you are having a mismatched " + "'" + "\"" + "' or '{'"); |
163 | |
} else { |
164 | 1 | mb.append("Sorry, " + getCharErrorDisplay(e.c)); |
165 | 1 | mb.append(" is not supported in JavaFX"); |
166 | |
} |
167 | 3 | } else if (e instanceof FailedPredicateException) { |
168 | 1 | mb.append("Sorry, I was trying to understand a " + getCharErrorDisplay(e.c) + ". "); |
169 | 1 | mb.append("Perhaps you are having a mismatched " + "'" + "\"" + "' or '{'"); |
170 | 1 | recover(e); |
171 | |
} else { |
172 | 0 | mb.append( super.getErrorMessage(e, tokenNames) ); |
173 | |
} |
174 | |
|
175 | 4 | return mb.toString(); |
176 | |
} |
177 | |
|
178 | |
|
179 | |
@Override |
180 | |
public void displayRecognitionError(String[] tokenNames, RecognitionException e) { |
181 | |
|
182 | 4 | String msg = getErrorMessage(e, tokenNames); |
183 | |
|
184 | 4 | log.error(getCharIndex(), MsgSym.MESSAGE_JAVAFX_GENERALERROR, msg); |
185 | 4 | } |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | 419 | protected class BraceQuoteTracker { |
191 | |
private int braceDepth; |
192 | |
private char quote; |
193 | |
private boolean percentIsFormat; |
194 | |
private BraceQuoteTracker next; |
195 | 1132 | private BraceQuoteTracker(BraceQuoteTracker prev, char quote, boolean percentIsFormat) { |
196 | 1132 | this.quote = quote; |
197 | 1132 | this.percentIsFormat = percentIsFormat; |
198 | 1132 | this.braceDepth = 1; |
199 | 1132 | this.next = prev; |
200 | 1132 | } |
201 | |
|
202 | |
void enterBrace(int quote, boolean percentIsFormat) { |
203 | 3798 | if (quote == 0) { |
204 | 3073 | if (quoteStack != NULL_BQT) { |
205 | 460 | ++quoteStack.braceDepth; |
206 | 460 | quoteStack.percentIsFormat = percentIsFormat; |
207 | |
} |
208 | |
} else { |
209 | 725 | quoteStack = new BraceQuoteTracker(quoteStack, (char)quote, percentIsFormat); |
210 | |
} |
211 | 3798 | } |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
char leaveBrace() { |
218 | 3795 | if (quoteStack != NULL_BQT && --quoteStack.braceDepth == 0) { |
219 | 1179 | return quoteStack.quote; |
220 | |
} |
221 | 2616 | return 0; |
222 | |
} |
223 | |
|
224 | |
boolean rightBraceLikeQuote(int quote) { |
225 | 37624 | return quoteStack != NULL_BQT && quoteStack.braceDepth == 1 && (quote == 0 || quoteStack.quote == (char)quote); |
226 | |
} |
227 | |
|
228 | |
void leaveQuote() { |
229 | 724 | assert (quoteStack != NULL_BQT && quoteStack.braceDepth == 0); |
230 | 724 | quoteStack = quoteStack.next; |
231 | 724 | } |
232 | |
|
233 | |
boolean percentIsFormat() { |
234 | 112 | return quoteStack != NULL_BQT && quoteStack.percentIsFormat; |
235 | |
} |
236 | |
|
237 | |
void resetPercentIsFormat() { |
238 | 40 | quoteStack.percentIsFormat = false; |
239 | 40 | } |
240 | |
|
241 | |
boolean inBraceQuote() { |
242 | 0 | return quoteStack != NULL_BQT; |
243 | |
} |
244 | |
} |
245 | |
} |