1 | namespace antlr |
---|
2 | { |
---|
3 | /* ANTLR Translator Generator |
---|
4 | * Project led by Terence Parr at http://www.jGuru.com |
---|
5 | * Software rights: http://www.antlr.org/license.html |
---|
6 | */ |
---|
7 | |
---|
8 | // |
---|
9 | // ANTLR C# Code Generator by Micheal Jordan |
---|
10 | // Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com |
---|
11 | // Anthony Oguntimehin |
---|
12 | // |
---|
13 | |
---|
14 | using System; |
---|
15 | using StringBuilder = System.Text.StringBuilder; |
---|
16 | using AST = antlr.collections.AST; |
---|
17 | |
---|
18 | public abstract class ParseTree : BaseAST |
---|
19 | { |
---|
20 | /// <summary> |
---|
21 | /// Walk parse tree and return requested number of derivation steps. |
---|
22 | /// If steps less-than 0, return node text. If steps equals 1, return derivation |
---|
23 | /// string at step. |
---|
24 | /// </summary> |
---|
25 | /// <param name="step">derivation steps</param> |
---|
26 | /// <returns></returns> |
---|
27 | public string getLeftmostDerivationStep(int step) |
---|
28 | { |
---|
29 | if ( step <= 0 ) |
---|
30 | { |
---|
31 | return ToString(); |
---|
32 | } |
---|
33 | StringBuilder buf = new StringBuilder (2000); |
---|
34 | getLeftmostDerivation(buf, step); |
---|
35 | return buf.ToString(); |
---|
36 | } |
---|
37 | |
---|
38 | public string getLeftmostDerivation(int maxSteps) |
---|
39 | { |
---|
40 | StringBuilder buf = new StringBuilder(2000); |
---|
41 | buf.Append(" " + this.ToString()); |
---|
42 | buf.Append("\n"); |
---|
43 | for (int d=1; d < maxSteps; d++) |
---|
44 | { |
---|
45 | buf.Append(" =>"); |
---|
46 | buf.Append(getLeftmostDerivationStep(d)); |
---|
47 | buf.Append("\n"); |
---|
48 | } |
---|
49 | return buf.ToString(); |
---|
50 | } |
---|
51 | |
---|
52 | /// <summary> |
---|
53 | /// Get derivation and return how many you did (less than requested for |
---|
54 | /// subtree roots. |
---|
55 | /// </summary> |
---|
56 | /// <param name="buf">string buffer</param> |
---|
57 | /// <param name="step">derivation steps</param> |
---|
58 | /// <returns></returns> |
---|
59 | protected internal abstract int getLeftmostDerivation(StringBuilder buf, int step); |
---|
60 | |
---|
61 | // just satisfy BaseAST interface; unused as we manually create nodes |
---|
62 | |
---|
63 | public override void initialize(int i, string s) |
---|
64 | { |
---|
65 | } |
---|
66 | |
---|
67 | public override void initialize(AST ast) |
---|
68 | { |
---|
69 | } |
---|
70 | |
---|
71 | public override void initialize(IToken token) |
---|
72 | { |
---|
73 | } |
---|
74 | } |
---|
75 | } |
---|