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

📄 codegentreewalker.java

📁 ANTLR(ANother Tool for Language Recognition)它是这样的一种工具
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
// $ANTLR 2.7.7 (2006-01-29): "codegen.g" -> "CodeGenTreeWalker.java"$/* [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;import antlr.TreeParser;import antlr.Token;import antlr.collections.AST;import antlr.RecognitionException;import antlr.ANTLRException;import antlr.NoViableAltException;import antlr.MismatchedTokenException;import antlr.SemanticException;import antlr.collections.impl.BitSet;import antlr.ASTPair;import antlr.collections.impl.ASTArray;/** Walk a grammar and generate code by gradually building up *  a bigger and bigger StringTemplate. * *  Terence Parr *  University of San Francisco *  June 15, 2004 */public class CodeGenTreeWalker extends antlr.TreeParser       implements CodeGenTreeWalkerTokenTypes {	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();    }public CodeGenTreeWalker() {	tokenNames = _tokenNames;}	public final void grammar(AST _t,		Grammar g,        StringTemplate recognizerST,        StringTemplate outputFileST,        StringTemplate headerFileST	) throws RecognitionException {				GrammarAST grammar_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;				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());						try {      // for error handling			{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case LEXER_GRAMMAR:			{				AST __t3 = _t;				GrammarAST tmp1_AST_in = (GrammarAST)_t;				match(_t,LEXER_GRAMMAR);				_t = _t.getFirstChild();				grammarSpec(_t);				_t = _retTree;				_t = __t3;				_t = _t.getNextSibling();				break;			}			case PARSER_GRAMMAR:			{				AST __t4 = _t;				GrammarAST tmp2_AST_in = (GrammarAST)_t;				match(_t,PARSER_GRAMMAR);				_t = _t.getFirstChild();				grammarSpec(_t);				_t = _retTree;				_t = __t4;				_t = _t.getNextSibling();				break;			}			case TREE_GRAMMAR:			{				AST __t5 = _t;				GrammarAST tmp3_AST_in = (GrammarAST)_t;				match(_t,TREE_GRAMMAR);				_t = _t.getFirstChild();				grammarSpec(_t);				_t = _retTree;				_t = __t5;				_t = _t.getNextSibling();				break;			}			case COMBINED_GRAMMAR:			{				AST __t6 = _t;				GrammarAST tmp4_AST_in = (GrammarAST)_t;				match(_t,COMBINED_GRAMMAR);				_t = _t.getFirstChild();				grammarSpec(_t);				_t = _retTree;				_t = __t6;				_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 grammarSpec(AST _t) throws RecognitionException {				GrammarAST grammarSpec_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;		GrammarAST name = null;		GrammarAST cmt = null;				try {      // for error handling			name = (GrammarAST)_t;			match(_t,ID);			_t = _t.getNextSibling();			{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case DOC_COMMENT:			{				cmt = (GrammarAST)_t;				match(_t,DOC_COMMENT);				_t = _t.getNextSibling();										 outputFileST.setAttribute("docComment", cmt.getText());						 headerFileST.setAttribute("docComment", cmt.getText());										break;			}			case OPTIONS:			case TOKENS:			case RULE:			case SCOPE:			case AMPERSAND:			{				break;			}			default:			{				throw new NoViableAltException(_t);			}			}			}								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());								{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case OPTIONS:			{				AST __t12 = _t;				GrammarAST tmp5_AST_in = (GrammarAST)_t;				match(_t,OPTIONS);				_t = _t.getFirstChild();				GrammarAST tmp6_AST_in = (GrammarAST)_t;				if ( _t==null ) throw new MismatchedTokenException();				_t = _t.getNextSibling();				_t = __t12;				_t = _t.getNextSibling();				break;			}			case TOKENS:			case RULE:			case SCOPE:			case AMPERSAND:			{				break;			}			default:			{				throw new NoViableAltException(_t);			}			}			}			{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case TOKENS:			{				AST __t14 = _t;				GrammarAST tmp7_AST_in = (GrammarAST)_t;				match(_t,TOKENS);				_t = _t.getFirstChild();				GrammarAST tmp8_AST_in = (GrammarAST)_t;				if ( _t==null ) throw new MismatchedTokenException();				_t = _t.getNextSibling();				_t = __t14;				_t = _t.getNextSibling();				break;			}			case RULE:			case SCOPE:			case AMPERSAND:			{				break;			}			default:			{				throw new NoViableAltException(_t);			}			}			}			{			_loop16:			do {				if (_t==null) _t=ASTNULL;				if ((_t.getType()==SCOPE)) {					attrScope(_t);					_t = _retTree;				}				else {					break _loop16;				}							} while (true);			}			{			_loop18:			do {				if (_t==null) _t=ASTNULL;				if ((_t.getType()==AMPERSAND)) {					GrammarAST tmp9_AST_in = (GrammarAST)_t;					match(_t,AMPERSAND);					_t = _t.getNextSibling();				}				else {					break _loop18;				}							} while (true);			}			rules(_t,recognizerST);			_t = _retTree;		}		catch (RecognitionException ex) {			reportError(ex);			if (_t!=null) {_t = _t.getNextSibling();}		}		_retTree = _t;	}		public final void attrScope(AST _t) throws RecognitionException {				GrammarAST attrScope_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;				try {      // for error handling			AST __t8 = _t;			GrammarAST tmp10_AST_in = (GrammarAST)_t;			match(_t,SCOPE);			_t = _t.getFirstChild();			GrammarAST tmp11_AST_in = (GrammarAST)_t;			match(_t,ID);			_t = _t.getNextSibling();			GrammarAST tmp12_AST_in = (GrammarAST)_t;			match(_t,ACTION);			_t = _t.getNextSibling();			_t = __t8;			_t = _t.getNextSibling();		}		catch (RecognitionException ex) {			reportError(ex);			if (_t!=null) {_t = _t.getNextSibling();}		}		_retTree = _t;	}		public final void rules(AST _t,		StringTemplate recognizerST	) throws RecognitionException {				GrammarAST rules_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;				StringTemplate rST;						try {      // for error handling			{			int _cnt22=0;			_loop22:			do {				if (_t==null) _t=ASTNULL;				if ((_t.getType()==RULE)) {					{													String ruleName = _t.getFirstChild().getText();								Rule r = grammar.getRule(ruleName);													if (_t==null) _t=ASTNULL;					if (((_t.getType()==RULE))&&(!r.isSynPred || grammar.synPredNamesUsedInDFA.contains(ruleName))) {						rST=rule(_t);						_t = _retTree;																if ( rST!=null ) {											recognizerST.setAttribute("rules", rST);											outputFileST.setAttribute("rules", rST);											headerFileST.setAttribute("rules", rST);										}															}					else if ((_t.getType()==RULE)) {						GrammarAST tmp13_AST_in = (GrammarAST)_t;						match(_t,RULE);						_t = _t.getNextSibling();					}					else {						throw new NoViableAltException(_t);					}										}				}

⌨️ 快捷键说明

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