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 Stack
= System
.Collections
.Stack
;
18 using BitSet
= antlr
.collections
.impl
.BitSet
;
21 /// Specifies the behaviour required (i.e. parser modifications)
22 /// specifically to support parse tree debugging and derivation.
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
31 /// class TinyCParser extends Parser(ParseTreeDebugParser);
34 public class ParseTreeDebugParser
: LLkParser
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.
40 protected Stack currentParseTreeRoot
= new Stack();
43 /// Track most recently created parse subtree so that when parsing
44 /// is finished, we can get to the root.
46 protected ParseTreeRule mostRecentParseTreeRoot
= null;
49 /// For every rule replacement with a production, we bump up count.
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();
85 public override void match(BitSet bitSet
) // throws MismatchedTokenException, TokenStreamException
87 addCurrentTokenToParseTree();
91 public override void matchNot(int i
) // throws MismatchedTokenException, TokenStreamException
93 addCurrentTokenToParseTree();
98 /// Adds LT(1) to the current parse subtree.
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).
110 protected void addCurrentTokenToParseTree() // throws TokenStreamException
112 if (inputState
.guessing
> 0)
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"));
124 tokenNode
= new ParseTreeToken(LT(1));
126 root
.addChild(tokenNode
);
130 /// Create a rule node, add to current tree, and make it current root
132 /// <param name="s"></param>
133 public override void traceIn(string s
) // throws TokenStreamException
135 if (inputState
.guessing
> 0)
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
++;
150 /// Pop current root; back to adding to old root
152 /// <param name="s"></param>
153 public override void traceOut(string s
) // throws TokenStreamException
155 if (inputState
.guessing
> 0)
159 mostRecentParseTreeRoot
= (ParseTreeRule
) currentParseTreeRoot
.Pop();