⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 treeparser.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
字号:
package antlr_oaa;

/* ANTLR Translator Generator
 * Project led by Terence Parr at http://www.jGuru.com
 * Software rights: http://www.antlr.org/RIGHTS.html
 *
 * $Id: TreeParser.java,v 1.1 2002/11/08 17:37:32 agno Exp $
 */

import java.util.NoSuchElementException;
import antlr_oaa.collections.AST;
import antlr_oaa.collections.impl.BitSet;

public class TreeParser {
	/** The AST Null object; the parsing cursor is set to this when
	 *  it is found to be null.  This way, we can test the
	 *  token type of a node without having to have tests for null
	 *  everywhere.
	 */
	public static ASTNULLType ASTNULL = new ASTNULLType();

	/** Where did this rule leave off parsing; avoids a return parameter */
	protected AST _retTree;
	
	/** guessing nesting level; guessing==0 implies not guessing */
	// protected int guessing = 0;
	
	/** Nesting level of registered handlers */
	// protected int exceptionLevel = 0;

	protected TreeParserSharedInputState inputState;
	
	/** Table of token type to token names */
	protected String[] tokenNames;

	/** AST return value for a rule is squirreled away here */
	protected AST returnAST;

	/** AST support code; parser and treeparser delegate to this object */
	protected ASTFactory astFactory = new ASTFactory();
	
	/** Used to keep track of indentdepth for traceIn/Out */
	protected int traceDepth = 0;

	public TreeParser() {
		inputState = new TreeParserSharedInputState();
	}
	/** Get the AST return value squirreled away in the parser */
	public AST getAST() {
		return returnAST;
	}
	public ASTFactory getASTFactory() {
		return astFactory;
	}
	public String getTokenName(int num) {
		return tokenNames[num];
	}
	public String[] getTokenNames() {
		return tokenNames;
	}
	protected void match(AST t, int ttype) throws MismatchedTokenException {
		//System.out.println("match("+ttype+"); cursor is "+t);
		if ( t==null || t==ASTNULL || t.getType() != ttype ) {
			throw new MismatchedTokenException(getTokenNames(), t, ttype, false);
		}
	}
	/**Make sure current lookahead symbol matches the given set
	 * Throw an exception upon mismatch, which is catch by either the
	 * error handler or by the syntactic predicate.
	 */
	public void match(AST t, BitSet b) throws MismatchedTokenException {
		if ( t==null || t==ASTNULL || !b.member(t.getType()) ) {
			throw new MismatchedTokenException(getTokenNames(), t, b, false);
		}
	}
	protected void matchNot(AST t, int ttype) throws MismatchedTokenException {
		//System.out.println("match("+ttype+"); cursor is "+t);
		if ( t==null || t==ASTNULL || t.getType() == ttype ) {
			throw new MismatchedTokenException(getTokenNames(), t, ttype, true);
		}
	}
	public static void panic() {
		System.err.println("TreeWalker: panic");
		System.exit(1);
	}
	/** Parser error-reporting function can be overridden in subclass */
	public void reportError(RecognitionException ex) {
		System.err.println(ex.toString());
	}
	/** Parser error-reporting function can be overridden in subclass */
	public void reportError(String s) {
		System.err.println("error: " + s);
	}
	/** Parser warning-reporting function can be overridden in subclass */
	public void reportWarning(String s) {
		System.err.println("warning: " + s);
	}
	/** Specify an object with support code (shared by
	 *  Parser and TreeParser.  Normally, the programmer
	 *  does not play with this, using setASTNodeType instead.
	 */
	public void setASTFactory(ASTFactory f) {
		astFactory = f;
	}
    
    /** Specify the type of node to create during tree building */
    public void setASTNodeType(String nodeType) {
	setASTNodeClass(nodeType);
    }

    /** Specify the type of node to create during tree building */
    public void setASTNodeClass(String nodeType) {
	astFactory.setASTNodeType(nodeType);
    }

	public void traceIndent() {
		for( int i = 0; i < traceDepth; i++ )
			System.out.print(" ");
	}
	public void traceIn(String rname, AST t) {
		traceDepth += 1;
		traceIndent();
		System.out.println("> "+rname+
			"("+(t!=null?t.toString():"null")+")"+
			((inputState.guessing>0)?" [guessing]":""));
	}
	public void traceOut(String rname, AST t) {
		traceIndent();
		System.out.println("< "+rname+
			"("+(t!=null?t.toString():"null")+")"+
			((inputState.guessing>0)?" [guessing]":""));
		traceDepth--;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -