1	package antlr;
2	
3	/**
4	 * <b>SOFTWARE RIGHTS</b>
5	 * <p>
6	 * ANTLR 2.5.0 MageLang Institute, 1998
7	 * <p>
8	 * We reserve no legal rights to the ANTLR--it is fully in the
9	 * public domain. An individual or company may do whatever
10	 * they wish with source code distributed with ANTLR or the
11	 * code generated by ANTLR, including the incorporation of
12	 * ANTLR, or its output, into commerical software.
13	 * <p>
14	 * We encourage users to develop software with ANTLR. However,
15	 * we do ask that credit is given to us for developing
16	 * ANTLR. By "credit", we mean that if you use ANTLR or
17	 * incorporate any source code into one of your programs
18	 * (commercial product, research project, or otherwise) that
19	 * you acknowledge this fact somewhere in the documentation,
20	 * research report, etc... If you like ANTLR and have
21	 * developed a nice tool with the output, please mention that
22	 * you developed it using ANTLR. In addition, we ask that the
23	 * headers remain intact in our source code. As long as these
24	 * guidelines are kept, we expect to continue enhancing this
25	 * system and expect to make other tools available as they are
26	 * completed.
27	 * <p>
28	 * The ANTLR gang:
29	 * @version ANTLR 2.5.0 MageLang Institute, 1998
30	 * @author Terence Parr, <a href=http://www.MageLang.com>MageLang Institute</a>
31	 * @author <br>John Lilley, <a href=http://www.Empathy.com>Empathy Software</a>
32	 */
33	import java.util.NoSuchElementException;
34	import antlr.collections.AST;
35	import antlr.collections.impl.BitSet;
36	
37	public class TreeParser {
38		/** The AST Null object; the parsing cursor is set to this when
39		 *  it is found to be null.  This way, we can test the
40		 *  token type of a node without having to have tests for null
41		 *  everywhere.
42		 */
43		public static ASTNULLType ASTNULL = new ASTNULLType();
44	
45		/** Where did this rule leave off parsing; avoids a return parameter */
46		protected AST _retTree;
47		
48		/** guessing nesting level; guessing==0 implies not guessing */
49		protected int guessing = 0;
50		
51		/** Nesting level of registered handlers */
52		protected int exceptionLevel = 0;
53		
54		/** Table of token type to token names */
55		protected String[] tokenNames;
56	
57		/** AST return value for a rule is squirreled away here */
58		protected AST returnAST;
59	
60		/** AST support code; parser and treeparser delegate to this object */
61		protected ASTFactory astFactory = new ASTFactory();
62	
63		/** Get the AST return value squirreled away in the parser */
64		public AST getAST() {
65			return returnAST;
66		}
67		public ASTFactory getASTFactory() {
68			return astFactory;
69		}
70		public String getTokenName(int num) {
71			return tokenNames[num];
72		}
73		public String[] getTokenNames() {
74			return tokenNames;
75		}
76		protected void match(AST t, int ttype) throws MismatchedTokenException {
77			//System.out.println("match("+ttype+"); cursor is "+t);
78			if ( t==null || t==ASTNULL || t.getType() != ttype ) {
79				throw new MismatchedTokenException(getTokenNames(), t, ttype, false);
80			}
81		}
82		/**Make sure current lookahead symbol matches the given set
83		 * Throw an exception upon mismatch, which is catch by either the
84		 * error handler or by the syntactic predicate.
85		 */
86		public void match(AST t, BitSet b) throws MismatchedTokenException {
87			if ( t==null || t==ASTNULL || !b.member(t.getType()) ) {
88				throw new MismatchedTokenException(getTokenNames(), t, b, false);
89			}
90		}
91		protected void matchNot(AST t, int ttype) throws MismatchedTokenException {
92			//System.out.println("match("+ttype+"); cursor is "+t);
93			if ( t==null || t==ASTNULL || t.getType() == ttype ) {
94				throw new MismatchedTokenException(getTokenNames(), t, ttype, true);
95			}
96		}
97		public static void panic() {
98			System.err.println("TreeWalker: panic");
99			System.exit(1);
100		}
101		/** Parser error-reporting function can be overridden in subclass */
102		public void reportError(ParserException ex) {
103			System.err.println("Error: " + ex.toString());
104		}
105		/** Parser error-reporting function can be overridden in subclass */
106		public void reportError(String s) {
107			System.err.println("Error: " + s);
108		}
109		/** Parser warning-reporting function can be overridden in subclass */
110		public void reportWarning(String s) {
111			System.err.println("Warning: " + s);
112		}
113		/** Specify an object with support code (shared by
114		 *  Parser and TreeParser.  Normally, the programmer
115		 *  does not play with this, using setASTNodeType instead.
116		 */
117		public void setASTFactory(ASTFactory f) {
118			astFactory = f;
119		}
120	/** Specify the type of node to create during tree building */
121	public void setASTNodeType (String nodeType) {
122		astFactory.setASTNodeType(nodeType);
123	}
124		public void traceIn(String rname, AST t) {
125			System.out.println("enter "+rname+
126				"("+(t!=null?t.toString():"null")+")"+
127				((guessing>0)?" [guessing]":""));
128		}
129		public void traceOut(String rname, AST t) {
130			System.out.println("exit "+rname+
131				"("+(t!=null?t.toString():"null")+")"+
132				((guessing>0)?" [guessing]":""));
133		}
134	}
135