boo-lang /lib/antlr-2.7.5/lib/csharp/src/antlr/Parser.cs

Language C# Lines 508
MD5 Hash ae0f6dd90fff1503f89ce3aad6a5b61b Estimated Cost $9,485 (why?)
Repository https://github.com/boo/boo-lang.git View Raw File View Project SPDX
  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
using System;
using EventHandlerList			= System.ComponentModel.EventHandlerList;

using BitSet					= antlr.collections.impl.BitSet;
using AST						= antlr.collections.AST;
using ASTArray					= antlr.collections.impl.ASTArray;
using antlr.debug;

using MessageListener				= antlr.debug.MessageListener;
using ParserListener				= antlr.debug.ParserListener;
using ParserMatchListener			= antlr.debug.ParserMatchListener;
using ParserTokenListener			= antlr.debug.ParserTokenListener;
using SemanticPredicateListener		= antlr.debug.SemanticPredicateListener;
using SyntacticPredicateListener	= antlr.debug.SyntacticPredicateListener;
using TraceListener					= antlr.debug.TraceListener;

/*
	private Vector messageListeners;
	private Vector newLineListeners;
	private Vector matchListeners;
	private Vector tokenListeners;
	private Vector semPredListeners;
	private Vector synPredListeners;
	private Vector traceListeners;
*/
	
namespace antlr
{
	/*ANTLR Translator Generator
	* Project led by Terence Parr at http://www.jGuru.com
	* Software rights: http://www.antlr.org/license.html
	*
	* $Id:$
	*/

	//
	// ANTLR C# Code Generator by Micheal Jordan
	//                            Kunle Odutola       : kunle UNDERSCORE odutola AT hotmail DOT com
	//                            Anthony Oguntimehin
	//
	// With many thanks to Eric V. Smith from the ANTLR list.
	//

	public abstract class Parser : IParserDebugSubject
	{
		// Used to store event delegates
		private EventHandlerList events_ = new EventHandlerList();

		protected internal EventHandlerList Events 
		{
			get	{ return events_;	}
		}

		// The unique keys for each event that Parser [objects] can generate
		internal static readonly object EnterRuleEventKey		= new object();
		internal static readonly object ExitRuleEventKey			= new object();
		internal static readonly object DoneEventKey				= new object();
		internal static readonly object ReportErrorEventKey		= new object();
		internal static readonly object ReportWarningEventKey	= new object();
		internal static readonly object NewLineEventKey			= new object();
		internal static readonly object MatchEventKey			= new object();
		internal static readonly object MatchNotEventKey			= new object();
		internal static readonly object MisMatchEventKey			= new object();
		internal static readonly object MisMatchNotEventKey		= new object();
		internal static readonly object ConsumeEventKey			= new object();
		internal static readonly object LAEventKey				= new object();
		internal static readonly object SemPredEvaluatedEventKey	= new object();
		internal static readonly object SynPredStartedEventKey	= new object();
		internal static readonly object SynPredFailedEventKey	= new object();
		internal static readonly object SynPredSucceededEventKey	= new object();

		protected internal ParserSharedInputState inputState;
		
		/*Nesting level of registered handlers */
		// protected int exceptionLevel = 0;
		
		/*Table of token type to token names */
		protected internal string[] tokenNames;
		
		/*AST return value for a rule is squirreled away here */
		protected internal AST returnAST;
		
		/*AST support code; parser and treeparser delegate to this object */
		protected internal ASTFactory astFactory = new ASTFactory();
		
		private bool ignoreInvalidDebugCalls = false;
		
		/*Used to keep track of indentdepth for traceIn/Out */
		protected internal int traceDepth = 0;
		
		public Parser()
		{
			inputState = new ParserSharedInputState();
		}
		
		public Parser(ParserSharedInputState state)
		{
			inputState = state;
		}
		
		/// <summary>
		/// 
		/// </summary>

		public event TraceEventHandler EnterRule
		{
			add		{	Events.AddHandler(EnterRuleEventKey, value);	}
			remove	{	Events.RemoveHandler(EnterRuleEventKey, value);	}
		}

		public event TraceEventHandler ExitRule
		{
			add		{	Events.AddHandler(ExitRuleEventKey, value);		}
			remove	{	Events.RemoveHandler(ExitRuleEventKey, value);	}
		}

		public event TraceEventHandler Done
		{
			add		{	Events.AddHandler(DoneEventKey, value);		}
			remove	{	Events.RemoveHandler(DoneEventKey, value);	}
		}

		public event MessageEventHandler ErrorReported
		{
			add		{	Events.AddHandler(ReportErrorEventKey, value);		}
			remove	{	Events.RemoveHandler(ReportErrorEventKey, value);	}
		}

		public event MessageEventHandler WarningReported
		{
			add		{	Events.AddHandler(ReportWarningEventKey, value);	}
			remove	{	Events.RemoveHandler(ReportWarningEventKey, value);	}
		}

		public event MatchEventHandler MatchedToken
		{
			add		{	Events.AddHandler(MatchEventKey, value);	}
			remove	{	Events.RemoveHandler(MatchEventKey, value);	}
		}

		public event MatchEventHandler MatchedNotToken
		{
			add		{	Events.AddHandler(MatchNotEventKey, value);		}
			remove	{	Events.RemoveHandler(MatchNotEventKey, value);	}
		}

		public event MatchEventHandler MisMatchedToken
		{
			add		{	Events.AddHandler(MisMatchEventKey, value);		}
			remove	{	Events.RemoveHandler(MisMatchEventKey, value);	}
		}

		public event MatchEventHandler MisMatchedNotToken
		{
			add		{	Events.AddHandler(MisMatchNotEventKey, value);		}
			remove	{	Events.RemoveHandler(MisMatchNotEventKey, value);	}
		}

		public event TokenEventHandler ConsumedToken
		{
			add		{	Events.AddHandler(ConsumeEventKey, value);		}
			remove	{	Events.RemoveHandler(ConsumeEventKey, value);	}
		}

		public event TokenEventHandler TokenLA
		{
			add		{	Events.AddHandler(LAEventKey, value);		}
			remove	{	Events.RemoveHandler(LAEventKey, value);	}
		}

		public event SemanticPredicateEventHandler SemPredEvaluated
		{
			add		{	Events.AddHandler(SemPredEvaluatedEventKey, value);		}
			remove	{	Events.RemoveHandler(SemPredEvaluatedEventKey, value);	}
		}

		public event SyntacticPredicateEventHandler SynPredStarted
		{
			add		{	Events.AddHandler(SynPredStartedEventKey, value);		}
			remove	{	Events.RemoveHandler(SynPredStartedEventKey, value);	}
		}

		public event SyntacticPredicateEventHandler SynPredFailed
		{
			add		{	Events.AddHandler(SynPredFailedEventKey, value);	}
			remove	{	Events.RemoveHandler(SynPredFailedEventKey, value);	}
		}

		public event SyntacticPredicateEventHandler SynPredSucceeded
		{
			add		{	Events.AddHandler(SynPredSucceededEventKey, value);		}
			remove	{	Events.RemoveHandler(SynPredSucceededEventKey, value);	}
		}

		
		public virtual void  addMessageListener(MessageListener l)
		{
			if (!ignoreInvalidDebugCalls)
				throw new System.ArgumentException("addMessageListener() is only valid if parser built for debugging");
		}
		
		public virtual void  addParserListener(ParserListener l)
		{
			if (!ignoreInvalidDebugCalls)
				throw new System.ArgumentException("addParserListener() is only valid if parser built for debugging");
		}
		
		public virtual void  addParserMatchListener(ParserMatchListener l)
		{
			if (!ignoreInvalidDebugCalls)
				throw new System.ArgumentException("addParserMatchListener() is only valid if parser built for debugging");
		}
		
		public virtual void  addParserTokenListener(ParserTokenListener l)
		{
			if (!ignoreInvalidDebugCalls)
				throw new System.ArgumentException("addParserTokenListener() is only valid if parser built for debugging");
		}
		
		public virtual void  addSemanticPredicateListener(SemanticPredicateListener l)
		{
			if (!ignoreInvalidDebugCalls)
				throw new System.ArgumentException("addSemanticPredicateListener() is only valid if parser built for debugging");
		}
		
		public virtual void  addSyntacticPredicateListener(SyntacticPredicateListener l)
		{
			if (!ignoreInvalidDebugCalls)
				throw new System.ArgumentException("addSyntacticPredicateListener() is only valid if parser built for debugging");
		}
		
