aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/antlr3_cpp_runtime/include/antlr3lexer.inl
blob: bf6960569f07a2af01d152a0a32be332441b39fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
namespace antlr3 {

template<class ImplTraits>
Lexer<ImplTraits>::Lexer(ANTLR_UINT32 sizeHint, RecognizerSharedStateType* state)
	:Lexer<ImplTraits>::RecognizerType(sizeHint, state)
	,m_input(NULL)
{
}

template<class ImplTraits>
Lexer<ImplTraits>::Lexer(ANTLR_UINT32 sizeHint, InputStreamType* input, RecognizerSharedStateType* state)
	:Lexer<ImplTraits>::RecognizerType(sizeHint, state)
{
	this->setCharStream(input);
}

template<class ImplTraits>
typename Lexer<ImplTraits>::InputStreamType* Lexer<ImplTraits>::get_input() const
{
	return m_input;
}

template<class ImplTraits>
typename Lexer<ImplTraits>::IntStreamType* Lexer<ImplTraits>::get_istream() const
{
	return m_input;
}

template<class ImplTraits>
typename Lexer<ImplTraits>::RecognizerType* Lexer<ImplTraits>::get_rec()
{
	return this;
}

template<class ImplTraits>
typename Lexer<ImplTraits>::TokenSourceType* Lexer<ImplTraits>::get_tokSource()
{
	return this;
}

template<class ImplTraits>
void Lexer<ImplTraits>::displayRecognitionError( ANTLR_UINT8** , ExceptionBaseType* ex)
{
	StringStreamType	err_stream;

	// See if there is a 'filename' we can use
    //
    if( ex->getName().empty() )
    {
		err_stream << "-unknown source-(";
    }
    else
    {
		err_stream << ex->get_streamName().c_str();
		err_stream << "(";
    }
    err_stream << ex->get_line() << ")";

	err_stream << ": lexer error " <<  ex->getName() << '(' << ex->getType() << ')' << " :\n\t"
		   << ex->get_message() << " at position [" << ex->get_line() << ", "
		   << ex->get_charPositionInLine()+1 << "], ";

	{
		ANTLR_UINT32	width;

		width	= ANTLR_UINT32_CAST(( (ANTLR_UINT8*)(m_input->get_data()) +
									  (m_input->size() )) - (ANTLR_UINT8*)( ex->get_index() ));

		if	(width >= 1)
		{
			if	(isprint(ex->get_c() ))
			{
				err_stream << "near '" << (typename StringType::value_type) ex->get_c() << "' :\n";
			}
			else
			{
				err_stream << "near char(" << std::hex << ex->get_c() << std::dec << ") :\n";
			}
			err_stream << "\t";
			err_stream.width( width > 20 ? 20 : width );
			err_stream << (typename StringType::const_pointer)ex->get_index() << "\n";
		}
		else
		{
			err_stream << "(end of input).\n\t This indicates a poorly specified lexer RULE\n\t or unterminated input element such as: \"STRING[\"]\n";
			err_stream << "\t The lexer was matching from line "
					   << this->get_state()->get_tokenStartLine()
					   << ", offset " << this->get_state()->get_tokenStartCharPositionInLine()
					   << ", which\n\t ";
			width = ANTLR_UINT32_CAST(((ANTLR_UINT8*)(m_input->get_data() )+
										(m_input->size())) -
										(ANTLR_UINT8*)(this->get_state()->get_tokenStartCharIndex() ));

			if	(width >= 1)
			{
				err_stream << "looks like this:\n\t\t";
				err_stream.width( width > 20 ? 20 : width );
				err_stream << (typename StringType::const_pointer)this->get_state()->get_tokenStartCharIndex() << "\n";
			}
			else
			{
				err_stream << "is also the end of the line, so you must check your lexer rules\n";
			}
		}
	}
	ImplTraits::displayRecognitionError( err_stream.str() );
}

template<class ImplTraits>
void Lexer<ImplTraits>::fillExceptionData( ExceptionBaseType* ex )
{
	ex->set_c( m_input->LA(1) );					/* Current input character			*/
	ex->set_line( m_input->get_line() );						/* Line number comes from stream		*/
	ex->set_charPositionInLine( m_input->get_charPositionInLine() );	    /* Line offset also comes from the stream   */
	ex->set_index( m_input->index() );
	ex->set_streamName( m_input->get_fileName() );
	ex->set_message( "Unexpected character" );
}

template<class ImplTraits>
void	Lexer<ImplTraits>::setCharStream(InputStreamType* input)
{
    /* Install the input interface
     */
    m_input	= input;

    /* Set the current token to nothing
     */
	RecognizerSharedStateType* state = this->get_rec()->get_state();
    state->set_token_present( false );
	state->set_text("");
    state->set_tokenStartCharIndex(-1);

    /* Copy the name of the char stream to the token source
     */
    this->get_tokSource()->set_fileName( input->get_fileName() );
}

template<class ImplTraits>
void	Lexer<ImplTraits>::pushCharStream(InputStreamType* input)
{
	// We have a stack, so we can save the current input stream
	// into it.
	//
	this->get_istream()->mark();
	this->get_rec()->get_state()->get_streams().push(this->get_input());

	// And now we can install this new one
	//
	this->setCharStream(input);
}

template<class ImplTraits>
void	Lexer<ImplTraits>::popCharStream()
{
	InputStreamType* input;

    // If we do not have a stream stack or we are already at the
    // stack bottom, then do nothing.
    //
    typename RecognizerSharedStateType::StreamsType& streams = this->get_rec()->get_state()->get_streams();
    if	( streams.size() > 0)
    {
		// We just leave the current stream to its fate, we do not close
		// it or anything as we do not know what the programmer intended
		// for it. This method can always be overridden of course.
		// So just find out what was currently saved on the stack and use
		// that now, then pop it from the stack.
		//
		input	= streams.top();
		streams.pop();

		// Now install the stream as the current one.
		//
		this->setCharStream(input);
		this->get_istream()->rewindLast();
    }
    return;
}

template<class ImplTraits>
void	Lexer<ImplTraits>::emit(const CommonTokenType* token)
{
	this->get_rec()->get_state()->set_token(token);
}

template<class ImplTraits>
typename Lexer<ImplTraits>::CommonTokenType*	Lexer<ImplTraits>::emit()
{
	/* We could check pointers to token factories and so on, but
    * we are in code that we want to run as fast as possible
    * so we are not checking any errors. So make sure you have installed an input stream before
    * trying to emit a new token.
    */
	RecognizerSharedStateType* state = this->get_rec()->get_state();
	state->set_token_present(true);
    CommonTokenType* token = state->get_token();
	token->set_input( this->get_input() );

    /* Install the supplied information, and some other bits we already know
    * get added automatically, such as the input stream it is associated with
    * (though it can all be overridden of course)
    */
    token->set_type( state->get_type() );
    token->set_channel( state->get_channel() );
    token->set_startIndex( state->get_tokenStartCharIndex() );
    token->set_stopIndex( this->getCharIndex() - 1 );
    token->set_line( state->get_tokenStartLine() );
    token->set_charPositionInLine( state->get_tokenStartCharPositionInLine() );

	token->set_tokText( state->get_text() );
    token->set_lineStart( this->get_input()->get_currentLine() );

    return  token;
}

template<class ImplTraits>
Lexer<ImplTraits>::~Lexer()
{
	// This may have ben a delegate or delegator lexer, in which case the
	// state may already have been freed (and set to NULL therefore)
	// so we ignore the state if we don't have it.
	//
	RecognizerSharedStateType* state = this->get_rec()->get_state();

	if	( state != NULL)
	{
		state->get_streams().clear();
	}
}

template<class ImplTraits>
bool	Lexer<ImplTraits>::matchs(ANTLR_UCHAR* str )
{
	RecognizerSharedStateType* state = this->get_rec()->get_state();
	while   (*str != ANTLR_STRING_TERMINATOR)
	{
		if  ( this->get_istream()->LA(1) != (*str))
		{
			if	( state->get_backtracking() > 0)
			{
				state->set_failed(true);
				return false;
			}

			this->exConstruct();
			state->set_failed( true );

			/* TODO: Implement exception creation more fully perhaps
			 */
			this->recover();
			return  false;
		}

		/* Matched correctly, do consume it
		 */
		this->get_istream()->consume();
		str++;

	}
	/* Reset any failed indicator
	 */
	state->set_failed( false );
	return  true;
}

template<class ImplTraits>
bool	Lexer<ImplTraits>::matchc(ANTLR_UCHAR c)
{
	if	(this->get_istream()->LA(1) == c)
	{
		/* Matched correctly, do consume it
		 */
		this->get_istream()->consume();

		/* Reset any failed indicator
		 */
		this->get_rec()->get_state()->set_failed( false );

		return	true;
	}

	/* Failed to match, exception and recovery time.
	 */
	if(this->get_rec()->get_state()->get_backtracking() > 0)
	{
		this->get_rec()->get_state()->set_failed( true );
		return	false;
	}

	this->exConstruct();

	/* TODO: Implement exception creation more fully perhaps
	 */
	this->recover();

	return  false;
}

template<class ImplTraits>
bool	Lexer<ImplTraits>::matchRange(ANTLR_UCHAR low, ANTLR_UCHAR high)
{
    ANTLR_UCHAR    c;

    /* What is in the stream at the moment?
     */
    c	= this->get_istream()->LA(1);
    if	( c >= low && c <= high)
    {
		/* Matched correctly, consume it
		 */
		this->get_istream()->consume();

		/* Reset any failed indicator
		 */
		this->get_rec()->get_state()->set_failed( false );

		return	true;
    }

    /* Failed to match, execption and recovery time.
     */

    if	(this->get_rec()->get_state()->get_backtracking() > 0)
    {
		this->get_rec()->get_state()->set_failed( true );
		return	false;
    }

    this->exConstruct();

    /* TODO: Implement exception creation more fully
     */
    this->recover();

    return  false;
}

template<class ImplTraits>
void		Lexer<ImplTraits>::matchAny()
{
	this->get_istream()->consume();
}

template<class ImplTraits>
void		Lexer<ImplTraits>::recover()
{
	this->get_istream()->consume();
}

template<class ImplTraits>
ANTLR_UINT32	Lexer<ImplTraits>::getLine()
{
	return  this->get_input()->get_line();
}

template<class ImplTraits>
ANTLR_MARKER	Lexer<ImplTraits>::getCharIndex()
{
	return this->get_istream()->index();
}

template<class ImplTraits>
ANTLR_UINT32	Lexer<ImplTraits>::getCharPositionInLine()
{
	return  this->get_input()->get_charPositionInLine();
}

template<class ImplTraits>
typename Lexer<ImplTraits>::StringType	Lexer<ImplTraits>::getText()
{
	RecognizerSharedStateType* state = this->get_rec()->get_state();
	if ( !state->get_text().empty() )
	{
		return	state->get_text();

	}
	return  this->get_input()->substr( state->get_tokenStartCharIndex(),
									this->getCharIndex() - this->get_input()->get_charByteSize()
							);
}

template<class ImplTraits>
void Lexer<ImplTraits>::exConstruct()
{
	new ANTLR_Exception<ImplTraits, RECOGNITION_EXCEPTION, InputStreamType>( this->get_rec(), "" );
}

template< class ImplTraits>
typename Lexer<ImplTraits>::TokenType*	Lexer<ImplTraits>::getMissingSymbol( IntStreamType*,
										  ExceptionBaseType*,
										  ANTLR_UINT32	, BitsetListType*)
{
	return NULL;
}

template< class ImplTraits>
ANTLR_INLINE const typename Lexer<ImplTraits>::RecognizerType* Lexer<ImplTraits>::get_rec() const
{
	return this;
}

template< class ImplTraits>
ANTLR_INLINE const typename Lexer<ImplTraits>::RecognizerType* Lexer<ImplTraits>::get_recognizer() const
{
	return this->get_rec();
}

template< class ImplTraits>
ANTLR_INLINE typename Lexer<ImplTraits>::RecognizerSharedStateType* Lexer<ImplTraits>::get_lexstate() const
{
	return this->get_rec()->get_state();
}

template< class ImplTraits>
ANTLR_INLINE void Lexer<ImplTraits>::set_lexstate( RecognizerSharedStateType* lexstate )
{
	this->get_rec()->set_state(lexstate);
}

template< class ImplTraits>
ANTLR_INLINE const typename Lexer<ImplTraits>::TokenSourceType* Lexer<ImplTraits>::get_tokSource() const
{
	return this;
}

template< class ImplTraits>
ANTLR_INLINE typename Lexer<ImplTraits>::CommonTokenType* Lexer<ImplTraits>::get_ltoken() const
{
	return this->get_lexstate()->token();
}

template< class ImplTraits>
ANTLR_INLINE void Lexer<ImplTraits>::set_ltoken( const CommonTokenType* ltoken )
{
	this->get_lexstate()->set_token( ltoken );
}

template< class ImplTraits>
ANTLR_INLINE bool Lexer<ImplTraits>::hasFailed() const
{
	return this->get_lexstate()->get_failed();
}

template< class ImplTraits>
ANTLR_INLINE ANTLR_INT32 Lexer<ImplTraits>::get_backtracking() const
{
	return this->get_lexstate()->get_backtracking();
}

template< class ImplTraits>
ANTLR_INLINE void Lexer<ImplTraits>::inc_backtracking()
{
	this->get_lexstate()->inc_backtracking();
}

template< class ImplTraits>
ANTLR_INLINE void Lexer<ImplTraits>::dec_backtracking()
{
	this->get_lexstate()->dec_backtracking();
}

template< class ImplTraits>
ANTLR_INLINE bool Lexer<ImplTraits>::get_failedflag() const
{
	return this->get_lexstate()->get_failed();
}

template< class ImplTraits>
ANTLR_INLINE void Lexer<ImplTraits>::set_failedflag( bool failed )
{
	this->get_lexstate()->set_failed(failed);
}

template< class ImplTraits>
ANTLR_INLINE typename Lexer<ImplTraits>::InputStreamType* Lexer<ImplTraits>::get_strstream() const
{
	return this->get_input();
}

template< class ImplTraits>
ANTLR_INLINE ANTLR_MARKER  Lexer<ImplTraits>::index() const
{
	return this->get_istream()->index();
}

template< class ImplTraits>
ANTLR_INLINE void	Lexer<ImplTraits>::seek(ANTLR_MARKER index)
{
	this->get_istream()->seek(index);
}

template< class ImplTraits>
ANTLR_INLINE const typename Lexer<ImplTraits>::CommonTokenType* Lexer<ImplTraits>::EOF_Token() const
{
	const CommonTokenType& eof_token = this->get_tokSource()->get_eofToken();
	return &eof_token;
}

template< class ImplTraits>
ANTLR_INLINE bool Lexer<ImplTraits>::hasException() const
{
	return this->get_lexstate()->get_error();
}

template< class ImplTraits>
ANTLR_INLINE typename Lexer<ImplTraits>::ExceptionBaseType* Lexer<ImplTraits>::get_exception() const
{
	return this->get_lexstate()->get_exception();
}

template< class ImplTraits>
ANTLR_INLINE void Lexer<ImplTraits>::constructEx()
{
	this->get_rec()->exConstruct();
}

template< class ImplTraits>
ANTLR_INLINE ANTLR_MARKER Lexer<ImplTraits>::mark()
{
	return this->get_istream()->mark();
}

template< class ImplTraits>
ANTLR_INLINE void Lexer<ImplTraits>::rewind(ANTLR_MARKER marker)
{
	this->get_istream()->rewind(marker);
}

template< class ImplTraits>
ANTLR_INLINE void Lexer<ImplTraits>::rewindLast()
{
	this->get_istream()->rewindLast();
}

template< class ImplTraits>
ANTLR_INLINE void Lexer<ImplTraits>::memoize(ANTLR_MARKER	ruleIndex, ANTLR_MARKER	ruleParseStart)
{
	this->get_rec()->memoize( ruleIndex, ruleParseStart );
}

template< class ImplTraits>
ANTLR_INLINE bool Lexer<ImplTraits>::haveParsedRule(ANTLR_MARKER	ruleIndex)
{
	return this->get_rec()->alreadyParsedRule(ruleIndex);
}

template< class ImplTraits>
ANTLR_INLINE void Lexer<ImplTraits>::setText( const StringType& text )
{
	this->get_lexstate()->set_text(text);
}

template< class ImplTraits>
ANTLR_INLINE void Lexer<ImplTraits>::skip()
{
	CommonTokenType& skipToken = this->get_tokSource()->get_skipToken();
	this->get_lexstate()->set_token( &skipToken );
}

template< class ImplTraits>
ANTLR_INLINE typename Lexer<ImplTraits>::RuleMemoType* Lexer<ImplTraits>::getRuleMemo() const
{
	return this->get_lexstate()->get_rulememo();
}

template< class ImplTraits>
ANTLR_INLINE void Lexer<ImplTraits>::setRuleMemo(RuleMemoType* rulememo)
{
	return this->get_lexstate()->set_rulememo(rulememo);
}

template< class ImplTraits>
ANTLR_INLINE typename Lexer<ImplTraits>::DebuggerType* Lexer<ImplTraits>::get_debugger() const
{
	return this->get_rec()->get_debugger();
}

template< class ImplTraits>
ANTLR_INLINE ANTLR_UINT32 Lexer<ImplTraits>::LA(ANTLR_INT32 i)
{
	return this->get_istream()->LA(i);
}

template< class ImplTraits>
ANTLR_INLINE void Lexer<ImplTraits>::consume()
{
	return this->get_istream()->consume();
}

}