svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.api / src / main / java / org / gvsig / expressionevaluator / spi / AbstractLexicalAnalyzer.java @ 44644
History | View | Annotate | Download (14.6 KB)
1 | 43983 | jjdelcerro | package org.gvsig.expressionevaluator.spi; |
---|---|---|---|
2 | |||
3 | import org.gvsig.expressionevaluator.LexicalAnalyzer; |
||
4 | import java.text.NumberFormat; |
||
5 | import java.text.ParsePosition; |
||
6 | import java.util.HashMap; |
||
7 | import java.util.Locale; |
||
8 | import java.util.Map; |
||
9 | 44139 | jjdelcerro | import java.util.Objects; |
10 | 43983 | jjdelcerro | import java.util.Stack; |
11 | 44139 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
12 | 44098 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
13 | 43983 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionSyntaxException; |
14 | 44098 | jjdelcerro | import org.gvsig.expressionevaluator.I18N; |
15 | 43983 | jjdelcerro | import org.gvsig.tools.lang.Cloneable; |
16 | |||
17 | public abstract class AbstractLexicalAnalyzer implements LexicalAnalyzer { |
||
18 | |||
19 | protected class DefaultToken implements Token { |
||
20 | |||
21 | private int type; |
||
22 | private String literal; |
||
23 | private Object value; |
||
24 | |||
25 | public DefaultToken() {
|
||
26 | } |
||
27 | |||
28 | @Override
|
||
29 | public Token clone() throws CloneNotSupportedException { |
||
30 | // We will assume that the properties of the class are immutable, so
|
||
31 | // it would suffice to call the super class.
|
||
32 | DefaultToken other = (DefaultToken) super.clone();
|
||
33 | return other;
|
||
34 | } |
||
35 | |||
36 | @Override
|
||
37 | public void set(int type, String literal) { |
||
38 | this.set(type, literal, literal);
|
||
39 | } |
||
40 | |||
41 | @Override
|
||
42 | public void set(int type, String literal, Object value) { |
||
43 | this.literal = literal;
|
||
44 | this.type = type;
|
||
45 | this.value = value;
|
||
46 | } |
||
47 | |||
48 | @Override
|
||
49 | public int getType() { |
||
50 | return type;
|
||
51 | } |
||
52 | |||
53 | @Override
|
||
54 | public Object getValue() { |
||
55 | return value;
|
||
56 | } |
||
57 | |||
58 | @Override
|
||
59 | public String getLiteral() { |
||
60 | return literal;
|
||
61 | } |
||
62 | |||
63 | 44379 | jjdelcerro | @Override
|
64 | 43983 | jjdelcerro | public void setLiteral(String literal) { |
65 | this.literal = literal;
|
||
66 | } |
||
67 | |||
68 | 44139 | jjdelcerro | @Override
|
69 | public boolean is(String... values) { |
||
70 | for (String theValue : values) { |
||
71 | if( StringUtils.isBlank(literal) ) {
|
||
72 | if( StringUtils.isBlank(theValue) ) {
|
||
73 | return true; |
||
74 | } |
||
75 | continue;
|
||
76 | } |
||
77 | if( StringUtils.isBlank(theValue) ) {
|
||
78 | continue;
|
||
79 | } |
||
80 | if( theValue.trim().equalsIgnoreCase(this.literal.trim()) ) { |
||
81 | return true; |
||
82 | } |
||
83 | } |
||
84 | return false; |
||
85 | } |
||
86 | |||
87 | @Override
|
||
88 | public String toString() { |
||
89 | return String.format("{%d,%s,%s}", this.type, Objects.toString(this.literal), Objects.toString(value)); |
||
90 | } |
||
91 | |||
92 | 43983 | jjdelcerro | } |
93 | |||
94 | protected class Buffer implements Cloneable { |
||
95 | |||
96 | StringBuilder builder;
|
||
97 | |||
98 | public Buffer() { |
||
99 | this.builder = new StringBuilder(); |
||
100 | } |
||
101 | |||
102 | @Override
|
||
103 | public Buffer clone() throws CloneNotSupportedException { |
||
104 | Buffer other = (Buffer) super.clone(); |
||
105 | other.builder = new StringBuilder(builder); |
||
106 | return other;
|
||
107 | } |
||
108 | |||
109 | public void clear() { |
||
110 | builder.delete(0, builder.length());
|
||
111 | } |
||
112 | |||
113 | public void add(char ch) { |
||
114 | builder.append(ch); |
||
115 | } |
||
116 | |||
117 | public int length() { |
||
118 | return this.builder.length(); |
||
119 | } |
||
120 | |||
121 | @Override
|
||
122 | public String toString() { |
||
123 | return this.builder.toString(); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | protected static final char EOF = 0; |
||
128 | |||
129 | private NumberFormat nf; |
||
130 | private ParsePosition nfPos; |
||
131 | private Stack<Integer> states; |
||
132 | private String source; |
||
133 | private int position; |
||
134 | 44379 | jjdelcerro | private int lineno; |
135 | private int column; |
||
136 | 43983 | jjdelcerro | |
137 | protected Buffer buffer; |
||
138 | protected Token token;
|
||
139 | protected Map<String, Integer> tokens; |
||
140 | 44139 | jjdelcerro | protected boolean useBracketsForIdentifiers; |
141 | |||
142 | 43983 | jjdelcerro | public AbstractLexicalAnalyzer(String source) { |
143 | 44139 | jjdelcerro | this.useBracketsForIdentifiers = false; |
144 | 43983 | jjdelcerro | this.position = 0; |
145 | this.source = source;
|
||
146 | this.states = new Stack<>(); |
||
147 | this.buffer = new Buffer(); |
||
148 | this.token = this.createToken(); |
||
149 | |||
150 | this.nf = NumberFormat.getInstance(Locale.UK); |
||
151 | 44210 | jjdelcerro | this.nf.setGroupingUsed(false); |
152 | |||
153 | 43983 | jjdelcerro | this.nfPos = new ParsePosition(0); |
154 | |||
155 | this.tokens = new HashMap<>(); |
||
156 | } |
||
157 | |||
158 | public AbstractLexicalAnalyzer() {
|
||
159 | this(null); |
||
160 | } |
||
161 | |||
162 | protected Token createToken() {
|
||
163 | return new DefaultToken(); |
||
164 | } |
||
165 | |||
166 | @Override
|
||
167 | public LexicalAnalyzer clone() throws CloneNotSupportedException { |
||
168 | AbstractLexicalAnalyzer other = (AbstractLexicalAnalyzer) super.clone();
|
||
169 | other.nf = NumberFormat.getInstance(Locale.UK); |
||
170 | other.nfPos = new ParsePosition(0); |
||
171 | other.buffer = buffer.clone(); |
||
172 | other.token = token.clone(); |
||
173 | other.states = new Stack<>(); |
||
174 | other.states.addAll(states); |
||
175 | other.tokens = new HashMap<>(tokens); |
||
176 | return other;
|
||
177 | } |
||
178 | |||
179 | @Override
|
||
180 | public void setSource(String source) { |
||
181 | this.source = source;
|
||
182 | this.position = 0; |
||
183 | } |
||
184 | |||
185 | @Override
|
||
186 | public String getSource() { |
||
187 | return this.source; |
||
188 | } |
||
189 | |||
190 | @Override
|
||
191 | public Token next() {
|
||
192 | return getToken();
|
||
193 | } |
||
194 | |||
195 | @Override
|
||
196 | public Token look() {
|
||
197 | push_state(); |
||
198 | try {
|
||
199 | return getToken();
|
||
200 | } finally {
|
||
201 | pop_state(); |
||
202 | } |
||
203 | } |
||
204 | |||
205 | abstract protected Token getToken(); |
||
206 | |||
207 | protected void push_state() { |
||
208 | this.states.push(position);
|
||
209 | } |
||
210 | |||
211 | protected void pop_state() { |
||
212 | position = this.states.pop();
|
||
213 | } |
||
214 | |||
215 | @Override
|
||
216 | public int getPosition() { |
||
217 | return position;
|
||
218 | } |
||
219 | 44379 | jjdelcerro | |
220 | private void calcualteLineAndColumn() { |
||
221 | final String s = this.source; |
||
222 | int max = s.length();
|
||
223 | if( max > this.position ) { |
||
224 | max = this.position;
|
||
225 | } |
||
226 | int line = 1; |
||
227 | int col = 0; |
||
228 | for (int i = 0; i < max ; i++) { |
||
229 | if( s.charAt(i)=='\n' ) { |
||
230 | line++; |
||
231 | col = 0;
|
||
232 | } |
||
233 | col++; |
||
234 | } |
||
235 | this.lineno = line;
|
||
236 | this.column = col;
|
||
237 | } |
||
238 | 43983 | jjdelcerro | |
239 | 44379 | jjdelcerro | @Override
|
240 | public int getLine() { |
||
241 | this.calcualteLineAndColumn();
|
||
242 | return lineno;
|
||
243 | } |
||
244 | |||
245 | @Override
|
||
246 | public int getColumn() { |
||
247 | this.calcualteLineAndColumn();
|
||
248 | return column;
|
||
249 | } |
||
250 | |||
251 | @Override
|
||
252 | 43983 | jjdelcerro | public boolean isEOF() { |
253 | return this.position >= this.source.length(); |
||
254 | } |
||
255 | |||
256 | protected void skipblanks() { |
||
257 | if (isEOF()) {
|
||
258 | return;
|
||
259 | } |
||
260 | char ch = getch();
|
||
261 | 44145 | jjdelcerro | while (ch != EOF && Character.isWhitespace(ch)) { |
262 | 43983 | jjdelcerro | ch = getch(); |
263 | } |
||
264 | ungetch(); |
||
265 | } |
||
266 | |||
267 | protected char lookch() { |
||
268 | if (this.position >= this.source.length()) { |
||
269 | return EOF;
|
||
270 | } |
||
271 | return this.source.charAt(this.position); |
||
272 | } |
||
273 | |||
274 | protected char getch() { |
||
275 | if (this.position >= this.source.length()) { |
||
276 | return EOF;
|
||
277 | } |
||
278 | 44379 | jjdelcerro | this.column++;
|
279 | 43983 | jjdelcerro | return this.source.charAt(this.position++); |
280 | } |
||
281 | |||
282 | protected void ungetch() { |
||
283 | this.position--;
|
||
284 | if (this.position < 0) { |
||
285 | this.position = 0; |
||
286 | } |
||
287 | 44379 | jjdelcerro | this.column--;
|
288 | if (this.column < 0) { |
||
289 | this.column = 0; |
||
290 | } |
||
291 | 43983 | jjdelcerro | } |
292 | |||
293 | protected void parseString() { |
||
294 | buffer.clear(); |
||
295 | char ch = getch();
|
||
296 | while (true) { |
||
297 | if (ch == EOF) {
|
||
298 | 44098 | jjdelcerro | throw new ExpressionSyntaxException(I18N.End_of_string_was_expected_and_end_of_source_was_found(), this); |
299 | 43983 | jjdelcerro | } |
300 | if (ch == '\'') { |
||
301 | ch = getch(); |
||
302 | if (ch == EOF) {
|
||
303 | break;
|
||
304 | } |
||
305 | if (ch != '\'') { |
||
306 | ungetch(); |
||
307 | break;
|
||
308 | } |
||
309 | } |
||
310 | buffer.add(ch); |
||
311 | ch = getch(); |
||
312 | } |
||
313 | token.set(Token.STRING_LITERAL, buffer.toString()); |
||
314 | } |
||
315 | |||
316 | 44430 | jjdelcerro | protected void parseDMSNumber() { |
317 | 44421 | jjdelcerro | int d;
|
318 | int m;
|
||
319 | int s_i;
|
||
320 | 44430 | jjdelcerro | double s_d;
|
321 | 44421 | jjdelcerro | double s;
|
322 | char ch;
|
||
323 | 44430 | jjdelcerro | Integer sign = null; |
324 | 44421 | jjdelcerro | |
325 | 44430 | jjdelcerro | skipblanks(); |
326 | ch = getch(); |
||
327 | if( ch!='@' ) { |
||
328 | throw new ExpressionSyntaxException(I18N.Wrong_special_number_start(), this); |
||
329 | } |
||
330 | |||
331 | 44421 | jjdelcerro | // Parseamos los grados
|
332 | skipblanks(); |
||
333 | ch = getch(); |
||
334 | 44430 | jjdelcerro | if( ch=='+' ) { |
335 | sign = 1;
|
||
336 | ch = getch(); |
||
337 | } else if ( ch=='-' ) { |
||
338 | sign = -1;
|
||
339 | ch = getch(); |
||
340 | } |
||
341 | |||
342 | 44421 | jjdelcerro | buffer.clear(); |
343 | if( !Character.isDigit(ch) ) { |
||
344 | throw new ExpressionSyntaxException(I18N.Expected_a_number_at_position_XpositionX(this.getPosition()), this); |
||
345 | } |
||
346 | while (true) { |
||
347 | if (ch == EOF) {
|
||
348 | throw new ExpressionSyntaxException(I18N.End_of_string_was_expected_and_end_of_source_was_found(), this); |
||
349 | } |
||
350 | if( !Character.isDigit(ch) ) { |
||
351 | break;
|
||
352 | } |
||
353 | buffer.add(ch); |
||
354 | ch = getch(); |
||
355 | } |
||
356 | if( !StringUtils.contains(" ?:", ch) ) { |
||
357 | throw new ExpressionSyntaxException(I18N.Expected_XexpectedX_and_found_XfoundX(" ", String.valueOf(ch)), this); |
||
358 | } |
||
359 | d = Integer.parseInt(buffer.toString());
|
||
360 | |||
361 | // Parseamos los minutos
|
||
362 | skipblanks(); |
||
363 | ch = getch(); |
||
364 | buffer.clear(); |
||
365 | if( !Character.isDigit(ch) ) { |
||
366 | throw new ExpressionSyntaxException(I18N.Expected_a_number_at_position_XpositionX(this.getPosition()), this); |
||
367 | } |
||
368 | while (true) { |
||
369 | if (ch == EOF) {
|
||
370 | throw new ExpressionSyntaxException(I18N.End_of_string_was_expected_and_end_of_source_was_found(), this); |
||
371 | } |
||
372 | if( !Character.isDigit(ch) ) { |
||
373 | break;
|
||
374 | } |
||
375 | buffer.add(ch); |
||
376 | ch = getch(); |
||
377 | } |
||
378 | if( !StringUtils.contains(" ':", ch) ) { |
||
379 | throw new ExpressionSyntaxException(I18N.Expected_XexpectedX_and_found_XfoundX(" ", String.valueOf(ch)), this); |
||
380 | } |
||
381 | m = Integer.parseInt(buffer.toString());
|
||
382 | |||
383 | // Parseamos la parte entera de los segundos
|
||
384 | skipblanks(); |
||
385 | ch = getch(); |
||
386 | buffer.clear(); |
||
387 | if( !Character.isDigit(ch) ) { |
||
388 | throw new ExpressionSyntaxException(I18N.Expected_a_number_at_position_XpositionX(this.getPosition()), this); |
||
389 | } |
||
390 | while (true) { |
||
391 | if (ch == EOF) {
|
||
392 | throw new ExpressionSyntaxException(I18N.End_of_string_was_expected_and_end_of_source_was_found(), this); |
||
393 | } |
||
394 | if( !Character.isDigit(ch) ) { |
||
395 | break;
|
||
396 | } |
||
397 | buffer.add(ch); |
||
398 | ch = getch(); |
||
399 | } |
||
400 | s_i = Integer.parseInt(buffer.toString());
|
||
401 | |||
402 | if( ch == '.' ) { |
||
403 | // Parseamos la parte decimal de los segundos
|
||
404 | skipblanks(); |
||
405 | ch = getch(); |
||
406 | buffer.clear(); |
||
407 | if( !Character.isDigit(ch) ) { |
||
408 | throw new ExpressionSyntaxException(I18N.Expected_a_number_at_position_XpositionX(this.getPosition()), this); |
||
409 | } |
||
410 | while (true) { |
||
411 | if (ch == EOF) {
|
||
412 | 44446 | jjdelcerro | // throw new ExpressionSyntaxException(I18N.End_of_string_was_expected_and_end_of_source_was_found(), this);
|
413 | break;
|
||
414 | 44421 | jjdelcerro | } |
415 | if( !Character.isDigit(ch) ) { |
||
416 | break;
|
||
417 | } |
||
418 | buffer.add(ch); |
||
419 | ch = getch(); |
||
420 | } |
||
421 | 44430 | jjdelcerro | String ss = buffer.toString();
|
422 | s_d = (double) Integer.parseInt(ss) / Math.pow(10, ss.length()); |
||
423 | 44421 | jjdelcerro | } else {
|
424 | s_d = 0;
|
||
425 | } |
||
426 | 44446 | jjdelcerro | if( ch!=EOF && !StringUtils.contains(" \"", ch) ) { |
427 | 44421 | jjdelcerro | throw new ExpressionSyntaxException(I18N.Expected_XexpectedX_and_found_XfoundX(" ", String.valueOf(ch)), this); |
428 | } |
||
429 | |||
430 | 44430 | jjdelcerro | s = s_i + s_d; |
431 | 44421 | jjdelcerro | |
432 | double dd = d + m / 60.0 + s / 3600.0; |
||
433 | |||
434 | 44430 | jjdelcerro | if( sign==null ) { |
435 | skipblanks(); |
||
436 | ch = getch(); |
||
437 | switch(ch) {
|
||
438 | case 'N': |
||
439 | case 'n': |
||
440 | if( dd>90 ) { |
||
441 | throw new ExpressionSyntaxException(I18N.Incorrect_value_for_latitude(dd), this); |
||
442 | } |
||
443 | sign = 1;
|
||
444 | break;
|
||
445 | |||
446 | case 'S': |
||
447 | case 's': |
||
448 | if( dd>90 ) { |
||
449 | throw new ExpressionSyntaxException(I18N.Incorrect_value_for_latitude(dd), this); |
||
450 | } |
||
451 | sign = -1;
|
||
452 | break;
|
||
453 | case 'E': |
||
454 | case 'e': |
||
455 | if( dd>180 ) { |
||
456 | throw new ExpressionSyntaxException(I18N.Incorrect_value_for_latitude(dd), this); |
||
457 | } |
||
458 | sign = 1;
|
||
459 | break;
|
||
460 | |||
461 | case 'O': |
||
462 | case 'o': |
||
463 | case 'W': |
||
464 | case 'w': |
||
465 | if( dd>180 ) { |
||
466 | throw new ExpressionSyntaxException(I18N.Incorrect_value_for_longitude(dd), this); |
||
467 | } |
||
468 | sign = -1;
|
||
469 | break;
|
||
470 | |||
471 | default:
|
||
472 | throw new ExpressionSyntaxException(I18N.Expected_XexpectedX_and_found_XfoundX("N/S/E/W", String.valueOf(ch)), this); |
||
473 | } |
||
474 | } |
||
475 | dd = dd * sign; |
||
476 | 44421 | jjdelcerro | token.set( |
477 | Token.FLOATING_POINT_LITERAL, |
||
478 | 44430 | jjdelcerro | String.format("@%s%d? %d' %f\"", sign<0? "-":"+", d,m,s) , |
479 | 44421 | jjdelcerro | dd |
480 | ); |
||
481 | } |
||
482 | |||
483 | 43983 | jjdelcerro | protected void parseNumber() { |
484 | this.nfPos.setIndex(this.position); |
||
485 | Number n = nf.parse(source, this.nfPos); |
||
486 | if (this.nfPos.getIndex() == this.position) { |
||
487 | 44098 | jjdelcerro | throw new ExpressionRuntimeException(I18N.Expected_a_number_at_position_XpositionX(this.nfPos.getIndex())); |
488 | 43983 | jjdelcerro | } |
489 | String literal = source.substring(this.position, this.nfPos.getIndex()); |
||
490 | this.position = this.nfPos.getIndex(); |
||
491 | 44139 | jjdelcerro | if( n instanceof Long ) { |
492 | long l = ((Long)n); |
||
493 | if( l>Integer.MIN_VALUE && l<Integer.MAX_VALUE ) { |
||
494 | token.set(Token.INTEGER_LITERAL, literal, (int)l);
|
||
495 | } else {
|
||
496 | token.set(Token.INTEGER_LITERAL, literal, n); |
||
497 | } |
||
498 | } else if( n instanceof Integer) { |
||
499 | 43983 | jjdelcerro | token.set(Token.INTEGER_LITERAL, literal, n); |
500 | } else {
|
||
501 | token.set(Token.FLOATING_POINT_LITERAL, literal, n); |
||
502 | } |
||
503 | } |
||
504 | 44139 | jjdelcerro | |
505 | 44379 | jjdelcerro | @Override
|
506 | 44139 | jjdelcerro | public void setUseBracketsForIdentifiers(boolean useBracketsForIdentifiers) { |
507 | this.useBracketsForIdentifiers = useBracketsForIdentifiers;
|
||
508 | } |
||
509 | |||
510 | 44379 | jjdelcerro | @Override
|
511 | 44139 | jjdelcerro | public boolean getUseBracketsForIdentifiers() { |
512 | return this.useBracketsForIdentifiers; |
||
513 | } |
||
514 | 43983 | jjdelcerro | } |