⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codegen.g

📁 ANTLR(ANother Tool for Language Recognition)它是这样的一种工具
💻 G
📖 第 1 页 / 共 3 页
字号:
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.codegen;    import org.antlr.tool.*;    import org.antlr.analysis.*;    import org.antlr.misc.*;	import java.util.*;	import org.antlr.stringtemplate.*;    import antlr.TokenWithIndex;    import antlr.CommonToken;}/** Walk a grammar and generate code by gradually building up *  a bigger and bigger StringTemplate. * *  Terence Parr *  University of San Francisco *  June 15, 2004 */class CodeGenTreeWalker extends TreeParser;options {    // warning! ANTLR cannot see another directory to get vocabs, so I had    // to copy the ANTLRTokenTypes.txt file into this dir from ../tools!    // Yuck!  If you modify ../tools/antlr.g, make sure to copy the vocab here.	importVocab = ANTLR;    codeGenBitsetTestThreshold=999;    ASTLabelType=GrammarAST;}{	protected static final int RULE_BLOCK_NESTING_LEVEL = 0;	protected static final int OUTER_REWRITE_NESTING_LEVEL = 0;    protected String currentRuleName = null;    protected int blockNestingLevel = 0;    protected int rewriteBlockNestingLevel = 0;	protected int outerAltNum = 0;    protected StringTemplate currentBlockST = null;    protected boolean currentAltHasASTRewrite = false;    protected int rewriteTreeNestingLevel = 0;    protected Set rewriteRuleRefs = null;    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,            "codegen: "+ex.toString(),            ex);    }    public void reportError(String s) {        System.out.println("codegen: error: " + s);    }    protected CodeGenerator generator;    protected Grammar grammar;    protected StringTemplateGroup templates;    /** The overall lexer/parser template; simulate dynamically scoped     *  attributes by making this an instance var of the walker.     */    protected StringTemplate recognizerST;    protected StringTemplate outputFileST;    protected StringTemplate headerFileST;    protected String outputOption = "";	protected StringTemplate getWildcardST(GrammarAST elementAST, GrammarAST ast_suffix, String label) {		String name = "wildcard";		if ( grammar.type==Grammar.LEXER ) {			name = "wildcardChar";		}		return getTokenElementST(name, name, elementAST, ast_suffix, label);	}	protected StringTemplate getRuleElementST(String name,										      String elementName,											  GrammarAST elementAST,    										  GrammarAST ast_suffix,    										  String label)	{		String suffix = getSTSuffix(ast_suffix,label);		name += suffix;		// if we're building trees and there is no label, gen a label		// unless we're in a synpred rule.		Rule r = grammar.getRule(currentRuleName);		if ( (grammar.buildAST()||suffix.length()>0) && label==null &&		     (r==null || !r.isSynPred) )		{			// we will need a label to do the AST or tracking, make one			label = generator.createUniqueLabel(elementName);			CommonToken labelTok = new CommonToken(ANTLRParser.ID, label);			grammar.defineRuleRefLabel(currentRuleName, labelTok, elementAST);		}		StringTemplate elementST = templates.getInstanceOf(name);		if ( label!=null ) {			elementST.setAttribute("label", label);		}		return elementST;	}	protected StringTemplate getTokenElementST(String name,											   String elementName,											   GrammarAST elementAST,											   GrammarAST ast_suffix,											   String label)	{		String suffix = getSTSuffix(ast_suffix,label);		name += suffix;		// if we're building trees and there is no label, gen a label		// unless we're in a synpred rule.		Rule r = grammar.getRule(currentRuleName);		if ( (grammar.buildAST()||suffix.length()>0) && label==null &&		     (r==null || !r.isSynPred) )		{			label = generator.createUniqueLabel(elementName);			CommonToken labelTok = new CommonToken(ANTLRParser.ID, label);			grammar.defineTokenRefLabel(currentRuleName, labelTok, elementAST);		}		StringTemplate elementST = templates.getInstanceOf(name);		if ( label!=null ) {			elementST.setAttribute("label", label);		}		return elementST;	}	/** Return a non-empty template name suffix if the token is to be	 *  tracked, added to a tree, or both.	 */	protected String getSTSuffix(GrammarAST ast_suffix, String label) {		if ( grammar.type==Grammar.LEXER ) {			return "";		}		// handle list label stuff; make element use "Track"		boolean hasListLabel=false;		if ( label!=null ) {			Rule r = grammar.getRule(currentRuleName);			String stName = null;			if ( r!=null ) {				Grammar.LabelElementPair pair = r.getLabel(label);				if ( pair!=null &&					 (pair.type==Grammar.TOKEN_LIST_LABEL||					  pair.type==Grammar.RULE_LIST_LABEL) )				{					hasListLabel=true;				}			}		}		String astPart = "";		String operatorPart = "";		String rewritePart = "";		String listLabelPart = "";		if ( grammar.buildAST() ) {			astPart = "AST";		}		if ( ast_suffix!=null ) {			if ( ast_suffix.getType()==ANTLRParser.ROOT ) {    			operatorPart = "Root";    		}    		else if ( ast_suffix.getType()==ANTLRParser.RULEROOT ) {    			operatorPart = "RuleRoot";    		}    		else if ( ast_suffix.getType()==ANTLRParser.BANG ) {    			operatorPart = "Bang";    		}   		}		if ( currentAltHasASTRewrite ) {			rewritePart = "Track";		}		if ( hasListLabel ) {			listLabelPart = "AndListLabel";		}		String STsuffix = operatorPart+rewritePart+listLabelPart;		//System.out.println("suffix = "+STsuffix);    	return STsuffix;	}    protected void init(Grammar g) {        this.grammar = g;        this.generator = grammar.getCodeGenerator();        this.templates = generator.getTemplates();    }}grammar[Grammar g,        StringTemplate recognizerST,        StringTemplate outputFileST,        StringTemplate headerFileST]{    init(g);    this.recognizerST = recognizerST;    this.outputFileST = outputFileST;    this.headerFileST = headerFileST;    String superClass = (String)g.getOption("superClass");    outputOption = (String)g.getOption("output");    recognizerST.setAttribute("superClass", superClass);    if ( g.type!=Grammar.LEXER ) {		recognizerST.setAttribute("ASTLabelType", g.getOption("ASTLabelType"));	}    if ( g.type==Grammar.TREE_PARSER && g.getOption("ASTLabelType")==null ) {		ErrorManager.grammarError(ErrorManager.MSG_MISSING_AST_TYPE_IN_TREE_GRAMMAR,								  g,								  null,								  g.name);	}    if ( g.type!=Grammar.TREE_PARSER ) {		recognizerST.setAttribute("labelType", g.getOption("TokenLabelType"));	}	recognizerST.setAttribute("numRules", grammar.getRules().size());	outputFileST.setAttribute("numRules", grammar.getRules().size());	headerFileST.setAttribute("numRules", grammar.getRules().size());}    :   ( #( LEXER_GRAMMAR grammarSpec )	    | #( PARSER_GRAMMAR grammarSpec )	    | #( TREE_GRAMMAR grammarSpec	       )	    | #( COMBINED_GRAMMAR grammarSpec )	    )    ;attrScope	:	#( "scope" ID ACTION )	;grammarSpec	:   name:ID		(cmt:DOC_COMMENT		 {		 outputFileST.setAttribute("docComment", #cmt.getText());		 headerFileST.setAttribute("docComment", #cmt.getText());		 }		)?		{		String suffix = Grammar.grammarTypeToFileNameSuffix[grammar.type];        String n = #name.getText()+suffix;		recognizerST.setAttribute("name", n);		outputFileST.setAttribute("name", n);		headerFileST.setAttribute("name", n);		recognizerST.setAttribute("scopes", grammar.getGlobalScopes());		headerFileST.setAttribute("scopes", grammar.getGlobalScopes());		}		( #(OPTIONS .) )?		( #(TOKENS .) )?        (attrScope)*        (AMPERSAND)*		rules[recognizerST]	;rules[StringTemplate recognizerST]{StringTemplate rST;}    :   (	(	{    			String ruleName = _t.getFirstChild().getText();    			Rule r = grammar.getRule(ruleName);    			}     		:     			// if synpred, only gen if used in a DFA    			{!r.isSynPred || grammar.synPredNamesUsedInDFA.contains(ruleName)}?    			rST=rule				{				if ( rST!=null ) {					recognizerST.setAttribute("rules", rST);					outputFileST.setAttribute("rules", rST);					headerFileST.setAttribute("rules", rST);				}				}    		|	RULE    		)   		)+    ;rule returns [StringTemplate code=null]{    String r;    String initAction = null;    StringTemplate b;	// get the dfa for the BLOCK    GrammarAST block=#rule.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.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();	}}    :   #( RULE id:ID {r=#id.getText(); currentRuleName = r;}		    (mod:modifier)?            #(ARG (ARG_ACTION)?)            #(RET (ARG_ACTION)?)			( #(OPTIONS .) )?			(ruleScopeSpec)?		    (AMPERSAND)*		    /*            #( INITACTION               (ia:ACTION {initAction=generator.translateAction(r,#ia);})?             )            */	     	b=block["ruleBlock", dfa]			{			String description =				grammar.grammarTreeToString(#rule.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.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")));            }			}	     	(exceptionGroup[code])?	     	EOR         )        {        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,false);				code.setAttribute("description", description);			}			Rule theRule = grammar.getRule(r);			generator.translateActionAttributeReferencesForSingleScope(				theRule,				theRule.getActions()			);			code.setAttribute("ruleName", r);			code.setAttribute("block", b);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -