1*16467b97STreehugger Robot /** \file 2*16467b97STreehugger Robot * Base implementation of an ANTLR3 parser. 3*16467b97STreehugger Robot * 4*16467b97STreehugger Robot * 5*16467b97STreehugger Robot */ 6*16467b97STreehugger Robot #ifndef _ANTLR3_PARSER_HPP 7*16467b97STreehugger Robot #define _ANTLR3_PARSER_HPP 8*16467b97STreehugger Robot 9*16467b97STreehugger Robot // [The "BSD licence"] 10*16467b97STreehugger Robot // Copyright (c) 2005-2009 Gokulakannan Somasundaram, ElectronDB 11*16467b97STreehugger Robot 12*16467b97STreehugger Robot // 13*16467b97STreehugger Robot // All rights reserved. 14*16467b97STreehugger Robot // 15*16467b97STreehugger Robot // Redistribution and use in source and binary forms, with or without 16*16467b97STreehugger Robot // modification, are permitted provided that the following conditions 17*16467b97STreehugger Robot // are met: 18*16467b97STreehugger Robot // 1. Redistributions of source code must retain the above copyright 19*16467b97STreehugger Robot // notice, this list of conditions and the following disclaimer. 20*16467b97STreehugger Robot // 2. Redistributions in binary form must reproduce the above copyright 21*16467b97STreehugger Robot // notice, this list of conditions and the following disclaimer in the 22*16467b97STreehugger Robot // documentation and/or other materials provided with the distribution. 23*16467b97STreehugger Robot // 3. The name of the author may not be used to endorse or promote products 24*16467b97STreehugger Robot // derived from this software without specific prior written permission. 25*16467b97STreehugger Robot // 26*16467b97STreehugger Robot // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 27*16467b97STreehugger Robot // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 28*16467b97STreehugger Robot // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29*16467b97STreehugger Robot // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 30*16467b97STreehugger Robot // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 31*16467b97STreehugger Robot // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32*16467b97STreehugger Robot // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33*16467b97STreehugger Robot // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34*16467b97STreehugger Robot // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35*16467b97STreehugger Robot // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36*16467b97STreehugger Robot 37*16467b97STreehugger Robot #include "antlr3defs.hpp" 38*16467b97STreehugger Robot 39*16467b97STreehugger Robot ANTLR_BEGIN_NAMESPACE() 40*16467b97STreehugger Robot 41*16467b97STreehugger Robot /** This is the main interface for an ANTLR3 parser. 42*16467b97STreehugger Robot */ 43*16467b97STreehugger Robot template< class ImplTraits > 44*16467b97STreehugger Robot class Parser : public ImplTraits::template RecognizerType< typename ImplTraits::TokenStreamType > 45*16467b97STreehugger Robot { 46*16467b97STreehugger Robot public: 47*16467b97STreehugger Robot typedef typename ImplTraits::StringType StringType; 48*16467b97STreehugger Robot typedef typename ImplTraits::TokenStreamType TokenStreamType; 49*16467b97STreehugger Robot typedef typename TokenStreamType::IntStreamType IntStreamType; 50*16467b97STreehugger Robot typedef TokenStreamType StreamType; 51*16467b97STreehugger Robot 52*16467b97STreehugger Robot typedef typename ImplTraits::template RecognizerType< typename ImplTraits::TokenStreamType > RecognizerType; 53*16467b97STreehugger Robot typedef typename RecognizerType::RecognizerSharedStateType RecognizerSharedStateType; 54*16467b97STreehugger Robot 55*16467b97STreehugger Robot typedef DebugEventListener<ImplTraits> DebugEventListenerType; 56*16467b97STreehugger Robot typedef typename ImplTraits::CommonTokenType CommonTokenType; 57*16467b97STreehugger Robot typedef CommonTokenType TokenType; 58*16467b97STreehugger Robot typedef typename ImplTraits::BitsetListType BitsetListType; 59*16467b97STreehugger Robot typedef ANTLR_ExceptionBase<ImplTraits, TokenStreamType> ExceptionBaseType; 60*16467b97STreehugger Robot typedef Empty TokenSourceType; 61*16467b97STreehugger Robot 62*16467b97STreehugger Robot typedef typename RecognizerSharedStateType::FollowingType FollowingType; 63*16467b97STreehugger Robot typedef typename RecognizerSharedStateType::RuleMemoType RuleMemoType; 64*16467b97STreehugger Robot typedef typename ImplTraits::DebugEventListenerType DebuggerType; 65*16467b97STreehugger Robot 66*16467b97STreehugger Robot private: 67*16467b97STreehugger Robot /** A provider of a tokenstream interface, for the parser to consume 68*16467b97STreehugger Robot * tokens from. 69*16467b97STreehugger Robot */ 70*16467b97STreehugger Robot TokenStreamType* m_tstream; 71*16467b97STreehugger Robot 72*16467b97STreehugger Robot public: 73*16467b97STreehugger Robot Parser( ANTLR_UINT32 sizeHint, RecognizerSharedStateType* state ); 74*16467b97STreehugger Robot Parser( ANTLR_UINT32 sizeHint, TokenStreamType* tstream, RecognizerSharedStateType* state ); 75*16467b97STreehugger Robot Parser( ANTLR_UINT32 sizeHint, TokenStreamType* tstream, DebugEventListenerType* dbg, 76*16467b97STreehugger Robot RecognizerSharedStateType* state ); 77*16467b97STreehugger Robot TokenStreamType* get_tstream() const; 78*16467b97STreehugger Robot TokenStreamType* get_input() const; 79*16467b97STreehugger Robot IntStreamType* get_istream() const; 80*16467b97STreehugger Robot RecognizerType* get_rec(); 81*16467b97STreehugger Robot 82*16467b97STreehugger Robot //same as above. Just that get_istream exists for lexer, parser, treeparser 83*16467b97STreehugger Robot //get_parser_istream exists only for parser, treeparser. So use it accordingly 84*16467b97STreehugger Robot IntStreamType* get_parser_istream() const; 85*16467b97STreehugger Robot 86*16467b97STreehugger Robot /** A pointer to a function that installs a debugger object (it also 87*16467b97STreehugger Robot * installs the debugging versions of the parser methods. This means that 88*16467b97STreehugger Robot * a non debug parser incurs no overhead because of the debugging stuff. 89*16467b97STreehugger Robot */ 90*16467b97STreehugger Robot void setDebugListener(DebugEventListenerType* dbg); 91*16467b97STreehugger Robot 92*16467b97STreehugger Robot /** A pointer to a function that installs a token stream 93*16467b97STreehugger Robot * for the parser. 94*16467b97STreehugger Robot */ 95*16467b97STreehugger Robot void setTokenStream(TokenStreamType*); 96*16467b97STreehugger Robot 97*16467b97STreehugger Robot /** A pointer to a function that returns the token stream for this 98*16467b97STreehugger Robot * parser. 99*16467b97STreehugger Robot */ 100*16467b97STreehugger Robot TokenStreamType* getTokenStream(); 101*16467b97STreehugger Robot 102*16467b97STreehugger Robot void exConstruct(); 103*16467b97STreehugger Robot TokenType* getMissingSymbol( IntStreamType* istream, ExceptionBaseType* e, 104*16467b97STreehugger Robot ANTLR_UINT32 expectedTokenType, BitsetListType* follow); 105*16467b97STreehugger Robot 106*16467b97STreehugger Robot void mismatch(ANTLR_UINT32 ttype, BitsetListType* follow); 107*16467b97STreehugger Robot 108*16467b97STreehugger Robot /** Pointer to a function that knows how to free resources of an ANTLR3 parser. 109*16467b97STreehugger Robot */ 110*16467b97STreehugger Robot ~Parser(); 111*16467b97STreehugger Robot 112*16467b97STreehugger Robot void fillExceptionData( ExceptionBaseType* ex ); 113*16467b97STreehugger Robot void displayRecognitionError( ANTLR_UINT8** tokenNames, ExceptionBaseType* ex ); 114*16467b97STreehugger Robot 115*16467b97STreehugger Robot //convenience functions exposed in .stg 116*16467b97STreehugger Robot const RecognizerType* get_recognizer() const; 117*16467b97STreehugger Robot RecognizerSharedStateType* get_psrstate() const; 118*16467b97STreehugger Robot void set_psrstate(RecognizerSharedStateType* state); 119*16467b97STreehugger Robot bool haveParsedRule(ANTLR_MARKER ruleIndex); 120*16467b97STreehugger Robot void memoize(ANTLR_MARKER ruleIndex, ANTLR_MARKER ruleParseStart); 121*16467b97STreehugger Robot ANTLR_MARKER index() const; 122*16467b97STreehugger Robot bool hasException() const; 123*16467b97STreehugger Robot ExceptionBaseType* get_exception() const; 124*16467b97STreehugger Robot const CommonTokenType* matchToken( ANTLR_UINT32 ttype, BitsetListType* follow ); 125*16467b97STreehugger Robot void matchAnyToken(); 126*16467b97STreehugger Robot const FollowingType& get_follow_stack() const; 127*16467b97STreehugger Robot void followPush( const BitsetListType& follow ); 128*16467b97STreehugger Robot void followPop(); 129*16467b97STreehugger Robot void precover(); 130*16467b97STreehugger Robot void preporterror(); 131*16467b97STreehugger Robot ANTLR_UINT32 LA(ANTLR_INT32 i); 132*16467b97STreehugger Robot const CommonTokenType* LT(ANTLR_INT32 k); 133*16467b97STreehugger Robot void constructEx(); 134*16467b97STreehugger Robot void consume(); 135*16467b97STreehugger Robot ANTLR_MARKER mark(); 136*16467b97STreehugger Robot void rewind(ANTLR_MARKER marker); 137*16467b97STreehugger Robot void rewindLast(); 138*16467b97STreehugger Robot void seek(ANTLR_MARKER index); 139*16467b97STreehugger Robot bool get_perror_recovery() const; 140*16467b97STreehugger Robot void set_perror_recovery( bool val ); 141*16467b97STreehugger Robot bool hasFailed() const; 142*16467b97STreehugger Robot bool get_failedflag() const; 143*16467b97STreehugger Robot void set_failedflag( bool failed ); 144*16467b97STreehugger Robot ANTLR_INT32 get_backtracking() const; 145*16467b97STreehugger Robot void inc_backtracking(); 146*16467b97STreehugger Robot void dec_backtracking(); 147*16467b97STreehugger Robot CommonTokenType* recoverFromMismatchedSet(BitsetListType* follow); 148*16467b97STreehugger Robot bool recoverFromMismatchedElement(BitsetListType* follow); 149*16467b97STreehugger Robot RuleMemoType* getRuleMemo() const; 150*16467b97STreehugger Robot DebuggerType* get_debugger() const; 151*16467b97STreehugger Robot TokenStreamType* get_strstream() const; 152*16467b97STreehugger Robot void setRuleMemo(RuleMemoType* rulememo); 153*16467b97STreehugger Robot 154*16467b97STreehugger Robot }; 155*16467b97STreehugger Robot 156*16467b97STreehugger Robot //Generic rule return value. Unlike the general ANTLR, this gets generated for 157*16467b97STreehugger Robot //every rule in the target. Handle rule exit here 158*16467b97STreehugger Robot template<class ImplTraits> 159*16467b97STreehugger Robot class RuleReturnValue 160*16467b97STreehugger Robot { 161*16467b97STreehugger Robot public: 162*16467b97STreehugger Robot typedef typename ImplTraits::BaseParserType BaseParserType; 163*16467b97STreehugger Robot typedef typename ImplTraits::CommonTokenType CommonTokenType; 164*16467b97STreehugger Robot 165*16467b97STreehugger Robot public: 166*16467b97STreehugger Robot const CommonTokenType* start; 167*16467b97STreehugger Robot const CommonTokenType* stop; 168*16467b97STreehugger Robot BaseParserType* parser; 169*16467b97STreehugger Robot 170*16467b97STreehugger Robot RuleReturnValue(BaseParserType* psr = NULL ); 171*16467b97STreehugger Robot RuleReturnValue( const RuleReturnValue& val ); 172*16467b97STreehugger Robot RuleReturnValue& operator=( const RuleReturnValue& val ); 173*16467b97STreehugger Robot void call_start_placeholder(); 174*16467b97STreehugger Robot void call_stop_placeholder(); 175*16467b97STreehugger Robot RuleReturnValue& get_struct(); 176*16467b97STreehugger Robot ~RuleReturnValue(); 177*16467b97STreehugger Robot }; 178*16467b97STreehugger Robot 179*16467b97STreehugger Robot //This kind makes sure that whenever tokens are condensed into a rule, 180*16467b97STreehugger Robot //all the tokens except the start and stop tokens are deleted 181*16467b97STreehugger Robot template<class ImplTraits> 182*16467b97STreehugger Robot class RuleReturnValue_1 : public RuleReturnValue<ImplTraits> 183*16467b97STreehugger Robot { 184*16467b97STreehugger Robot public: 185*16467b97STreehugger Robot typedef RuleReturnValue<ImplTraits> BaseType; 186*16467b97STreehugger Robot typedef typename BaseType::BaseParserType BaseParserType; 187*16467b97STreehugger Robot 188*16467b97STreehugger Robot public: 189*16467b97STreehugger Robot RuleReturnValue_1(); 190*16467b97STreehugger Robot RuleReturnValue_1( BaseParserType* psr); 191*16467b97STreehugger Robot RuleReturnValue_1( const RuleReturnValue_1& val ); 192*16467b97STreehugger Robot void call_start_placeholder(); //its dummy here 193*16467b97STreehugger Robot ~RuleReturnValue_1(); 194*16467b97STreehugger Robot }; 195*16467b97STreehugger Robot 196*16467b97STreehugger Robot ANTLR_END_NAMESPACE() 197*16467b97STreehugger Robot 198*16467b97STreehugger Robot #include "antlr3parser.inl" 199*16467b97STreehugger Robot 200*16467b97STreehugger Robot #endif 201