source: trunk/yao/share/antlr-2.7.7/lib/csharp/antlr.runtime/antlr.debug/ParseTreeDebugParser.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: 4.4 KB
Line 
1namespace antlr.debug
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 Stack = System.Collections.Stack;
17        using antlr;
18        using BitSet = antlr.collections.impl.BitSet;
19
20        /// <summary>
21        /// Specifies the behaviour required (i.e. parser modifications)
22        /// specifically to support parse tree debugging and derivation.
23        /// </summary>
24        /// <remarks>
25        /// <para>
26        /// Override the standard matching and rule entry/exit routines
27        /// to build parse trees.  This class is useful for 2.7.3 where
28        /// you can specify a superclass like
29        /// </para>
30        /// <para>
31        /// class TinyCParser extends Parser(ParseTreeDebugParser);
32        /// </para>
33        /// </remarks>
34        public class ParseTreeDebugParser : LLkParser
35        {
36                /// <summary>
37                /// Each new rule invocation must have it's own subtree. Tokens are
38                /// added to the current root so we must have a stack of subtree roots.
39                /// </summary>
40                protected Stack currentParseTreeRoot = new Stack();
41
42                /// <summary>
43                /// Track most recently created parse subtree so that when parsing
44                /// is finished, we can get to the root.
45                /// </summary>
46                protected ParseTreeRule mostRecentParseTreeRoot = null;
47
48                /// <summary>
49                /// For every rule replacement with a production, we bump up count.
50                /// </summary>
51                protected int numberOfDerivationSteps = 1; // n replacements plus step 0
52
53                public ParseTreeDebugParser(int k_) : base(k_)
54                {
55                }
56
57                public ParseTreeDebugParser(ParserSharedInputState state, int k_) : base(state, k_)
58                {
59                }
60
61                public ParseTreeDebugParser(TokenBuffer tokenBuf, int k_) : base(tokenBuf, k_)
62                {
63                }
64
65                public ParseTreeDebugParser(TokenStream lexer, int k_) : base(lexer,k_)
66                {
67                }
68
69                public ParseTree getParseTree() 
70                {
71                        return mostRecentParseTreeRoot;
72                }
73
74                public int getNumberOfDerivationSteps() 
75                {
76                        return numberOfDerivationSteps;
77                }
78
79                public override void match(int i)                       // throws MismatchedTokenException, TokenStreamException
80                {
81                        addCurrentTokenToParseTree();
82                        base.match(i);
83                }
84
85                public override void match(BitSet bitSet)       // throws MismatchedTokenException, TokenStreamException
86                {
87                        addCurrentTokenToParseTree();
88                        base.match(bitSet);
89                }
90
91                public override void matchNot(int i)            // throws MismatchedTokenException, TokenStreamException
92                {
93                        addCurrentTokenToParseTree();
94                        base.matchNot(i);
95                }
96
97                /// <summary>
98                /// Adds LT(1) to the current parse subtree.
99                /// </summary>
100                /// <remarks>
101                /// <para>
102                /// Note that the match() routines add the node before checking for
103                /// correct match.  This means that, upon mismatched token, there
104                /// will a token node in the tree corresponding to where that token
105                /// was expected.  For no viable alternative errors, no node will
106                /// be in the tree as nothing was matched() (the lookahead failed
107                /// to predict an alternative).
108                /// </para>
109                /// </remarks>
110                protected void addCurrentTokenToParseTree()             // throws TokenStreamException
111                {
112                        if (inputState.guessing > 0) 
113                        {
114                                return;
115                        }
116                        ParseTreeRule root = (ParseTreeRule) currentParseTreeRoot.Peek();
117                        ParseTreeToken tokenNode = null;
118                        if ( LA(1) == Token.EOF_TYPE ) 
119                        {
120                                tokenNode = new ParseTreeToken(new antlr.CommonToken("EOF"));
121                        }
122                        else 
123                        {
124                                tokenNode = new ParseTreeToken(LT(1));
125                        }
126                        root.addChild(tokenNode);
127                }
128
129                /// <summary>
130                /// Create a rule node, add to current tree, and make it current root
131                /// </summary>
132                /// <param name="s"></param>
133                public override void traceIn(string s)                          // throws TokenStreamException
134                {
135                        if (inputState.guessing > 0) 
136                        {
137                                return;
138                        }
139                        ParseTreeRule subRoot = new ParseTreeRule(s);
140                        if ( currentParseTreeRoot.Count > 0 ) 
141                        {
142                                ParseTreeRule oldRoot = (ParseTreeRule) currentParseTreeRoot.Peek();
143                                oldRoot.addChild(subRoot);
144                        }
145                        currentParseTreeRoot.Push(subRoot);
146                        numberOfDerivationSteps++;
147                }
148
149                /// <summary>
150                /// Pop current root; back to adding to old root
151                /// </summary>
152                /// <param name="s"></param>
153                public override void traceOut(string s)                                 // throws TokenStreamException
154                {
155                        if (inputState.guessing > 0) 
156                        {
157                                return;
158                        }
159                        mostRecentParseTreeRoot = (ParseTreeRule) currentParseTreeRoot.Pop();
160                }
161        }
162}
Note: See TracBrowser for help on using the repository browser.