1/* 2 [The "BSD license"] 3 Copyright (c) 2005-2009 Gokulakannan Somasundaram, 4 5 All rights reserved. 6 7 Redistribution and use in source and binary forms, with or without 8 modification, are permitted provided that the following conditions 9 are met: 10 1. Redistributions of source code must retain the above copyright 11 notice, this list of conditions and the following disclaimer. 12 2. Redistributions in binary form must reproduce the above copyright 13 notice, this list of conditions and the following disclaimer in the 14 documentation and/or other materials provided with the distribution. 15 3. The name of the author may not be used to endorse or promote products 16 derived from this software without specific prior written permission. 17 18 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28*/ 29 30/* 31 * This code generating template and the associated Cpp runtime was produced by: 32 * Gokulakannan Somasundaram ( heavy lifting from C Run-time by Jim Idle ) 33 */ 34cTypeInitMap ::= [ 35 "int" : "0", // Integers start out being 0 36 "long" : "0", // Longs start out being 0 37 "float" : "0.0", // Floats start out being 0 38 "double" : "0.0", // Doubles start out being 0 39 "bool" : "false", // Booleans start out being Antlr C for false 40 "byte" : "0", // Bytes start out being 0 41 "short" : "0", // Shorts start out being 0 42 "char" : "0" // Chars start out being 0 43] 44 45leadIn(type) ::= 46<< 47/** \file 48 * This <type> file was generated by $ANTLR version <ANTLRVersion> 49 * 50 * - From the grammar source file : <fileName> 51 * - On : <generatedTimestamp> 52<if(LEXER)> 53 * - for the lexer : <name>Lexer 54<endif> 55<if(PARSER)> 56 * - for the parser : <name>Parser 57<endif> 58<if(TREE_PARSER)> 59 * - for the tree parser : <name>TreeParser 60<endif> 61 * 62 * Editing it, at least manually, is not wise. 63 * 64 * C++ language generator and runtime by Gokulakannan Somasundaram ( heavy lifting from C Run-time by Jim Idle ) 65 * 66 * 67>> 68 69/** The overall file structure of a recognizer; stores methods for rules 70 * and cyclic DFAs plus support code. 71 */ 72outputFile( LEXER, 73 PARSER, 74 TREE_PARSER, 75 actionScope, 76 actions, 77 docComment, 78 recognizer, 79 name, 80 tokens, 81 tokenNames, 82 rules, 83 cyclicDFAs, 84 bitsets, 85 buildTemplate, 86 buildAST, 87 rewriteMode, 88 profile, 89 backtracking, 90 synpreds, 91 memoize, 92 numRules, 93 fileName, 94 ANTLRVersion, 95 generatedTimestamp, 96 trace, 97 scopes, 98 superClass, 99 literals 100 ) ::= 101<< 102<leadIn("C++ source")> 103*/ 104// [The "BSD license"] 105// Copyright (c) 2005-2009 Gokulakannan Somasundaram, ElectronDB 106// 107// All rights reserved. 108// 109// Redistribution and use in source and binary forms, with or without 110// modification, are permitted provided that the following conditions 111// are met: 112// 1. Redistributions of source code must retain the above copyright 113// notice, this list of conditions and the following disclaimer. 114// 2. Redistributions in binary form must reproduce the above copyright 115// notice, this list of conditions and the following disclaimer in the 116// documentation and/or other materials provided with the distribution. 117// 3. The name of the author may not be used to endorse or promote products 118// derived from this software without specific prior written permission. 119// 120// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 121// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 122// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 123// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 124// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 125// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 126// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 127// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 128// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 129// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 130 131<if(actions.(actionScope).header)> 132 133/* ============================================================================= 134 * This is what the grammar programmer asked us to put at the top of every file. 135 */ 136<actions.(actionScope).header> 137/* End of Header action. 138 * ============================================================================= 139 */ 140<endif> 141 142/* ----------------------------------------- 143 * Include the ANTLR3 generated header file. 144 */ 145#include "<name>.hpp" 146<if(trace)> 147#include \<iostream> 148<endif> 149<if(recognizer.grammar.delegators)> 150// Include delegator definition header files 151// 152<recognizer.grammar.delegators: {g|#include "<g.recognizerName>.hpp" }; separator="\n"> 153<endif> 154 155<actions.(actionScope).postinclude> 156/* ----------------------------------------- */ 157 158<docComment> 159 160<if(literals)> 161 162<beginNamespace(actions)> 163 164/** String literals used by <name> that we must do things like MATCHS() with. 165 * C will normally just lay down 8 bit characters, and you can use L"xxx" to 166 * get wchar_t, but wchar_t is 16 bits on Windows, which is not UTF32 and so 167 * we perform this little trick of defining the literals as arrays of UINT32 168 * and passing in the address of these. 169 */ 170<literals:{it | static ANTLR_UCHAR lit_<i>[] = <it>;}; separator="\n"> 171 172<endNamespace(actions)> 173 174<endif> 175 176/* ============================================================================= */ 177 178/* ============================================================================= 179 * Start of recognizer 180 */ 181 182<recognizer> 183 184/* End of code 185 * ============================================================================= 186 */ 187 188>> 189headerFileExtension() ::= ".hpp" 190 191beginNamespace(actions) ::= <% 192 <if(actions.(actionScope).namespace)> 193 namespace <actions.(actionScope).namespace> { 194 <endif> 195%> 196 197endNamespace(actions) ::= <% 198 <if(actions.(actionScope).namespace)> 199 } 200 <endif> 201%> 202 203 204headerFile( LEXER, 205 PARSER, 206 TREE_PARSER, 207 actionScope, 208 actions, 209 docComment, 210 recognizer, 211 name, 212 tokens, 213 tokenNames, 214 rules, 215 cyclicDFAs, 216 bitsets, 217 buildTemplate, 218 buildAST, 219 rewriteMode, 220 profile, 221 backtracking, 222 synpreds, 223 memoize, 224 numRules, 225 fileName, 226 ANTLRVersion, 227 generatedTimestamp, 228 trace, 229 scopes, 230 superClass, 231 literals 232 ) ::= 233<< 234<leadIn("C++ header")> 235<if(PARSER)> 236 * The parser <mainName()> has the callable functions (rules) shown below, 237<endif> 238<if(LEXER)> 239 * The lexer <mainName()> has the callable functions (rules) shown below, 240<endif> 241<if(TREE_PARSER)> 242 * The tree parser <mainName()> has the callable functions (rules) shown below, 243<endif> 244 * which will invoke the code for the associated rule in the source grammar 245 * assuming that the input stream is pointing to a token/text stream that could begin 246 * this rule. 247 * 248 * For instance if you call the first (topmost) rule in a parser grammar, you will 249 * get the results of a full parse, but calling a rule half way through the grammar will 250 * allow you to pass part of a full token stream to the parser, such as for syntax checking 251 * in editors and so on. 252 * 253 */ 254// [The "BSD license"] 255// Copyright (c) 2005-2009 Gokulakannan Somasundaram. 256// 257// All rights reserved. 258// 259// Redistribution and use in source and binary forms, with or without 260// modification, are permitted provided that the following conditions 261// are met: 262// 1. Redistributions of source code must retain the above copyright 263// notice, this list of conditions and the following disclaimer. 264// 2. Redistributions in binary form must reproduce the above copyright 265// notice, this list of conditions and the following disclaimer in the 266// documentation and/or other materials provided with the distribution. 267// 3. The name of the author may not be used to endorse or promote products 268// derived from this software without specific prior written permission. 269// 270// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 271// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 272// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 273// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 274// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 275// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 276// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 277// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 278// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 279// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 280 281#ifndef _<name>_H 282#define _<name>_H 283<actions.(actionScope).preincludes> 284/* ============================================================================= 285 * Standard antlr3 C++ runtime definitions 286 */ 287#include \<antlr3.hpp> 288 289/* End of standard antlr 3 runtime definitions 290 * ============================================================================= 291 */ 292 293<actions.(actionScope).includes> 294 295<if(recognizer.grammar.delegates)> 296// Include delegate definition header files 297// 298<recognizer.grammar.delegates: {g|#include "<g.recognizerName>.hpp"}; separator="\n"> 299 300<endif> 301 302 303<actions.(actionScope).header> 304 305#ifdef WIN32 306// Disable: Unreferenced parameter, - Rules with parameters that are not used 307// constant conditional, - ANTLR realizes that a prediction is always true (synpred usually) 308// initialized but unused variable - tree rewrite variables declared but not needed 309// Unreferenced local variable - lexer rule declares but does not always use _type 310// potentially unitialized variable used - retval always returned from a rule 311// unreferenced local function has been removed - susually getTokenNames or freeScope, they can go without warnigns 312// 313// These are only really displayed at warning level /W4 but that is the code ideal I am aiming at 314// and the codegen must generate some of these warnings by necessity, apart from 4100, which is 315// usually generated when a parser rule is given a parameter that it does not use. Mostly though 316// this is a matter of orthogonality hence I disable that one. 317// 318#pragma warning( disable : 4100 ) 319#pragma warning( disable : 4101 ) 320#pragma warning( disable : 4127 ) 321#pragma warning( disable : 4189 ) 322#pragma warning( disable : 4505 ) 323#pragma warning( disable : 4701 ) 324#endif 325<if(backtracking)> 326 327/* ======================== 328 * BACKTRACKING IS ENABLED 329 * ======================== 330 */ 331<endif> 332 333<beginNamespace(actions)> 334 335<if(recognizer.grammar.delegators)> 336// Include delegator definition classes 337// 338<recognizer.grammar.delegators: {g|class <g.recognizerName>; }; separator="\n"> 339<endif> 340 341<actions.(actionScope).traits> 342typedef <name>Traits <name>ImplTraits; 343 344<rules:{r | <if(r.ruleDescriptor.isSynPred)> struct <r.ruleDescriptor.name> {\}; <endif>}; separator="\n"> 345 346class <name>Tokens 347{ 348public: 349 /** Symbolic definitions of all the tokens that the <grammarType()> will work with. 350 * 351 * Antlr will define EOF, but we can't use that as it it is too common in 352 * in C header files and that would be confusing. There is no way to filter this out at the moment 353 * so we just undef it here for now. That isn't the value we get back from C recognizers 354 * anyway. We are looking for ANTLR_TOKEN_EOF. 355 */ 356 enum Tokens 357 { 358 EOF_TOKEN = <name>ImplTraits::CommonTokenType::TOKEN_EOF 359 <tokens:{it | , <it.name> = <it.type> }; separator="\n"> 360 }; 361 362}; 363 364/** Context tracking structure for <mainName()> 365 */ 366class <name> : public <componentBaseType()>, public <name>Tokens 367{ 368public: 369 typedef <name>ImplTraits ImplTraits; 370 typedef <name> ComponentType; 371 typedef ComponentType::StreamType StreamType; 372 typedef <componentBaseType()> BaseType; 373 typedef ImplTraits::RecognizerSharedStateType\<StreamType> RecognizerSharedStateType; 374 typedef StreamType InputType; 375<if(recognizer.filterMode)> 376 static const bool IsFiltered = true; 377<else> 378 static const bool IsFiltered = false; 379<endif> 380 381 <scopes:{it | <if(it.isDynamicGlobalScope)><globalAttributeScopeDecl(it)><endif>}> 382 <rules:{r | <if(r.ruleDescriptor.ruleScope)><ruleAttributeScopeDecl(scope=r.ruleDescriptor.ruleScope)><endif>}> 383 384private: 385<if(recognizer.grammar.delegates)> 386 <recognizer.grammar.delegates: 387 {g|<g.recognizerName>* m_<g:delegateName()>;}; separator="\n"> 388<endif> 389<if(recognizer.grammar.delegators)> 390 <recognizer.grammar.delegators: 391 {g|<g.recognizerName>* m_<g:delegateName()>;}; separator="\n"> 392<endif> 393<scopes:{it | <if(it.isDynamicGlobalScope)> 394 <globalAttributeScopeDef(it)> 395<endif>}; separator="\n\n"> 396<rules: {r |<if(r.ruleDescriptor.ruleScope)> 397 <ruleAttributeScopeDef(scope=r.ruleDescriptor.ruleScope)> 398<endif>}> 399 <@members> 400 <@end> 401 402public: 403 <name>(InputType* instream<recognizer.grammar.delegators:{g|, <g.recognizerName>* <g:delegateName()>}>); 404 <name>(InputType* instream, RecognizerSharedStateType* state<recognizer.grammar.delegators:{g|, <g.recognizerName>* <g:delegateName()>}>); 405 406 void init(InputType* instream <recognizer.grammar.delegators:{g|, <g.recognizerName>* <g:delegateName()>}> ); 407 408 <actions.(actionScope).context> 409 410<if(LEXER)> 411<if(recognizer.filterMode)> 412 void memoize(ANTLR_MARKER ruleIndex, ANTLR_MARKER ruleParseStart); 413 bool alreadyParsedRule(ANTLR_MARKER ruleIndex); 414 <filteringNextToken()> 415<endif> 416 <rules:{r | <if(!r.ruleDescriptor.isSynPred)><headerReturnType(ruleDescriptor=r.ruleDescriptor)> m<r.ruleDescriptor.name>( <r.ruleDescriptor.parameterScope:parameterScope()>);<endif>}; separator="\n"> 417 <rules:{r | <if(r.ruleDescriptor.isSynPred)> <headerReturnType(ruleDescriptor=r.ruleDescriptor)> msynpred( antlr3::ClassForwarder\< <r.ruleDescriptor.name> > <r.ruleDescriptor.parameterScope:parameterScope()>); 418 void m<r.ruleDescriptor.name>_fragment (<r.ruleDescriptor.parameterScope:parameterScope()>);<endif>}; separator="\n"> 419<endif> 420<if(!LEXER)> 421 <rules:{r | <headerReturnScope(ruleDescriptor=r.ruleDescriptor)>}> 422 <rules:{r | <if(!r.ruleDescriptor.isSynPred)> <headerReturnType(ruleDescriptor=r.ruleDescriptor)> <r.ruleDescriptor.name> (<r.ruleDescriptor.parameterScope:parameterScope()>); <endif>}; separator="\n"> 423 <rules:{r | <if(r.ruleDescriptor.isSynPred)> <headerReturnType(ruleDescriptor=r.ruleDescriptor)> msynpred( antlr3::ClassForwarder\< <r.ruleDescriptor.name> > <r.ruleDescriptor.parameterScope:parameterScope()>); 424 void m<r.ruleDescriptor.name>_fragment (<r.ruleDescriptor.parameterScope:parameterScope()>);<endif>}; separator="\n"> 425<! generate rule/method definitions for imported rules so they 426 appear to be defined in this recognizer. !> 427 // Delegated rules 428<recognizer.grammar.delegatedRules:{ruleDescriptor| 429 <headerReturnType(ruleDescriptor)> <ruleDescriptor.name>(<ruleDescriptor.parameterScope:parameterScope()>);}; separator="\n"> 430<endif> 431 432 const char * getGrammarFileName(); 433 void reset(); 434 ~<name>(); 435 436}; 437 438// Function protoypes for the constructor functions that external translation units 439// such as delegators and delegates may wish to call. 440// 441<if(!recognizer.grammar.grammarIsRoot)> 442extern ANTLR_UINT8* <recognizer.grammar.composite.rootGrammar.recognizerName>TokenNames[]; 443<endif> 444 445 446/* End of token definitions for <name> 447 * ============================================================================= 448 */ 449 450<endNamespace(actions)> 451 452#endif 453 454/* END - Note:Keep extra line feed to satisfy UNIX systems */ 455 456>> 457 458grammarType() ::= <% 459<if(PARSER)> 460parser 461<endif> 462<if(LEXER)> 463lexer 464<endif> 465<if(TREE_PARSER)> 466tree parser 467<endif> 468%> 469 470componentType() ::= << 471<if(PARSER)> 472<name>ImplTraits::ParserType 473<endif> 474<if(LEXER)> 475<name>ImplTraits::LexerType 476<endif> 477<if(TREE_PARSER)> 478<name>ImplTraits::TreeParserType 479<endif> 480>> 481 482componentBaseType() ::= <% 483<if(PARSER)> 484<name>ImplTraits::BaseParserType 485<endif> 486<if(LEXER)> 487<name>ImplTraits::BaseLexerType 488<endif> 489<if(TREE_PARSER)> 490<name>ImplTraits::BaseTreeParserType 491<endif> 492%> 493 494streamType() ::= << 495<if(PARSER)> 496<name>ImplTraits::ParserType::StreamType 497<endif> 498<if(LEXER)> 499<name>ImplTraits::LexerType::StreamType 500<endif> 501<if(TREE_PARSER)> 502<name>ImplTraits::TreeParserType::StreamType 503<endif> 504>> 505 506 507mainName() ::= <% 508<if(PARSER)> 509<name> 510<endif> 511<if(LEXER)> 512<name> 513<endif> 514<if(TREE_PARSER)> 515<name> 516<endif> 517%> 518 519headerReturnScope(ruleDescriptor) ::= "<returnScope(scope=ruleDescriptor.returnScope)>" 520 521headerReturnType(ruleDescriptor) ::= <% 522<if(LEXER)> 523<if(!ruleDescriptor.isSynPred)> 524 void 525<else> 526 <returnType()> 527<endif> 528<else> 529 <returnType()> 530<endif> 531%> 532 533// Produce the lexer output 534// 535lexer( grammar, 536 name, 537 tokens, 538 scopes, 539 rules, 540 numRules, 541 filterMode, 542 superClass, 543 labelType="ImplTraits::CommonTokenType*") ::= << 544 545using namespace antlr3; 546 547<beginNamespace(actions)> 548 549<if(filterMode)> 550 551/* Override the normal MEMOIZE and HAVEALREADYPARSED macros as this is a filtering 552 * lexer. In filter mode, the memoizing and backtracking are gated at BACKTRACKING > 1 rather 553 * than just BACKTRACKING. In some cases this might generate code akin to: 554 * if (BACKTRACKING) if (BACKTRACKING > 1) memoize. 555 */ 556void <name>::memoize(ANTLR_MARKER ruleIndex, ANTLR_MARKER ruleParseStart) 557{ 558 BaseType* base = this; 559 if ( this->get_backtracking()>1 ) 560 base->memoize( ruleIndex, ruleParseStart ); 561 562} 563 564bool <name>::alreadyParsedRule(ANTLR_MARKER ruleIndex) 565{ 566 BaseType* base = this; 567 if ( this->get_backtracking() > 1 ) 568 return base->haveParsedRule(ruleIndex); 569 return false; 570} 571 572<endif> 573 574/* ========================================================================= 575 * Lexer matching rules end. 576 * ========================================================================= 577 */ 578 579<scopes:{it |<if(it.isDynamicGlobalScope)><globalAttributeScope(it)><endif>}> 580 581<actions.lexer.members> 582 583<name>::~<name>() 584{ 585<if(memoize)> 586 RuleMemoType* rulememo = this->getRuleMemo(); 587 if(rulememo != NULL) 588 { 589 delete rulememo; 590 this->setRuleMemo(NULL); 591 } 592<endif> 593<if(grammar.directDelegates)> 594 // Free the lexers that we delegated to 595 // functions to. NULL the state so we only free it once. 596 // 597 <grammar.directDelegates: 598 {g| m_<g:delegateName()>->set_lexstate(NULL); 599 delete m_<g:delegateName()>; }; separator="\n"> 600<endif> 601} 602 603void 604<name>::reset() 605{ 606 this->get_rec()->reset(); 607} 608 609/** \brief Name of the grammar file that generated this code 610 */ 611static const char fileName[] = "<fileName>"; 612 613/** \brief Return the name of the grammar file that generated this code. 614 */ 615const char* <name>::getGrammarFileName() 616{ 617 return fileName; 618} 619 620/** \brief Create a new lexer called <name> 621 * 622 * \param[in] instream Pointer to an initialized input stream 623 * \return 624 * - Success p<name> initialized for the lex start 625 * - Fail NULL 626 */ 627<name>::<name>(StreamType* instream<grammar.delegators:{g|, <g.recognizerName>* <g:delegateName()>}>) 628:<name>ImplTraits::BaseLexerType(ANTLR_SIZE_HINT, instream, NULL) 629{ 630 // See if we can create a new lexer with the standard constructor 631 // 632 this->init(instream <grammar.delegators:{g|, <g:delegateName()>}>); 633} 634 635/** \brief Create a new lexer called <name> 636 * 637 * \param[in] instream Pointer to an initialized input stream 638 * \param[state] state Previously created shared recognizer stat 639 * \return 640 * - Success p<name> initialized for the lex start 641 * - Fail NULL 642 */ 643<name>::<name>(StreamType* instream, RecognizerSharedStateType* state<grammar.delegators:{g|, <g.recognizerName>* <g:delegateName()>}>) 644:<name>ImplTraits::BaseLexerType(ANTLR_SIZE_HINT, instream, state) 645{ 646 this->init(instream <grammar.delegators:{g|, <g:delegateName()>} >); 647} 648 649void <name>::init(StreamType* instream<grammar.delegators:{g|, <g.recognizerName>* <g:delegateName()>} >) 650{ 651 /* ------------------------------------------------------------------- 652 * Memory for basic structure is allocated, now to fill in 653 * in base ANTLR3 structures. We initialize the function pointers 654 * for the standard ANTLR3 lexer function set, but upon return 655 * from here, the programmer may set the pointers to provide custom 656 * implementations of each function. 657 * 658 * We don't use the macros defined in <name>.h here so you can get a sense 659 * of what goes where. 660 */ 661 662<if(memoize)> 663<if(grammar.grammarIsRoot)> 664 // Create a LIST for recording rule memos. 665 // 666 this->setRuleMemo( new IntTrie(15) ); /* 16 bit depth is enough for 32768 rules! */ 667<endif> 668<endif> 669 670<if(grammar.directDelegates)> 671 // Initialize the lexers that we are going to delegate some 672 // functions to. 673 // 674 <grammar.directDelegates: 675 {g|m_<g:delegateName()> = new <g.recognizerName>(instream, this->get_lexstate(), this<grammar.delegators:{g|, <g:delegateName()>}>);}; separator="\n"> 676<endif> 677<if(grammar.delegators)> 678 // Install the pointers back to lexers that will delegate us to perform certain functions 679 // for them. 680 // 681 <grammar.delegators: 682 {g| m_<g:delegateName()> = <g:delegateName()>;}; separator="\n"> 683<endif> 684} 685 686<if(cyclicDFAs)> 687 688/* ========================================================================= 689 * DFA tables for the lexer 690 */ 691<cyclicDFAs:cyclicDFA()> <! dump tables for all DFA !> 692/* ========================================================================= 693 * End of DFA tables for the lexer 694 */ 695<endif> 696 697/* ========================================================================= 698 * Functions to match the lexer grammar defined tokens from the input stream 699 */ 700 701<rules; separator="\n\n"> 702 703/* ========================================================================= 704 * Lexer matching rules end. 705 * ========================================================================= 706 */ 707<if(synpreds)> 708 709/* ========================================================================= 710 * Lexer syntactic predicates 711 */ 712<synpreds:{p | <lexerSynpred(predname=p)>}> 713/* ========================================================================= 714 * Lexer syntactic predicates end. 715 * ========================================================================= 716 */ 717<endif> 718 719/* End of Lexer code 720 * ================================================ 721 * ================================================ 722 */ 723 724<endNamespace(actions)> 725 726>> 727 728 729filteringNextToken() ::= << 730<name>ImplTraits::CommonTokenType* 731<name>ImplTraits::TokenSourceType::nextToken() 732{ 733 LexerType* lexer; 734 typename LexerType::RecognizerSharedStateType* state; 735 736 lexer = this->get_super(); 737 state = lexer->get_lexstate(); 738 739 /* Get rid of any previous token (token factory takes care of 740 * any deallocation when this token is finally used up. 741 */ 742 state->set_token_present( false ); 743 state->set_error( false ); /* Start out without an exception */ 744 state->set_failedflag(false); 745 746 /* Record the start of the token in our input stream. 747 */ 748 state->set_tokenStartCharIndex( lexer->index(); 749 state->set_tokenStartCharPositionInLine( lexer->getCharPositionInLine() ); 750 state->set_tokenStartLine( lexer->getLine() ); 751 state->set_text(""); 752 753 /* Now call the matching rules and see if we can generate a new token 754 */ 755 for (;;) 756 { 757 if (lexer->LA(1) == ANTLR_CHARSTREAM_EOF) 758 { 759 /* Reached the end of the stream, nothing more to do. 760 */ 761 CommonTokenType& teof = m_eofToken; 762 763 teof.set_startIndex(lexer->getCharIndex()); 764 teof.set_stopIndex(lexer->getCharIndex()); 765 teof.setLine(lexer->getLine()); 766 return &teof; 767 } 768 769 state->set_token_present(false); 770 state->set_error(false); /* Start out without an exception */ 771 772 { 773 ANTLR_MARKER m; 774 775 m = this->get_istream()->mark(); 776 state->set_backtracking(1); /* No exceptions */ 777 state->set_failedflag(false); 778 779 /* Call the generated lexer, see if it can get a new token together. 780 */ 781 lexer->mTokens(); 782 state->set_backtracking(0); 783 784 <! mTokens backtracks with synpred at BACKTRACKING==2 785 and we set the synpredgate to allow actions at level 1. !> 786 787 if(state->get_failed()) 788 { 789 lexer->rewind(m); 790 lexer->consume(); <! advance one char and try again !> 791 } 792 else 793 { 794 lexer->emit(); /* Assemble the token and emit it to the stream */ 795 TokenType& tok = state->get_token(); 796 return &tok; 797 } 798 } 799 } 800} 801>> 802 803actionGate() ::= "this->get_backtracking()==0" 804 805filteringActionGate() ::= "this->get_backtracking()==1" 806 807/** How to generate a parser */ 808genericParser( grammar, name, scopes, tokens, tokenNames, rules, numRules, 809 bitsets, inputStreamType, superClass, 810 labelType, members, rewriteElementType, 811 filterMode, ASTLabelType="ImplTraits::TreeType*") ::= << 812 813using namespace antlr3; 814<if(grammar.grammarIsRoot)> 815/** \brief Table of all token names in symbolic order, mainly used for 816 * error reporting. 817 */ 818ANTLR_UINT8* <name>TokenNames[<length(tokenNames)>+4] 819 = { 820 (ANTLR_UINT8*) "\<invalid>", /* String to print to indicate an invalid token */ 821 (ANTLR_UINT8*) "\<EOR>", 822 (ANTLR_UINT8*) "\<DOWN>", 823 (ANTLR_UINT8*) "\<UP>", 824 <tokenNames:{it |(ANTLR_UINT8*) <it>}; separator=",\n"> 825 }; 826<endif> 827 828 <@members> 829 830 <@end> 831 832/** \brief Name of the grammar file that generated this code 833 */ 834static const char fileName[] = "<fileName>"; 835 836/** \brief Return the name of the grammar file that generated this code. 837 */ 838const char* <name>::getGrammarFileName() 839{ 840 return fileName; 841} 842/** \brief Create a new <name> parser and return a context for it. 843 * 844 * \param[in] instream Pointer to an input stream interface. 845 * 846 * \return Pointer to new parser context upon success. 847 */ 848<name>::<name>( StreamType* instream<grammar.delegators:{g|, <g.recognizerName>* <g:delegateName()>}>) 849<constructorInitializerType("NULL")> 850{ 851 // See if we can create a new parser with the standard constructor 852 // 853 this->init(instream<grammar.delegators:{g|, <g:delegateName()>}>); 854} 855 856/** \brief Create a new <name> parser and return a context for it. 857 * 858 * \param[in] instream Pointer to an input stream interface. 859 * 860 * \return Pointer to new parser context upon success. 861 */ 862<name>::<name>( StreamType* instream, RecognizerSharedStateType* state<grammar.delegators:{g|, <g.recognizerName>* <g:delegateName()>}>) 863<constructorInitializerType("state")> 864{ 865 this->init(instream <grammar.delegators:{g|, <g:delegateName()>}>); 866} 867 868void <name>::init(StreamType* instream<grammar.delegators:{g|, <g.recognizerName>* <g:delegateName()>}>) 869{ 870 <actions.parser.apifuncs> 871<if(memoize)> 872<if(grammar.grammarIsRoot)> 873 /* Create a LIST for recording rule memos. 874 */ 875 typedef RecognizerSharedStateType::RuleMemoType RuleMemoType; 876 this->setRuleMemo( new RuleMemoType(15) ); /* 16 bit depth is enough for 32768 rules! */<\n> 877<endif> 878<endif> 879<if(grammar.directDelegates)> 880 // Initialize the lexers that we are going to delegate some 881 // functions to. 882 // 883 <grammar.directDelegates: 884 {g|m_<g:delegateName()> = new <g.recognizerName>(instream, this->get_psrstate(), this<grammar.delegators:{g|, <g:delegateName()>}>);}; separator="\n"> 885 <endif> 886 <if(grammar.delegators)> 887 // Install the pointers back to lexers that will delegate us to perform certain functions 888 // for them. 889 // 890 <grammar.delegators: {g| m_<g:delegateName()> = <g:delegateName()>;}; separator="\n"> 891 <endif> 892 /* Install the token table 893 */ 894 this->get_psrstate()->set_tokenNames( <grammar.composite.rootGrammar.recognizerName>TokenNames ); 895 896 <@debugStuff()> 897 898} 899 900void 901<name>::reset() 902{ 903 this->get_rec()->reset(); 904} 905 906/** Free the parser resources 907 */ 908<name>::~<name>() 909 { 910 <@cleanup> 911 <@end> 912<if(grammar.directDelegates)> 913 // Free the parsers that we delegated to 914 // functions to.NULL the state so we only free it once. 915 // 916 <grammar.directDelegates: 917 {g| m_<g:delegateName()>->set_psrstate( NULL ); 918 delete m_<g:delegateName()>;}; separator="\n"> 919<endif> 920<if(memoize)> 921<if(grammar.grammarIsRoot)> 922 if(this->getRuleMemo() != NULL) 923 { 924 delete this->getRuleMemo(); 925 this->setRuleMemo(NULL); 926 } 927<endif> 928<endif> 929} 930 931/** Return token names used by this <grammarType()> 932 * 933 * The returned pointer is used as an index into the token names table (using the token 934 * number as the index). 935 * 936 * \return Pointer to first char * in the table. 937 */ 938static ANTLR_UINT8** getTokenNames() 939{ 940 return <grammar.composite.rootGrammar.recognizerName>TokenNames; 941} 942 943 <members> 944 945/* Declare the bitsets 946 */ 947<bitsets:{it | <bitsetDeclare(bitsetname={FOLLOW_<it.name>_in_<it.inName><it.tokenIndex>}, 948 words64=it.bits, traits={<name>ImplTraits} )>}> 949 950 951<if(cyclicDFAs)> 952 953/* ========================================================================= 954 * DFA tables for the parser 955 */ 956<cyclicDFAs:cyclicDFA()> <! dump tables for all DFA !> 957/* ========================================================================= 958 * End of DFA tables for the parser 959 */ 960<endif> 961 962/* ============================================== 963 * Parsing rules 964 */ 965<rules; separator="\n\n"> 966<if(grammar.delegatedRules)> 967 // Delegated methods that appear to be a part of this 968 // parser 969 // 970<grammar.delegatedRules:{ruleDescriptor| 971 <returnType()> <name>::<ruleDescriptor.name>(<ruleDescriptor.parameterScope:parameterScope()>) 972 { 973 <if(ruleDescriptor.hasReturnValue)>return <endif>m_<ruleDescriptor.grammar:delegateName()>-><ruleDescriptor.name>(<if(ruleDescriptor.parameterScope)><ruleDescriptor.parameterScope.attributes:{a|<a.name>}; separator=", "><endif>); 974 \}}; separator="\n"> 975 976<endif> 977/* End of parsing rules 978 * ============================================== 979 */ 980 981/* ============================================== 982 * Syntactic predicates 983 */ 984<synpreds:{p | <synpred(predname=p)>}> 985/* End of syntactic predicates 986 * ============================================== 987 */ 988 989>> 990 991constructorInitializerType(rec_state) ::=<< 992<if(PARSER)> 993 :ImplTraits::BaseParserType(ANTLR_SIZE_HINT, instream, <rec_state>) 994<endif> 995<if(TREE_PARSER)> 996 :ImplTraits::BaseTreeParserType(ANTLR_SIZE_HINT, instream, <rec_state>) 997<endif> 998>> 999 1000parser( grammar, 1001 name, 1002 scopes, 1003 tokens, 1004 tokenNames, 1005 rules, 1006 numRules, 1007 bitsets, 1008 ASTLabelType, 1009 superClass="Parser", 1010 labelType="ImplTraits::CommonTokenType*", 1011 members={<actions.parser.members>} 1012 ) ::= << 1013<beginNamespace(actions)> 1014<genericParser(inputStreamType="CommonTokenStreamType*", rewriteElementType="Token", filterMode=false, ...)> 1015<endNamespace(actions)> 1016>> 1017 1018/** How to generate a tree parser; same as parser except the input 1019 * stream is a different type. 1020 */ 1021treeParser( grammar, 1022 name, 1023 scopes, 1024 tokens, 1025 tokenNames, 1026 globalAction, 1027 rules, 1028 numRules, 1029 bitsets, 1030 filterMode, 1031 labelType={<ASTLabelType>}, 1032 ASTLabelType="ImplTraits::TreeType*", 1033 superClass="TreeParser", 1034 members={<actions.treeparser.members>} 1035 ) ::= << 1036<beginNamespace(actions)> 1037<genericParser(inputStreamType="CommonTreeNodeStream*", rewriteElementType="Node", ...)> 1038<endNamespace(actions)> 1039>> 1040 1041/** A simpler version of a rule template that is specific to the imaginary 1042 * rules created for syntactic predicates. As they never have return values 1043 * nor parameters etc..., just give simplest possible method. Don't do 1044 * any of the normal memoization stuff in here either; it's a waste. 1045 * As predicates cannot be inlined into the invoking rule, they need to 1046 * be in a rule by themselves. 1047 */ 1048synpredRule(ruleName, ruleDescriptor, block, description, nakedBlock) ::= 1049<< 1050// $ANTLR start <ruleName> 1051void <name>::m<ruleName>_fragment( <ruleDescriptor.parameterScope:parameterScope()> ) 1052{ 1053 <ruleLabelDefs()> 1054 <ruleLabelInitializations()> 1055<if(trace)> 1056 ANTLR_PRINTF("enter <ruleName> %d failed = %d, backtracking = %d\\n", this->LT(1),failed,this->get_backtracking() ); 1057 <block> 1058 ANTLR_PRINTF("exit <ruleName> %d, failed = %d, backtracking = %d\\n", this->LT(1),failed,this->get_backtracking()); 1059 1060<else> 1061 <block> 1062<endif> 1063 1064goto rule<ruleDescriptor.name>Ex; /* Prevent compiler warnings */ 1065rule<ruleDescriptor.name>Ex: ; 1066} 1067// $ANTLR end <ruleName> 1068>> 1069 1070synpred(predname) ::= << 1071 1072bool <name>::msynpred( antlr3::ClassForwarder\< <predname> > ) 1073{ 1074 ANTLR_MARKER start; 1075 bool success; 1076 1077 this->inc_backtracking(); 1078 <@start()> 1079 start = this->mark(); 1080 this->m<predname>_fragment(); // can never throw exception 1081 success = !( this->get_failedflag() ); 1082 this->rewind(start); 1083 <@stop()> 1084 this->dec_backtracking(); 1085 this->set_failedflag(false); 1086 return success; 1087}<\n> 1088>> 1089 1090lexerSynpred(predname) ::= << 1091<synpred(predname)> 1092>> 1093 1094ruleMemoization(rname) ::= << 1095<if(memoize)> 1096if ( (this->get_backtracking()>0) && (this->haveParsedRule(<ruleDescriptor.index>)) ) 1097{ 1098<if(ruleDescriptor.hasMultipleReturnValues)> 1099<if(!ruleDescriptor.isSynPred)> 1100 retval.start = 0;<\n> 1101<endif> 1102<endif> 1103 <(ruleDescriptor.actions.after):execAfter()> 1104 <finalCode(finalBlock=finally)> 1105<if(!ruleDescriptor.isSynPred)> 1106 <scopeClean()><\n> 1107<endif> 1108 return <ruleReturnValue()>; 1109} 1110<endif> 1111>> 1112 1113/** How to test for failure and return from rule */ 1114checkRuleBacktrackFailure() ::= << 1115if (this->hasException()) 1116{ 1117 goto rule<ruleDescriptor.name>Ex; 1118} 1119<if(backtracking)> 1120if (this->hasFailed()) 1121{ 1122 <scopeClean()> 1123 <@debugClean()> 1124 return <ruleReturnValue()>; 1125} 1126<endif> 1127>> 1128 1129/** This rule has failed, exit indicating failure during backtrack */ 1130ruleBacktrackFailure() ::= << 1131<if(backtracking)> 1132if (this->get_backtracking()>0) 1133{ 1134 this->set_failedflag( true ); 1135 <scopeClean()> 1136 return <ruleReturnValue()>; 1137} 1138<endif> 1139>> 1140 1141/** How to generate code for a rule. This includes any return type 1142 * data aggregates required for multiple return values. 1143 */ 1144rule(ruleName,ruleDescriptor,block,emptyRule,description,exceptions,finally,memoize) ::= << 1145/** 1146 * $ANTLR start <ruleName> 1147 * <fileName>:<description> 1148 */ 1149<returnType()> 1150<name>::<ruleName>(<ruleDescriptor.parameterScope:parameterScope()>) 1151{ 1152 <if(trace)>ANTLR_PRINTF("enter <ruleName> %s failed=%d, backtracking=%d\n", this->LT(1), this->get_backtracking() );<endif> 1153 <ruleDeclarations()> 1154 <ruleDescriptor.actions.declarations> 1155 <ruleLabelDefs()> 1156 <ruleInitializations()> 1157 <ruleDescriptor.actions.init> 1158 <ruleMemoization(rname=ruleName)> 1159 <ruleLabelInitializations()> 1160 1161 <if(actions.(actionScope).rulecatch)> 1162 try { 1163 <else> 1164 <if(exceptions)> 1165 try { 1166 <endif> 1167 <endif> 1168 <@preamble()> 1169 { 1170 <block> 1171 } 1172 <ruleCleanUp()> 1173 1174<if(exceptions)> 1175 <(ruleDescriptor.actions.after):execAfter()> 1176 <exceptions:{e|<catch(decl=e.decl,action=e.action)><\n>}> 1177<else> 1178 <if(!emptyRule)> 1179 if (this->hasException()) 1180 { 1181 this->preporterror(); 1182 this->precover(); 1183 <@setErrorReturnValue()> 1184 } 1185 <if(ruleDescriptor.actions.after)> 1186 else 1187 { 1188 <(ruleDescriptor.actions.after):execAfter()> 1189 }<\n> 1190 <endif> 1191 <if(actions.(actionScope).rulecatch)> 1192 } <actions.(actionScope).rulecatch> 1193 <endif> 1194 <endif> 1195<endif> 1196 1197 <if(trace)>ANTLR_PRINTF("exit <ruleName> %d failed=%s backtracking=%s\n", this->LT(1), failed, this->get_backtracking() );<endif> 1198 <memoize()> 1199<if(finally)> 1200 <finalCode(finalBlock=finally)> 1201<endif> 1202 <scopeClean()> 1203 <@postamble()> 1204 return <ruleReturnValue()>; 1205} 1206/* $ANTLR end <ruleName> */ 1207>> 1208 1209finalCode(finalBlock) ::= << 1210{ 1211 <finalBlock> 1212} 1213 1214>> 1215 1216catch(decl,action) ::= << 1217/* catch(decl,action) 1218 */ 1219}catch (<e.decl>) { 1220 <e.action> 1221} 1222>> 1223 1224ruleDeclarations() ::= << 1225 1226<if(ruleDescriptor.hasMultipleReturnValues)> 1227<returnType()> retval(this);<\n> 1228<else> 1229<if(PARSER)> 1230 <name>ImplTraits::RuleReturnValueType _antlr_rule_exit(this); 1231<endif> 1232<if(ruleDescriptor.returnScope)> 1233<ruleDescriptor.returnScope.attributes:{ a | 1234<a.type> <a.name>; 1235}> 1236<endif> 1237<endif> 1238<if(memoize)> 1239ANTLR_MARKER <ruleDescriptor.name>_StartIndex; 1240<endif> 1241>> 1242 1243ruleInitializations() ::= << 1244/* Initialize rule variables 1245 */ 1246<if(ruleDescriptor.returnScope)> 1247<if(ruleDescriptor.hasMultipleReturnValues)> 1248<ruleDescriptor.returnScope.attributes:{ a | <if(a.initValue)>retval.<a.name> = <a.initValue>;<endif> }> 1249<else> 1250<ruleDescriptor.returnScope.attributes:{ a | <if(a.initValue)><a.name> = <a.initValue>;<endif> }> 1251<endif> 1252<endif> 1253<if(memoize)> 1254<ruleDescriptor.name>_StartIndex = this->index();<\n> 1255<endif> 1256<ruleDescriptor.useScopes:{it | m_<it>_stack.push(<it>Scope()); }; separator="\n"> 1257<ruleDescriptor.ruleScope:{it | m_<it.name>_stack.push(<it.name>Scope()); }; separator="\n"> 1258>> 1259 1260ruleLabelDefs() ::= << 1261<[ruleDescriptor.tokenLabels,ruleDescriptor.tokenListLabels, 1262 ruleDescriptor.wildcardTreeLabels,ruleDescriptor.wildcardTreeListLabels] 1263 :{it |<labelType> <it.label.text> = NULL;}; separator="\n" 1264> 1265<[ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels,ruleDescriptor.wildcardTreeListLabels] 1266 :{it |ImplTraits::TokenPtrsListType list_<it.label.text>;}; separator="\n" 1267> 1268<ruleDescriptor.ruleLabels:ruleLabelDef(); separator="\n"> 1269>> 1270 1271ruleLabelInitializations() ::= << 1272<if(ruleDescriptor.hasMultipleReturnValues)> 1273<if(!ruleDescriptor.isSynPred)> 1274retval.call_start_placeholder(); 1275<endif> 1276<endif> 1277>> 1278 1279lexerRuleLabelDefs() ::= << 1280<[ruleDescriptor.tokenLabels, 1281 ruleDescriptor.tokenListLabels, 1282 ruleDescriptor.ruleLabels] 1283 :{it |<labelType> <it.label.text> = NULL;}; separator="\n" 1284> 1285<ruleDescriptor.charLabels:{it |ANTLR_UINT32 <it.label.text>;}; separator="\n"> 1286<[ruleDescriptor.tokenListLabels, 1287 ruleDescriptor.ruleListLabels, 1288 ruleDescriptor.ruleListLabels] 1289 :{it | ImplTraits::IntTrieType<CommonTokenType>* list_<it.label.text>;}; separator="\n" 1290> 1291>> 1292 1293lexerRuleLabelInit() ::= << 1294<[ruleDescriptor.tokenListLabels, 1295 ruleDescriptor.ruleListLabels, 1296 ruleDescriptor.ruleListLabels] 1297 :{it |list_<it.label.text> = new ImplTraits::IntTrieType<CommonTokenType>(31);}; separator="\n" 1298> 1299>> 1300 1301lexerRuleLabelFree() ::= << 1302<[ruleDescriptor.tokenLabels, 1303 ruleDescriptor.tokenListLabels, 1304 ruleDescriptor.ruleLabels] 1305 :{it |<it.label.text> = NULL;}; separator="\n" 1306> 1307<[ruleDescriptor.tokenListLabels, 1308 ruleDescriptor.ruleListLabels, 1309 ruleDescriptor.ruleListLabels] 1310 :{it | delete list_<it.label.text>;}; separator="\n" 1311> 1312>> 1313 1314ruleReturnValue() ::= <% 1315<if(!ruleDescriptor.isSynPred)> 1316<if(ruleDescriptor.hasReturnValue)> 1317<if(ruleDescriptor.hasSingleReturnValue)> 1318<ruleDescriptor.singleValueReturnName> 1319<else> 1320retval 1321<endif> 1322<endif> 1323<endif> 1324%> 1325 1326memoize() ::= << 1327<if(memoize)> 1328<if(backtracking)> 1329if ( this->get_backtracking() > 0 ) { this->memoize(<ruleDescriptor.index>, <ruleDescriptor.name>_StartIndex); } 1330<endif> 1331<endif> 1332>> 1333 1334ruleCleanUp() ::= << 1335 1336// This is where rules clean up and exit 1337// 1338goto rule<ruleDescriptor.name>Ex; /* Prevent compiler warnings */ 1339rule<ruleDescriptor.name>Ex: ; 1340<if(ruleDescriptor.hasMultipleReturnValues)> 1341<if(!TREE_PARSER)> 1342<if(!ruleDescriptor.isSynPred)> 1343retval.call_stop_placeholder();<\n> 1344<endif> 1345<endif> 1346<endif> 1347>> 1348 1349scopeClean() ::= << 1350<ruleDescriptor.useScopes:{it | m_<it>_stack.pop(); }; separator="\n"> 1351<ruleDescriptor.ruleScope:{it | m_<it.name>_stack.pop(); }; separator="\n"> 1352 1353>> 1354/** How to generate a rule in the lexer; naked blocks are used for 1355 * fragment rules, which do not produce tokens. 1356 */ 1357lexerRule(ruleName,nakedBlock,ruleDescriptor,block,memoize) ::= << 1358// Comes from: <block.description> 1359/** \brief Lexer rule generated by ANTLR3 1360 * 1361 * $ANTLR start <ruleName> 1362 * 1363 * Looks to match the characters the constitute the token <ruleName> 1364 * from the attached input stream. 1365 * 1366 * 1367 * \remark 1368 * - lexer->error == true if an exception was thrown. 1369 */ 1370void <name>::m<ruleName>(<ruleDescriptor.parameterScope:parameterScope()>) 1371{ 1372 ANTLR_UINT32 _type; 1373 <ruleDeclarations()> 1374 <ruleDescriptor.actions.declarations> 1375 <lexerRuleLabelDefs()> 1376 <if(trace)> 1377 std::cout \<\< "enter <ruleName> '" \<\< (char)this->LA(1) 1378 \<\< "' line=" \<\< this->getLine() \<\< ":" \<\< this->getCharPositionInLine() 1379 \<\< " failed=" \<\< this->get_failedflag() \<\< " backtracking=" \<\< this->get_backtracking() \<\< std::endl; 1380 <endif> 1381 1382<if(nakedBlock)> 1383 <ruleMemoization(rname=ruleName)> 1384 <lexerRuleLabelInit()> 1385 <ruleDescriptor.actions.init> 1386 1387 <block><\n> 1388<else> 1389 <ruleMemoization(rname=ruleName)> 1390 <lexerRuleLabelInit()> 1391 _type = <ruleName>; 1392 1393 <ruleDescriptor.actions.init> 1394 1395 <block> 1396 this->get_lexstate()->set_type(_type); 1397<endif> 1398 <if(trace)> 1399 std::cout \<\< "exit <ruleName> '" \<\< (char)this->LA(1) 1400 \<\< "' line=" \<\< this->getLine() \<\< ":" \<\< this->getCharPositionInLine() 1401 \<\< " failed=" \<\< this->get_failedflag() \<\< " backtracking=" \<\< this->get_backtracking() \<\< std::endl; 1402 <endif> 1403 <ruleCleanUp()> 1404 <lexerRuleLabelFree()> 1405 <(ruleDescriptor.actions.after):execAfter()> 1406 <memoize> 1407} 1408// $ANTLR end <ruleName> 1409>> 1410 1411/** How to generate code for the implicitly-defined lexer grammar rule 1412 * that chooses between lexer rules. 1413 */ 1414tokensRule(ruleName,nakedBlock,args,block,ruleDescriptor) ::= << 1415/** This is the entry point in to the lexer from an object that 1416 * wants to generate the next token, such as a pCOMMON_TOKEN_STREAM 1417 */ 1418void 1419<name>::mTokens() 1420{ 1421 <block><\n> 1422 1423 goto ruleTokensEx; /* Prevent compiler warnings */ 1424ruleTokensEx: ; 1425} 1426>> 1427 1428// S U B R U L E S 1429 1430/** A (...) subrule with multiple alternatives */ 1431block(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= << 1432 1433// <fileName>:<description> 1434{ 1435 int alt<decisionNumber>=<maxAlt>; 1436 <decls> 1437 <@predecision()> 1438 <decision> 1439 <@postdecision()> 1440 <@prebranch()> 1441 switch (alt<decisionNumber>) 1442 { 1443 <alts:{a | <altSwitchCase(i,a)>}> 1444 } 1445 <@postbranch()> 1446} 1447>> 1448 1449/** A rule block with multiple alternatives */ 1450ruleBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= << 1451{ 1452 // <fileName>:<description> 1453 1454 ANTLR_UINT32 alt<decisionNumber>; 1455 1456 alt<decisionNumber>=<maxAlt>; 1457 1458 <decls> 1459 <@predecision()> 1460 <decision> 1461 <@postdecision()> 1462 switch (alt<decisionNumber>) 1463 { 1464 <alts:{a | <altSwitchCase(i,a)>}> 1465 } 1466} 1467>> 1468 1469ruleBlockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,description) ::= << 1470// <fileName>:<description> 1471<decls> 1472<@prealt()> 1473<alts> 1474<@postalt()> 1475>> 1476 1477/** A special case of a (...) subrule with a single alternative */ 1478blockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,description) ::= << 1479// <fileName>:<description> 1480<decls> 1481<@prealt()> 1482<alts> 1483<@postalt()> 1484>> 1485 1486/** A (..)+ block with 1 or more alternatives */ 1487positiveClosureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= << 1488// <fileName>:<description> 1489{ 1490 int cnt<decisionNumber>=0; 1491 <decls> 1492 <@preloop()> 1493 1494 for (;;) 1495 { 1496 int alt<decisionNumber>=<maxAlt>; 1497 <@predecision()> 1498 <decision> 1499 <@postdecision()> 1500 switch (alt<decisionNumber>) 1501 { 1502 <alts:{a | <altSwitchCase(i,a)>}> 1503 default: 1504 1505 if ( cnt<decisionNumber> >= 1 ) 1506 { 1507 goto loop<decisionNumber>; 1508 } 1509 <ruleBacktrackFailure()> 1510 <earlyExitEx()> 1511 <@earlyExitException()> 1512 goto rule<ruleDescriptor.name>Ex; 1513 } 1514 cnt<decisionNumber>++; 1515 } 1516 loop<decisionNumber>: ; /* Jump to here if this rule does not match */ 1517 <@postloop()> 1518} 1519>> 1520 1521earlyExitEx() ::= << 1522/* mismatchedSetEx() 1523 */ 1524new ANTLR_Exception\< <name>ImplTraits, EARLY_EXIT_EXCEPTION, StreamType>( this->get_rec(), "" ); 1525<\n> 1526>> 1527positiveClosureBlockSingleAlt ::= positiveClosureBlock 1528 1529/** A (..)* block with 1 or more alternatives */ 1530closureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= << 1531 1532// <fileName>:<description> 1533<decls> 1534 1535<@preloop()> 1536for (;;) 1537{ 1538 int alt<decisionNumber>=<maxAlt>; 1539 <@predecision()> 1540 <decision> 1541 <@postdecision()> 1542 switch (alt<decisionNumber>) 1543 { 1544 <alts:{a | <altSwitchCase(i,a)>}> 1545 default: 1546 goto loop<decisionNumber>; /* break out of the loop */ 1547 break; 1548 } 1549} 1550loop<decisionNumber>: ; /* Jump out to here if this rule does not match */ 1551<@postloop()> 1552>> 1553 1554closureBlockSingleAlt ::= closureBlock 1555 1556/** Optional blocks (x)? are translated to (x|) by antlr before code generation 1557 * so we can just use the normal block template 1558 */ 1559optionalBlock ::= block 1560 1561optionalBlockSingleAlt ::= block 1562 1563/** A case in a switch that jumps to an alternative given the alternative 1564 * number. A DFA predicts the alternative and then a simple switch 1565 * does the jump to the code that actually matches that alternative. 1566 */ 1567altSwitchCase(altNum,alt) ::= << 1568case <altNum>: 1569 <@prealt()> 1570 <alt> 1571 break;<\n> 1572>> 1573 1574/** An alternative is just a list of elements; at outermost level */ 1575alt(elements,altNum,description,autoAST,outerAlt,treeLevel,rew) ::= << 1576// <fileName>:<description> 1577{ 1578 <@declarations()> 1579 <@initializations()> 1580 <elements:element()> 1581 <rew> 1582 <@cleanup()> 1583} 1584>> 1585 1586// E L E M E N T S 1587/** What to emit when there is no rewrite. For auto build 1588 * mode, does nothing. 1589 */ 1590noRewrite(rewriteBlockLevel, treeLevel) ::= "" 1591 1592/** Dump the elements one per line */ 1593element(e) ::= << 1594<@prematch()> 1595<e.el><\n> 1596>> 1597 1598/** match a token optionally with a label in front */ 1599tokenRef(token,label,elementIndex,terminalOptions) ::= << 1600<if(label)><label> = <endif> this->matchToken(<token>, &FOLLOW_<token>_in_<ruleName><elementIndex>); 1601<checkRuleBacktrackFailure()> 1602>> 1603 1604/** ids+=ID */ 1605tokenRefAndListLabel(token,label,elementIndex,terminalOptions) ::= << 1606<tokenRef(...)> 1607<listLabel(elem=label,...)> 1608>> 1609 1610listLabel(label,elem) ::= << 1611 list_<label>.push_back(<elem>); 1612>> 1613 1614 1615/** match a character */ 1616charRef(char,label) ::= << 1617<if(label)> 1618<label> = this->LA(1);<\n> 1619<endif> 1620 this->matchc(<char>); 1621<checkRuleBacktrackFailure()> 1622>> 1623 1624/** match a character range */ 1625charRangeRef(a,b,label) ::= << 1626<if(label)> 1627<label> = this->LA(1);<\n> 1628<endif> 1629this->matchRange(<a>, <b>); 1630<checkRuleBacktrackFailure()> 1631>> 1632 1633/** For now, sets are interval tests and must be tested inline */ 1634matchSet(s,label,elementIndex,terminalOptions,postmatchCode="") ::= << 1635<if(label)> 1636<if(LEXER)> 1637<label>= this->LA(1);<\n> 1638<else> 1639<label>=(<labelType>) this->LT(1);<\n> 1640<endif> 1641<endif> 1642if ( <s> ) 1643{ 1644 this->consume(); 1645 <postmatchCode> 1646<if(!LEXER)> 1647 this->set_perror_recovery(false); 1648<endif> 1649 <if(backtracking)> this->set_failedflag(false); <\n><endif> 1650} 1651else 1652{ 1653 <ruleBacktrackFailure()> 1654 <mismatchedSetEx()> 1655 <@mismatchedSetException()> 1656<if(LEXER)> 1657 this->recover(); 1658<else> 1659<! use following code to make it recover inline; 1660 this->recoverFromMismatchedSet(&FOLLOW_set_in_<ruleName><elementIndex>); 1661!> 1662<endif> 1663 goto rule<ruleDescriptor.name>Ex; 1664}<\n> 1665>> 1666 1667mismatchedSetEx() ::= << 1668new ANTLR_Exception\< <name>ImplTraits, MISMATCHED_SET_EXCEPTION, StreamType>( this->get_rec(), "" ); 1669<if(PARSER)> 1670this->get_exception()->set_expectingSet(NULL); 1671<! use following code to make it recover inline; 1672this->get_exception()->set_expectingSet( &FOLLOW_set_in_<ruleName><elementIndex> ); 1673!> 1674<endif> 1675>> 1676 1677matchRuleBlockSet ::= matchSet 1678 1679matchSetAndListLabel(s,label,elementIndex,postmatchCode) ::= << 1680<matchSet(...)> 1681<listLabel(elem=label,...)> 1682>> 1683 1684/** Match a string literal */ 1685lexerStringRef(string,label,elementIndex) ::= << 1686<if(label)> 1687ANTLR_MARKER <label>Start = this->getCharIndex(); 1688ANTLR_UINT32 <label>StartLine<elementIndex> = this->getLine(); 1689ANTLR_UINT32 <label>StartCharPos<elementIndex> = this->getCharPositionInLine(); 1690this->matchs(<string>); 1691<checkRuleBacktrackFailure()> 1692<label> = new CommonTokenType; 1693<label>->set_type( CommonTokenType::TOKEN_INVALID ); 1694<label>->set_startIndex( <label>Start); 1695<label>->set_stopIndex( this->getCharIndex()-1); 1696<label>->set_input( this->get_input() ); 1697<label>->set_line( <label>StartLine<elementIndex> ); 1698<label>->set_charPositionInLine( <label>StartCharPos<elementIndex> ); 1699<else> 1700this->matchs(<string>); 1701<checkRuleBacktrackFailure()><\n> 1702<endif> 1703>> 1704 1705wildcard(token,label,elementIndex,terminalOptions) ::= << 1706<if(label)> 1707<label>=(<labelType>)this->LT(1);<\n> 1708<endif> 1709this->matchAnyToken(); 1710<checkRuleBacktrackFailure()> 1711>> 1712 1713wildcardAndListLabel(token,label,elementIndex,terminalOptions) ::= << 1714<wildcard(...)> 1715<listLabel(elem=label,...)> 1716>> 1717 1718/** Match . wildcard in lexer */ 1719wildcardChar(label, elementIndex) ::= << 1720<if(label)> 1721<label> = this->LA(1);<\n> 1722<endif> 1723this->matchAny(); 1724<checkRuleBacktrackFailure()> 1725>> 1726 1727wildcardCharListLabel(label, elementIndex) ::= << 1728<wildcardChar(...)> 1729<listLabel(elem=label,...)> 1730>> 1731 1732/** Match a rule reference by invoking it possibly with arguments 1733 * and a return value or values. The 'rule' argument was the 1734 * target rule name, but now is type Rule, whose toString is 1735 * same: the rule name. Now though you can access full rule 1736 * descriptor stuff. 1737 */ 1738ruleRef(rule,label,elementIndex,args,scope) ::= << 1739this->followPush(FOLLOW_<rule.name>_in_<ruleName><elementIndex>); 1740<if(label)><label>=<endif><if(scope)>m_<scope:delegateName()>-><endif><rule.name>(<if(args)><args; separator=", "><endif>);<\n> 1741this->followPop(); 1742<checkRuleBacktrackFailure()> 1743>> 1744 1745/** ids+=r */ 1746ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= << 1747<ruleRef(...)> 1748<listLabel(elem=label,...)> 1749>> 1750 1751/** A lexer rule reference 1752 * The 'rule' argument was the target rule name, but now 1753 * is type Rule, whose toString is same: the rule name. 1754 * Now though you can access full rule descriptor stuff. 1755 */ 1756lexerRuleRef(rule,label,args,elementIndex,scope) ::= << 1757/* <description> */ 1758<if(label)> 1759{ 1760 ANTLR_MARKER <label>Start<elementIndex> = this->getCharIndex(); 1761 ANTLR_UINT32 <label>StartLine<elementIndex> = this->getLine(); 1762 ANTLR_UINT32 <label>StartCharPos<elementIndex> = this->getCharPositionInLine(); 1763 <if(scope)>m_<scope:delegateName()>-><endif>m<rule.name>(<if(scope)>m_<scope:delegateName()><endif> <if(args)>, <endif><args; separator=", ">); 1764 <checkRuleBacktrackFailure()> 1765 <label> = new CommonTokenType(); 1766 <label>->set_type( CommonTokenType::TOKEN_INVALID); 1767 <label>->set_startIndex( <label>Start<elementIndex> ); 1768 <label>->set_stopIndex( this->getCharIndex()-1 ); 1769 <label>->set_input( this->get_input() ); 1770 <label>->set_line( <label>StartLine<elementIndex> ); 1771 <label>->set_charPositionInLine( <label>StartCharPos<elementIndex> ); 1772} 1773<else> 1774<if(scope)>m_<scope:delegateName()>-><endif>m<rule.name>(<args; separator=", ">); 1775<checkRuleBacktrackFailure()> 1776<endif> 1777>> 1778 1779/** i+=INT in lexer */ 1780lexerRuleRefAndListLabel(rule,label,args,elementIndex,scope) ::= << 1781<lexerRuleRef(...)> 1782<listLabel(elem=label,...)> 1783>> 1784 1785/** EOF in the lexer */ 1786lexerMatchEOF(label,elementIndex) ::= << 1787<if(label)> 1788{ 1789 ANTLR_UINT32 <label>Start<elementIndex>; 1790 ANTLR_UINT32 <label>StartLine<elementIndex> = this->getLine(); 1791 ANTLR_UINT32 <label>StartCharPos<elementIndex> = this->getCharPositionInLine(); 1792 <labelType> <label>; 1793 <label>Start<elementIndex> = this->getCharIndex(); 1794 this->matchc(ANTLR_CHARSTREAM_EOF); 1795 <checkRuleBacktrackFailure()> 1796 <label> = new CommonTokenType(); 1797 <label>->set_type( CommonTokenType::TOKEN_EOF ); 1798 <label>->set_startIndex(<label>Start<elementIndex>); 1799 <label>->set_stopIndex(this->getCharIndex()-1); 1800 <label>->set_input( this->get_input() ); 1801 <label>->set_line( <label>StartLine<elementIndex> ); 1802 <label>->set_charPositionInLine( <label>StartCharPos<elementIndex> ); 1803} 1804<else> 1805 this->matchc(ANTLR_CHARSTREAM_EOF); 1806 <checkRuleBacktrackFailure()> 1807 <endif> 1808>> 1809 1810// used for left-recursive rules 1811recRuleDefArg() ::= "int <recRuleArg()>" 1812recRuleArg() ::= "_p" 1813recRuleAltPredicate(ruleName,opPrec) ::= "<recRuleArg()> \<= <opPrec>" 1814recRuleSetResultAction() ::= "root_0=$<ruleName>_primary.tree;" 1815recRuleSetReturnAction(src,name) ::= "$<name>=$<src>.<name>;" 1816 1817/** match ^(root children) in tree parser */ 1818tree(root, actionsAfterRoot, children, nullableChildList, enclosingTreeLevel, treeLevel) ::= << 1819<root:element()> 1820<actionsAfterRoot:element()> 1821<if(nullableChildList)> 1822if ( this->LA(1)== CommonTokenType::TOKEN_DOWN ) { 1823 this->matchToken(CommonTokenType::TOKEN_DOWN, NULL); 1824 <checkRuleBacktrackFailure()> 1825 <children:element()> 1826 this->matchToken(CommonTokenType::TOKEN_UP, NULL); 1827 <checkRuleBacktrackFailure()> 1828} 1829<else> 1830this->matchToken(CommonTokenType::TOKEN_DOWN, NULL); 1831<checkRuleBacktrackFailure()> 1832<children:element()> 1833this->matchToken(CommonTokenType::TOKEN_UP, NULL); 1834<checkRuleBacktrackFailure()> 1835<endif> 1836>> 1837 1838/** Every predicate is used as a validating predicate (even when it is 1839 * also hoisted into a prediction expression). 1840 */ 1841validateSemanticPredicate(pred,description) ::= << 1842if ( !(<evalPredicate(...)>) ) 1843{ 1844 <ruleBacktrackFailure()> 1845 <newFPE(...)> 1846} 1847>> 1848 1849newFPE() ::= << 1850 ExceptionBaseType* ex = new ANTLR_Exception\< <name>ImplTraits, FAILED_PREDICATE_EXCEPTION, StreamType>( this->get_rec(), "<description>" ); 1851 ex->set_ruleName( "<ruleName>" ); 1852 <\n> 1853>> 1854 1855// F i x e d D F A (if-then-else) 1856 1857dfaState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= << 1858 1859{ 1860 int LA<decisionNumber>_<stateNumber> = this->LA(<k>); 1861 <edges; separator="\nelse "> 1862 else 1863 { 1864<if(eotPredictsAlt)> 1865 alt<decisionNumber>=<eotPredictsAlt>; 1866<else> 1867 <ruleBacktrackFailure()> 1868 1869 <newNVException()> 1870 goto rule<ruleDescriptor.name>Ex; 1871 1872<endif> 1873 } 1874} 1875>> 1876 1877newNVException() ::= << 1878ExceptionBaseType* ex = new ANTLR_Exception\< <name>ImplTraits, NO_VIABLE_ALT_EXCEPTION, StreamType>( this->get_rec(), "<description>" ); 1879ex->set_decisionNum( <decisionNumber> ); 1880ex->set_state( <stateNumber> ); 1881<@noViableAltException()> 1882<\n> 1883>> 1884 1885/** Same as a normal DFA state except that we don't examine lookahead 1886 * for the bypass alternative. It delays error detection but this 1887 * is faster, smaller, and more what people expect. For (X)? people 1888 * expect "if ( LA(1)==X ) match(X);" and that's it. 1889 */ 1890dfaOptionalBlockState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= << 1891{ 1892 int LA<decisionNumber>_<stateNumber> = this->LA(<k>); 1893 <edges; separator="\nelse "> 1894} 1895>> 1896 1897/** A DFA state that is actually the loopback decision of a closure 1898 * loop. If end-of-token (EOT) predicts any of the targets then it 1899 * should act like a default clause (i.e., no error can be generated). 1900 * This is used only in the lexer so that for ('a')* on the end of a rule 1901 * anything other than 'a' predicts exiting. 1902 */ 1903 1904dfaLoopbackStateDecls()::= << 1905ANTLR_UINT32 LA<decisionNumber>_<stateNumber>; 1906>> 1907dfaLoopbackState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= << 1908{ 1909 /* dfaLoopbackState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) 1910 */ 1911 int LA<decisionNumber>_<stateNumber> = this->LA(<k>); 1912 <edges; separator="\nelse "><\n> 1913 <if(eotPredictsAlt)> 1914 <if(!edges)> 1915 alt<decisionNumber>=<eotPredictsAlt>; <! if no edges, don't gen ELSE !> 1916 <else> 1917 else 1918 { 1919 alt<decisionNumber>=<eotPredictsAlt>; 1920 }<\n> 1921 <endif> 1922 <endif> 1923} 1924>> 1925 1926/** An accept state indicates a unique alternative has been predicted */ 1927dfaAcceptState(alt) ::= "alt<decisionNumber>=<alt>;" 1928 1929/** A simple edge with an expression. If the expression is satisfied, 1930 * enter to the target state. To handle gated productions, we may 1931 * have to evaluate some predicates for this edge. 1932 */ 1933dfaEdge(labelExpr, targetState, predicates) ::= << 1934if ( (<labelExpr>)<if(predicates)> && (<predicates>)<endif>) 1935{ 1936 <targetState> 1937} 1938>> 1939 1940// F i x e d D F A (switch case) 1941 1942/** A DFA state where a SWITCH may be generated. The code generator 1943 * decides if this is possible: CodeGenerator.canGenerateSwitch(). 1944 */ 1945dfaStateSwitch(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= << 1946switch ( this->LA(<k>) ) 1947{ 1948<edges; separator="\n"> 1949 1950default: 1951<if(eotPredictsAlt)> 1952 alt<decisionNumber>=<eotPredictsAlt>; 1953<else> 1954 <ruleBacktrackFailure()> 1955 <newNVException()> 1956 goto rule<ruleDescriptor.name>Ex;<\n> 1957<endif> 1958}<\n> 1959>> 1960 1961dfaOptionalBlockStateSwitch(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= << 1962switch ( this->LA(<k>) ) 1963{ 1964 <edges; separator="\n"> 1965}<\n> 1966>> 1967 1968dfaLoopbackStateSwitch(k, edges,eotPredictsAlt,description,stateNumber,semPredState) ::= << 1969switch ( this->LA(<k>) ) 1970{ 1971<edges; separator="\n"><\n> 1972<if(eotPredictsAlt)> 1973default: 1974 alt<decisionNumber>=<eotPredictsAlt>; 1975 break;<\n> 1976<endif> 1977}<\n> 1978>> 1979 1980dfaEdgeSwitch(labels, targetState) ::= << 1981<labels:{it |case <it>:}; separator="\n"> 1982 { 1983 <targetState> 1984 } 1985 break; 1986>> 1987 1988// C y c l i c D F A 1989 1990/** The code to initiate execution of a cyclic DFA; this is used 1991 * in the rule to predict an alt just like the fixed DFA case. 1992 * The <name> attribute is inherited via the parser, lexer, ... 1993 */ 1994dfaDecision(decisionNumber,description) ::= << 1995alt<decisionNumber> = cdfa<decisionNumber>.predict(this, this->get_rec(), this->get_istream(), cdfa<decisionNumber> ); 1996<checkRuleBacktrackFailure()> 1997>> 1998 1999/* Dump DFA tables as static initialized arrays of shorts(16 bits)/characters(8 bits) 2000 * which are then used to statically initialize the dfa structure, which means that there 2001 * is no runtime initialization whatsoever, other than anything the C compiler might 2002 * need to generate. In general the C compiler will lay out memory such that there is no 2003 * runtime code required. 2004 */ 2005cyclicDFA(dfa) ::= << 2006/** Static dfa state tables for Cyclic dfa: 2007 * <dfa.description> 2008 */ 2009static const ANTLR_INT32 dfa<dfa.decisionNumber>_eot[<dfa.numberOfStates>] = 2010 { 2011 <dfa.eot; wrap="\n", separator=", ", null="-1"> 2012 }; 2013static const ANTLR_INT32 dfa<dfa.decisionNumber>_eof[<dfa.numberOfStates>] = 2014 { 2015 <dfa.eof; wrap="\n", separator=", ", null="-1"> 2016 }; 2017static const ANTLR_INT32 dfa<dfa.decisionNumber>_min[<dfa.numberOfStates>] = 2018 { 2019 <dfa.min; wrap="\n", separator=", ", null="-1"> 2020 }; 2021static const ANTLR_INT32 dfa<dfa.decisionNumber>_max[<dfa.numberOfStates>] = 2022 { 2023 <dfa.max; wrap="\n", separator=", ", null="-1"> 2024 }; 2025static const ANTLR_INT32 dfa<dfa.decisionNumber>_accept[<dfa.numberOfStates>] = 2026 { 2027 <dfa.accept; wrap="\n", separator=", ", null="-1"> 2028 }; 2029static const ANTLR_INT32 dfa<dfa.decisionNumber>_special[<dfa.numberOfStates>] = 2030 { 2031 <dfa.special; wrap="\n", separator=", ", null="-1"> 2032 }; 2033 2034/** Used when there is no transition table entry for a particular state */ 2035static const ANTLR_INT32* dfa<dfa.decisionNumber>_T_empty = NULL; 2036 2037<dfa.edgeTransitionClassMap.keys:{ table | 2038static const ANTLR_INT32 dfa<dfa.decisionNumber>_T<i0>[] = 2039 { 2040 <table; separator=", ", wrap="\n", null="-1"> 2041 \};<\n>}; null = ""> 2042 2043/* Transition tables are a table of sub tables, with some tables 2044 * reused for efficiency. 2045 */ 2046static const ANTLR_INT32 * const dfa<dfa.decisionNumber>_transitions[] = 2047{ 2048 <dfa.transitionEdgeTables:{xref|dfa<dfa.decisionNumber>_T<xref>}; separator=", ", wrap="\n", null="NULL"> 2049}; 2050 2051<@errorMethod()> 2052 2053/* Declare tracking structure for Cyclic DFA <dfa.decisionNumber> 2054 */ 2055class <name>CyclicDFA<dfa.decisionNumber> : public CyclicDFA\< <name>ImplTraits, <name> >, public <name>Tokens 2056{ 2057public: 2058 typedef CyclicDFA\< <name>ImplTraits, <name> > BaseType; 2059 typedef BaseType::ContextType CtxType; 2060 2061private: 2062<if(dfa.specialStateSTs)> 2063 //to maintain C-Target compatibility, we need to make some of ctx functions look like member funcs 2064 CtxType* m_ctx; 2065<endif> 2066 2067public: 2068 <name>CyclicDFA<dfa.decisionNumber>( ANTLR_INT32 decisionNumber 2069 , const ANTLR_UCHAR* description 2070 , const ANTLR_INT32* const eot 2071 , const ANTLR_INT32* const eof 2072 , const ANTLR_INT32* const min 2073 , const ANTLR_INT32* const max 2074 , const ANTLR_INT32* const accept 2075 , const ANTLR_INT32* const special 2076 , const ANTLR_INT32* const *const transition) 2077 :BaseType( decisionNumber, description, eot, eof, min, max, accept, 2078 special, transition ) 2079 { 2080 <if(dfa.specialStateSTs)> 2081 m_ctx = NULL; 2082 <endif> 2083 } 2084 2085 <if(dfa.specialStateSTs)> 2086 ANTLR_UINT32 LA(ANTLR_INT32 i) 2087 { 2088 return m_ctx->LA(i); 2089 } 2090 2091 <if(PARSER)> 2092 const CtxType::CommonTokenType* LT(ANTLR_INT32 k) 2093 { 2094 return m_ctx->LT(k); 2095 } 2096 <endif> 2097 <if(synpreds)> 2098 template\<typename PredType> 2099 bool msynpred( PredType pred ) 2100 { 2101 return m_ctx->msynpred(pred); 2102 } 2103 <endif> 2104 2105 ANTLR_INT32 specialStateTransition(CtxType * ctx, RecognizerType* recognizer, IntStreamType* is, ANTLR_INT32 s) 2106 { 2107 ANTLR_INT32 _s; 2108 2109 m_ctx = ctx; 2110 _s = s; 2111 switch (s) 2112 { 2113 <dfa.specialStateSTs:{state | 2114 case <i0>: 2115 2116 <state>}; separator="\n"> 2117 } 2118 <if(backtracking)> 2119 if ( ctx->get_backtracking() > 0) 2120 { 2121 ctx->set_failedflag( true ); 2122 return -1; 2123 } 2124 <endif> 2125 ExceptionBaseType* ex = new ANTLR_Exception\< <name>ImplTraits, NO_VIABLE_ALT_EXCEPTION, StreamType>( recognizer, "<dfa.description>" ); 2126 ex->set_decisionNum( <dfa.decisionNumber> ); 2127 ex->set_state(_s); 2128 <@noViableAltException()> 2129 return -1; 2130 } 2131 <endif> 2132}; 2133 2134static <name>CyclicDFA<dfa.decisionNumber> cdfa<dfa.decisionNumber>( 2135 <dfa.decisionNumber>, /* Decision number of this dfa */ 2136 /* Which decision this represents: */ 2137 (const ANTLR_UCHAR*)"<dfa.description>", 2138 dfa<dfa.decisionNumber>_eot, /* EOT table */ 2139 dfa<dfa.decisionNumber>_eof, /* EOF table */ 2140 dfa<dfa.decisionNumber>_min, /* Minimum tokens for each state */ 2141 dfa<dfa.decisionNumber>_max, /* Maximum tokens for each state */ 2142 dfa<dfa.decisionNumber>_accept, /* Accept table */ 2143 dfa<dfa.decisionNumber>_special, /* Special transition states */ 2144 dfa<dfa.decisionNumber>_transitions /* Table of transition tables */ 2145 2146 ); 2147 2148 2149/* End of Cyclic DFA <dfa.decisionNumber> 2150 * --------------------- 2151 */ 2152>> 2153 2154/** A state in a cyclic DFA; it's a special state and part of a big switch on 2155 * state. 2156 */ 2157cyclicDFAState(decisionNumber,stateNumber,edges,needErrorClause,semPredState) ::= << 2158{ 2159 ANTLR_UINT32 LA<decisionNumber>_<stateNumber>;<\n> 2160 ANTLR_MARKER index<decisionNumber>_<stateNumber>;<\n> 2161 2162 LA<decisionNumber>_<stateNumber> = ctx->LA(1);<\n> 2163 <if(semPredState)> <! get next lookahead symbol to test edges, then rewind !> 2164 index<decisionNumber>_<stateNumber> = ctx->index();<\n> 2165 ctx->rewindLast();<\n> 2166 <endif> 2167 s = -1; 2168 <edges; separator="\nelse "> 2169 <if(semPredState)> <! return input cursor to state before we rewound !> 2170 ctx->seek(index<decisionNumber>_<stateNumber>);<\n> 2171 <endif> 2172 if ( s>=0 ) 2173 { 2174 return s; 2175 } 2176} 2177break; 2178>> 2179 2180/** Just like a fixed DFA edge, test the lookahead and indicate what 2181 * state to jump to next if successful. 2182 */ 2183cyclicDFAEdge(labelExpr, targetStateNumber, edgeNumber, predicates) ::= << 2184if ( (<labelExpr>) <if(predicates)>&& (<predicates>)<endif> ) 2185{ 2186 s = <targetStateNumber>; 2187}<\n> 2188>> 2189 2190/** An edge pointing at end-of-token; essentially matches any char; 2191 * always jump to the target. 2192 */ 2193eotDFAEdge(targetStateNumber,edgeNumber, predicates) ::= << 2194 s = <targetStateNumber>;<\n> 2195>> 2196 2197 2198// D F A E X P R E S S I O N S 2199 2200andPredicates(left,right) ::= "( (<left>) && (<right>) )" 2201 2202orPredicates(operands) ::= "(<operands:{o|(<o>)}; separator=\"||\">)" 2203 2204notPredicate(pred) ::= "!( <evalPredicate(pred,{})> )" 2205 2206evalPredicate(pred,description) ::= "(<pred>)" 2207 2208evalSynPredicate(pred,description) ::= "this->msynpred( antlr3::ClassForwarder\<<pred>>() )" 2209 2210lookaheadTest(atom,k,atomAsInt) ::= "LA<decisionNumber>_<stateNumber> == <atom>" 2211 2212/** Sometimes a lookahead test cannot assume that LA(k) is in a temp variable 2213 * somewhere. Must ask for the lookahead directly. 2214 */ 2215isolatedLookaheadTest(atom,k,atomAsInt) ::= "this->LA(<k>) == <atom>" 2216 2217lookaheadRangeTest(lower,upper,k,rangeNumber,lowerAsInt,upperAsInt) ::= <% 2218((LA<decisionNumber>_<stateNumber> >= <lower>) && (LA<decisionNumber>_<stateNumber> \<= <upper>)) 2219%> 2220 2221isolatedLookaheadRangeTest(lower,upper,k,rangeNumber,lowerAsInt,upperAsInt) ::= "((this->LA(<k>) >= <lower>) && (this->LA(<k>) \<= <upper>))" 2222 2223setTest(ranges) ::= "<ranges; separator=\" || \">" 2224 2225// A T T R I B U T E S 2226 2227makeScopeSet() ::= << 2228/* makeScopeSet() 2229 */ 2230 /** Definition of the <scope.name> scope variable tracking 2231 * structure. An instance of this structure is created by calling 2232 * <name>_<scope.name>Push(). 2233 */ 2234struct <scopeStruct(sname=scope.name,...)> 2235{ 2236 /* ============================================================================= 2237 * Programmer defined variables... 2238 */ 2239 <scope.attributes:{it |<it.decl>;}; separator="\n"> 2240 2241 /* End of programmer defined variables 2242 * ============================================================================= 2243 */ 2244}; 2245 2246>> 2247 2248globalAttributeScopeDecl(scope) ::= << 2249<if(scope.attributes)> 2250/* globalAttributeScopeDecl(scope) 2251 */ 2252<makeScopeSet(...)> 2253<endif> 2254>> 2255 2256ruleAttributeScopeDecl(scope) ::= << 2257<if(scope.attributes)> 2258/* ruleAttributeScopeDecl(scope) 2259 */ 2260<makeScopeSet(...)> 2261<endif> 2262>> 2263 2264globalAttributeScopeDef(scope) ::= 2265<< 2266/* globalAttributeScopeDef(scope) 2267 */ 2268<if(scope.attributes)> 2269 2270StackType\< <scopeStruct(sname=scope.name)> > <scopeStack(sname=scope.name)>; 2271 2272<endif> 2273>> 2274 2275ruleAttributeScopeDef(scope) ::= << 2276<if(scope.attributes)> 2277/* ruleAttributeScopeDef(scope) 2278 */ 2279StackType\< <scopeStruct(sname=scope.name)> > <scopeStack(sname=scope.name,...)>; 2280 2281<endif> 2282>> 2283 2284scopeStruct(sname) ::= << 2285<sname>Scope 2286>> 2287 2288scopeStack(sname) ::= << 2289m_<sname>_stack 2290>> 2291 2292returnStructName(r) ::= "<r.name>_return" 2293 2294returnType() ::= <% 2295<if(!ruleDescriptor.isSynPred)> 2296<if(ruleDescriptor.hasMultipleReturnValues)> 2297<ruleDescriptor.grammar.recognizerName>::<ruleDescriptor:returnStructName()> 2298<else> 2299<if(ruleDescriptor.hasSingleReturnValue)> 2300<ruleDescriptor.singleValueReturnType> 2301<else> 2302void 2303<endif> 2304<endif> 2305<else> 2306bool 2307<endif> 2308%> 2309 2310/** Generate the C type associated with a single or multiple return 2311 * value(s). 2312 */ 2313ruleLabelType(referencedRule) ::= <% 2314<if(referencedRule.hasMultipleReturnValues)> 2315<referencedRule.name>_return 2316<else> 2317<if(referencedRule.hasSingleReturnValue)> 2318<referencedRule.singleValueReturnType> 2319<else> 2320void 2321<endif> 2322<endif> 2323%> 2324 2325delegateName(d) ::= << 2326<if(d.label)><d.label><else>g<d.name><endif> 2327>> 2328 2329/** Using a type to init value map, try to init a type; if not in table 2330 * must be an object, default value is "0". 2331 */ 2332initValue(typeName) ::= << 2333 = <cTypeInitMap.(typeName)> 2334>> 2335 2336/** Define a rule label */ 2337ruleLabelDef(label) ::= << 2338<ruleLabelType(referencedRule=label.referencedRule)> <label.label.text>; 2339>> 2340/** Rule label default value */ 2341ruleLabelInitVal(label) ::= << 2342>> 2343 2344ASTLabelType() ::= "<if(recognizer.ASTLabelType)><recognizer.ASTLabelType><else>ImplTraits::TreeType*<endif>" 2345 2346/** Define a return struct for a rule if the code needs to access its 2347 * start/stop tokens, tree stuff, attributes, ... Leave a hole for 2348 * subgroups to stick in members. 2349 */ 2350returnScope(scope) ::= << 2351<if(!ruleDescriptor.isSynPred)> 2352<if(ruleDescriptor.hasMultipleReturnValues)> 2353<if(!TREE_PARSER)> 2354struct <ruleDescriptor:returnStructName()> : public <name>ImplTraits::RuleReturnValueType 2355{ 2356public: 2357 typedef <name>ImplTraits::RuleReturnValueType BaseType; 2358 <ruleDescriptor:returnStructName()>() 2359 : BaseType() 2360 <if(scope)>, <scope.attributes:{it | <it.name>() }; separator=","><endif> 2361 { init(); } 2362 <ruleDescriptor:returnStructName()>( BaseParserType* parser ) 2363 : BaseType(parser) 2364 <if(scope)>, <scope.attributes:{it | <it.name>() }; separator=","><endif> 2365 { init(); } 2366 <ruleDescriptor:returnStructName()>( const <ruleDescriptor:returnStructName()>& other ) 2367 : BaseType(other) 2368 <if(scope)>, <scope.attributes:{it | <it.name>(other.<it.name>) }; separator=", "><endif> 2369 { copy(other); } 2370 ~<ruleDescriptor:returnStructName()>() 2371 { 2372 <@ruleReturnMembersDelete()> 2373 } 2374 2375 <ruleDescriptor:returnStructName()>& 2376 operator=( const <ruleDescriptor:returnStructName()>& other ) 2377 { 2378 BaseType::operator=( other ); 2379 <if(scope)><scope.attributes:{it | <it.name> = other.<it.name>; }; separator="\n"><endif> 2380 copy(other); 2381 return *this; 2382 } 2383 <@ruleReturnMembers()> 2384 void init() { <@ruleReturnMembersInit()> } 2385 void copy( const <ruleDescriptor:returnStructName()>& other) { <@ruleReturnMembersCopy()> } 2386<else> 2387struct <ruleDescriptor:returnStructName()> 2388{ 2389public: 2390 <name>ImplTraits::<recognizer.ASTLabelType> start; 2391 <name>ImplTraits::<recognizer.ASTLabelType> stop; 2392 <ruleDescriptor:returnStructName()>( const <ruleDescriptor:returnStructName()>& other ) 2393 <if(scope.attributes)> 2394 <scope.attributes:{it | <it.name>(other.<it.name>) }; separator=","> 2395 <endif> 2396 { 2397 start = other.start; 2398 stop = other.stop; 2399 } 2400 2401 <ruleDescriptor:returnStructName()>& 2402 operator=( const <ruleDescriptor:returnStructName()>& other ) 2403 { 2404 start = other.start; 2405 stop = other.stop; 2406 2407 <scope.attributes:{it | <it.name> = other.<it.name>; }; separator="\n"> 2408 return *this; 2409 } 2410<endif> 2411 <if(scope)><scope.attributes:{it |<it.type> <it.name>;}; separator="\n"><endif> 2412}; 2413 2414<endif> 2415<endif> 2416>> 2417 2418parameterScope(scope) ::= << 2419<scope.attributes:{it |<it.decl>}; separator=", "> 2420>> 2421 2422parameterAttributeRef(attr) ::= "<attr.name>" 2423parameterSetAttributeRef(attr,expr) ::= "<attr.name>=<expr>;" 2424 2425/** Note that the scopeAttributeRef does not have access to the 2426 * grammar name directly 2427 */ 2428scopeAttributeRef(scope,attr,index,negIndex) ::= <% 2429<if(negIndex)> 2430 m_<scope>_stack.at( m_<scope>_stack.size()-<negIndex>-1).<attr.name> 2431<else> 2432<if(index)> 2433 m_<scope>_stack.at(<index>).<attr.name> 2434<else> 2435 m_<scope>_stack.peek().<attr.name> 2436<endif> 2437<endif> 2438%> 2439 2440scopeSetAttributeRef(scope,attr,expr,index,negIndex) ::= <% 2441<if(negIndex)> 2442 m_<scope>_stack.at( m_<scope>_stack.size()-<negIndex>-1).<attr.name> = <expr>; 2443<else> 2444<if(index)> 2445 m_<scope>_stack.at(<index>).<attr.name> = <expr>; 2446<else> 2447 m_<scope>_stack.peek().<attr.name> =<expr>; 2448<endif> 2449<endif> 2450%> 2451 2452/** $x is either global scope or x is rule with dynamic scope; refers 2453 * to stack itself not top of stack. This is useful for predicates 2454 * like {$function.size()>0 && $function::name.equals("foo")}? 2455 */ 2456isolatedDynamicScopeRef(scope) ::= "<scope>_stack" 2457 2458/** reference an attribute of rule; might only have single return value */ 2459ruleLabelRef(referencedRule,scope,attr) ::= << 2460<if(referencedRule.hasMultipleReturnValues)> 2461<scope>.<attr.name> 2462<else> 2463<scope> 2464<endif> 2465>> 2466 2467returnAttributeRef(ruleDescriptor,attr) ::= << 2468<if(ruleDescriptor.hasMultipleReturnValues)> 2469retval.<attr.name> 2470<else> 2471<attr.name> 2472<endif> 2473>> 2474 2475returnSetAttributeRef(ruleDescriptor,attr,expr) ::= << 2476<if(ruleDescriptor.hasMultipleReturnValues)> 2477retval.<attr.name>=<expr>; 2478<else> 2479<attr.name>=<expr>; 2480<endif> 2481>> 2482 2483/** How to translate $tokenLabel */ 2484tokenLabelRef(label) ::= "<label>" 2485 2486/** ids+=ID {$ids} or e+=expr {$e} */ 2487listLabelRef(label) ::= "list_<label>" 2488 2489 2490// not sure the next are the right approach 2491// 2492tokenLabelPropertyRef_text(scope,attr) ::= "(<scope>->getText())" 2493tokenLabelPropertyRef_type(scope,attr) ::= "(<scope>->get_type())" 2494tokenLabelPropertyRef_line(scope,attr) ::= "(<scope>->get_line())" 2495tokenLabelPropertyRef_pos(scope,attr) ::= "(<scope>->get_charPositionInLine())" 2496tokenLabelPropertyRef_channel(scope,attr) ::= "(<scope>->get_channel())" 2497tokenLabelPropertyRef_index(scope,attr) ::= "(<scope>->get_tokenIndex())" 2498tokenLabelPropertyRef_tree(scope,attr) ::= "(<scope>->get_tree())" 2499tokenLabelPropertyRef_int(scope,attr) ::= "(<name>ImplTraits::ConvertToInt32(<scope>->getText()))" 2500 2501ruleLabelPropertyRef_start(scope,attr) ::= "(<scope>.start)" 2502ruleLabelPropertyRef_stop(scope,attr) ::= "(<scope>.stop)" 2503ruleLabelPropertyRef_tree(scope,attr) ::= "(<scope>.tree)" 2504ruleLabelPropertyRef_text(scope,attr) ::= << 2505<if(TREE_PARSER)> 2506(this->get_strstream()->toStringSS(<scope>.start, <scope>.start)) 2507<else> 2508(this->get_strstream()->toStringTT(<scope>.start, <scope>.stop)) 2509<endif> 2510>> 2511 2512ruleLabelPropertyRef_st(scope,attr) ::= "<scope>.st" 2513 2514/** Isolated $RULE ref ok in lexer as it's a Token */ 2515lexerRuleLabel(label) ::= "<label>" 2516 2517lexerRuleLabelPropertyRef_type(scope,attr) ::= "(<scope>->get_type())" 2518lexerRuleLabelPropertyRef_line(scope,attr) ::= "(<scope>->get_line())" 2519lexerRuleLabelPropertyRef_pos(scope,attr) ::= "(<scope>->get_charPositionInLine())" 2520lexerRuleLabelPropertyRef_channel(scope,attr) ::= "(<scope>->get_channel())" 2521lexerRuleLabelPropertyRef_index(scope,attr) ::= "(<scope>->get_tokenIndex())" 2522lexerRuleLabelPropertyRef_text(scope,attr) ::= "(<scope>->getText())" 2523 2524// Somebody may ref $template or $tree or $stop within a rule: 2525rulePropertyRef_start(scope,attr) ::= "retval.start" 2526rulePropertyRef_stop(scope,attr) ::= "retval.stop" 2527rulePropertyRef_tree(scope,attr) ::= "retval.tree" 2528rulePropertyRef_text(scope,attr) ::= << 2529<if(TREE_PARSER)> 2530this->get_input()->toStringSS( this->get_adaptor()->getTokenStartIndex(retval.start), this->get_adaptor()->getTokenStopIndex(retval.start)) 2531<else> 2532this->get_strstream()->toStringTT(retval.start, this->LT(-1)) 2533<endif> 2534>> 2535rulePropertyRef_st(scope,attr) ::= "retval.st" 2536 2537lexerRulePropertyRef_text(scope,attr) ::= "this->getText()" 2538lexerRulePropertyRef_type(scope,attr) ::= "_type" 2539lexerRulePropertyRef_line(scope,attr) ::= "this->get_state()->get_tokenStartLine()" 2540lexerRulePropertyRef_pos(scope,attr) ::= "this->get_state()->get_tokenStartCharPositionInLine()" 2541lexerRulePropertyRef_channel(scope,attr) ::= "this->get_state()->get_channel()" 2542lexerRulePropertyRef_start(scope,attr) ::= "this->get_state()->get_tokenStartCharIndex()" 2543lexerRulePropertyRef_stop(scope,attr) ::= "(this->getCharIndex()-1)" 2544lexerRulePropertyRef_index(scope,attr) ::= "-1" // undefined token index in lexer 2545lexerRulePropertyRef_int(scope,attr) ::= "(<name>ImplTraits::ConvertToInt32(<scope>->getText()))" 2546 2547 2548// setting $st and $tree is allowed in local rule. everything else is flagged as error 2549ruleSetPropertyRef_tree(scope,attr,expr) ::= "retval.tree=<expr>;" 2550ruleSetPropertyRef_st(scope,attr,expr) ::= "retval.st=<expr>;" 2551 2552 2553/** How to deal with an @after for C targets. Because we cannot rely on 2554 * any garbage collection, after code is executed even in backtracking 2555 * mode. Must be documented clearly. 2556 */ 2557execAfter(action) ::= << 2558{ 2559 <action> 2560} 2561>> 2562 2563/** How to execute an action (when not backtracking) */ 2564execAction(action) ::= << 2565<if(backtracking)> 2566<if(actions.(actionScope).synpredgate)> 2567if ( <actions.(actionScope).synpredgate> ) 2568{ 2569 <action> 2570} 2571<else> 2572if ( BACKTRACKING == 0 ) 2573{ 2574 <action> 2575} 2576<endif> 2577<else> 2578{ 2579 <action> 2580} 2581<endif> 2582>> 2583 2584// M I S C (properties, etc...) 2585 2586bitsetDeclare(bitsetname, words64, traits) ::= << 2587 2588/** Bitset defining follow set for error recovery in rule state: <name> */ 2589static ANTLR_BITWORD <bitsetname>_bits[] = { <words64:{it |ANTLR_UINT64_LIT(<it>)}; separator=", "> }; 2590static <traits>::BitsetListType <bitsetname>( <bitsetname>_bits, <length(words64)> ); 2591>> 2592 2593codeFileExtension() ::= ".cpp" 2594 2595true_value() ::= "true" 2596false_value() ::= "false" 2597 2598