antlrparser.java
来自「antlr最新版本V3源代码」· Java 代码 · 共 2,609 行 · 第 1/5 页
JAVA
2,609 行
ASTPair currentAST = new ASTPair(); GrammarAST ruleAction_AST = null; try { // for error handling GrammarAST tmp34_AST = null; tmp34_AST = (GrammarAST)astFactory.create(LT(1)); astFactory.makeASTRoot(currentAST, tmp34_AST); match(AMPERSAND); id(); astFactory.addASTChild(currentAST, returnAST); GrammarAST tmp35_AST = null; tmp35_AST = (GrammarAST)astFactory.create(LT(1)); astFactory.addASTChild(currentAST, tmp35_AST); match(ACTION); ruleAction_AST = (GrammarAST)currentAST.root; } catch (RecognitionException ex) { reportError(ex); recover(ex,_tokenSet_14); } returnAST = ruleAction_AST; } public final void idList() throws RecognitionException, TokenStreamException { returnAST = null; ASTPair currentAST = new ASTPair(); GrammarAST idList_AST = null; try { // for error handling { int _cnt103=0; _loop103: do { if ((LA(1)==TOKEN_REF||LA(1)==RULE_REF)) { id(); astFactory.addASTChild(currentAST, returnAST); } else { if ( _cnt103>=1 ) { break _loop103; } else {throw new NoViableAltException(LT(1), getFilename());} } _cnt103++; } while (true); } idList_AST = (GrammarAST)currentAST.root; } catch (RecognitionException ex) { reportError(ex); recover(ex,_tokenSet_9); } returnAST = idList_AST; } /** Build #(BLOCK ( #(ALT ...) EOB )+ ) */ public final void block() throws RecognitionException, TokenStreamException { returnAST = null; ASTPair currentAST = new ASTPair(); GrammarAST block_AST = null; Token lp = null; GrammarAST lp_AST = null; GrammarAST a1_AST = null; GrammarAST a2_AST = null; Token rp = null; GrammarAST rp_AST = null; GrammarAST save = currentBlockAST; Map opts=null; try { // for error handling lp = LT(1); lp_AST = (GrammarAST)astFactory.create(lp); astFactory.makeASTRoot(currentAST, lp_AST); match(LPAREN); lp_AST.setType(BLOCK); lp_AST.setText("BLOCK"); { if ((LA(1)==OPTIONS||LA(1)==AMPERSAND||LA(1)==COLON)) { { switch ( LA(1)) { case OPTIONS: { opts=optionsSpec(); astFactory.addASTChild(currentAST, returnAST); block_AST = (GrammarAST)currentAST.root; block_AST.setOptions(grammar,opts); break; } case AMPERSAND: case COLON: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } { switch ( LA(1)) { case AMPERSAND: { ruleActions(); astFactory.addASTChild(currentAST, returnAST); break; } case COLON: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } match(COLON); } else if ((LA(1)==ACTION) && (LA(2)==COLON)) { GrammarAST tmp37_AST = null; tmp37_AST = (GrammarAST)astFactory.create(LT(1)); astFactory.addASTChild(currentAST, tmp37_AST); match(ACTION); match(COLON); } else if ((_tokenSet_15.member(LA(1))) && (_tokenSet_16.member(LA(2)))) { } else { throw new NoViableAltException(LT(1), getFilename()); } } currentBlockAST = lp_AST; alternative(); a1_AST = (GrammarAST)returnAST; astFactory.addASTChild(currentAST, returnAST); rewrite(); astFactory.addASTChild(currentAST, returnAST); if (LA(1)==OR||(LA(2)==QUESTION||LA(2)==PLUS||LA(2)==STAR)) prefixWithSynPred(a1_AST); { _loop59: do { if ((LA(1)==OR)) { match(OR); alternative(); a2_AST = (GrammarAST)returnAST; astFactory.addASTChild(currentAST, returnAST); rewrite(); astFactory.addASTChild(currentAST, returnAST); if (LA(1)==OR||(LA(2)==QUESTION||LA(2)==PLUS||LA(2)==STAR)) prefixWithSynPred(a2_AST); } else { break _loop59; } } while (true); } rp = LT(1); rp_AST = (GrammarAST)astFactory.create(rp); match(RPAREN); block_AST = (GrammarAST)currentAST.root; currentBlockAST = save; GrammarAST eob = (GrammarAST)astFactory.create(EOB,"<end-of-block>"); eob.setLine(rp.getLine()); eob.setColumn(rp.getColumn()); block_AST.addChild(eob); block_AST = (GrammarAST)currentAST.root; } catch (RecognitionException ex) { reportError(ex); recover(ex,_tokenSet_17); } returnAST = block_AST; } public final void alternative() throws RecognitionException, TokenStreamException { returnAST = null; ASTPair currentAST = new ASTPair(); GrammarAST alternative_AST = null; GrammarAST el_AST = null; GrammarAST eoa = (GrammarAST)astFactory.create(EOA,"<end-of-alt>"); GrammarAST altRoot = (GrammarAST)astFactory.create(ALT,"ALT"); altRoot.setLine(LT(1).getLine()); altRoot.setColumn(LT(1).getColumn()); try { // for error handling switch ( LA(1)) { case ACTION: case STRING_LITERAL: case CHAR_LITERAL: case TOKEN_REF: case LPAREN: case SEMPRED: case RULE_REF: case NOT: case TREE_BEGIN: case WILDCARD: { { int _cnt65=0; _loop65: do { if ((_tokenSet_18.member(LA(1)))) { element(); el_AST = (GrammarAST)returnAST; astFactory.addASTChild(currentAST, returnAST); } else { if ( _cnt65>=1 ) { break _loop65; } else {throw new NoViableAltException(LT(1), getFilename());} } _cnt65++; } while (true); } alternative_AST = (GrammarAST)currentAST.root; if ( alternative_AST==null ) { alternative_AST = (GrammarAST)astFactory.make( (new ASTArray(3)).add(altRoot).add((GrammarAST)astFactory.create(EPSILON,"epsilon")).add(eoa)); } else { // we have a real list of stuff alternative_AST = (GrammarAST)astFactory.make( (new ASTArray(3)).add(altRoot).add(alternative_AST).add(eoa)); } currentAST.root = alternative_AST; currentAST.child = alternative_AST!=null &&alternative_AST.getFirstChild()!=null ? alternative_AST.getFirstChild() : alternative_AST; currentAST.advanceChildToEnd(); alternative_AST = (GrammarAST)currentAST.root; break; } case SEMI: case OR: case RPAREN: case REWRITE: { alternative_AST = (GrammarAST)currentAST.root; GrammarAST eps = (GrammarAST)astFactory.create(EPSILON,"epsilon"); eps.setLine(LT(0).getLine()); // get line/col of '|' or ':' (prev token) eps.setColumn(LT(0).getColumn()); alternative_AST = (GrammarAST)astFactory.make( (new ASTArray(3)).add(altRoot).add(eps).add(eoa)); currentAST.root = alternative_AST; currentAST.child = alternative_AST!=null &&alternative_AST.getFirstChild()!=null ? alternative_AST.getFirstChild() : alternative_AST; currentAST.advanceChildToEnd(); alternative_AST = (GrammarAST)currentAST.root; break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } catch (RecognitionException ex) { reportError(ex); recover(ex,_tokenSet_19); } returnAST = alternative_AST; } public final void rewrite() throws RecognitionException, TokenStreamException { returnAST = null; ASTPair currentAST = new ASTPair(); GrammarAST rewrite_AST = null; Token rew = null; GrammarAST rew_AST = null; Token pred = null; GrammarAST pred_AST = null; GrammarAST alt_AST = null; Token rew2 = null; GrammarAST rew2_AST = null; GrammarAST alt2_AST = null; GrammarAST root = new GrammarAST(); try { // for error handling switch ( LA(1)) { case REWRITE: { { _loop108: do { if ((LA(1)==REWRITE) && (LA(2)==SEMPRED)) { rew = LT(1); rew_AST = (GrammarAST)astFactory.create(rew); match(REWRITE); pred = LT(1); pred_AST = (GrammarAST)astFactory.create(pred); match(SEMPRED); rewrite_alternative(); alt_AST = (GrammarAST)returnAST; root.addChild( (GrammarAST)astFactory.make( (new ASTArray(3)).add(rew_AST).add(pred_AST).add(alt_AST)) ); pred_AST.setEnclosingRule(currentRuleName); rew_AST.setEnclosingRule(currentRuleName); } else { break _loop108; } } while (true); } rew2 = LT(1); rew2_AST = (GrammarAST)astFactory.create(rew2); match(REWRITE); rewrite_alternative(); alt2_AST = (GrammarAST)returnAST; rewrite_AST = (GrammarAST)currentAST.root; root.addChild( (GrammarAST)astFactory.make( (new ASTArray(2)).add(rew2_AST).add(alt2_AST)) ); rewrite_AST = (GrammarAST)root.getFirstChild(); currentAST.root = rewrite_AST; currentAST.child = rewrite_AST!=null &&rewrite_AST.getFirstChild()!=null ? rewrite_AST.getFirstChild() : rewrite_AST; currentAST.advanceChildToEnd(); break; } case SEMI: case OR: case RPAREN: { rewrite_AST = (GrammarAST)currentAST.root; break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } catch (RecognitionException ex) { reportError(ex); recover(ex,_tokenSet_20); } returnAST = rewrite_AST; } public final void element() throws RecognitionException, TokenStreamException { returnAST = null; ASTPair currentAST = new ASTPair(); GrammarAST element_AST = null; try { // for error handling elementNoOptionSpec(); astFactory.addASTChild(currentAST, returnAST); element_AST = (GrammarAST)currentAST.root; } catch (RecognitionException ex) { reportError(ex); recover(ex,_tokenSet_21); } returnAST = element_AST; } public final void exceptionHandler() throws RecognitionException, TokenStreamException { returnAST = null; ASTPair currentAST = new ASTPair(); GrammarAST exceptionHandler_AST = null; try { // for error handling GrammarAST tmp40_AST = null; tmp40_AST = (GrammarAST)astFactory.create(LT(1)); astFactory.makeASTRoot(currentAST, tmp40_AST); match(LITERAL_catch); GrammarAST tmp41_AST = null; tmp41_AST = (GrammarAST)astFactory.create(LT(1)); astFactory.addASTChild(currentAST, tmp41_AST); match(ARG_ACTION); GrammarAST tmp42_AST = null; tmp42_AST = (GrammarAST)astFactory.create(LT(1)); astFactory.addASTChild(currentAST, tmp42_AST); match(ACTION); exceptionHandler_AST = (GrammarAST)currentAST.root; } catch (RecognitionException ex) { reportError(ex); recover(ex,_tokenSet_22); } returnAST = exceptionHandler_AST; } public final void finallyClause() throws RecognitionException, TokenStreamException { returnAST = null; ASTPair currentAST = new ASTPair(); GrammarAST finallyClause_AST = null; try { // for error handling GrammarAST tmp43_AST = null; tmp43_AST = (GrammarAST)astFactory.create(LT(1)); astFactory.makeASTRoot(currentAST, tmp43_AST); match(LITERAL_finally); GrammarAST tmp44_AST = null; tmp44_AST = (GrammarAST)astFactory.create(LT(1)); astFactory.addASTChild(currentAST, tmp44_AST); match(ACTION); finallyClause_AST = (GrammarAST)currentAST.root; } catch (RecognitionException ex) { reportError(ex); recover(ex,_tokenSet_11); } returnAST = finallyClause_AST; } public final void elementNoOptionSpec() throws RecognitionException, TokenStreamException { returnAST = null; ASTPair currentAST = new ASTPair(); GrammarAST elementNoOptionSpec_AST = null; Token p = null; GrammarAST p_AST = null; GrammarAST t3_AST = null; IntSet elements=null; GrammarAST sub, sub2; try { // for error handling switch ( LA(1)) { case LPAREN: { ebnf(); astFactory.addASTChild(currentAST, returnAST); elementNoOptionSpec_AST = (GrammarAST)currentAST.root; break; } case ACTION: { GrammarAST tmp45_AST = null; tmp45_AST = (GrammarAST)astFactory.create(LT(1)); astFactory.addASTChild(currentAST, tmp45_AST); match(ACTION); elementNoOptionSpec_AST = (GrammarAST)currentAST.root; break; } case SEMPRED: { p = LT(1); p_AST = (GrammarAST)astFactory.create(p); astFactory.addASTChild(currentAST, p_AST); match(SEMPRED); { switch ( LA(1)) { case IMPLIES: { match(IMPLIES); p_AST.setType(GATED_SEMPRED); break; } case ACTION: case SEMI: case STRING_LITERAL: case CHAR_LITERAL: case TOKEN_REF: case LPAREN: case OR: case RPAREN: case SEMPRED: case RULE_REF: case NOT: case TREE_BEGIN: case WILDCARD: case REWRITE: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } p_AST.setEnclosingRule(currentRuleName); grammar.blocksWithSemPreds.add(currentBlockAST); elementNoOptionSpec_AST = (GrammarAST)currentAST.root; break; } case TREE_BEGIN: { tree(); t3_AST = (GrammarAST)returnAST; astFactory.addASTChild(currentAST, returnAST); elementNoOptionSpec_AST = (GrammarAST)currentAST.root; break; } default: if ((LA(1)==TOKEN_REF||LA(1)==RULE_REF) && (LA(2)==ASSIGN||LA(2)==PLUS_ASSIGN)) { id(); astFactory.addASTChild(currentAST, returnAST); { switch ( LA(1)) { case ASSIGN: { GrammarAST tmp47_AST = null; tmp47_AST = (GrammarAST)astFactory.create(LT(1)); astFactory.makeASTRoot(currentAST, tmp47_AST); match(ASSIGN); break; } case PLUS_ASSIGN: { GrammarAST tmp48_AST = null; tmp48_AST = (GrammarAST)astFactory.create(LT(1));
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?