• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1ANTLR_BEGIN_NAMESPACE()
2
3template< class ImplTraits >
4Parser<ImplTraits>::Parser( ANTLR_UINT32 sizeHint, RecognizerSharedStateType* state )
5	:RecognizerType( sizeHint, state )
6{
7	m_tstream = NULL;
8}
9
10template< class ImplTraits >
11Parser<ImplTraits>::Parser( ANTLR_UINT32 sizeHint, TokenStreamType* tstream,
12												RecognizerSharedStateType* state )
13												:RecognizerType( sizeHint, state )
14{
15	this->setTokenStream( tstream );
16}
17
18template< class ImplTraits >
19Parser<ImplTraits>::Parser( ANTLR_UINT32 sizeHint, TokenStreamType* tstream,
20											DebugEventListenerType* dbg,
21											RecognizerSharedStateType* state )
22											:RecognizerType( sizeHint, state )
23{
24	this->setTokenStream( tstream );
25	this->setDebugListener( dbg );
26}
27
28template< class ImplTraits >
29ANTLR_INLINE typename Parser<ImplTraits>::TokenStreamType* Parser<ImplTraits>::get_tstream() const
30{
31	return m_tstream;
32}
33
34template< class ImplTraits >
35ANTLR_INLINE typename Parser<ImplTraits>::IntStreamType* Parser<ImplTraits>::get_istream() const
36{
37	return m_tstream;
38}
39
40template< class ImplTraits >
41ANTLR_INLINE typename Parser<ImplTraits>::IntStreamType* Parser<ImplTraits>::get_parser_istream() const
42{
43	return m_tstream;
44}
45
46template< class ImplTraits >
47ANTLR_INLINE typename Parser<ImplTraits>::TokenStreamType* Parser<ImplTraits>::get_input() const
48{
49	return m_tstream;
50}
51
52template< class ImplTraits >
53void Parser<ImplTraits>::fillExceptionData( ExceptionBaseType* ex )
54{
55	ex->set_token( m_tstream->_LT(1) );	    /* Current input token			    */
56	ex->set_line( ex->get_token()->get_line() );
57	ex->set_charPositionInLine( ex->get_token()->get_charPositionInLine() );
58	ex->set_index( this->get_istream()->index() );
59	if( ex->get_token()->get_type() == CommonTokenType::TOKEN_EOF)
60	{
61		ex->set_streamName("");
62	}
63	else
64	{
65		ex->set_streamName( ex->get_token()->get_input()->get_fileName() );
66	}
67	ex->set_message("Unexpected token");
68}
69
70template< class ImplTraits >
71void Parser<ImplTraits>::displayRecognitionError( ANTLR_UINT8** tokenNames, ExceptionBaseType* ex )
72{
73	typename ImplTraits::StringStreamType errtext;
74	// See if there is a 'filename' we can use
75	//
76	if( ex->get_streamName().empty() )
77	{
78		if(ex->get_token()->get_type() == CommonTokenType::TOKEN_EOF)
79		{
80			errtext << "-end of input-(";
81		}
82		else
83		{
84			errtext << "-unknown source-(";
85		}
86	}
87	else
88	{
89		errtext << ex->get_streamName() << "(";
90	}
91
92	// Next comes the line number
93	//
94	errtext << this->get_rec()->get_state()->get_exception()->get_line() << ") ";
95	errtext << " : error " << this->get_rec()->get_state()->get_exception()->getType()
96							<< " : "
97							<< this->get_rec()->get_state()->get_exception()->get_message();
98
99	// Prepare the knowledge we know we have
100	//
101	const CommonTokenType* theToken   = this->get_rec()->get_state()->get_exception()->get_token();
102	StringType ttext			= theToken->toString();
103
104	errtext << ", at offset , "
105			<< this->get_rec()->get_state()->get_exception()->get_charPositionInLine();
106	if  (theToken != NULL)
107	{
108		if (theToken->get_type() == CommonTokenType::TOKEN_EOF)
109		{
110			errtext << ", at <EOF>";
111		}
112		else
113		{
114			// Guard against null text in a token
115			//
116			errtext << "\n    near " << ( ttext.empty()
117											? "<no text for the token>" : ttext ) << "\n";
118		}
119	}
120
121	ex->displayRecognitionError( tokenNames, errtext );
122	ImplTraits::displayRecognitionError( errtext.str() );
123}
124
125template< class ImplTraits >
126Parser<ImplTraits>::~Parser()
127{
128    if	(this->get_rec() != NULL)
129    {
130		// This may have ben a delegate or delegator parser, in which case the
131		// state may already have been freed (and set to NULL therefore)
132		// so we ignore the state if we don't have it.
133		//
134		RecognizerSharedStateType* state = this->get_rec()->get_state();
135		if	(state != NULL)
136		{
137			state->get_following().clear();
138		}
139    }
140}
141
142template< class ImplTraits >
143void	Parser<ImplTraits>::setDebugListener(DebugEventListenerType* dbg)
144{
145		// Set the debug listener. There are no methods to override
146	// because currently the only ones that notify the debugger
147	// are error reporting and recovery. Hence we can afford to
148	// check and see if the debugger interface is null or not
149	// there. If there is ever an occasion for a performance
150	// sensitive function to use the debugger interface, then
151	// a replacement function for debug mode should be supplied
152	// and installed here.
153	//
154	this->get_rec()->set_debugger(dbg);
155
156	// If there was a tokenstream installed already
157	// then we need to tell it about the debug interface
158	//
159	if	(this->get_tstream() != NULL)
160	{
161		this->get_tstream()->setDebugListener(dbg);
162	}
163}
164
165template< class ImplTraits >
166ANTLR_INLINE void	Parser<ImplTraits>::setTokenStream(TokenStreamType* tstream)
167{
168	m_tstream = tstream;
169    this->get_rec()->reset();
170}
171
172template< class ImplTraits >
173ANTLR_INLINE typename Parser<ImplTraits>::TokenStreamType*	Parser<ImplTraits>::getTokenStream()
174{
175	return m_tstream;
176}
177
178template< class ImplTraits >
179ANTLR_INLINE typename Parser<ImplTraits>::RecognizerType* Parser<ImplTraits>::get_rec()
180{
181	return this;
182}
183
184template< class ImplTraits >
185ANTLR_INLINE void Parser<ImplTraits>::exConstruct()
186{
187	new ANTLR_Exception<ImplTraits, MISMATCHED_TOKEN_EXCEPTION, StreamType>( this->get_rec(), "" );
188}
189
190template< class ImplTraits >
191typename Parser<ImplTraits>::TokenType*	Parser<ImplTraits>::getMissingSymbol( IntStreamType* istream,
192										  ExceptionBaseType*,
193										  ANTLR_UINT32			expectedTokenType,
194										  BitsetListType*	)
195{
196	TokenStreamType*		cts;
197	CommonTokenType*		token;
198	const CommonTokenType*		current;
199	StringType				text;
200
201	// Dereference the standard pointers
202	//
203	cts		= static_cast<TokenStreamType*>(istream);
204
205	// Work out what to use as the current symbol to make a line and offset etc
206	// If we are at EOF, we use the token before EOF
207	//
208	current	= cts->_LT(1);
209	if	(current->get_type() == CommonTokenType::TOKEN_EOF)
210	{
211		current = cts->_LT(-1);
212	}
213
214	token	= new CommonTokenType;
215
216	// Set some of the token properties based on the current token
217	//
218	token->set_line(current->get_line());
219	token->set_charPositionInLine( current->get_charPositionInLine());
220	token->set_channel( TOKEN_DEFAULT_CHANNEL );
221	token->set_type(expectedTokenType);
222    token->set_lineStart( current->get_lineStart() );
223
224	// Create the token text that shows it has been inserted
225	//
226	token->setText("<missing ");
227	text = token->getText();
228
229	if	(!text.empty())
230	{
231		text.append((const char *) this->get_rec()->get_state()->get_tokenName(expectedTokenType) );
232		text.append(">");
233	}
234
235	// Finally return the pointer to our new token
236	//
237	return	token;
238}
239
240template< class ImplTraits >
241void Parser<ImplTraits>::mismatch(ANTLR_UINT32 ttype, BitsetListType* follow)
242{
243    // Install a mismatched token exception in the exception stack
244    //
245	new ANTLR_Exception<ImplTraits, MISMATCHED_TOKEN_EXCEPTION, StreamType>(this, "");
246
247	//With the statement below, only the parsers are allowed to compile fine
248	IntStreamType* is = this->get_istream();
249
250
251	if	(this->mismatchIsUnwantedToken(is, ttype))
252	{
253		// Now update it to indicate this is an unwanted token exception
254		//
255		new ANTLR_Exception<ImplTraits, UNWANTED_TOKEN_EXCEPTION, StreamType>(this, "");
256		return;
257	}
258
259	if	( this->mismatchIsMissingToken(is, follow))
260	{
261		// Now update it to indicate this is an unwanted token exception
262		//
263		new ANTLR_Exception<ImplTraits, MISSING_TOKEN_EXCEPTION, StreamType>(this, "");
264		return;
265	}
266
267	// Just a mismatched token is all we can dtermine
268	//
269	new ANTLR_Exception<ImplTraits, MISMATCHED_TOKEN_EXCEPTION, StreamType>(this, "");
270
271	return;
272}
273
274template< class ImplTraits>
275ANTLR_INLINE const typename Parser<ImplTraits>::RecognizerType* Parser<ImplTraits>::get_recognizer() const
276{
277	return this;
278}
279
280template< class ImplTraits>
281ANTLR_INLINE typename Parser<ImplTraits>::RecognizerSharedStateType* Parser<ImplTraits>::get_psrstate() const
282{
283	return this->get_recognizer()->get_state();
284}
285
286template< class ImplTraits>
287ANTLR_INLINE void Parser<ImplTraits>::set_psrstate(RecognizerSharedStateType* state)
288{
289	this->get_rec()->set_state( state );
290}
291
292template< class ImplTraits>
293ANTLR_INLINE bool Parser<ImplTraits>::haveParsedRule(ANTLR_MARKER	ruleIndex)
294{
295	return this->get_rec()->alreadyParsedRule(ruleIndex);
296}
297
298template< class ImplTraits>
299ANTLR_INLINE void Parser<ImplTraits>::memoize(ANTLR_MARKER	ruleIndex, ANTLR_MARKER	ruleParseStart)
300{
301	return this->get_rec()->memoize( ruleIndex, ruleParseStart );
302}
303
304template< class ImplTraits>
305ANTLR_INLINE ANTLR_MARKER  Parser<ImplTraits>::index() const
306{
307	return this->get_istream()->index();
308}
309
310template< class ImplTraits>
311ANTLR_INLINE bool Parser<ImplTraits>::hasException() const
312{
313	return this->get_psrstate()->get_error();
314}
315
316template< class ImplTraits>
317ANTLR_INLINE typename Parser<ImplTraits>::ExceptionBaseType* Parser<ImplTraits>::get_exception() const
318{
319	return this->get_psrstate()->get_exception();
320}
321
322template< class ImplTraits>
323ANTLR_INLINE const typename Parser<ImplTraits>::CommonTokenType* Parser<ImplTraits>::matchToken( ANTLR_UINT32 ttype, BitsetListType* follow )
324{
325	return this->get_rec()->match( ttype, follow );
326}
327
328template< class ImplTraits>
329ANTLR_INLINE void Parser<ImplTraits>::matchAnyToken()
330{
331	return this->get_rec()->matchAny();
332}
333
334template< class ImplTraits>
335ANTLR_INLINE const typename Parser<ImplTraits>::FollowingType& Parser<ImplTraits>::get_follow_stack() const
336{
337	return this->get_psrstate()->get_following();
338}
339
340template< class ImplTraits>
341ANTLR_INLINE void Parser<ImplTraits>::followPush(const BitsetListType& follow)
342{
343#ifndef  SKIP_FOLLOW_SETS
344	this->get_rec()->get_state()->get_following().push(follow);
345#endif
346}
347
348template< class ImplTraits>
349ANTLR_INLINE void Parser<ImplTraits>::followPop()
350{
351#ifndef  SKIP_FOLLOW_SETS
352	this->get_rec()->get_state()->get_following().pop();
353#endif
354}
355
356template< class ImplTraits>
357ANTLR_INLINE void Parser<ImplTraits>::precover()
358{
359	return this->get_rec()->recover();
360}
361
362template< class ImplTraits>
363ANTLR_INLINE void Parser<ImplTraits>::preporterror()
364{
365	return this->get_rec()->reportError();
366}
367
368template< class ImplTraits>
369ANTLR_INLINE ANTLR_UINT32 Parser<ImplTraits>::LA(ANTLR_INT32 i)
370{
371	return this->get_istream()->_LA(i);
372}
373
374template< class ImplTraits>
375ANTLR_INLINE const typename Parser<ImplTraits>::CommonTokenType*  Parser<ImplTraits>::LT(ANTLR_INT32 k)
376{
377	return this->get_input()->_LT(k);
378}
379
380template< class ImplTraits>
381ANTLR_INLINE void Parser<ImplTraits>::constructEx()
382{
383	this->get_rec()->constructEx();
384}
385
386template< class ImplTraits>
387ANTLR_INLINE void Parser<ImplTraits>::consume()
388{
389	this->get_istream()->consume();
390}
391
392template< class ImplTraits>
393ANTLR_INLINE ANTLR_MARKER Parser<ImplTraits>::mark()
394{
395	return this->get_istream()->mark();
396}
397
398template< class ImplTraits>
399ANTLR_INLINE void Parser<ImplTraits>::rewind(ANTLR_MARKER marker)
400{
401	this->get_istream()->rewind(marker);
402}
403
404template< class ImplTraits>
405ANTLR_INLINE void Parser<ImplTraits>::rewindLast()
406{
407	this->get_istream()->rewindLast();
408}
409
410template< class ImplTraits>
411ANTLR_INLINE void Parser<ImplTraits>::seek(ANTLR_MARKER index)
412{
413	this->get_istream()->seek(index);
414}
415
416template< class ImplTraits>
417ANTLR_INLINE bool Parser<ImplTraits>::get_perror_recovery() const
418{
419	return this->get_psrstate()->get_errorRecovery();
420}
421
422template< class ImplTraits>
423ANTLR_INLINE void Parser<ImplTraits>::set_perror_recovery( bool val )
424{
425	this->get_psrstate()->set_errorRecovery(val);
426}
427
428template< class ImplTraits>
429ANTLR_INLINE bool Parser<ImplTraits>::hasFailed() const
430{
431	return this->get_psrstate()->get_failed();
432}
433
434template< class ImplTraits>
435ANTLR_INLINE bool Parser<ImplTraits>::get_failedflag() const
436{
437	return this->get_psrstate()->get_failed();
438}
439
440template< class ImplTraits>
441ANTLR_INLINE void Parser<ImplTraits>::set_failedflag( bool failed )
442{
443	this->get_psrstate()->set_failed(failed);
444}
445
446template< class ImplTraits>
447ANTLR_INLINE ANTLR_INT32 Parser<ImplTraits>::get_backtracking() const
448{
449	return this->get_psrstate()->get_backtracking();
450}
451
452template< class ImplTraits>
453ANTLR_INLINE void Parser<ImplTraits>::inc_backtracking()
454{
455	this->get_psrstate()->inc_backtracking();
456}
457
458template< class ImplTraits>
459ANTLR_INLINE void Parser<ImplTraits>::dec_backtracking()
460{
461	this->get_psrstate()->dec_backtracking();
462}
463
464template< class ImplTraits>
465ANTLR_INLINE typename Parser<ImplTraits>::CommonTokenType* Parser<ImplTraits>::recoverFromMismatchedSet(BitsetListType*	follow)
466{
467	return this->get_rec()->recoverFromMismatchedSet(follow);
468}
469
470template< class ImplTraits>
471ANTLR_INLINE bool	Parser<ImplTraits>::recoverFromMismatchedElement(BitsetListType*	follow)
472{
473	return this->get_rec()->recoverFromMismatchedElement(follow);
474}
475
476template< class ImplTraits>
477ANTLR_INLINE typename Parser<ImplTraits>::RuleMemoType* Parser<ImplTraits>::getRuleMemo() const
478{
479	return this->get_psrstate()->get_ruleMemo();
480}
481
482template< class ImplTraits>
483ANTLR_INLINE void Parser<ImplTraits>::setRuleMemo(RuleMemoType* rulememo)
484{
485	this->get_psrstate()->set_ruleMemo(rulememo);
486}
487
488template< class ImplTraits>
489ANTLR_INLINE typename Parser<ImplTraits>::DebuggerType* Parser<ImplTraits>::get_debugger() const
490{
491	return this->get_rec()->get_debugger();
492}
493
494template< class ImplTraits>
495ANTLR_INLINE typename Parser<ImplTraits>::TokenStreamType* Parser<ImplTraits>::get_strstream() const
496{
497	return this->get_tstream();
498}
499
500template< class ImplTraits>
501ANTLR_INLINE RuleReturnValue<ImplTraits>::RuleReturnValue(BaseParserType* psr)
502{
503	parser = psr;
504	start = NULL;
505	stop = NULL;
506}
507
508template< class ImplTraits>
509ANTLR_INLINE RuleReturnValue<ImplTraits>::RuleReturnValue( const RuleReturnValue& val )
510{
511	parser	= val.parser;
512	start	= val.start;
513	stop	= val.stop;
514}
515
516template< class ImplTraits>
517ANTLR_INLINE RuleReturnValue<ImplTraits>& RuleReturnValue<ImplTraits>::operator=( const RuleReturnValue& val )
518{
519	parser	= val.parser;
520	start	= val.start;
521	stop	= val.stop;
522	return *this;
523}
524
525template< class ImplTraits>
526ANTLR_INLINE RuleReturnValue<ImplTraits>::~RuleReturnValue()
527{
528}
529
530template< class ImplTraits>
531ANTLR_INLINE void RuleReturnValue<ImplTraits>::call_start_placeholder()
532{
533	start = parser->LT(1);
534	stop = start;
535}
536
537template< class ImplTraits>
538ANTLR_INLINE void RuleReturnValue<ImplTraits>::call_stop_placeholder()
539{
540	stop = parser->LT(-1);
541}
542
543template< class ImplTraits>
544ANTLR_INLINE RuleReturnValue_1<ImplTraits>::RuleReturnValue_1()
545{
546}
547
548template< class ImplTraits>
549RuleReturnValue_1<ImplTraits>::RuleReturnValue_1( BaseParserType* psr )
550	:RuleReturnValue_1<ImplTraits>::BaseType(psr)
551{
552	BaseType::start = psr->LT(1);
553	BaseType::stop = BaseType::start;
554}
555
556template< class ImplTraits>
557RuleReturnValue_1<ImplTraits>::RuleReturnValue_1( const RuleReturnValue_1& val )
558	:BaseType(val)
559{
560}
561
562template< class ImplTraits>
563void RuleReturnValue_1<ImplTraits>::call_start_placeholder()
564{
565}
566
567template< class ImplTraits>
568RuleReturnValue_1<ImplTraits>::~RuleReturnValue_1()
569{
570	if( BaseType::parser && ( BaseType::parser->get_backtracking() == 0 ) )
571	{
572		if( BaseType::stop == NULL )
573			BaseType::stop = BaseType::parser->LT(-1);
574		if( BaseType::stop != NULL )
575		{
576			ANTLR_MARKER start_token_idx	= BaseType::start->get_index() + 1;
577			ANTLR_MARKER stop_token_idx		= BaseType::stop->get_index() - 1;
578			if( start_token_idx > stop_token_idx )
579				return;
580			BaseType::parser->getTokenStream()->discardTokens( start_token_idx, stop_token_idx);
581		}
582	}
583}
584
585ANTLR_END_NAMESPACE()
586