📄 rcparser.java
字号:
// $ANTLR 2.7.4: "Resource file parser.g" -> "RCParser.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.main;
import java.util.ArrayList;
import net.softgems.resourceparser.main.*;
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 RCParser extends antlr.LLkParser implements RCParserTokenTypes
{
/** List of event listeners who want to get notified about an parser event. */
ArrayList listeners = new ArrayList();
boolean hadErrors;
boolean hadWarnings;
//------------------------------------------------------------------------------------------------
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 String getFilename()
{
// This method must be overidden because the parser class maintains an own file name variable
// and does not consider that the lexer could switch the files. So we ask the lexer for the
// actual file name.
RCParserSharedInputState state = (RCParserSharedInputState) getInputState();
RCLexer lexer = (RCLexer) state.getInput().getInput();
return lexer.getFilename();
}
//------------------------------------------------------------------------------------------------
public boolean hadErrors()
{
return hadErrors;
}
//------------------------------------------------------------------------------------------------
public boolean hadWarnings()
{
return hadWarnings;
}
//------------------------------------------------------------------------------------------------
protected RCParser(TokenBuffer tokenBuf, int k) {
super(tokenBuf,k);
tokenNames = _tokenNames;
buildTokenTypeASTClassMap();
astFactory = new ASTFactory(getTokenTypeToASTClassMap());
}
public RCParser(TokenBuffer tokenBuf) {
this(tokenBuf,3);
}
protected RCParser(TokenStream lexer, int k) {
super(lexer,k);
tokenNames = _tokenNames;
buildTokenTypeASTClassMap();
astFactory = new ASTFactory(getTokenTypeToASTClassMap());
}
public RCParser(TokenStream lexer) {
this(lexer,3);
}
public RCParser(ParserSharedInputState state) {
super(state,3);
tokenNames = _tokenNames;
buildTokenTypeASTClassMap();
astFactory = new ASTFactory(getTokenTypeToASTClassMap());
}
public final void resource_definition() throws RecognitionException, TokenStreamException {
returnAST = null;
ASTPair currentAST = new ASTPair();
AST resource_definition_AST = null;
try { // for error handling
{
_loop3:
do {
if ((_tokenSet_0.member(LA(1)))) {
resource_statement();
astFactory.addASTChild(currentAST, returnAST);
}
else {
break _loop3;
}
} while (true);
}
resource_definition_AST = (AST)currentAST.root;
}
catch (RecognitionException ex) {
reportError(ex);
consume();
consumeUntil(_tokenSet_1);
}
returnAST = resource_definition_AST;
}
public final void resource_statement() throws RecognitionException, TokenStreamException {
returnAST = null;
ASTPair currentAST = new ASTPair();
AST resource_statement_AST = null;
try { // for error handling
switch ( LA(1)) {
case NUMBER_SIGN:
{
match(NUMBER_SIGN);
pragma_directive();
astFactory.addASTChild(currentAST, returnAST);
resource_statement_AST = (AST)currentAST.root;
break;
}
case LITERAL_language:
{
language_entry();
astFactory.addASTChild(currentAST, returnAST);
resource_statement_AST = (AST)currentAST.root;
break;
}
case LITERAL_stringtable:
{
string_table();
astFactory.addASTChild(currentAST, returnAST);
resource_statement_AST = (AST)currentAST.root;
break;
}
case LONG_LITERAL:
case NUMERAL:
case IDENTIFIER:
case HEX_LITERAL:
case OCTAL_LITERAL:
{
{
resource_identifier();
astFactory.addASTChild(currentAST, returnAST);
named_entry();
astFactory.addASTChild(currentAST, returnAST);
resource_statement_AST = (AST)currentAST.root;
resource_statement_AST = (AST)astFactory.make( (new ASTArray(2)).add(astFactory.create(NAMED_RESOURCE,"named resource")).add(resource_statement_AST));
currentAST.root = resource_statement_AST;
currentAST.child = resource_statement_AST!=null &&resource_statement_AST.getFirstChild()!=null ?
resource_statement_AST.getFirstChild() : resource_statement_AST;
currentAST.advanceChildToEnd();
}
resource_statement_AST = (AST)currentAST.root;
break;
}
default:
{
throw new NoViableAltException(LT(1), getFilename());
}
}
}
catch (RecognitionException ex) {
reportError(ex);
consume();
consumeUntil(_tokenSet_2);
}
returnAST = resource_statement_AST;
}
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 tmp2_AST = null;
tmp2_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp2_AST);
match(NUMERAL);
integer_literal_AST = (AST)currentAST.root;
break;
}
case LONG_LITERAL:
{
AST tmp3_AST = null;
tmp3_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp3_AST);
match(LONG_LITERAL);
integer_literal_AST = (AST)currentAST.root;
break;
}
case HEX_LITERAL:
{
AST tmp4_AST = null;
tmp4_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp4_AST);
match(HEX_LITERAL);
integer_literal_AST = (AST)currentAST.root;
break;
}
case OCTAL_LITERAL:
{
AST tmp5_AST = null;
tmp5_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp5_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_3);
}
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 tmp6_AST = null;
tmp6_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp6_AST);
match(IDENTIFIER);
resource_identifier_AST = (AST)currentAST.root;
break;
}
case LONG_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_4);
}
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 LONG_LITERAL:
case NUMERAL:
case HEX_LITERAL:
case OCTAL_LITERAL:
{
integer_literal();
astFactory.addASTChild(currentAST, returnAST);
literal_AST = (AST)currentAST.root;
break;
}
case STRING_LITERAL:
case LITERAL_l:
{
resource_string();
astFactory.addASTChild(currentAST, returnAST);
literal_AST = (AST)currentAST.root;
break;
}
default:
{
throw new NoViableAltException(LT(1), getFilename());
}
}
}
catch (RecognitionException ex) {
reportError(ex);
consume();
consumeUntil(_tokenSet_5);
}
returnAST = literal_AST;
}
public final void resource_string() throws RecognitionException, TokenStreamException {
returnAST = null;
ASTPair currentAST = new ASTPair();
AST resource_string_AST = null;
try { // for error handling
{
switch ( LA(1)) {
case LITERAL_l:
{
AST tmp7_AST = null;
tmp7_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp7_AST);
match(LITERAL_l);
break;
}
case STRING_LITERAL:
{
break;
}
default:
{
throw new NoViableAltException(LT(1), getFilename());
}
}
}
AST tmp8_AST = null;
tmp8_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp8_AST);
match(STRING_LITERAL);
resource_string_AST = (AST)currentAST.root;
}
catch (RecognitionException ex) {
reportError(ex);
consume();
consumeUntil(_tokenSet_6);
}
returnAST = resource_string_AST;
}
public final void pragma_directive() throws RecognitionException, TokenStreamException {
returnAST = null;
ASTPair currentAST = new ASTPair();
AST pragma_directive_AST = null;
try { // for error handling
AST tmp9_AST = null;
tmp9_AST = astFactory.create(LT(1));
astFactory.makeASTRoot(currentAST, tmp9_AST);
match(LITERAL_pragma);
AST tmp10_AST = null;
tmp10_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp10_AST);
match(LITERAL_code_page);
match(LEFT_PARENTHESE);
codepage();
astFactory.addASTChild(currentAST, returnAST);
match(RIGHT_PARENTHESE);
pragma_directive_AST = (AST)currentAST.root;
}
catch (RecognitionException ex) {
reportError(ex);
consume();
consumeUntil(_tokenSet_2);
}
returnAST = pragma_directive_AST;
}
public final void codepage() throws RecognitionException, TokenStreamException {
returnAST = null;
ASTPair currentAST = new ASTPair();
AST codepage_AST = null;
try { // for error handling
switch ( LA(1)) {
case LITERAL_default:
{
AST tmp13_AST = null;
tmp13_AST = astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp13_AST);
match(LITERAL_default);
codepage_AST = (AST)currentAST.root;
break;
}
case LONG_LITERAL:
case NUMERAL:
case IDENTIFIER:
case HEX_LITERAL:
case OCTAL_LITERAL:
{
resource_identifier();
astFactory.addASTChild(currentAST, returnAST);
codepage_AST = (AST)currentAST.root;
break;
}
default:
{
throw new NoViableAltException(LT(1), getFilename());
}
}
}
catch (RecognitionException ex) {
reportError(ex);
consume();
consumeUntil(_tokenSet_7);
}
returnAST = codepage_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) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -