4 /* ANTLR Translator Generator
5 * Project led by Terence Parr at http://www.jGuru.com
6 * Software rights: http://www.antlr.org/license.html
10 // ANTLR C# Code Generator by Micheal Jordan
11 // Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com
12 // Anthony Oguntimehin
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()
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.
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;
55 buf
.Append(ToString());
56 return numReplacements
;
58 AST child
= getFirstChild();
60 // walk child printing them out, descending into at most one
61 while ( child
!= null )
63 if ( (numReplacements
>= step
) || (child
is ParseTreeToken
) )
66 buf
.Append(child
.ToString());
70 // descend for at least one more derivation; update count
71 int remainingReplacements
= step
- numReplacements
;
72 int n
= ((ParseTree
) child
).getLeftmostDerivation(buf
, remainingReplacements
);
75 child
= child
.getNextSibling();
77 return numReplacements
;
80 public override string ToString()
82 if ( altNumber
== INVALID_ALT
)
84 return '<'+ruleName
+'>';
88 return '<'+ruleName
+"["+altNumber
+"]>";