📄 define.g
字号:
// TODO: this does nothing now! subrules cannot have init actions. :(blockAction : #(amp:AMPERSAND id:ID a:ACTION ) // {r.defineAction(#amp,#id,#a);} ;alternative{if ( grammar.type!=Grammar.LEXER && grammar.getOption("output")!=null && blockLevel==1 ) { GrammarAST aRewriteNode = #alternative.findFirstType(REWRITE); if ( aRewriteNode!=null|| (#alternative.getNextSibling()!=null && #alternative.getNextSibling().getType()==REWRITE) ) { Rule r = grammar.getRule(currentRuleName); r.trackAltsWithRewrites(#alternative,this.outerAltNum); }}} : #( ALT (element)+ EOA ) ;exceptionGroup : ( exceptionHandler )+ (finallyClause)? | finallyClause ;exceptionHandler : #("catch" ARG_ACTION ACTION) {trackInlineAction(#ACTION);} ;finallyClause : #("finally" ACTION) {trackInlineAction(#ACTION);} ;element : #(ROOT element) | #(BANG element) | atom | #(NOT element) | #(RANGE atom atom) | #(CHAR_RANGE atom atom) | #(ASSIGN id:ID el:element) { if ( #el.getType()==ANTLRParser.ROOT || #el.getType()==ANTLRParser.BANG ) { #el = (GrammarAST)#el.getFirstChild(); } if ( #el.getType()==RULE_REF) { grammar.defineRuleRefLabel(currentRuleName,#id.getToken(),#el); } else { grammar.defineTokenRefLabel(currentRuleName,#id.getToken(),#el); } } | #( PLUS_ASSIGN id2:ID a2:element { if ( #a2.getType()==ANTLRParser.ROOT || #a2.getType()==ANTLRParser.BANG ) { #a2 = (GrammarAST)#a2.getFirstChild(); } if ( #a2.getType()==RULE_REF ) { grammar.defineRuleListLabel(currentRuleName,#id2.getToken(),#a2); } else { grammar.defineTokenListLabel(currentRuleName,#id2.getToken(),#a2); } } ) | ebnf | tree | #( SYNPRED block ) | act:ACTION { #act.outerAltNum = this.outerAltNum; trackInlineAction(#act); } | SEMPRED { #SEMPRED.outerAltNum = this.outerAltNum; trackInlineAction(#SEMPRED); } | SYN_SEMPRED | BACKTRACK_SEMPRED | GATED_SEMPRED { #GATED_SEMPRED.outerAltNum = this.outerAltNum; trackInlineAction(#GATED_SEMPRED); } | EPSILON ;ebnf: (dotLoop)=> dotLoop // .* or .+ | block | #( OPTIONAL block ) | #( CLOSURE block ) | #( POSITIVE_CLOSURE block ) ;/** Track the .* and .+ idioms and make them nongreedy by default. */dotLoop{ GrammarAST block = (GrammarAST)#dotLoop.getFirstChild();} : ( #( CLOSURE dotBlock ) | #( POSITIVE_CLOSURE dotBlock ) ) { Map opts=new HashMap(); opts.put("greedy", "false"); if ( grammar.type!=Grammar.LEXER ) { // parser grammars assume k=1 for .* loops // otherwise they (analysis?) look til EOF! opts.put("k", Utils.integer(1)); } block.setOptions(grammar,opts); } ;dotBlock : #( BLOCK #( ALT WILDCARD EOA ) EOB ) ;tree: #(TREE_BEGIN element (element)*) ;atom : #( rr:RULE_REF (rarg:ARG_ACTION)? ) { grammar.altReferencesRule(currentRuleName, #rr, this.outerAltNum); if ( #rarg!=null ) { #rarg.outerAltNum = this.outerAltNum; trackInlineAction(#rarg); } } | #( t:TOKEN_REF (targ:ARG_ACTION )? ) { if ( #targ!=null ) { #targ.outerAltNum = this.outerAltNum; trackInlineAction(#targ); } if ( grammar.type==Grammar.LEXER ) { grammar.altReferencesRule(currentRuleName, #t, this.outerAltNum); } else { grammar.altReferencesTokenID(currentRuleName, #t, this.outerAltNum); } } | c:CHAR_LITERAL { if ( grammar.type!=Grammar.LEXER ) { Rule rule = grammar.getRule(currentRuleName); if ( rule!=null ) { rule.trackTokenReferenceInAlt(#c, outerAltNum); } } } | s:STRING_LITERAL { if ( grammar.type!=Grammar.LEXER ) { Rule rule = grammar.getRule(currentRuleName); if ( rule!=null ) { rule.trackTokenReferenceInAlt(#s, outerAltNum); } } } | WILDCARD ;ast_suffix : ROOT | BANG ;rewrite{currentRewriteRule = #rewrite; // has to execute during guessingif ( grammar.buildAST() ) { #rewrite.rewriteRefsDeep = new HashSet<GrammarAST>();}} : ( #( REWRITE (pred:SEMPRED)? rewrite_alternative ) { if ( #pred!=null ) { #pred.outerAltNum = this.outerAltNum; trackInlineAction(#pred); } } )* //{System.out.println("-> refs = "+#rewrite.rewriteRefs);} ;rewrite_block{GrammarAST enclosingBlock = currentRewriteBlock;if ( inputState.guessing==0 ) { // don't do if guessing currentRewriteBlock=#rewrite_block; // pts to BLOCK node currentRewriteBlock.rewriteRefsShallow = new HashSet<GrammarAST>(); currentRewriteBlock.rewriteRefsDeep = new HashSet<GrammarAST>();}} : #( BLOCK rewrite_alternative EOB ) //{System.out.println("atoms="+currentRewriteBlock.rewriteRefs);} { // copy the element refs in this block to the surrounding block if ( enclosingBlock!=null ) { enclosingBlock.rewriteRefsDeep .addAll(currentRewriteBlock.rewriteRefsShallow); } currentRewriteBlock = enclosingBlock; // restore old BLOCK ptr } ;rewrite_alternative : {grammar.buildAST()}? #( a:ALT ( ( rewrite_element )+ | EPSILON ) EOA ) | {grammar.buildTemplate()}? rewrite_template ;rewrite_element : rewrite_atom | rewrite_ebnf | rewrite_tree ;rewrite_ebnf : #( OPTIONAL rewrite_block ) | #( CLOSURE rewrite_block ) | #( POSITIVE_CLOSURE rewrite_block ) ;rewrite_tree : #( TREE_BEGIN rewrite_atom ( rewrite_element )* ) ;rewrite_atom{Rule r = grammar.getRule(currentRuleName);Set tokenRefsInAlt = r.getTokenRefsInAlt(outerAltNum);boolean imaginary = #rewrite_atom.getType()==TOKEN_REF && !tokenRefsInAlt.contains(#rewrite_atom.getText());if ( !imaginary && grammar.buildAST() && (#rewrite_atom.getType()==RULE_REF || #rewrite_atom.getType()==LABEL || #rewrite_atom.getType()==TOKEN_REF || #rewrite_atom.getType()==CHAR_LITERAL || #rewrite_atom.getType()==STRING_LITERAL) ){ // track per block and for entire rewrite rule if ( currentRewriteBlock!=null ) { currentRewriteBlock.rewriteRefsShallow.add(#rewrite_atom); currentRewriteBlock.rewriteRefsDeep.add(#rewrite_atom); } currentRewriteRule.rewriteRefsDeep.add(#rewrite_atom);}} : RULE_REF | ( #(TOKEN_REF (arg:ARG_ACTION)?) | CHAR_LITERAL | STRING_LITERAL ) { if ( #arg!=null ) { #arg.outerAltNum = this.outerAltNum; trackInlineAction(#arg); } } | LABEL | ACTION { #ACTION.outerAltNum = this.outerAltNum; trackInlineAction(#ACTION); } ;rewrite_template : #( ALT EPSILON EOA ) | #( TEMPLATE (id:ID|ind:ACTION) #( ARGLIST ( #( ARG arg:ID a:ACTION ) { #a.outerAltNum = this.outerAltNum; trackInlineAction(#a); } )* ) { if ( #ind!=null ) { #ind.outerAltNum = this.outerAltNum; trackInlineAction(#ind); } } ( DOUBLE_QUOTE_STRING_LITERAL | DOUBLE_ANGLE_STRING_LITERAL )? ) | act:ACTION { #act.outerAltNum = this.outerAltNum; trackInlineAction(#act); } ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -