KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > TreeParser


1 package persistence.antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/license.html
6  *
7  */

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

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

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

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

77     public void match(AST t, BitSet b) throws MismatchedTokenException {
78         if (t == null || t == ASTNULL || !b.member(t.getType())) {
79             throw new MismatchedTokenException(getTokenNames(), t, b, false);
80         }
81     }
82
83     protected void matchNot(AST t, int ttype) throws MismatchedTokenException {
84         //System.out.println("match("+ttype+"); cursor is "+t);
85
if (t == null || t == ASTNULL || t.getType() == ttype) {
86             throw new MismatchedTokenException(getTokenNames(), t, ttype, true);
87         }
88     }
89
90     /** @deprecated as of 2.7.2. This method calls System.exit() and writes
91      * directly to stderr, which is usually not appropriate when
92      * a parser is embedded into a larger application. Since the method is
93      * <code>static</code>, it cannot be overridden to avoid these problems.
94      * ANTLR no longer uses this method internally or in generated code.
95      */

96     public static void panic() {
97         System.err.println("TreeWalker: panic");
98         System.exit(1);
99     }
100
101     /** Parser error-reporting function can be overridden in subclass */
102     public void reportError(RecognitionException ex) {
103         System.err.println(ex.toString());
104     }
105
106     /** Parser error-reporting function can be overridden in subclass */
107     public void reportError(String JavaDoc s) {
108         System.err.println("error: " + s);
109     }
110
111     /** Parser warning-reporting function can be overridden in subclass */
112     public void reportWarning(String JavaDoc s) {
113         System.err.println("warning: " + s);
114     }
115
116     /** Specify an object with support code (shared by
117      * Parser and TreeParser. Normally, the programmer
118      * does not play with this, using setASTNodeType instead.
119      */

120     public void setASTFactory(ASTFactory f) {
121         astFactory = f;
122     }
123
124     /** Specify the type of node to create during tree building.
125      * @deprecated since 2.7.2
126      */

127     public void setASTNodeType(String JavaDoc nodeType) {
128         setASTNodeClass(nodeType);
129     }
130
131     /** Specify the type of node to create during tree building */
132     public void setASTNodeClass(String JavaDoc nodeType) {
133         astFactory.setASTNodeType(nodeType);
134     }
135
136     public void traceIndent() {
137         for (int i = 0; i < traceDepth; i++)
138             System.out.print(" ");
139     }
140
141     public void traceIn(String JavaDoc rname, AST t) {
142         traceDepth += 1;
143         traceIndent();
144         System.out.println("> " + rname +
145                            "(" + (t != null?t.toString():"null") + ")" +
146                            ((inputState.guessing > 0)?" [guessing]":""));
147     }
148
149     public void traceOut(String JavaDoc rname, AST t) {
150         traceIndent();
151         System.out.println("< " + rname +
152                            "(" + (t != null?t.toString():"null") + ")" +
153                            ((inputState.guessing > 0)?" [guessing]":""));
154         traceDepth--;
155     }
156 }
157
Popular Tags