Added ability to order the execution of dictionary adapter behaviors.
[castle.git] / Experiments / Attic / Rook / Castle.Rook.Compiler / Parser / antlr / debug / ParseTreeDebugParser.cs
blobeeb2f1dcc2a663f74a8ad5ffd2c2761f83ff2c8c
1 namespace antlr.debug
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 Stack = System.Collections.Stack;
17 using antlr;
18 using BitSet = antlr.collections.impl.BitSet;
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
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();
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;
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
53 public ParseTreeDebugParser(int k_) : base(k_)
57 public ParseTreeDebugParser(ParserSharedInputState state, int k_) : base(state, k_)
61 public ParseTreeDebugParser(TokenBuffer tokenBuf, int k_) : base(tokenBuf, k_)
65 public ParseTreeDebugParser(TokenStream lexer, int k_) : base(lexer,k_)
69 public ParseTree getParseTree()
71 return mostRecentParseTreeRoot;
74 public int getNumberOfDerivationSteps()
76 return numberOfDerivationSteps;
79 public override void match(int i) // throws MismatchedTokenException, TokenStreamException
81 addCurrentTokenToParseTree();
82 base.match(i);
85 public override void match(BitSet bitSet) // throws MismatchedTokenException, TokenStreamException
87 addCurrentTokenToParseTree();
88 base.match(bitSet);
91 public override void matchNot(int i) // throws MismatchedTokenException, TokenStreamException
93 addCurrentTokenToParseTree();
94 base.matchNot(i);
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
112 if (inputState.guessing > 0)
114 return;
116 ParseTreeRule root = (ParseTreeRule) currentParseTreeRoot.Peek();
117 ParseTreeToken tokenNode = null;
118 if ( LA(1) == Token.EOF_TYPE )
120 tokenNode = new ParseTreeToken(new antlr.CommonToken("EOF"));
122 else
124 tokenNode = new ParseTreeToken(LT(1));
126 root.addChild(tokenNode);
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
135 if (inputState.guessing > 0)
137 return;
139 ParseTreeRule subRoot = new ParseTreeRule(s);
140 if ( currentParseTreeRoot.Count > 0 )
142 ParseTreeRule oldRoot = (ParseTreeRule) currentParseTreeRoot.Peek();
143 oldRoot.addChild(subRoot);
145 currentParseTreeRoot.Push(subRoot);
146 numberOfDerivationSteps++;
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
155 if (inputState.guessing > 0)
157 return;
159 mostRecentParseTreeRoot = (ParseTreeRule) currentParseTreeRoot.Pop();