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