📄 expressionparser.java
字号:
// $ANTLR 2.7.4: "Expression parser.g" -> "ExpressionParser.java"$
/**
* Soft Gems Resource parser. Created by Mike Lischke.
*
* The source code in this file can freely be used for any purpose provided this notice remains
* unchanged in the file.
*
* Copyright 2004 by Mike Lischke, www.soft-gems.net, public@soft-gems.net. All rights reserved.
*/
package net.softgems.resourceparser.expressions;
import java.io.StringReader;
import java.util.ArrayList;
import antlr.collections.AST;
import net.softgems.resourceparser.main.IParseEventListener;
import antlr.TokenBuffer;
import antlr.TokenStreamException;
import antlr.TokenStreamIOException;
import antlr.ANTLRException;
import antlr.LLkParser;
import antlr.Token;
import antlr.TokenStream;
import antlr.RecognitionException;
import antlr.NoViableAltException;
import antlr.MismatchedTokenException;
import antlr.SemanticException;
import antlr.ParserSharedInputState;
import antlr.collections.impl.BitSet;
import antlr.collections.AST;
import java.util.Hashtable;
import antlr.ASTFactory;
import antlr.ASTPair;
import antlr.collections.impl.ASTArray;
public class ExpressionParser extends antlr.LLkParser implements ExpressionLexerTokenTypes
{
/** List of event listeners who want to get notified about an parser event. */
ArrayList listeners = new ArrayList();
boolean hadErrors;
boolean hadWarnings;
//------------------------------------------------------------------------------------------------
public static AST parse(String expression, String filename, int line, int column,
IParseEventListener listener) throws RecognitionException, TokenStreamException
{
StringReader input = new StringReader(expression);
ExpressionLexer lexer = new ExpressionLexer(input);
lexer.addLexerEventListener(listener);
lexer.setFilename(filename);
lexer.setLine(line);
lexer.setColumn(column);
ExpressionParser parser = new ExpressionParser(lexer);
parser.addParserEventListener(listener);
parser.setFilename(filename);
// Start parsing.
parser.expression();
return parser.getAST();
}
//------------------------------------------------------------------------------------------------
public void addParserEventListener(IParseEventListener listener)
{
listeners.add(listener);
}
//------------------------------------------------------------------------------------------------
public void removeParserEventListener(IParseEventListener listener)
{
listeners.remove(listener);
}
//------------------------------------------------------------------------------------------------
public void reportError(RecognitionException ex)
{
hadErrors = true;
doEvent(IParseEventListener.ERROR, ex.toString());
}
//------------------------------------------------------------------------------------------------
public void reportError(String s)
{
hadErrors = true;
if (getFilename() == null)
{
doEvent(IParseEventListener.ERROR, s);
}
else
{
doEvent(IParseEventListener.ERROR, getFilename() + ": " + s);
}
}
//------------------------------------------------------------------------------------------------
public void reportWarning(String s)
{
hadWarnings = true;
if (getFilename() == null)
{
doEvent(IParseEventListener.WARNING, s);
}
else
{
doEvent(IParseEventListener.WARNING, getFilename() + ": " + s);
}
}
//------------------------------------------------------------------------------------------------
private void doEvent(int event, String message)
{
for (int i = 0; i < listeners.size(); i++)
{
IParseEventListener listener = (IParseEventListener)listeners.get(i);
listener.handleEvent(event, message);
}
}
//------------------------------------------------------------------------------------------------
public boolean hadErrors()
{
return hadErrors;
}
//------------------------------------------------------------------------------------------------
public boolean hadWarnings()
{
return hadWarnings;
}
//------------------------------------------------------------------------------------------------
protected ExpressionParser(TokenBuffer tokenBuf, int k) {
super(tokenBuf,k);
tokenNames = _tokenNames;
buildTokenTypeASTClassMap();
astFactory = new ASTFactory(getTokenTypeToASTClassMap());
}
public ExpressionParser(TokenBuffer tokenBuf) {
this(tokenBuf,2);
}
protected ExpressionParser(TokenStream lexer, int k) {
super(lexer,k);
tokenNames = _tokenNames;
buildTokenTypeASTClassMap();
astFactory = new ASTFactory(getTokenTypeToASTClassMap());
}
public ExpressionParser(TokenStream lexer) {
this(lexer,2);
}
public ExpressionParser(ParserSharedInputState state) {
super(state,2);
tokenNames = _tokenNames;
buildTokenTypeASTClassMap();
astFactory = new ASTFactory(getTokenTypeToASTClassMap());
}
public final void integer_literal() throws RecognitionException, TokenStreamException {
returnAST = null;
ASTPair currentAST = new ASTPair();
AST integer_literal_AST = null;
try { // for error handling
switch ( LA(1)) {
case NUMERAL:
{
AST tmp1_AST = null;
tmp1_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp1_AST);
match(NUMERAL);
integer_literal_AST = (AST)currentAST.root;
break;
}
case BYTE_LITERAL:
{
AST tmp2_AST = null;
tmp2_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp2_AST);
match(BYTE_LITERAL);
integer_literal_AST = (AST)currentAST.root;
break;
}
case SHORT_LITERAL:
{
AST tmp3_AST = null;
tmp3_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp3_AST);
match(SHORT_LITERAL);
integer_literal_AST = (AST)currentAST.root;
break;
}
case INTEGER_LITERAL:
{
AST tmp4_AST = null;
tmp4_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp4_AST);
match(INTEGER_LITERAL);
integer_literal_AST = (AST)currentAST.root;
break;
}
case LONG_LITERAL:
{
AST tmp5_AST = null;
tmp5_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp5_AST);
match(LONG_LITERAL);
integer_literal_AST = (AST)currentAST.root;
break;
}
case BIGINT_LITERAL:
{
AST tmp6_AST = null;
tmp6_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp6_AST);
match(BIGINT_LITERAL);
integer_literal_AST = (AST)currentAST.root;
break;
}
case HEX_LITERAL:
{
AST tmp7_AST = null;
tmp7_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp7_AST);
match(HEX_LITERAL);
integer_literal_AST = (AST)currentAST.root;
break;
}
case OCTAL_LITERAL:
{
AST tmp8_AST = null;
tmp8_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp8_AST);
match(OCTAL_LITERAL);
integer_literal_AST = (AST)currentAST.root;
break;
}
default:
{
throw new NoViableAltException(LT(1), getFilename());
}
}
}
catch (RecognitionException ex) {
reportError(ex);
consume();
consumeUntil(_tokenSet_0);
}
returnAST = integer_literal_AST;
}
public final void resource_identifier() throws RecognitionException, TokenStreamException {
returnAST = null;
ASTPair currentAST = new ASTPair();
AST resource_identifier_AST = null;
try { // for error handling
switch ( LA(1)) {
case IDENTIFIER:
{
AST tmp9_AST = null;
tmp9_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp9_AST);
match(IDENTIFIER);
resource_identifier_AST = (AST)currentAST.root;
break;
}
case BIGINT_LITERAL:
case LONG_LITERAL:
case INTEGER_LITERAL:
case SHORT_LITERAL:
case BYTE_LITERAL:
case NUMERAL:
case HEX_LITERAL:
case OCTAL_LITERAL:
{
integer_literal();
astFactory.addASTChild(currentAST, returnAST);
resource_identifier_AST = (AST)currentAST.root;
break;
}
default:
{
throw new NoViableAltException(LT(1), getFilename());
}
}
}
catch (RecognitionException ex) {
reportError(ex);
consume();
consumeUntil(_tokenSet_1);
}
returnAST = resource_identifier_AST;
}
public final void literal() throws RecognitionException, TokenStreamException {
returnAST = null;
ASTPair currentAST = new ASTPair();
AST literal_AST = null;
try { // for error handling
switch ( LA(1)) {
case BIGINT_LITERAL:
case LONG_LITERAL:
case INTEGER_LITERAL:
case SHORT_LITERAL:
case BYTE_LITERAL:
case NUMERAL:
case HEX_LITERAL:
case OCTAL_LITERAL:
{
integer_literal();
astFactory.addASTChild(currentAST, returnAST);
literal_AST = (AST)currentAST.root;
break;
}
case CHARACTER_LITERAL:
{
AST tmp10_AST = null;
tmp10_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp10_AST);
match(CHARACTER_LITERAL);
literal_AST = (AST)currentAST.root;
break;
}
case STRING_LITERAL:
{
AST tmp11_AST = null;
tmp11_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp11_AST);
match(STRING_LITERAL);
literal_AST = (AST)currentAST.root;
break;
}
default:
{
throw new NoViableAltException(LT(1), getFilename());
}
}
}
catch (RecognitionException ex) {
reportError(ex);
consume();
consumeUntil(_tokenSet_0);
}
returnAST = literal_AST;
}
public final void expression() throws RecognitionException, TokenStreamException {
returnAST = null;
ASTPair currentAST = new ASTPair();
AST expression_AST = null;
try { // for error handling
assignmentExpression();
astFactory.addASTChild(currentAST, returnAST);
expression_AST = (AST)currentAST.root;
expression_AST = (AST)astFactory.make( (new ASTArray(2)).add(astFactory.create(EXPR,"expression")).add(expression_AST));
currentAST.root = expression_AST;
currentAST.child = expression_AST!=null &&expression_AST.getFirstChild()!=null ?
expression_AST.getFirstChild() : expression_AST;
currentAST.advanceChildToEnd();
expression_AST = (AST)currentAST.root;
}
catch (RecognitionException ex) {
reportError(ex);
consume();
consumeUntil(_tokenSet_1);
}
returnAST = expression_AST;
}
public final void assignmentExpression() throws RecognitionException, TokenStreamException {
returnAST = null;
ASTPair currentAST = new ASTPair();
AST assignmentExpression_AST = null;
try { // for error handling
conditionalExpression();
astFactory.addASTChild(currentAST, returnAST);
assignmentExpression_AST = (AST)currentAST.root;
}
catch (RecognitionException ex) {
reportError(ex);
consume();
consumeUntil(_tokenSet_2);
}
returnAST = assignmentExpression_AST;
}
public final void conditionalExpression() throws RecognitionException, TokenStreamException {
returnAST = null;
ASTPair currentAST = new ASTPair();
AST conditionalExpression_AST = null;
try { // for error handling
logicalOrExpression();
astFactory.addASTChild(currentAST, returnAST);
conditionalExpression_AST = (AST)currentAST.root;
}
catch (RecognitionException ex) {
reportError(ex);
consume();
consumeUntil(_tokenSet_2);
}
returnAST = conditionalExpression_AST;
}
public final void logicalOrExpression() throws RecognitionException, TokenStreamException {
returnAST = null;
ASTPair currentAST = new ASTPair();
AST logicalOrExpression_AST = null;
try { // for error handling
logicalAndExpression();
astFactory.addASTChild(currentAST, returnAST);
{
_loop113:
do {
if ((LA(1)==LOGICAL_OR)) {
AST tmp12_AST = null;
tmp12_AST = astFactory.create(LT(1));
astFactory.makeASTRoot(currentAST, tmp12_AST);
match(LOGICAL_OR);
logicalAndExpression();
astFactory.addASTChild(currentAST, returnAST);
}
else {
break _loop113;
}
} while (true);
}
logicalOrExpression_AST = (AST)currentAST.root;
}
catch (RecognitionException ex) {
reportError(ex);
consume();
consumeUntil(_tokenSet_2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -