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