📄 define.g
字号:
header {/* [The "BSD licence"] Copyright (c) 2005-2006 Terence Parr All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ package org.antlr.tool; import java.util.*; import org.antlr.misc.*;}class DefineGrammarItemsWalker extends TreeParser;options { importVocab = ANTLR; ASTLabelType = "GrammarAST"; codeGenBitsetTestThreshold=999;}{protected Grammar grammar;protected GrammarAST root;protected String currentRuleName;protected GrammarAST currentRewriteBlock;protected GrammarAST currentRewriteRule;protected int outerAltNum = 0;protected int blockLevel = 0; public void reportError(RecognitionException ex) { Token token = null; if ( ex instanceof MismatchedTokenException ) { token = ((MismatchedTokenException)ex).token; } else if ( ex instanceof NoViableAltException ) { token = ((NoViableAltException)ex).token; } ErrorManager.syntaxError( ErrorManager.MSG_SYNTAX_ERROR, grammar, token, "define: "+ex.toString(), ex); } protected void finish() { trimGrammar(); } /** Remove any lexer rules from a COMBINED; already passed to lexer */ protected void trimGrammar() { if ( grammar.type!=Grammar.COMBINED ) { return; } // form is (header ... ) ( grammar ID (scope ...) ... ( rule ... ) ( rule ... ) ... ) GrammarAST p = root; // find the grammar spec while ( !p.getText().equals("grammar") ) { p = (GrammarAST)p.getNextSibling(); } p = (GrammarAST)p.getFirstChild(); // jump down to first child of grammar // look for first RULE def GrammarAST prev = p; // points to the ID (grammar name) while ( p.getType()!=RULE ) { prev = p; p = (GrammarAST)p.getNextSibling(); } // prev points at last node before first rule subtree at this point while ( p!=null ) { String ruleName = p.getFirstChild().getText(); //System.out.println("rule "+ruleName+" prev="+prev.getText()); if ( Character.isUpperCase(ruleName.charAt(0)) ) { // remove lexer rule prev.setNextSibling(p.getNextSibling()); } else { prev = p; // non-lexer rule; move on } p = (GrammarAST)p.getNextSibling(); } //System.out.println("root after removal is: "+root.toStringList()); } protected void trackInlineAction(GrammarAST actionAST) { Rule r = grammar.getRule(currentRuleName); if ( r!=null ) { r.trackInlineAction(actionAST); } }}grammar[Grammar g]{grammar = g;root = #grammar;} : ( #( LEXER_GRAMMAR {grammar.type = Grammar.LEXER;} grammarSpec ) | #( PARSER_GRAMMAR {grammar.type = Grammar.PARSER;} grammarSpec ) | #( TREE_GRAMMAR {grammar.type = Grammar.TREE_PARSER;} grammarSpec ) | #( COMBINED_GRAMMAR {grammar.type = Grammar.COMBINED;} grammarSpec ) ) {finish();} ;attrScope : #( "scope" name:ID attrs:ACTION ) { AttributeScope scope = grammar.defineGlobalScope(name.getText(),#attrs.token); scope.isDynamicGlobalScope = true; scope.addAttributes(attrs.getText(), ";"); } ;grammarSpec{Map opts=null;Token optionsStartToken=null;} : id:ID (cmt:DOC_COMMENT)? //(#(OPTIONS .))? // already parsed these in assign.types.g ( {optionsStartToken=((GrammarAST)_t).getToken();} optionsSpec )? (tokensSpec)? (attrScope)* (actions)? rules ;actions : ( action )+ ;action{String scope=null;GrammarAST nameAST=null, actionAST=null;} : #(amp:AMPERSAND id1:ID ( id2:ID a1:ACTION {scope=#id1.getText(); nameAST=#id2; actionAST=#a1;} | a2:ACTION {scope=null; nameAST=#id1; actionAST=#a2;} ) ) { grammar.defineNamedAction(#amp,scope,nameAST,actionAST); } ;optionsSpec : OPTIONS ;tokensSpec : #( TOKENS ( tokenSpec )+ ) ;tokenSpec : t:TOKEN_REF | #( ASSIGN t2:TOKEN_REF ( s:STRING_LITERAL | c:CHAR_LITERAL ) ) ;rules : ( rule )+ ;rule{String mod=null;String name=null;Map opts=null;Rule r = null;} : #( RULE id:ID {opts = #RULE.options;} (mod=modifier)? #( ARG (args:ARG_ACTION)? ) #( RET (ret:ARG_ACTION)? ) (optionsSpec)? { name = #id.getText(); currentRuleName = name; if ( Character.isUpperCase(name.charAt(0)) && grammar.type==Grammar.COMBINED ) { // a merged grammar spec, track lexer rules and send to another grammar grammar.defineLexerRuleFoundInParser(#id.getToken(), #rule); } else { int numAlts = countAltsForRule(#rule); grammar.defineRule(#id.getToken(), mod, opts, #rule, #args, numAlts); r = grammar.getRule(name); if ( #args!=null ) { r.parameterScope = grammar.createParameterScope(name,#args.token); r.parameterScope.addAttributes(#args.getText(), ","); } if ( #ret!=null ) { r.returnScope = grammar.createReturnScope(name,#ret.token); r.returnScope.addAttributes(#ret.getText(), ","); } } } (ruleScopeSpec[r])? (ruleAction[r])* {this.blockLevel=0;} b:block (exceptionGroup)? EOR { // copy rule options into the block AST, which is where // the analysis will look for k option etc... #b.options = opts; } ) ;countAltsForRule returns [int n=0] : #( RULE id:ID (modifier)? ARG RET (OPTIONS)? ("scope")? (AMPERSAND)* #( BLOCK (OPTIONS)? (ALT (REWRITE)* {n++;})+ EOB ) (exceptionGroup)? EOR ) ;ruleAction[Rule r] : #(amp:AMPERSAND id:ID a:ACTION ) {if (r!=null) r.defineNamedAction(#amp,#id,#a);} ;modifier returns [String mod]{mod = #modifier.getText();} : "protected" | "public" | "private" | "fragment" ;ruleScopeSpec[Rule r] : #( "scope" ( attrs:ACTION { r.ruleScope = grammar.createRuleScope(r.name,#attrs.token); r.ruleScope.isDynamicRuleScope = true; r.ruleScope.addAttributes(#attrs.getText(), ";"); } )? ( uses:ID { if ( grammar.getGlobalScope(#uses.getText())==null ) { ErrorManager.grammarError(ErrorManager.MSG_UNKNOWN_DYNAMIC_SCOPE, grammar, #uses.token, #uses.getText()); } else { if ( r.useScopes==null ) {r.useScopes=new ArrayList();} r.useScopes.add(#uses.getText()); } } )* ) ;block{this.blockLevel++;if ( this.blockLevel==1 ) {this.outerAltNum=1;}} : #( BLOCK (optionsSpec)? (blockAction)* ( alternative rewrite {if ( this.blockLevel==1 ) {this.outerAltNum++;}} )+ EOB ) {this.blockLevel--;} ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -