boolangstudio /Dependencies/boo/lib/antlr-2.7.5/lib/csharp/src/antlr/BaseAST.cs

Language C# Lines 616
MD5 Hash 926f13ee6de0ec263aca8cb3a83a5475 Estimated Cost $11,546 (why?)
Repository https://github.com/w4x/boolangstudio.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
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
using System;
using StringBuilder		= System.Text.StringBuilder;
using ISerializable		= System.Runtime.Serialization.ISerializable;
using TextWriter		= System.IO.TextWriter;
using ArrayList			= System.Collections.ArrayList;
using IEnumerator		= System.Collections.IEnumerator;

using AST				= antlr.collections.AST;

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.
	//

	/*
	* A Child-Sibling Tree.
	*
	* A tree with PLUS at the root and with two children 3 and 4 is
	* structured as:
	*
	*		PLUS
	*		  |
	*		  3 -- 4
	*
	* and can be specified easily in LISP notation as
	*
	* (PLUS 3 4)
	*
	* where every '(' starts a new subtree.
	*
	* These trees are particular useful for translators because of
	* the flexibility of the children lists.  They are also very easy
	* to walk automatically, whereas trees with specific children
	* reference fields can't easily be walked automatically.
	*
	* This class contains the basic support for an AST.
	* Most people will create ASTs that are subclasses of
	* BaseAST or of CommonAST.
	*/
	[Serializable()] 
	public abstract class BaseAST : AST
	{
		protected internal BaseAST down;
		protected internal BaseAST right;
		
		private static bool verboseStringConversion = false;
		private static string[] tokenNames = null;

		/*Add a node to the end of the child list for this node */
		public virtual void  addChild(AST node)
		{
			if (node == null)
				return ;
			BaseAST t = this.down;
			if (t != null)
			{
				while (t.right != null)
				{
					t = t.right;
				}
				t.right = (BaseAST) node;
			}
			else
			{
				this.down = (BaseAST) node;
			}
		}
		
		private void  doWorkForFindAll(ArrayList v, AST target, bool partialMatch)
		{
			AST sibling;
			
			// Start walking sibling lists, looking for matches.
//siblingWalk: 
			 for (sibling = this; sibling != null; sibling = sibling.getNextSibling())
			{
				if ((partialMatch && sibling.EqualsTreePartial(target)) || (!partialMatch && sibling.EqualsTree(target)))
				{
					v.Add(sibling);
				}
				// regardless of match or not, check any children for matches
				if (sibling.getFirstChild() != null)
				{
					((BaseAST) sibling.getFirstChild()).doWorkForFindAll(v, target, partialMatch);
				}
			}
		}
		
		public override bool Equals(object obj) 
		{      
			if (obj == null) 
				return false;       			
			if (this.GetType() != obj.GetType()) 
				return false;       			
			return Equals((AST)obj);       
		}    
		
		/*Is node t equal to this in terms of token type and text? */
		public virtual bool Equals(AST t)
		{
			if (t == null)
				return false;

			return	(Object.Equals(this.getText(), t.getText())) && 
					(this.Type == t.Type);
		}
		
		/*Is t an exact structural and equals() match of this tree.  The
		*  'this' reference is considered the start of a sibling list.
		*/
		public virtual bool EqualsList(AST t)
		{
			AST sibling;
			
			// the empty tree is not a match of any non-null tree.
			if (t == null)
			{
				return false;
			}
			
			// Otherwise, start walking sibling lists.  First mismatch, return false.
			 for (sibling = this; sibling != null && t != null; sibling = sibling.getNextSibling(), t = t.getNextSibling())
			{
				// as a quick optimization, check roots first.
				if (!sibling.Equals(t))
				{
					return false;
				}
				// if roots match, do full list match test on children.
				if (sibling.getFirstChild() != null)
				{
					if (!sibling.getFirstChild().EqualsList(t.getFirstChild()))
					{
						return false;
					}
				}
				else if (t.getFirstChild() != null)
				{
					return false;
				}
			}
			if (sibling == null && t == null)
			{
				return true;
			}
			// one sibling list has more than the other
			return false;
		}
		
		/*Is 'sub' a subtree of this list?
		*  The siblings of the root are NOT ignored.
		*/
		public virtual bool EqualsListPartial(AST sub)
		{
			AST sibling;
			
			// the empty tree is always a subset of any tree.
			if (sub == null)
			{
				return true;
			}
			
			// Otherwise, start walking sibling lists.  First mismatch, return false.
			 for (sibling = this; sibling != null && sub != null; sibling = sibling.getNextSibling(), sub = sub.getNextSibling())
			{
				// as a quick optimization, check roots first.
				if (!sibling.Equals(sub))
					return false;
				// if roots match, do partial list match test on children.
				if (sibling.getFirstChild() != null)
				{
					if (!sibling.getFirstChild().EqualsListPartial(sub.getFirstChild()))
						return false;
				}
			}
			if (sibling == null && sub != null)
			{
				// nothing left to match in this tree, but subtree has more
				return false;
			}
			// either both are null or sibling has more, but subtree doesn't
			return true;
		}
		
		/*Is tree rooted at 'this' equal to 't'?  The siblings
		*  of 'this' are ignored.
		*/
		public virtual bool EqualsTree(AST t)
		{
			// check roots first.
			if (!this.Equals(t))
				return false;
			// if roots match, do full list match test on children.
			if (this.getFirstChild() != null)
			{
				if (!this.getFirstChild().EqualsList(t.getFirstChild()))
					return false;
			}
			else if (t.getFirstChild() != null)
			{
				return false;
			}
			return true;
		}
		
		/*Is 't' a subtree of the tree rooted at 'this'?  The siblings
		*  of 'this' are ignored.
		*/
		public virtual bool EqualsTreePartial(AST sub)
		{
			// the empty tree is always a subset of any tree.
			if (sub == null)
			{
				return true;
			}
			
			// check roots first.
			if (!this.Equals(sub))
				return false;
			// if roots match, do full list partial match test on children.
			if (this.getFirstChild() != null)
			{
				if (!this.getFirstChild().EqualsListPartial(sub.getFirstChild()))
					return false;
			}
			return true;
		}
		
		/*Walk the tree looking for all exact subtree matches.  Return
		*  an IEnumerator that lets the caller walk the list
		*  of subtree roots found herein.
		*/
		public virtual IEnumerator findAll(AST target)
		{
			ArrayList roots = new ArrayList(10);
			//AST sibling;
			
			// the empty tree cannot result in an enumeration
			if (target == null)
			{
				return null;
			}
			
			doWorkForFindAll(roots, target, false); // find all matches recursively
			
			return roots.GetEnumerator();
		}
		
		/*Walk the tree looking for all subtrees.  Return
		*  an IEnumerator that lets the caller walk the list
		*  of subtree roots found herein.
		*/
		public virtual IEnumerator findAllPartial(AST sub)
		{
			ArrayList roots = new ArrayList(10);
			//AST sibling;
			
			// the empty tree cannot result in an enumeration
			if (sub == null)
			{
				return null;
			}
			
			doWorkForFindAll(roots, sub, true); // find all matches recursively
			
			return roots.GetEnumerator();
		}
		
		/*Get the first child of this node; null if not children */
		public virtual AST getFirstChild()
		{
			return down;
		}
		
		/*Get the next sibling in line after this one */
		public virtual AST getNextSibling()
		{
			return right;
		}
		
		/*Get the token text for this node */
		public virtual string getText()
		{
			return "";
		}
		
		/*Get the token type for this node */
		public virtual int Type
		{
			get { return 0; }
			set { ; }
		}
		
		/// <summary>
		/// Get number of children of this node; if leaf, returns 0
		/// </summary>
		/// <returns>Number of children</returns>
		public int getNumberOfChildren() 
		{
			BaseAST t = this.down;
			int n = 0;
			if (t != null) 
			{
				n = 1;
				while (t.right != null) 
				{
					t = t.right;
					n++;
				}
			}
			return n;
		}

		public abstract void  initialize(int t, string txt);
		
		public abstract void  initialize(AST t);
		
		public abstract void  initialize(IToken t);
		
		/*Remove all children */
		public virtual void  removeChildren()
		{
			down = null;
		}
		
		public virtual void  setFirstChild(AST c)
		{
			down = (BaseAST) c;
		}
		
		public virtual void  setNextSibling(AST n)
		{
			right = (BaseAST) n;
		}
		
		/*Set the token text for this node */
		public virtual void  setText(string text)
		{
			;
		}
		
		/*Set the token type for this node */
		public virtual void  setType(int ttype)
		{
			this.Type = ttype;
		}
		
		public static void  setVerboseStringConversion(bool verbose, string[] names)
		{
			verboseStringConversion = verbose;
			tokenNames = names;
		}
		
		override public string ToString()
		{
			StringBuilder b = new StringBuilder();
			// if verbose and type name not same as text (keyword probably)
			if (verboseStringConversion && 
					(0 != String.Compare(getText(), (tokenNames[Type]), true)) &&
					(0 != String.Compare(getText(), StringUtils.stripFrontBack(tokenNames[Type], @"""", @""""), true)))
			{
				b.Append('[');
				b.Append(getText());
				b.Append(",<");
				b.Append(tokenNames[Type]);
				b.Append(">]");
				return b.ToString();
			}
			return getText();
		}
		
		/*Print out a child-sibling tree in LISP notation */
		public virtual string ToStringList()
		{
			AST t = this;
			string ts = "";
			if (t.getFirstChild() != null)
				ts += " (";
			ts += " " + this.ToString();
			if (t.getFirstChild() != null)
			{
				ts += ((BaseAST) t.getFirstChild()).ToStringList();
			}
			if (t.getFirstChild() != null)
				ts += " )";
			if (t.getNextSibling() != null)
			{
				ts += ((BaseAST) t.getNextSibling()).ToStringList();
			}
			return ts;
		}
		
		public virtual string ToStringTree() 
		{
			AST t = this;
			string ts = "";
			if (t.getFirstChild() != null) 
			{
				ts += " (";
			}
			ts += " " + this.ToString();
			if (t.getFirstChild() != null)
			{
				ts += ((BaseAST) t.getFirstChild()).ToStringList();
			}
			if (t.getFirstChild() != null)
			{
				ts += " )";
			}
			return ts;
		 }

		public virtual string ToTree()
		{
			return ToTree(string.Empty);
		}
		
		public virtual string ToTree(string prefix) 
		{
			StringBuilder sb = new StringBuilder(prefix);
		
			// Replace vertical bar if there is no next sibling.
			if ( (getNextSibling() == null) )
				sb.Append("+--");
			else
				sb.Append("|--");
		
			sb.Append( ToString() );
			sb.Append( Environment.NewLine );

			if ( getFirstChild() != null ) 
			{
				// Replace vertical bar if there is no next sibling.
				if ( getNextSibling() == null )
					sb.Append( ((BaseAST) getFirstChild()).ToTree(prefix + "   ") );
				else
					sb.Append( ((BaseAST) getFirstChild()).ToTree(prefix + "|  ") );
			}

			if ( getNextSibling() != null )
				sb.Append( ((BaseAST) getNextSibling()).ToTree(prefix) );

			return sb.ToString();
		}

		public static string decode(string text)
		{
			char c, c1, c2, c3, c4, c5;
			StringBuilder n = new StringBuilder();
			 for (int i = 0; i < text.Length; i++)
			{
				c = text[i];
				if (c == '&')
				{
					c1 = text[i + 1];
					c2 = text[i + 2];
					c3 = text[i + 3];
					c4 = text[i + 4];
					c5 = text[i + 5];
					
					if (c1 == 'a' && c2 == 'm' && c3 == 'p' && c4 == ';')
					{
						n.Append("&");
						i += 5;
					}
					else if (c1 == 'l' && c2 == 't' && c3 == ';')
					{
						n.Append("<");
						i += 4;
					}
					else if (c1 == 'g' && c2 == 't' && c3 == ';')
					{
						n.Append(">");
						i += 4;
					}
					else if (c1 == 'q' && c2 == 'u' && c3 == 'o' && c4 == 't' && c5 == ';')
					{
						n.Append("\"");
						i += 6;
					}
					else if (c1 == 'a' && c2 == 'p' && c3 == 'o' && c4 == 's' && c5 == ';')
					{
						n.Append("'");
						i += 6;
					}
					else
						n.Append("&");
				}
				else
					n.Append(c);
			}
			return n.ToString();
		}
		
		public static string encode(string text)
		{
			char c;
			StringBuilder n = new StringBuilder();
			 for (int i = 0; i < text.Length; i++)
			{
				c = text[i];
				switch (c)
				{
					case '&': 
					{
						n.Append("&amp;");
						break;
					}
					
					case '<': 
					{
						n.Append("&lt;");
						break;
					}
					
					case '>': 
					{
						n.Append("&gt;");
						break;
					}
					
					case '"': 
					{
						n.Append("&quot;");
						break;
					}
					
					case '\'': 
					{
						n.Append("&apos;");
						break;
					}
					
					default: 
					{
						n.Append(c);
						break;
					}
					
				}
			}
			return n.ToString();
		}
		
		public virtual void  xmlSerializeNode(TextWriter outWriter)
		{
			StringBuilder buf = new StringBuilder(100);
			buf.Append("<");
			buf.Append(GetType().FullName + " ");
			buf.Append("text=\"" + encode(getText()) + "\" type=\"" + Type + "\"/>");
			outWriter.Write(buf.ToString());
		}
		
		public virtual void  xmlSerializeRootOpen(TextWriter outWriter)
		{
			StringBuilder buf = new StringBuilder(100);
			buf.Append("<");
			buf.Append(GetType().FullName + " ");
			buf.Append("text=\"" + encode(getText()) + "\" type=\"" + Type + "\">\n");
			outWriter.Write(buf.ToString());
		}
		
		public virtual void  xmlSerializeRootClose(TextWriter outWriter)
		{
			outWriter.Write("</" + GetType().FullName + ">\n");
		}
		
		public virtual void  xmlSerialize(TextWriter outWriter)
		{
			// print out this node and all siblings
			 for (AST node = this; node != null; node = node.getNextSibling())
			{
				if (node.getFirstChild() == null)
				{
					// print guts (class name, attributes)
					((BaseAST) node).xmlSerializeNode(outWriter);
				}
				else
				{
					((BaseAST) node).xmlSerializeRootOpen(outWriter);
					
					// print children
					((BaseAST) node.getFirstChild()).xmlSerialize(outWriter);
					
					// print end tag
					((BaseAST) node).xmlSerializeRootClose(outWriter);
				}
			}
		}

		#region Implementation of ICloneable
		[Obsolete("Deprecated since version 2.7.2. Use ASTFactory.dup() instead.", false)]
		public virtual object Clone()
		{
			return MemberwiseClone();
		}
		#endregion

		public override Int32 GetHashCode() 
		{
			return base.GetHashCode();
		}	
	}
}
Back to Top