2 using AST
= antlr
.collections
.AST
;
3 using BitSet
= antlr
.collections
.impl
.BitSet
;
7 /*ANTLR Translator Generator
8 * Project led by Terence Parr at http://www.jGuru.com
9 * Software rights: http://www.antlr.org/license.html
15 // ANTLR C# Code Generator by Micheal Jordan
16 // Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com
17 // Anthony Oguntimehin
19 // With many thanks to Eric V. Smith from the ANTLR list.
22 public class TreeParser
24 /*The AST Null object; the parsing cursor is set to this when
25 * it is found to be null. This way, we can test the
26 * token type of a node without having to have tests for null
29 public static ASTNULLType ASTNULL
= new ASTNULLType();
31 /*Where did this rule leave off parsing; avoids a return parameter */
32 protected internal AST retTree_
;
34 /*guessing nesting level; guessing==0 implies not guessing */
35 // protected int guessing = 0;
37 /*Nesting level of registered handlers */
38 // protected int exceptionLevel = 0;
40 protected internal TreeParserSharedInputState inputState
;
42 /*Table of token type to token names */
43 protected internal string[] tokenNames
;
45 /*AST return value for a rule is squirreled away here */
46 protected internal AST returnAST
;
48 /*AST support code; parser and treeparser delegate to this object */
49 protected internal ASTFactory astFactory
= new ASTFactory();
51 /*Used to keep track of indentdepth for traceIn/Out */
52 protected internal int traceDepth
= 0;
56 inputState
= new TreeParserSharedInputState();
58 /*Get the AST return value squirreled away in the parser */
59 public virtual AST
getAST()
63 public virtual ASTFactory
getASTFactory()
67 public virtual void resetState()
74 public virtual string getTokenName(int num
)
76 return tokenNames
[num
];
78 public virtual string[] getTokenNames()
82 protected internal virtual void match(AST t
, int ttype
)
84 //System.out.println("match("+ttype+"); cursor is "+t);
85 if (t
== null || t
== ASTNULL
|| t
.Type
!= ttype
)
87 throw new MismatchedTokenException(getTokenNames(), t
, ttype
, false);
90 /*Make sure current lookahead symbol matches the given set
91 * Throw an exception upon mismatch, which is catch by either the
92 * error handler or by the syntactic predicate.
94 public virtual void match(AST t
, BitSet b
)
96 if (t
== null || t
== ASTNULL
|| !b
.member(t
.Type
))
98 throw new MismatchedTokenException(getTokenNames(), t
, b
, false);
101 protected internal virtual void matchNot(AST t
, int ttype
)
103 //System.out.println("match("+ttype+"); cursor is "+t);
104 if (t
== null || t
== ASTNULL
|| t
.Type
== ttype
)
106 throw new MismatchedTokenException(getTokenNames(), t
, ttype
, true);
111 /// @deprecated as of 2.7.2. This method calls System.exit() and writes
112 /// directly to stderr, which is usually not appropriate when
113 /// a parser is embedded into a larger application. Since the method is
114 /// <code>static</code>, it cannot be overridden to avoid these problems.
115 /// ANTLR no longer uses this method internally or in generated code.
118 [Obsolete("De-activated since version 2.7.2.6 as it cannot be overidden.", true)]
119 public static void panic()
121 Console
.Error
.WriteLine("TreeWalker: panic");
122 System
.Environment
.Exit(1);
124 /*Parser error-reporting function can be overridden in subclass */
125 public virtual void reportError(RecognitionException ex
)
127 Console
.Error
.WriteLine(ex
.ToString());
129 /*Parser error-reporting function can be overridden in subclass */
130 public virtual void reportError(string s
)
132 Console
.Error
.WriteLine("error: " + s
);
134 /*Parser warning-reporting function can be overridden in subclass */
135 public virtual void reportWarning(string s
)
137 Console
.Error
.WriteLine("warning: " + s
);
139 /*Specify an object with support code (shared by
140 * Parser and TreeParser. Normally, the programmer
141 * does not play with this, using setASTNodeType instead.
143 public virtual void setASTFactory(ASTFactory f
)
148 /*Specify the type of node to create during tree building */
149 public virtual void setASTNodeType(string nodeType
)
151 setASTNodeClass(nodeType
);
154 /*Specify the type of node to create during tree building */
155 public virtual void setASTNodeClass(string nodeType
)
157 astFactory
.setASTNodeType(nodeType
);
160 public virtual void traceIndent()
162 for (int i
= 0; i
< traceDepth
; i
++)
163 Console
.Out
.Write(" ");
165 public virtual void traceIn(string rname
, AST t
)
169 Console
.Out
.WriteLine("> " + rname
+ "(" + ((t
!= null) ? t
.ToString() : "null") + ")" + ((inputState
.guessing
> 0) ? " [guessing]" : ""));
171 public virtual void traceOut(string rname
, AST t
)
174 Console
.Out
.WriteLine("< " + rname
+ "(" + ((t
!= null) ? t
.ToString() : "null") + ")" + ((inputState
.guessing
> 0) ? " [guessing]" : ""));