KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > TreeParser


1 package antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/RIGHTS.html
6  *
7  * $Id: //depot/code/org.antlr/main/main/antlr/TreeParser.java#5 $
8  */

9
10 import java.util.NoSuchElementException JavaDoc;
11
12 import antlr.collections.AST;
13 import antlr.collections.impl.BitSet;
14
15 public class TreeParser {
16     /** The AST Null object; the parsing cursor is set to this when
17      * it is found to be null. This way, we can test the
18      * token type of a node without having to have tests for null
19      * everywhere.
20      */

21     public static ASTNULLType ASTNULL = new ASTNULLType();
22
23     /** Where did this rule leave off parsing; avoids a return parameter */
24     protected AST _retTree;
25
26     /** guessing nesting level; guessing==0 implies not guessing */
27     // protected int guessing = 0;
28

29     /** Nesting level of registered handlers */
30     // protected int exceptionLevel = 0;
31

32     protected TreeParserSharedInputState inputState;
33
34     /** Table of token type to token names */
35     protected String JavaDoc[] tokenNames;
36
37     /** AST return value for a rule is squirreled away here */
38     protected AST returnAST;
39
40     /** AST support code; parser and treeparser delegate to this object */
41     protected ASTFactory astFactory = new ASTFactory();
42
43     /** Used to keep track of indentdepth for traceIn/Out */
44     protected int traceDepth = 0;
45
46     public TreeParser() {
47         inputState = new TreeParserSharedInputState();
48     }
49
50     /** Get the AST return value squirreled away in the parser */
51     public AST getAST() {
52         return returnAST;
53     }
54
55     public ASTFactory getASTFactory() {
56         return astFactory;
57     }
58
59     public String JavaDoc getTokenName(int num) {
60         return tokenNames[num];
61     }
62
63     public String JavaDoc[] getTokenNames() {
64         return tokenNames;
65     }
66
67     protected void match(AST t, int ttype) throws MismatchedTokenException {
68         //System.out.println("match("+ttype+"); cursor is "+t);
69
if (t == null || t == ASTNULL || t.getType() != ttype) {
70             throw new MismatchedTokenException(getTokenNames(), t, ttype, false);
71         }
72     }
73
74     /**Make sure current lookahead symbol matches the given set
75      * Throw an exception upon mismatch, which is catch by either the
76      * error handler or by the syntactic predicate.
77      */

78     public void match(AST t, BitSet b) throws MismatchedTokenException {
79         if (t == null || t == ASTNULL || !b.member(t.getType())) {
80             throw new MismatchedTokenException(getTokenNames(), t, b, false);
81         }
82     }
83
84     protected void matchNot(AST t, int ttype) throws MismatchedTokenException {
85         //System.out.println("match("+ttype+"); cursor is "+t);
86
if (t == null || t == ASTNULL || t.getType() == ttype) {
87             throw new MismatchedTokenException(getTokenNames(), t, ttype, true);
88         }
89     }
90
91     public static void panic() {
92         System.err.println("TreeWalker: panic");
93         System.exit(1);
94     }
95
96     /** Parser error-reporting function can be overridden in subclass */
97     public void reportError(RecognitionException ex) {
98         System.err.println(ex.toString());
99     }
100
101     /** Parser error-reporting function can be overridden in subclass */
102     public void reportError(String JavaDoc s) {
103         System.err.println("error: " + s);
104     }
105
106     /** Parser warning-reporting function can be overridden in subclass */
107     public void reportWarning(String JavaDoc s) {
108         System.err.println("warning: " + s);
109     }
110
111     /** Specify an object with support code (shared by
112      * Parser and TreeParser. Normally, the programmer
113      * does not play with this, using setASTNodeType instead.
114      */

115     public void setASTFactory(ASTFactory f) {
116         astFactory = f;
117     }
118
119     /** Specify the type of node to create during tree building */
120     public void setASTNodeType(String JavaDoc nodeType) {
121         setASTNodeClass(nodeType);
122     }
123
124     /** Specify the type of node to create during tree building */
125     public void setASTNodeClass(String JavaDoc nodeType) {
126         astFactory.setASTNodeType(nodeType);
127     }
128
129     public void traceIndent() {
130         for (int i = 0; i < traceDepth; i++)
131             System.out.print(" ");
132     }
133
134     public void traceIn(String JavaDoc rname, AST t) {
135         traceDepth += 1;
136         traceIndent();
137         System.out.println("> " + rname +
138                            "(" + (t != null?t.toString():"null") + ")" +
139                            ((inputState.guessing > 0)?" [guessing]":""));
140     }
141
142     public void traceOut(String JavaDoc rname, AST t) {
143         traceIndent();
144         System.out.println("< " + rname +
145                            "(" + (t != null?t.toString():"null") + ")" +
146                            ((inputState.guessing > 0)?" [guessing]":""));
147         traceDepth--;
148     }
149 }
150
Popular Tags