📄 codegentreewalker.java
字号:
else { if ( _cnt22>=1 ) { break _loop22; } else {throw new NoViableAltException(_t);} } _cnt22++; } while (true); } } catch (RecognitionException ex) { reportError(ex); if (_t!=null) {_t = _t.getNextSibling();} } _retTree = _t; } public final StringTemplate rule(AST _t) throws RecognitionException { StringTemplate code=null; GrammarAST rule_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t; GrammarAST id = null; GrammarAST mod = null; String r; String initAction = null; StringTemplate b; // get the dfa for the BLOCK GrammarAST block=rule_AST_in.getFirstChildWithType(BLOCK); DFA dfa=block.getLookaheadDFA(); // init blockNestingLevel so it's block level RULE_BLOCK_NESTING_LEVEL // for alts of rule blockNestingLevel = RULE_BLOCK_NESTING_LEVEL-1; Rule ruleDescr = grammar.getRule(rule_AST_in.getFirstChild().getText()); // For syn preds, we don't want any AST code etc... in there. // Save old templates ptr and restore later. Base templates include Dbg. StringTemplateGroup saveGroup = templates; if ( ruleDescr.isSynPred ) { templates = generator.getBaseTemplates(); } try { // for error handling AST __t24 = _t; GrammarAST tmp14_AST_in = (GrammarAST)_t; match(_t,RULE); _t = _t.getFirstChild(); id = (GrammarAST)_t; match(_t,ID); _t = _t.getNextSibling(); r=id.getText(); currentRuleName = r; { if (_t==null) _t=ASTNULL; switch ( _t.getType()) { case FRAGMENT: case LITERAL_protected: case LITERAL_public: case LITERAL_private: { mod = _t==ASTNULL ? null : (GrammarAST)_t; modifier(_t); _t = _retTree; break; } case ARG: { break; } default: { throw new NoViableAltException(_t); } } } AST __t26 = _t; GrammarAST tmp15_AST_in = (GrammarAST)_t; match(_t,ARG); _t = _t.getFirstChild(); { if (_t==null) _t=ASTNULL; switch ( _t.getType()) { case ARG_ACTION: { GrammarAST tmp16_AST_in = (GrammarAST)_t; match(_t,ARG_ACTION); _t = _t.getNextSibling(); break; } case 3: { break; } default: { throw new NoViableAltException(_t); } } } _t = __t26; _t = _t.getNextSibling(); AST __t28 = _t; GrammarAST tmp17_AST_in = (GrammarAST)_t; match(_t,RET); _t = _t.getFirstChild(); { if (_t==null) _t=ASTNULL; switch ( _t.getType()) { case ARG_ACTION: { GrammarAST tmp18_AST_in = (GrammarAST)_t; match(_t,ARG_ACTION); _t = _t.getNextSibling(); break; } case 3: { break; } default: { throw new NoViableAltException(_t); } } } _t = __t28; _t = _t.getNextSibling(); { if (_t==null) _t=ASTNULL; switch ( _t.getType()) { case OPTIONS: { AST __t31 = _t; GrammarAST tmp19_AST_in = (GrammarAST)_t; match(_t,OPTIONS); _t = _t.getFirstChild(); GrammarAST tmp20_AST_in = (GrammarAST)_t; if ( _t==null ) throw new MismatchedTokenException(); _t = _t.getNextSibling(); _t = __t31; _t = _t.getNextSibling(); break; } case BLOCK: case SCOPE: case AMPERSAND: { break; } default: { throw new NoViableAltException(_t); } } } { if (_t==null) _t=ASTNULL; switch ( _t.getType()) { case SCOPE: { ruleScopeSpec(_t); _t = _retTree; break; } case BLOCK: case AMPERSAND: { break; } default: { throw new NoViableAltException(_t); } } } { _loop34: do { if (_t==null) _t=ASTNULL; if ((_t.getType()==AMPERSAND)) { GrammarAST tmp21_AST_in = (GrammarAST)_t; match(_t,AMPERSAND); _t = _t.getNextSibling(); } else { break _loop34; } } while (true); } b=block(_t,"ruleBlock", dfa); _t = _retTree; String description = grammar.grammarTreeToString(rule_AST_in.getFirstChildWithType(BLOCK), false); description = generator.target.getTargetStringLiteralFromString(description); b.setAttribute("description", description); /* System.out.println("rule "+r+" tokens="+ grammar.getRule(r).getAllTokenRefsInAltsWithRewrites()); System.out.println("rule "+r+" rules="+ grammar.getRule(r).getAllRuleRefsInAltsWithRewrites()); */ // do not generate lexer rules in combined grammar String stName = null; if ( ruleDescr.isSynPred ) { stName = "synpredRule"; } else if ( grammar.type==Grammar.LEXER ) { if ( r.equals(Grammar.ARTIFICIAL_TOKENS_RULENAME) ) { stName = "tokensRule"; } else { stName = "lexerRule"; } } else { if ( !(grammar.type==Grammar.COMBINED && Character.isUpperCase(r.charAt(0))) ) { stName = "rule"; } } code = templates.getInstanceOf(stName); if ( code.getName().equals("rule") ) { code.setAttribute("emptyRule", Boolean.valueOf(grammar.isEmptyRule(block))); } code.setAttribute("ruleDescriptor", ruleDescr); String memo = (String)rule_AST_in.getOption("memoize"); if ( memo==null ) { memo = (String)grammar.getOption("memoize"); } if ( memo!=null && memo.equals("true") && (stName.equals("rule")||stName.equals("lexerRule")) ) { code.setAttribute("memoize", Boolean.valueOf(memo!=null && memo.equals("true"))); } { if (_t==null) _t=ASTNULL; switch ( _t.getType()) { case LITERAL_exception: { exceptionGroup(_t,code); _t = _retTree; break; } case EOR: { break; } default: { throw new NoViableAltException(_t); } } } GrammarAST tmp22_AST_in = (GrammarAST)_t; match(_t,EOR); _t = _t.getNextSibling(); _t = __t24; _t = _t.getNextSibling(); if ( code!=null ) { if ( grammar.type==Grammar.LEXER ) { boolean naked = r.equals(Grammar.ARTIFICIAL_TOKENS_RULENAME) || (mod!=null&&mod.getText().equals(Grammar.FRAGMENT_RULE_MODIFIER)); code.setAttribute("nakedBlock", Boolean.valueOf(naked)); } else { description = grammar.grammarTreeToString(rule_AST_in,false); code.setAttribute("description", description); } Rule theRule = grammar.getRule(r); generator.translateActionAttributeReferencesForSingleScope( theRule, theRule.getActions() ); code.setAttribute("ruleName", r); code.setAttribute("block", b); if ( initAction!=null ) { code.setAttribute("initAction", initAction); } } templates = saveGroup; } catch (RecognitionException ex) { reportError(ex); if (_t!=null) {_t = _t.getNextSibling();} } _retTree = _t; return code; } public final void modifier(AST _t) throws RecognitionException { GrammarAST modifier_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t; try { // for error handling if (_t==null) _t=ASTNULL; switch ( _t.getType()) { case LITERAL_protected: { GrammarAST tmp23_AST_in = (GrammarAST)_t; match(_t,LITERAL_protected); _t = _t.getNextSibling(); break; } case LITERAL_public: { GrammarAST tmp24_AST_in = (GrammarAST)_t; match(_t,LITERAL_public); _t = _t.getNextSibling(); break; } case LITERAL_private: { GrammarAST tmp25_AST_in = (GrammarAST)_t; match(_t,LITERAL_private); _t = _t.getNextSibling(); break; } case FRAGMENT: { GrammarAST tmp26_AST_in = (GrammarAST)_t; match(_t,FRAGMENT); _t = _t.getNextSibling(); break; } default: { throw new NoViableAltException(_t); } } } catch (RecognitionException ex) { reportError(ex); if (_t!=null) {_t = _t.getNextSibling();} } _retTree = _t; } public final void ruleScopeSpec(AST _t) throws RecognitionException { GrammarAST ruleScopeSpec_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t; try { // for error handling AST __t38 = _t; GrammarAST tmp27_AST_in = (GrammarAST)_t; match(_t,SCOPE); _t = _t.getFirstChild(); { if (_t==null) _t=ASTNULL; switch ( _t.getType()) { case ACTION: { GrammarAST tmp28_AST_in = (GrammarAST)_t; match(_t,ACTION); _t = _t.getNextSibling(); break; } case 3: case ID: { break; } default: { throw new NoViableAltException(_t); } } } { _loop41: do { if (_t==null) _t=ASTNULL; if ((_t.getType()==ID)) { GrammarAST tmp29_AST_in = (GrammarAST)_t; match(_t,ID); _t = _t.getNextSibling(); } else { break _loop41; } } while (true); } _t = __t38; _t = _t.getNextSibling(); } catch (RecognitionException ex) { reportError(ex); if (_t!=null) {_t = _t.getNextSibling();} } _retTree = _t; } public final StringTemplate block(AST _t, String blockTemplateName, DFA dfa ) throws RecognitionException { StringTemplate code=null; GrammarAST block_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t; StringTemplate decision = null; if ( dfa!=null ) { code = templates.getInstanceOf(blockTemplateName); decision = generator.genLookaheadDecision(recognizerST,dfa); code.setAttribute("decision", decision); code.setAttribute("decisionNumber", dfa.getDecisionNumber()); code.setAttribute("maxK",dfa.getMaxLookaheadDepth()); code.setAttribute("maxAlt",dfa.getNumberOfAlts()); } else { code = templates.getInstanceOf(blockTemplateName+"SingleAlt"); } blockNestingLevel++; code.setAttribute("blockLevel", blockNestingLevel); code.setAttribute("enclosingBlockLevel", blockNestingLevel-1); StringTemplate alt = null; StringTemplate rew = null; GrammarAST r = null; int altNum = 1; if ( this.blockNestingLevel==RULE_BLOCK_NESTING_LEVEL ) {this.outerAltNum=1;} try { // for error handling AST __t43 = _t; GrammarAST tmp30_AST_in = (GrammarAST)_t; match(_t,BLOCK); _t = _t.getFirstChild(); { if (_t==null) _t=ASTNULL; switch ( _t.getType()) { case OPTIONS: { GrammarAST tmp31_AST_in = (GrammarAST)_t; match(_t,OPTIONS); _t = _t.getNextSibling(); break; } case ALT: { break; } default: { throw new NoViableAltException(_t); } } } { int _cnt46=0; _loop46: do { if (_t==null) _t=ASTNULL; if ((_t.getType()==ALT)) { alt=alternative(_t); _t = _retTree; r=(GrammarAST)_t; rew=rewrite(_t); _t = _retTree; if ( this.blockNestingLevel==RULE_BLOCK_NESTING_LEVEL ) { this.outerAltNum++; } // add the rewrite code as just another element in the alt :) if ( rew!=null ) { alt.setAttribute("elements.{el,line,pos}", rew, Utils.integer(r.getLine()), Utils.integer(r.getColumn())); } // add this alt to the list of alts for this block code.setAttribute("alts",alt); alt.setAttribute("altNum", Utils.integer(altNum)); alt.setAttribute("outerAlt", Boolean.valueOf(blockNestingLevel==RULE_BLOCK_NESTING_LEVEL)); altNum++; } else { if ( _cnt46>=1 ) { break _loop46; } else {throw new NoViableAltException(_t);} } _cnt46++; } while (true); } GrammarAST tmp32_AST_in = (GrammarAST)_t; match(_t,EOB); _t = _t.getNextSibling(); _t = __t43; _t = _t.getNextSibling(); blockNestingLevel--; } catch (RecognitionException ex) { reportError(ex); if (_t!=null) {_t = _t.getNextSibling();} } _retTree = _t; return code; } public final void exceptionGroup(AST _t, StringTemplate ruleST ) throws RecognitionException { GrammarAST exceptionGroup_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t; try { // for error handling { int _cnt49=0; _loop49: do { if (_t==null) _t=ASTNULL; if ((_t.getType()==LITERAL_exception)) { exceptionSpec(_t,ruleST); _t = _retTree; } else { if ( _cnt49>=1 ) { break _loop49; } else {throw new NoViableAltException(_t);} } _cnt49++; } while (true); } } catch (RecognitionException ex) { reportError(ex); if (_t!=null) {_t = _t.getNextSibling();} } _retTree = _t; } public final StringTemplate alternative(AST _t) throws RecognitionException { StringTemplate code=templates.getInstanceOf("alt"); GrammarAST alternative_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -