Added ability to order the execution of dictionary adapter behaviors.
[castle.git] / Experiments / Attic / Rook / Castle.Rook.Compiler / Parser / antlr / ParseTreeRule.cs
blob74fec02c606372df85f42502e8c277078337d420
1 namespace antlr
4 /* ANTLR Translator Generator
5 * Project led by Terence Parr at http://www.jGuru.com
6 * Software rights: http://www.antlr.org/license.html
7 */
9 //
10 // ANTLR C# Code Generator by Micheal Jordan
11 // Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com
12 // Anthony Oguntimehin
15 using System;
16 using StringBuilder = System.Text.StringBuilder;
17 using AST = antlr.collections.AST;
19 public class ParseTreeRule : ParseTree
21 public const int INVALID_ALT = -1;
23 protected string ruleName;
24 protected int altNumber; // unused until I modify antlr to record this
26 public ParseTreeRule(string ruleName) : this(ruleName, INVALID_ALT)
30 public ParseTreeRule(string ruleName, int altNumber)
32 this.ruleName = ruleName;
33 this.altNumber = altNumber;
36 public string getRuleName()
38 return ruleName;
41 /// <summary>
42 /// Do a step-first walk, building up a buffer of tokens until
43 /// you've reached a particular step and print out any rule subroots
44 /// insteads of descending.
45 /// </summary>
46 /// <param name="buf">derivation buffer</param>
47 /// <param name="step">derivation steps</param>
48 /// <returns></returns>
49 protected internal override int getLeftmostDerivation(StringBuilder buf, int step)
51 int numReplacements = 0;
52 if ( step <= 0 )
54 buf.Append(' ');
55 buf.Append(ToString());
56 return numReplacements;
58 AST child = getFirstChild();
59 numReplacements = 1;
60 // walk child printing them out, descending into at most one
61 while ( child != null )
63 if ( (numReplacements >= step) || (child is ParseTreeToken) )
65 buf.Append(' ');
66 buf.Append(child.ToString());
68 else
70 // descend for at least one more derivation; update count
71 int remainingReplacements = step - numReplacements;
72 int n = ((ParseTree) child).getLeftmostDerivation(buf, remainingReplacements);
73 numReplacements += n;
75 child = child.getNextSibling();
77 return numReplacements;
80 public override string ToString()
82 if ( altNumber == INVALID_ALT )
84 return '<'+ruleName+'>';
86 else
88 return '<'+ruleName+"["+altNumber+"]>";