		public virtual void  addTraceListener(TraceListener l)
		{
			if (!ignoreInvalidDebugCalls)
				throw new System.ArgumentException("addTraceListener() is only valid if parser built for debugging");
		}
		
		/*Get another token object from the token stream */
		public abstract void  consume();
		/*Consume tokens until one matches the given token */
		public virtual void  consumeUntil(int tokenType)
		{
			while (LA(1) != Token.EOF_TYPE && LA(1) != tokenType)
			{
				consume();
			}
		}
		/*Consume tokens until one matches the given token set */
		public virtual void  consumeUntil(BitSet bset)
		{
			while (LA(1) != Token.EOF_TYPE && !bset.member(LA(1)))
			{
				consume();
			}
		}
		protected internal virtual void  defaultDebuggingSetup(TokenStream lexer, TokenBuffer tokBuf)
		{
			// by default, do nothing -- we're not debugging
		}
		/*Get the AST return value squirreled away in the parser */
		public virtual AST getAST()
		{
			return returnAST;
		}
		public virtual ASTFactory getASTFactory()
		{
			return astFactory;
		}
		public virtual string getFilename()
		{
			return inputState.filename;
		}
		
		public virtual ParserSharedInputState getInputState()
		{
			return inputState;
		}
		
		public virtual void  setInputState(ParserSharedInputState state)
		{
			inputState = state;
		}
		
		public virtual void resetState()
		{
			traceDepth = 0;
			inputState.reset();
		}

		public virtual string getTokenName(int num)
		{
			return tokenNames[num];
		}
		public virtual string[] getTokenNames()
		{
			return tokenNames;
		}
		public virtual bool isDebugMode()
		{
			return false;
		}
		/*Return the token type of the ith token of lookahead where i=1
		* is the current token being examined by the parser (i.e., it
		* has not been matched yet).
		*/
		public abstract int LA(int i);
		/*Return the ith token of lookahead */
		public abstract IToken LT(int i);
		// Forwarded to TokenBuffer
		public virtual int mark()
		{
			return inputState.input.mark();
		}
		/*Make sure current lookahead symbol matches token type <tt>t</tt>.
		* Throw an exception upon mismatch, which is catch by either the
		* error handler or by the syntactic predicate.
		*/
		public virtual void  match(int t)
		{
			if (LA(1) != t)
				throw new MismatchedTokenException(tokenNames, LT(1), t, false, getFilename());
			else
				consume();
		}
		/*Make sure current lookahead symbol matches the given set
		* Throw an exception upon mismatch, which is catch by either the
		* error handler or by the syntactic predicate.
		*/
		public virtual void  match(BitSet b)
		{
			if (!b.member(LA(1)))
				throw new MismatchedTokenException(tokenNames, LT(1), b, false, getFilename());
			else
				consume();
		}
		public virtual void  matchNot(int t)
		{
			if (LA(1) == t)
				throw new MismatchedTokenException(tokenNames, LT(1), t, true, getFilename());
			else
				consume();
		}

		/// <summary>
		/// @deprecated as of 2.7.2. This method calls System.exit() and writes
		/// directly to stderr, which is usually not appropriate when
		/// a parser is embedded into a larger application. Since the method is
		/// <code>static</code>, it cannot be overridden to avoid these problems.
		/// ANTLR no longer uses this method internally or in generated code.
		/// </summary>
		/// 
		[Obsolete("De-activated since version 2.7.2.6 as it cannot be overidden.", true)]
		public static void  panic()
		{
			System.Console.Error.WriteLine("Parser: panic");
			System.Environment.Exit(1);
		}

