001 // $ANTLR 2.7.5 (20050128): "../src/csheets/core/formula/compiler/FormulaCompiler.g" -> "FormulaParser.java"$
002 package csheets.core.formula.compiler;
003 import antlr.ASTFactory;
004 import antlr.ASTPair;
005 import antlr.NoViableAltException;
006 import antlr.ParserSharedInputState;
007 import antlr.RecognitionException;
008 import antlr.Token;
009 import antlr.TokenBuffer;
010 import antlr.TokenStream;
011 import antlr.TokenStreamException;
012 import antlr.collections.AST;
013 /**
014 * A parser that generates expressions from lists of lexical tokens.
015 * @author Einar Pehrson
016 */
017 public class FormulaParser extends antlr.LLkParser implements FormulaParserTokenTypes
018 {
019
020 protected FormulaParser(TokenBuffer tokenBuf, int k) {
021 super(tokenBuf,k);
022 tokenNames = _tokenNames;
023 buildTokenTypeASTClassMap();
024 astFactory = new ASTFactory(getTokenTypeToASTClassMap());
025 }
026
027 public FormulaParser(TokenBuffer tokenBuf) {
028 this(tokenBuf,1);
029 }
030
031 protected FormulaParser(TokenStream lexer, int k) {
032 super(lexer,k);
033 tokenNames = _tokenNames;
034 buildTokenTypeASTClassMap();
035 astFactory = new ASTFactory(getTokenTypeToASTClassMap());
036 }
037
038 public FormulaParser(TokenStream lexer) {
039 this(lexer,1);
040 }
041
042 public FormulaParser(ParserSharedInputState state) {
043 super(state,1);
044 tokenNames = _tokenNames;
045 buildTokenTypeASTClassMap();
046 astFactory = new ASTFactory(getTokenTypeToASTClassMap());
047 }
048
049 /**
050 * The start rule for formula expressions.
051 */
052 public final void expression() throws RecognitionException, TokenStreamException {
053
054 returnAST = null;
055 ASTPair currentAST = new ASTPair();
056 AST expression_AST = null;
057
058 match(EQ);
059 comparison();
060 astFactory.addASTChild(currentAST, returnAST);
061 match(Token.EOF_TYPE);
062 expression_AST = (AST)currentAST.root;
063 returnAST = expression_AST;
064 }
065
066 public final void comparison() throws RecognitionException, TokenStreamException {
067
068 returnAST = null;
069 ASTPair currentAST = new ASTPair();
070 AST comparison_AST = null;
071
072 concatenation();
073 astFactory.addASTChild(currentAST, returnAST);
074 {
075 switch ( LA(1)) {
076 case EQ:
077 case NEQ:
078 case GT:
079 case LT:
080 case LTEQ:
081 case GTEQ:
082 {
083 {
084 switch ( LA(1)) {
085 case EQ:
086 {
087 AST tmp3_AST = null;
088 tmp3_AST = astFactory.create(LT(1));
089 astFactory.makeASTRoot(currentAST, tmp3_AST);
090 match(EQ);
091 break;
092 }
093 case NEQ:
094 {
095 AST tmp4_AST = null;
096 tmp4_AST = astFactory.create(LT(1));
097 astFactory.makeASTRoot(currentAST, tmp4_AST);
098 match(NEQ);
099 break;
100 }
101 case GT:
102 {
103 AST tmp5_AST = null;
104 tmp5_AST = astFactory.create(LT(1));
105 astFactory.makeASTRoot(currentAST, tmp5_AST);
106 match(GT);
107 break;
108 }
109 case LT:
110 {
111 AST tmp6_AST = null;
112 tmp6_AST = astFactory.create(LT(1));
113 astFactory.makeASTRoot(currentAST, tmp6_AST);
114 match(LT);
115 break;
116 }
117 case LTEQ:
118 {
119 AST tmp7_AST = null;
120 tmp7_AST = astFactory.create(LT(1));
121 astFactory.makeASTRoot(currentAST, tmp7_AST);
122 match(LTEQ);
123 break;
124 }
125 case GTEQ:
126 {
127 AST tmp8_AST = null;
128 tmp8_AST = astFactory.create(LT(1));
129 astFactory.makeASTRoot(currentAST, tmp8_AST);
130 match(GTEQ);
131 break;
132 }
133 default:
134 {
135 throw new NoViableAltException(LT(1), getFilename());
136 }
137 }
138 }
139 concatenation();
140 astFactory.addASTChild(currentAST, returnAST);
141 break;
142 }
143 case EOF:
144 case RPAR:
145 case SEMI:
146 {
147 break;
148 }
149 default:
150 {
151 throw new NoViableAltException(LT(1), getFilename());
152 }
153 }
154 }
155 comparison_AST = (AST)currentAST.root;
156 returnAST = comparison_AST;
157 }
158
159 public final void concatenation() throws RecognitionException, TokenStreamException {
160
161 returnAST = null;
162 ASTPair currentAST = new ASTPair();
163 AST concatenation_AST = null;
164
165 arithmetic_lowest();
166 astFactory.addASTChild(currentAST, returnAST);
167 {
168 _loop7:
169 do {
170 if ((LA(1)==AMP)) {
171 AST tmp9_AST = null;
172 tmp9_AST = astFactory.create(LT(1));
173 astFactory.makeASTRoot(currentAST, tmp9_AST);
174 match(AMP);
175 arithmetic_lowest();
176 astFactory.addASTChild(currentAST, returnAST);
177 }
178 else {
179 break _loop7;
180 }
181
182 } while (true);
183 }
184 concatenation_AST = (AST)currentAST.root;
185 returnAST = concatenation_AST;
186 }
187
188 public final void arithmetic_lowest() throws RecognitionException, TokenStreamException {
189
190 returnAST = null;
191 ASTPair currentAST = new ASTPair();
192 AST arithmetic_lowest_AST = null;
193
194 arithmetic_low();
195 astFactory.addASTChild(currentAST, returnAST);
196 {
197 _loop11:
198 do {
199 if ((LA(1)==PLUS||LA(1)==MINUS)) {
200 {
201 switch ( LA(1)) {
202 case PLUS:
203 {
204 AST tmp10_AST = null;
205 tmp10_AST = astFactory.create(LT(1));
206 astFactory.makeASTRoot(currentAST, tmp10_AST);
207 match(PLUS);
208 break;
209 }
210 case MINUS:
211 {
212 AST tmp11_AST = null;
213 tmp11_AST = astFactory.create(LT(1));
214 astFactory.makeASTRoot(currentAST, tmp11_AST);
215 match(MINUS);
216 break;
217 }
218 default:
219 {
220 throw new NoViableAltException(LT(1), getFilename());
221 }
222 }
223 }
224 arithmetic_low();
225 astFactory.addASTChild(currentAST, returnAST);
226 }
227 else {
228 break _loop11;
229 }
230
231 } while (true);
232 }
233 arithmetic_lowest_AST = (AST)currentAST.root;
234 returnAST = arithmetic_lowest_AST;
235 }
236
237 public final void arithmetic_low() throws RecognitionException, TokenStreamException {
238
239 returnAST = null;
240 ASTPair currentAST = new ASTPair();
241 AST arithmetic_low_AST = null;
242
243 arithmetic_medium();
244 astFactory.addASTChild(currentAST, returnAST);
245 {
246 _loop15:
247 do {
248 if ((LA(1)==MULTI||LA(1)==DIV)) {
249 {
250 switch ( LA(1)) {
251 case MULTI:
252 {
253 AST tmp12_AST = null;
254 tmp12_AST = astFactory.create(LT(1));
255 astFactory.makeASTRoot(currentAST, tmp12_AST);
256 match(MULTI);
257 break;
258 }
259 case DIV:
260 {
261 AST tmp13_AST = null;
262 tmp13_AST = astFactory.create(LT(1));
263 astFactory.makeASTRoot(currentAST, tmp13_AST);
264 match(DIV);
265 break;
266 }
267 default:
268 {
269 throw new NoViableAltException(LT(1), getFilename());
270 }
271 }
272 }
273 arithmetic_medium();
274 astFactory.addASTChild(currentAST, returnAST);
275 }
276 else {
277 break _loop15;
278 }
279
280 } while (true);
281 }
282 arithmetic_low_AST = (AST)currentAST.root;
283 returnAST = arithmetic_low_AST;
284 }
285
286 public final void arithmetic_medium() throws RecognitionException, TokenStreamException {
287
288 returnAST = null;
289 ASTPair currentAST = new ASTPair();
290 AST arithmetic_medium_AST = null;
291
292 arithmetic_high();
293 astFactory.addASTChild(currentAST, returnAST);
294 {
295 switch ( LA(1)) {
296 case POWER:
297 {
298 AST tmp14_AST = null;
299 tmp14_AST = astFactory.create(LT(1));
300 astFactory.makeASTRoot(currentAST, tmp14_AST);
301 match(POWER);
302 arithmetic_high();
303 astFactory.addASTChild(currentAST, returnAST);
304 break;
305 }
306 case EOF:
307 case EQ:
308 case NEQ:
309 case GT:
310 case LT:
311 case LTEQ:
312 case GTEQ:
313 case AMP:
314 case PLUS:
315 case MINUS:
316 case MULTI:
317 case DIV:
318 case RPAR:
319 case SEMI:
320 {
321 break;
322 }
323 default:
324 {
325 throw new NoViableAltException(LT(1), getFilename());
326 }
327 }
328 }
329 arithmetic_medium_AST = (AST)currentAST.root;
330 returnAST = arithmetic_medium_AST;
331 }
332
333 public final void arithmetic_high() throws RecognitionException, TokenStreamException {
334
335 returnAST = null;
336 ASTPair currentAST = new ASTPair();
337 AST arithmetic_high_AST = null;
338
339 arithmetic_highest();
340 astFactory.addASTChild(currentAST, returnAST);
341 {
342 switch ( LA(1)) {
343 case PERCENT:
344 {
345 AST tmp15_AST = null;
346 tmp15_AST = astFactory.create(LT(1));
347 astFactory.makeASTRoot(currentAST, tmp15_AST);
348 match(PERCENT);
349 break;
350 }
351 case EOF:
352 case EQ:
353 case NEQ:
354 case GT:
355 case LT:
356 case LTEQ:
357 case GTEQ:
358 case AMP:
359 case PLUS:
360 case MINUS:
361 case MULTI:
362 case DIV:
363 case POWER:
364 case RPAR:
365 case SEMI:
366 {
367 break;
368 }
369 default:
370 {
371 throw new NoViableAltException(LT(1), getFilename());
372 }
373 }
374 }
375 arithmetic_high_AST = (AST)currentAST.root;
376 returnAST = arithmetic_high_AST;
377 }
378
379 public final void arithmetic_highest() throws RecognitionException, TokenStreamException {
380
381 returnAST = null;
382 ASTPair currentAST = new ASTPair();
383 AST arithmetic_highest_AST = null;
384
385 {
386 switch ( LA(1)) {
387 case MINUS:
388 {
389 AST tmp16_AST = null;
390 tmp16_AST = astFactory.create(LT(1));
391 astFactory.makeASTRoot(currentAST, tmp16_AST);
392 match(MINUS);
393 break;
394 }
395 case LPAR:
396 case FUNCTION:
397 case CELL_REF:
398 case NAME:
399 case NUMBER:
400 case STRING:
401 {
402 break;
403 }
404 default:
405 {
406 throw new NoViableAltException(LT(1), getFilename());
407 }
408 }
409 }
410 atom();
411 astFactory.addASTChild(currentAST, returnAST);
412 arithmetic_highest_AST = (AST)currentAST.root;
413 returnAST = arithmetic_highest_AST;
414 }
415
416 public final void atom() throws RecognitionException, TokenStreamException {
417
418 returnAST = null;
419 ASTPair currentAST = new ASTPair();
420 AST atom_AST = null;
421
422 switch ( LA(1)) {
423 case FUNCTION:
424 {
425 function_call();
426 astFactory.addASTChild(currentAST, returnAST);
427 atom_AST = (AST)currentAST.root;
428 break;
429 }
430 case CELL_REF:
431 case NAME:
432 {
433 reference();
434 astFactory.addASTChild(currentAST, returnAST);
435 atom_AST = (AST)currentAST.root;
436 break;
437 }
438 case NUMBER:
439 case STRING:
440 {
441 literal();
442 astFactory.addASTChild(currentAST, returnAST);
443 atom_AST = (AST)currentAST.root;
444 break;
445 }
446 case LPAR:
447 {
448 match(LPAR);
449 comparison();
450 astFactory.addASTChild(currentAST, returnAST);
451 match(RPAR);
452 atom_AST = (AST)currentAST.root;
453 break;
454 }
455 default:
456 {
457 throw new NoViableAltException(LT(1), getFilename());
458 }
459 }
460 returnAST = atom_AST;
461 }
462
463 public final void function_call() throws RecognitionException, TokenStreamException {
464
465 returnAST = null;
466 ASTPair currentAST = new ASTPair();
467 AST function_call_AST = null;
468
469 AST tmp19_AST = null;
470 tmp19_AST = astFactory.create(LT(1));
471 astFactory.makeASTRoot(currentAST, tmp19_AST);
472 match(FUNCTION);
473 {
474 switch ( LA(1)) {
475 case MINUS:
476 case LPAR:
477 case FUNCTION:
478 case CELL_REF:
479 case NAME:
480 case NUMBER:
481 case STRING:
482 {
483 comparison();
484 astFactory.addASTChild(currentAST, returnAST);
485 {
486 _loop26:
487 do {
488 if ((LA(1)==SEMI)) {
489 match(SEMI);
490 comparison();
491 astFactory.addASTChild(currentAST, returnAST);
492 }
493 else {
494 break _loop26;
495 }
496
497 } while (true);
498 }
499 break;
500 }
501 case RPAR:
502 {
503 break;
504 }
505 default:
506 {
507 throw new NoViableAltException(LT(1), getFilename());
508 }
509 }
510 }
511 match(RPAR);
512 function_call_AST = (AST)currentAST.root;
513 returnAST = function_call_AST;
514 }
515
516 public final void reference() throws RecognitionException, TokenStreamException {
517
518 returnAST = null;
519 ASTPair currentAST = new ASTPair();
520 AST reference_AST = null;
521
522 switch ( LA(1)) {
523 case CELL_REF:
524 {
525 AST tmp22_AST = null;
526 tmp22_AST = astFactory.create(LT(1));
527 astFactory.addASTChild(currentAST, tmp22_AST);
528 match(CELL_REF);
529 {
530 switch ( LA(1)) {
531 case COLON:
532 {
533 {
534 AST tmp23_AST = null;
535 tmp23_AST = astFactory.create(LT(1));
536 astFactory.makeASTRoot(currentAST, tmp23_AST);
537 match(COLON);
538 }
539 AST tmp24_AST = null;
540 tmp24_AST = astFactory.create(LT(1));
541 astFactory.addASTChild(currentAST, tmp24_AST);
542 match(CELL_REF);
543 break;
544 }
545 case EOF:
546 case EQ:
547 case NEQ:
548 case GT:
549 case LT:
550 case LTEQ:
551 case GTEQ:
552 case AMP:
553 case PLUS:
554 case MINUS:
555 case MULTI:
556 case DIV:
557 case POWER:
558 case PERCENT:
559 case RPAR:
560 case SEMI:
561 {
562 break;
563 }
564 default:
565 {
566 throw new NoViableAltException(LT(1), getFilename());
567 }
568 }
569 }
570 reference_AST = (AST)currentAST.root;
571 break;
572 }
573 case NAME:
574 {
575 AST tmp25_AST = null;
576 tmp25_AST = astFactory.create(LT(1));
577 astFactory.addASTChild(currentAST, tmp25_AST);
578 match(NAME);
579 reference_AST = (AST)currentAST.root;
580 break;
581 }
582 default:
583 {
584 throw new NoViableAltException(LT(1), getFilename());
585 }
586 }
587 returnAST = reference_AST;
588 }
589
590 public final void literal() throws RecognitionException, TokenStreamException {
591
592 returnAST = null;
593 ASTPair currentAST = new ASTPair();
594 AST literal_AST = null;
595
596 switch ( LA(1)) {
597 case NUMBER:
598 {
599 AST tmp26_AST = null;
600 tmp26_AST = astFactory.create(LT(1));
601 astFactory.addASTChild(currentAST, tmp26_AST);
602 match(NUMBER);
603 literal_AST = (AST)currentAST.root;
604 break;
605 }
606 case STRING:
607 {
608 AST tmp27_AST = null;
609 tmp27_AST = astFactory.create(LT(1));
610 astFactory.addASTChild(currentAST, tmp27_AST);
611 match(STRING);
612 literal_AST = (AST)currentAST.root;
613 break;
614 }
615 default:
616 {
617 throw new NoViableAltException(LT(1), getFilename());
618 }
619 }
620 returnAST = literal_AST;
621 }
622
623
624 public static final String[] _tokenNames = {
625 "<0>",
626 "EOF",
627 "<2>",
628 "NULL_TREE_LOOKAHEAD",
629 "EQ",
630 "NEQ",
631 "GT",
632 "LT",
633 "LTEQ",
634 "GTEQ",
635 "AMP",
636 "PLUS",
637 "MINUS",
638 "MULTI",
639 "DIV",
640 "POWER",
641 "PERCENT",
642 "LPAR",
643 "RPAR",
644 "FUNCTION",
645 "SEMI",
646 "CELL_REF",
647 "COLON",
648 "NAME",
649 "NUMBER",
650 "STRING",
651 "LETTER",
652 "ALPHABETICAL",
653 "QUOT",
654 "DIGIT",
655 "ABS",
656 "EXCL",
657 "COMMA",
658 "WS"
659 };
660
661 protected void buildTokenTypeASTClassMap() {
662 tokenTypeToASTClassMap=null;
663 };
664
665
666 }