001: package persistence.antlr;
002:
003: /* ANTLR Translator Generator
004: * Project led by Terence Parr at http://www.jGuru.com
005: * Software rights: http://www.antlr.org/license.html
006: *
007: */
008:
009: import java.util.NoSuchElementException;
010:
011: import persistence.antlr.collections.AST;
012: import persistence.antlr.collections.impl.BitSet;
013:
014: public class TreeParser {
015: /** The AST Null object; the parsing cursor is set to this when
016: * it is found to be null. This way, we can test the
017: * token type of a node without having to have tests for null
018: * everywhere.
019: */
020: public static ASTNULLType ASTNULL = new ASTNULLType();
021:
022: /** Where did this rule leave off parsing; avoids a return parameter */
023: protected AST _retTree;
024:
025: /** guessing nesting level; guessing==0 implies not guessing */
026: // protected int guessing = 0;
027: /** Nesting level of registered handlers */
028: // protected int exceptionLevel = 0;
029: protected TreeParserSharedInputState inputState;
030:
031: /** Table of token type to token names */
032: protected String[] tokenNames;
033:
034: /** AST return value for a rule is squirreled away here */
035: protected AST returnAST;
036:
037: /** AST support code; parser and treeparser delegate to this object */
038: protected ASTFactory astFactory = new ASTFactory();
039:
040: /** Used to keep track of indentdepth for traceIn/Out */
041: protected int traceDepth = 0;
042:
043: public TreeParser() {
044: inputState = new TreeParserSharedInputState();
045: }
046:
047: /** Get the AST return value squirreled away in the parser */
048: public AST getAST() {
049: return returnAST;
050: }
051:
052: public ASTFactory getASTFactory() {
053: return astFactory;
054: }
055:
056: public String getTokenName(int num) {
057: return tokenNames[num];
058: }
059:
060: public String[] getTokenNames() {
061: return tokenNames;
062: }
063:
064: protected void match(AST t, int ttype)
065: throws MismatchedTokenException {
066: //System.out.println("match("+ttype+"); cursor is "+t);
067: if (t == null || t == ASTNULL || t.getType() != ttype) {
068: throw new MismatchedTokenException(getTokenNames(), t,
069: ttype, false);
070: }
071: }
072:
073: /**Make sure current lookahead symbol matches the given set
074: * Throw an exception upon mismatch, which is catch by either the
075: * error handler or by the syntactic predicate.
076: */
077: public void match(AST t, BitSet b) throws MismatchedTokenException {
078: if (t == null || t == ASTNULL || !b.member(t.getType())) {
079: throw new MismatchedTokenException(getTokenNames(), t, b,
080: false);
081: }
082: }
083:
084: protected void matchNot(AST t, int ttype)
085: throws MismatchedTokenException {
086: //System.out.println("match("+ttype+"); cursor is "+t);
087: if (t == null || t == ASTNULL || t.getType() == ttype) {
088: throw new MismatchedTokenException(getTokenNames(), t,
089: ttype, true);
090: }
091: }
092:
093: /** @deprecated as of 2.7.2. This method calls System.exit() and writes
094: * directly to stderr, which is usually not appropriate when
095: * a parser is embedded into a larger application. Since the method is
096: * <code>static</code>, it cannot be overridden to avoid these problems.
097: * ANTLR no longer uses this method internally or in generated code.
098: */
099: public static void panic() {
100: System.err.println("TreeWalker: panic");
101: System.exit(1);
102: }
103:
104: /** Parser error-reporting function can be overridden in subclass */
105: public void reportError(RecognitionException ex) {
106: System.err.println(ex.toString());
107: }
108:
109: /** Parser error-reporting function can be overridden in subclass */
110: public void reportError(String s) {
111: System.err.println("error: " + s);
112: }
113:
114: /** Parser warning-reporting function can be overridden in subclass */
115: public void reportWarning(String s) {
116: System.err.println("warning: " + s);
117: }
118:
119: /** Specify an object with support code (shared by
120: * Parser and TreeParser. Normally, the programmer
121: * does not play with this, using setASTNodeType instead.
122: */
123: public void setASTFactory(ASTFactory f) {
124: astFactory = f;
125: }
126:
127: /** Specify the type of node to create during tree building.
128: * @deprecated since 2.7.2
129: */
130: public void setASTNodeType(String nodeType) {
131: setASTNodeClass(nodeType);
132: }
133:
134: /** Specify the type of node to create during tree building */
135: public void setASTNodeClass(String nodeType) {
136: astFactory.setASTNodeType(nodeType);
137: }
138:
139: public void traceIndent() {
140: for (int i = 0; i < traceDepth; i++)
141: System.out.print(" ");
142: }
143:
144: public void traceIn(String rname, AST t) {
145: traceDepth += 1;
146: traceIndent();
147: System.out.println("> " + rname + "("
148: + (t != null ? t.toString() : "null") + ")"
149: + ((inputState.guessing > 0) ? " [guessing]" : ""));
150: }
151:
152: public void traceOut(String rname, AST t) {
153: traceIndent();
154: System.out.println("< " + rname + "("
155: + (t != null ? t.toString() : "null") + ")"
156: + ((inputState.guessing > 0) ? " [guessing]" : ""));
157: traceDepth--;
158: }
159: }
|