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

📄 mismatchedtokenexception.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: MismatchedTokenException.java,v 1.1 2002/11/08 17:37:52 agno Exp $
 */

import antlr_oaa.collections.impl.BitSet;
import antlr_oaa.collections.AST;

public class MismatchedTokenException extends RecognitionException {
    // Token names array for formatting 
    String[] tokenNames;
    // The token that was encountered
    public Token token;
    // The offending AST node if tree walking
    public AST node;

    String tokenText=null; // taken from node or token object

	// Types of tokens
    public static final int TOKEN = 1;
    public static final int NOT_TOKEN = 2;
    public static final int RANGE = 3;
    public static final int NOT_RANGE = 4;
    public static final int SET = 5;
    public static final int NOT_SET = 6;
    // One of the above
    public int mismatchType;

	// For TOKEN/NOT_TOKEN and RANGE/NOT_RANGE
    public int expecting;

    // For RANGE/NOT_RANGE (expecting is lower bound of range)
    public int upper;

    // For SET/NOT_SET
    public BitSet set;

    /** Looking for AST wildcard, didn't find it */
    public MismatchedTokenException() {
	super("Mismatched Token: expecting any AST node");
    }
    // Expected range / not range
    public MismatchedTokenException(String[] tokenNames_, AST node, int lower, int upper_, boolean matchNot) {
	super("Mismatched Token");
	tokenNames = tokenNames_;
	this.node = node;
	if ( node==null ) {
	    tokenText = "<empty tree>";
	}
	else {
	    tokenText = node.toString();
	}
	expecting = lower;
	upper = upper_;
	this.fileName = "<AST>";
	mismatchType = matchNot ? NOT_RANGE : RANGE;
    }
    // Expected token / not token
    public MismatchedTokenException(String[] tokenNames_, AST node, int expecting_, boolean matchNot) {
	super("Mismatched Token");
	tokenNames = tokenNames_;
	this.node = node;
	if ( node==null ) {
	    tokenText = "<empty tree>";
	}
	else {
	    tokenText = node.toString();
	}
	expecting = expecting_;
	this.fileName = "<AST>";
	mismatchType = matchNot ? NOT_TOKEN : TOKEN;
    }
    // Expected BitSet / not BitSet
    public MismatchedTokenException(String[] tokenNames_, AST node, BitSet set_, boolean matchNot) {
	super("Mismatched Token");
	tokenNames = tokenNames_;
	this.node = node;
	if ( node==null ) {
	    tokenText = "<empty tree>";
	}
	else {
	    tokenText = node.toString();
	}
	set = set_;
	this.fileName = "<AST>";
	mismatchType = matchNot ? NOT_SET : SET;
    }
    // Expected range / not range
    public MismatchedTokenException(String[] tokenNames_, Token token_, int lower, int upper_, boolean matchNot, String fileName) {
	super("Mismatched Token");
	tokenNames = tokenNames_;
	token = token_;
	line = token.getLine();
	column = token.getColumn();
	tokenText = token.getText();
	expecting = lower;
	upper = upper_;
	this.fileName = fileName;
	mismatchType = matchNot ? NOT_RANGE : RANGE;
    }
    // Expected token / not token
    public MismatchedTokenException(String[] tokenNames_, Token token_, int expecting_, boolean matchNot, String fileName) {
	super("Mismatched Token");
	tokenNames = tokenNames_;
	token = token_;
	line = token.getLine();
	column = token.getColumn();
	tokenText = token.getText();
	expecting = expecting_;
	this.fileName = fileName;
	mismatchType = matchNot ? NOT_TOKEN : TOKEN;
    }
    // Expected BitSet / not BitSet
    public MismatchedTokenException(String[] tokenNames_, Token token_, BitSet set_, boolean matchNot, String fileName) {
	super("Mismatched Token");
	tokenNames = tokenNames_;
	token = token_;
	line = token.getLine();
	column = token.getColumn();
	tokenText = token.getText();
	set = set_;
	this.fileName = fileName;
	mismatchType = matchNot ? NOT_SET : SET;
    }

    /**
     * @deprecated As of ANTLR 2.7.0
     */
    public String getErrorMessage() {
	return getMessage();
    }

    /**
     * Returns the error message that happened on the line/col given.
     * Copied from toString().
     */
    public String getMessage() {
	StringBuffer sb = new StringBuffer();

	switch (mismatchType) {
	case TOKEN :
	    sb.append("expecting " + tokenName(expecting) + ", found '" + tokenText + "'");
	    break;
	case NOT_TOKEN :
	    sb.append("expecting anything but " + tokenName(expecting) + "; got it anyway");
	    break;
	case RANGE :
	    sb.append("expecting token in range: " + tokenName(expecting) + ".." + tokenName(upper) + ", found '" + tokenText + "'");
	    break;
	case NOT_RANGE :
	    sb.append("expecting token NOT in range: " + tokenName(expecting) + ".." + tokenName(upper) + ", found '" + tokenText + "'");
	    break;
	case SET :
	case NOT_SET :
	    sb.append("expecting " + (mismatchType == NOT_SET ? "NOT " : "") + "one of (");
	    int[] elems = set.toArray();
	    for (int i = 0; i < elems.length; i++) {
		sb.append(" ");
		sb.append(tokenName(elems[i]));
	    }
	    sb.append("), found '" + tokenText + "'");
	    break;
	default :
	    sb.append(super.getMessage());
	    break;
	}

	return sb.toString();
    }

    private String tokenName(int tokenType)
    {
	if (tokenType == Token.INVALID_TYPE) {
	    return "<Set of tokens>";
	}
	else if (tokenType < 0 || tokenType >= tokenNames.length) {
	    return "<" + String.valueOf(tokenType) + ">";
	} 
	else {
	    return tokenNames[tokenType];
	}
    }

    /**
     * @return a string representation of this exception.
     */
    public String toString() {
	if (token != null) { // AST or Token?
	    return FileLineFormatter.getFormatter().getFormatString(fileName,line)+getMessage();
	}
	return getMessage();
    }
}

⌨️ 快捷键说明

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