abstractsyntaxnode.java
来自「是有关解释器的.用JAVA编写.可以解释一般的JAVA程序.」· Java 代码 · 共 570 行 · 第 1/2 页
JAVA
570 行
*/
static boolean matchBooleanLiteral(Scanner s) throws ParserException,
ScannerException {
Token t = s.token();
if (!t.isBooleanLiteral())
parserError(t, "Boolean literal expected");
s.advance();
return ((BooleanLiteralToken) t).content();
}
/**
* If the next token is a <b>number literal </b> the scanner moves on to the
* next token and the matched token is returned.
*
* @param s
* reference to the scanner returning the tokens.
* @throws ScannerException
* If an input error occured
* @throws ParserException
* If the token doesn't match.
* @return the matched token
* @see NumberLiteralToken
*/
static int matchNumberLiteral(Scanner s) throws ParserException,
ScannerException {
Token t = s.token();
if (!t.isNumberLiteral())
parserError(t, "Number literal expected");
s.advance();
return ((NumberLiteralToken) t).content();
}
/**
* If the next token is a <b>simple token </b> true is returned. The scanner
* does not move to the next token.
*
* @param s
* reference to the scanner returning the tokens.
* @throws ScannerException
* If an input error occured
* @return true, if the token type matches, false if not
* @see SimpleToken
*/
static boolean isSimpleToken(Scanner s) throws ScannerException {
Token t = s.token();
return t.isSimple();
}
/**
* If the next token is a <b>bracket </b> true is returned. The scanner does
* not move to the next token.
*
* @param s
* reference to the scanner returning the tokens.
* @throws ScannerException
* If an input error occured
* @return true, if the token type matches, false if not
* @see BracketToken
*/
static boolean isBracketToken(Scanner s) throws ScannerException {
Token t = s.token();
return t.isBracket();
}
/**
* If the next token is an <b>operator </b> true is returned. The scanner
* does not move to the next token.
*
* @param s
* reference to the scanner returning the tokens.
* @throws ScannerException
* If an input error occured
* @return true, if the token type matches, false if not
* @see OperatorToken
*/
static boolean isOperatorToken(Scanner s) throws ScannerException {
Token t = s.token();
return t.isOperator();
}
/**
* If the next token is a <b>keyword </b> true is returned. The scanner does
* not move to the next token.
*
* @param s
* reference to the scanner returning the tokens.
* @throws ScannerException
* If an input error occured
* @return true, if the token type matches, false if not
* @see KeywordToken
*/
static boolean isKeywordToken(Scanner s) throws ScannerException {
Token t = s.token();
return t.isKeyword();
}
/**
* If the next token is an <b>identifier </b> true is returned. The scanner
* does not move to the next token.
*
* @param s
* reference to the scanner returning the tokens.
* @throws ScannerException
* If an input error occured
* @return true, if the token type matches, false if not
* @see IdentifierToken
*/
static boolean isIdentifierToken(Scanner s) throws ScannerException {
Token t = s.token();
return t.isIdentifier();
}
/**
* If the next token is a <b>boolean literal </b> true is returned. The
* scanner does not move to the next token.
*
* @param s
* reference to the scanner returning the tokens.
* @throws ScannerException
* If an input error occured
* @return true, if the token type matches, false if not
* @see BooleanLiteralToken
*/
static boolean isBooleanLiteral(Scanner s) throws ScannerException {
Token t = s.token();
return t.isBooleanLiteral();
}
/**
* If the next token is a <b>number literal </b> true is returned. The
* scanner does not move to the next token.
*
* @param s
* reference to the scanner returning the tokens.
* @throws ScannerException
* If an input error occured
* @return true, if the token type matches, false if not
* @see NumberLiteralToken
*/
static boolean isNumberLiteral(Scanner s) throws ScannerException {
Token t = s.token();
return t.isNumberLiteral();
}
/**
* Returns true if the next token is a <b>simple token </b> and matches the
* given char. The scanner does not move to the next token.
*
* @param s
* reference to the scanner returning the tokens.
* @throws ScannerException
* If an input error occured
* @return true, if the type and token match, false if not
* @see SimpleToken
*/
static boolean isSimpleToken(Scanner s, char c) throws ScannerException {
Token t = s.token();
if (t.isSimple())
return ((SimpleToken) t).content() == c;
return false;
}
/**
* Returns true if the next token is a <b>bracket </b> and matches the given
* char. The scanner does not move to the next token.
*
* @param s
* reference to the scanner returning the tokens.
* @throws ScannerException
* If an input error occured
* @return true, if the type and token match, false if not
* @see BracketToken
*/
static boolean isBracketToken(Scanner s, char c) throws ScannerException {
Token t = s.token();
if (t.isBracket())
return ((BracketToken) t).content() == c;
return false;
}
/**
* Returns true if the next token is an <b>operator </b> and matches the
* given char. The scanner does not move to the next token. According to
* <code>OperatorToken</code> the operators "&&", "||", "==" are
* represented by '&', '|', '='.
*
* @param s
* reference to the scanner returning the tokens.
* @throws ScannerException
* If an input error occured
* @return true, if the type and token match, false if not
* @see OperatorToken
*/
static boolean isOperatorToken(Scanner s, char c) throws ScannerException {
Token t = s.token();
if (t.isOperator())
return ((OperatorToken) t).content() == c;
return false;
}
/**
* Returns true if the next token is a <b>keyword </b> and matches the given
* char. The scanner does not move to the next token.
*
* @param s
* reference to the scanner returning the tokens.
* @throws ScannerException
* If an input error occured
* @return true, if the type and token match, false if not
* @see KeywordToken
*/
static boolean isKeywordToken(Scanner s, String w) throws ScannerException {
Token t = s.token();
if (t.isKeyword())
return ((KeywordToken) t).content().equals(w);
return false;
}
/**
* Returns true if the next token is an <b>identifier </b> and matches the
* given char. The scanner does not move to the next token.
*
* @param s
* reference to the scanner returning the tokens.
* @throws ScannerException
* If an input error occured
* @return true, if the type and token match, false if not
* @see IdentifierToken
*/
static boolean isIdentifierToken(Scanner s, String value)
throws ScannerException {
Token t = s.token();
if (t.isIdentifier())
return ((IdentifierToken) t).content().equals(value);
return false;
}
/**
* Throws a ParserException with the line of the given token, its position
* and the given error description.
*
* @param currentToken
* the token which created the parsing error
* @param errorDescription
* an error description
* @throws ParserException
* the generated exception
* @see ParserException
*/
static void parserError(Token currentToken, String errorDescription)
throws ParserException {
throw new ParserException(currentToken.line(), currentToken.pos(),
errorDescription);
}
/**
* Throws an InterpreterException created from the given error description.
*
* @param errorDescription
* an error description
* @throws InterpreterException
* the generated exception
* @see InterpreterException
*/
static void interpreterError(String errorDescription)
throws InterpreterException {
throw new InterpreterException(errorDescription);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?