001: package antlr;
002:
003: /* ANTLR Translator Generator
004: * Project led by Terence Parr at http://www.cs.usfca.edu
005: * Software rights: http://www.antlr.org/license.html
006: */
007:
008: import antlr.collections.AST;
009: import antlr.collections.impl.BitSet;
010:
011: public class TreeParser extends MatchExceptionState {
012: /** The AST Null object; the parsing cursor is set to this when
013: * it is found to be null. This way, we can test the
014: * token type of a node without having to have tests for null
015: * everywhere.
016: */
017: public static ASTNULLType ASTNULL = new ASTNULLType();
018:
019: /** Where did this rule leave off parsing; avoids a return parameter */
020: protected AST _retTree;
021:
022: /** guessing nesting level; guessing==0 implies not guessing */
023: // protected int guessing = 0;
024: /** Nesting level of registered handlers */
025: // protected int exceptionLevel = 0;
026: protected TreeParserSharedInputState inputState;
027:
028: /** Table of token type to token names */
029: protected String[] tokenNames;
030:
031: /** AST return value for a rule is squirreled away here */
032: protected AST returnAST;
033:
034: /** AST support code; parser and treeparser delegate to this object */
035: protected ASTFactory astFactory = new ASTFactory();
036:
037: /** Used to keep track of indentdepth for traceIn/Out */
038: protected int traceDepth = 0;
039:
040: public TreeParser() {
041: inputState = new TreeParserSharedInputState();
042: }
043:
044: /** Get the AST return value squirreled away in the parser */
045: public AST getAST() {
046: return returnAST;
047: }
048:
049: public ASTFactory getASTFactory() {
050: return astFactory;
051: }
052:
053: public String getTokenName(int num) {
054: return tokenNames[num];
055: }
056:
057: public String[] getTokenNames() {
058: return tokenNames;
059: }
060:
061: protected void match(AST t, int ttype)
062: throws MismatchedTokenException {
063: //System.out.println("match("+ttype+"); cursor is "+t);
064: if (t == null || t == ASTNULL || t.getType() != ttype) {
065: throw new MismatchedTokenException(getTokenNames(), t,
066: ttype, false);
067: }
068: }
069:
070: /**Make sure current lookahead symbol matches the given set
071: * Throw an exception upon mismatch, which is catch by either the
072: * error handler or by the syntactic predicate.
073: */
074: public void match(AST t, BitSet b) throws MismatchedTokenException {
075: if (t == null || t == ASTNULL || !b.member(t.getType())) {
076: throw new MismatchedTokenException(getTokenNames(), t, b,
077: false);
078: }
079: }
080:
081: protected void matchNot(AST t, int ttype)
082: throws MismatchedTokenException {
083: //System.out.println("match("+ttype+"); cursor is "+t);
084: if (t == null || t == ASTNULL || t.getType() == ttype) {
085: throw new MismatchedTokenException(getTokenNames(), t,
086: ttype, true);
087: }
088: }
089:
090: /** @deprecated as of 2.7.2. This method calls System.exit() and writes
091: * directly to stderr, which is usually not appropriate when
092: * a parser is embedded into a larger application. Since the method is
093: * <code>static</code>, it cannot be overridden to avoid these problems.
094: * ANTLR no longer uses this method internally or in generated code.
095: */
096: public static void panic() {
097: System.err.println("TreeWalker: panic");
098: Utils.error("");
099: }
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 s) {
108: System.err.println("error: " + s);
109: }
110:
111: /** Parser warning-reporting function can be overridden in subclass */
112: public void reportWarning(String 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 nodeType) {
128: setASTNodeClass(nodeType);
129: }
130:
131: /** Specify the type of node to create during tree building */
132: public void setASTNodeClass(String nodeType) {
133: astFactory.setASTNodeClass(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 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 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: }
|