		public virtual void  removeMessageListener(MessageListener l)
		{
			if (!ignoreInvalidDebugCalls)
				throw new System.SystemException("removeMessageListener() is only valid if parser built for debugging");
		}
		public virtual void  removeParserListener(ParserListener l)
		{
			if (!ignoreInvalidDebugCalls)
				throw new System.SystemException("removeParserListener() is only valid if parser built for debugging");
		}
		public virtual void  removeParserMatchListener(ParserMatchListener l)
		{
			if (!ignoreInvalidDebugCalls)
				throw new System.SystemException("removeParserMatchListener() is only valid if parser built for debugging");
		}
		public virtual void  removeParserTokenListener(ParserTokenListener l)
		{
			if (!ignoreInvalidDebugCalls)
				throw new System.SystemException("removeParserTokenListener() is only valid if parser built for debugging");
		}
		public virtual void  removeSemanticPredicateListener(SemanticPredicateListener l)
		{
			if (!ignoreInvalidDebugCalls)
				throw new System.ArgumentException("removeSemanticPredicateListener() is only valid if parser built for debugging");
		}
		public virtual void  removeSyntacticPredicateListener(SyntacticPredicateListener l)
		{
			if (!ignoreInvalidDebugCalls)
				throw new System.ArgumentException("removeSyntacticPredicateListener() is only valid if parser built for debugging");
		}
		public virtual void  removeTraceListener(TraceListener l)
		{
			if (!ignoreInvalidDebugCalls)
				throw new System.SystemException("removeTraceListener() is only valid if parser built for debugging");
		}
		
		/*Parser error-reporting function can be overridden in subclass */
		public virtual void reportError(RecognitionException ex)
		{
			Console.Error.WriteLine(ex);
		}
		
		/*Parser error-reporting function can be overridden in subclass */
		public virtual void reportError(string s)
		{
			if (getFilename() == null)
			{
				Console.Error.WriteLine("error: " + s);
			}
			else
			{
				Console.Error.WriteLine(getFilename() + ": error: " + s);
			}
		}
		
		/*Parser warning-reporting function can be overridden in subclass */
		public virtual void  reportWarning(string s)
		{
			if (getFilename() == null)
			{
				Console.Error.WriteLine("warning: " + s);
			}
			else
			{
				Console.Error.WriteLine(getFilename() + ": warning: " + s);
			}
		}
		
		public virtual void recover(RecognitionException ex, BitSet tokenSet)
		{
			consume();
			consumeUntil(tokenSet);
		}
		
		public virtual void  rewind(int pos)
		{
			inputState.input.rewind(pos);
		}

		/// <summary>
		/// Specify an object with support code (shared by Parser and TreeParser.
		/// Normally, the programmer does not play with this, using 
		/// <see cref="setASTNodeClass"/> instead.
		/// </summary>
		/// <param name="f"></param>
		public virtual void  setASTFactory(ASTFactory f)
		{
			astFactory = f;
		}

		/// <summary>
		/// Specify the type of node to create during tree building. 
		/// </summary>
		/// <param name="cl">Fully qualified AST Node type name.</param>
		public virtual void  setASTNodeClass(string cl)
		{
			astFactory.setASTNodeType(cl);
		}

		/// <summary>
		/// Specify the type of node to create during tree building. 
		/// use <see cref="setASTNodeClass"/> now to be consistent with 
		/// Token Object Type accessor.
		/// </summary>
		/// <param name="nodeType">Fully qualified AST Node type name.</param>
		[Obsolete("Replaced by setASTNodeClass(string) since version 2.7.1", true)]
		public virtual void  setASTNodeType(string nodeType)
		{
			setASTNodeClass(nodeType);
		}

		public virtual void  setDebugMode(bool debugMode)
		{
			if (!ignoreInvalidDebugCalls)
				throw new System.SystemException("setDebugMode() only valid if parser built for debugging");
		}
		public virtual void  setFilename(string f)
		{
			inputState.filename = f;
		}
		public virtual void  setIgnoreInvalidDebugCalls(bool Value)
		{
			ignoreInvalidDebugCalls = Value;
		}
		/*Set or change the input token buffer */
		public virtual void  setTokenBuffer(TokenBuffer t)
		{
			inputState.input = t;
		}
		
		public virtual void  traceIndent()
		{
			 for (int i = 0; i < traceDepth; i++)
				Console.Out.Write(" ");
		}
		public virtual void  traceIn(string rname)
		{
			traceDepth += 1;
			traceIndent();
			Console.Out.WriteLine("> " + rname + "; LA(1)==" + LT(1).getText() + ((inputState.guessing > 0)?" [guessing]":""));
		}
		public virtual void  traceOut(string rname)
		{
			traceIndent();
			Console.Out.WriteLine("< " + rname + "; LA(1)==" + LT(1).getText() + ((inputState.guessing > 0)?" [guessing]":""));
			traceDepth -= 1;
		}
	}
}
Back to Top