source: trunk/yao/share/antlr-2.7.7/lib/csharp/antlr.runtime/antlr/ParseTreeRule.cs @ 1

Last change on this file since 1 was 1, checked in by lnalod, 15 years ago

Initial import of YAO sources

File size: 2.2 KB
Line 
1namespace antlr
2{
3
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         */
8
9        //
10        // ANTLR C# Code Generator by Micheal Jordan
11        //                            Kunle Odutola       : kunle UNDERSCORE odutola AT hotmail DOT com
12        //                            Anthony Oguntimehin
13        //
14
15        using System;
16        using StringBuilder     = System.Text.StringBuilder;
17        using AST                               = antlr.collections.AST;
18
19        public class ParseTreeRule : ParseTree
20        {
21                public const int INVALID_ALT = -1;
22
23                protected string ruleName;
24                protected int altNumber;  // unused until I modify antlr to record this
25
26                public ParseTreeRule(string ruleName) : this(ruleName, INVALID_ALT)
27                {
28                }
29
30                public ParseTreeRule(string ruleName, int altNumber) 
31                {
32                        this.ruleName  = ruleName;
33                        this.altNumber = altNumber;
34                }
35
36                public string getRuleName() 
37                {
38                        return ruleName;
39                }
40
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) 
50                {
51                        int numReplacements = 0;
52                        if ( step <= 0 ) 
53                        {
54                                buf.Append(' ');
55                                buf.Append(ToString());
56                                return numReplacements;
57                        }
58                        AST child = getFirstChild();
59                        numReplacements = 1;
60                        // walk child printing them out, descending into at most one
61                        while ( child != null ) 
62                        {
63                                if ( (numReplacements >= step) || (child is ParseTreeToken) )
64                                {
65                                        buf.Append(' ');
66                                        buf.Append(child.ToString());
67                                }
68                                else 
69                                {
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;
74                                }
75                                child = child.getNextSibling();
76                        }
77                        return numReplacements;
78                }
79
80                public override string ToString()
81                {
82                        if ( altNumber == INVALID_ALT ) 
83                        {
84                                return '<'+ruleName+'>';
85                        }
86                        else 
87                        {
88                                return '<'+ruleName+"["+altNumber+"]>";
89                        }
90                }
91        }
92}
Note: See TracBrowser for help on using the repository browser.