antlr3parser.hpp 7.2 KB

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