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

📄 rule.java

📁 antlr最新版本V3源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* [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 antlr.CommonToken;import org.antlr.analysis.NFAState;import org.antlr.codegen.CodeGenerator;import java.util.*;/** Combine the info associated with a rule */public class Rule {	public String name;	public int index;	public String modifier;	public NFAState startState;	public NFAState stopState;	/** This rule's options */	protected Map options;	public static final Set legalOptions =			new HashSet() {{add("k"); add("greedy"); add("memoize"); add("backtrack");}};	/** The AST representing the whole rule */	public GrammarAST tree;	/** To which grammar does this belong? */	public Grammar grammar;	/** For convenience, track the argument def AST action node if any */	public GrammarAST argActionAST;	public GrammarAST EORNode;	/** The return values of a rule and predefined rule attributes */	public AttributeScope returnScope;	public AttributeScope parameterScope;	/** the attributes defined with "scope {...}" inside a rule */	public AttributeScope ruleScope;	/** A list of scope names (String) used by this rule */	public List useScopes;	/** A list of all LabelElementPair attached to tokens like id=ID */	public LinkedHashMap tokenLabels;	/** A list of all LabelElementPair attached to single char literals like x='a' */	public LinkedHashMap charLabels;	/** A list of all LabelElementPair attached to rule references like f=field */	public LinkedHashMap ruleLabels;	/** A list of all Token list LabelElementPair like ids+=ID */	public LinkedHashMap tokenListLabels;	/** A list of all rule ref list LabelElementPair like ids+=expr */	public LinkedHashMap ruleListLabels;	/** All labels go in here (plus being split per the above lists) to	 *  catch dup label and label type mismatches.	 */	protected Map<String, Grammar.LabelElementPair> labelNameSpace =		new HashMap<String, Grammar.LabelElementPair>();	/** Map a name to an action for this rule.  Currently init is only	 *  one we use, but we can add more in future.	 *  The code generator will use this to fill holes in the rule template.	 *  I track the AST node for the action in case I need the line number	 *  for errors.  A better name is probably namedActions, but I don't	 *  want everyone to have to change their code gen templates now.	 */	protected Map<String, GrammarAST> actions =		new HashMap<String, GrammarAST>();	/** Track all executable actions other than named actions like @init.	 *  Also tracks exception handlers, predicates, and rewrite rewrites.	 *  We need to examine these actions before code generation so	 *  that we can detect refs to $rule.attr etc...	 */	protected List<GrammarAST> inlineActions = new ArrayList<GrammarAST>();	public int numberOfAlts;	/** Each alt has a Map<tokenRefName,List<tokenRefAST>>; range 1..numberOfAlts.	 *  So, if there are 3 ID refs in a rule's alt number 2, you'll have	 *  altToTokenRef[2].get("ID").size()==3.  This is used to see if $ID is ok.	 *  There must be only one ID reference in the alt for $ID to be ok in	 *  an action--must be unique.	 *	 *  This also tracks '+' and "int" literal token references	 *  (if not in LEXER).	 *	 *  Rewrite rules force tracking of all tokens.	 */	protected Map<String, List<GrammarAST>>[] altToTokenRefMap;	/** Each alt has a Map<ruleRefName,List<ruleRefAST>>; range 1..numberOfAlts	 *  So, if there are 3 expr refs in a rule's alt number 2, you'll have	 *  altToRuleRef[2].get("expr").size()==3.  This is used to see if $expr is ok.	 *  There must be only one expr reference in the alt for $expr to be ok in	 *  an action--must be unique.	 *	 *  Rewrite rules force tracking of all rule result ASTs. 1..n	 */	protected Map<String, List<GrammarAST>>[] altToRuleRefMap;	/** Track which alts have rewrite rules associated with them. 1..n */	protected boolean[] altsWithRewrites;	/** Do not generate start, stop etc... in a return value struct unless	 *  somebody references $r.start somewhere.	 */	public boolean referencedPredefinedRuleAttributes = false;	public boolean isSynPred = false;	public Rule(Grammar grammar,				String ruleName,				int ruleIndex,				int numberOfAlts)	{		this.name = ruleName;		this.index = ruleIndex;		this.numberOfAlts = numberOfAlts;		this.grammar = grammar;		altToTokenRefMap = new Map[numberOfAlts+1];		altToRuleRefMap = new Map[numberOfAlts+1];		altsWithRewrites = new boolean[numberOfAlts+1];		for (int alt=1; alt<=numberOfAlts; alt++) {			altToTokenRefMap[alt] = new HashMap<String, List<GrammarAST>>();			altToRuleRefMap[alt] = new HashMap<String, List<GrammarAST>>();		}	}	public void defineLabel(antlr.Token label, GrammarAST elementRef, int type) {		Grammar.LabelElementPair pair = grammar.new LabelElementPair(label,elementRef);		pair.type = type;		labelNameSpace.put(label.getText(), pair);		switch ( type ) {			case Grammar.TOKEN_LABEL :				if ( tokenLabels==null ) {					tokenLabels = new LinkedHashMap();				}				tokenLabels.put(label.getText(), pair);				break;			case Grammar.RULE_LABEL :				if ( ruleLabels==null ) {					ruleLabels = new LinkedHashMap();				}				ruleLabels.put(label.getText(), pair);				break;			case Grammar.TOKEN_LIST_LABEL :				if ( tokenListLabels==null ) {					tokenListLabels = new LinkedHashMap();				}				tokenListLabels.put(label.getText(), pair);				break;			case Grammar.RULE_LIST_LABEL :				if ( ruleListLabels==null ) {					ruleListLabels = new LinkedHashMap();				}				ruleListLabels.put(label.getText(), pair);				break;			case Grammar.CHAR_LABEL :				if ( charLabels==null ) {					charLabels = new LinkedHashMap();				}				charLabels.put(label.getText(), pair);				break;		}	}	public Grammar.LabelElementPair getLabel(String name) {		return (Grammar.LabelElementPair)labelNameSpace.get(name);	}	public Grammar.LabelElementPair getTokenLabel(String name) {		Grammar.LabelElementPair pair = null;		if ( tokenLabels!=null ) {			return (Grammar.LabelElementPair)tokenLabels.get(name);		}		return pair;	}	public Map getRuleLabels() {		return ruleLabels;	}	public Map getRuleListLabels() {		return ruleListLabels;	}	public Grammar.LabelElementPair getRuleLabel(String name) {		Grammar.LabelElementPair pair = null;		if ( ruleLabels!=null ) {			return (Grammar.LabelElementPair)ruleLabels.get(name);		}		return pair;	}	public Grammar.LabelElementPair getTokenListLabel(String name) {		Grammar.LabelElementPair pair = null;		if ( tokenListLabels!=null ) {			return (Grammar.LabelElementPair)tokenListLabels.get(name);		}		return pair;	}	public Grammar.LabelElementPair getRuleListLabel(String name) {		Grammar.LabelElementPair pair = null;		if ( ruleListLabels!=null ) {			return (Grammar.LabelElementPair)ruleListLabels.get(name);		}		return pair;	}	/** Track a token ID or literal like '+' and "void" as having been referenced	 *  somewhere within the alts (not rewrite sections) of a rule.	 *	 *  This differs from Grammar.altReferencesTokenID(), which tracks all	 *  token IDs to check for token IDs without corresponding lexer rules.	 */	public void trackTokenReferenceInAlt(GrammarAST refAST, int outerAltNum) {		List refs = (List)altToTokenRefMap[outerAltNum].get(refAST.getText());		if ( refs==null ) {			refs = new ArrayList();			altToTokenRefMap[outerAltNum].put(refAST.getText(), refs);		}		refs.add(refAST);	}	public List getTokenRefsInAlt(String ref, int outerAltNum) {		if ( altToTokenRefMap[outerAltNum]!=null ) {			List tokenRefASTs = (List)altToTokenRefMap[outerAltNum].get(ref);			return tokenRefASTs;		}		return null;	}	public void trackRuleReferenceInAlt(GrammarAST refAST, int outerAltNum) {		List refs = (List)altToRuleRefMap[outerAltNum].get(refAST.getText());		if ( refs==null ) {			refs = new ArrayList();			altToRuleRefMap[outerAltNum].put(refAST.getText(), refs);		}		refs.add(refAST);	}	public List getRuleRefsInAlt(String ref, int outerAltNum) {		if ( altToRuleRefMap[outerAltNum]!=null ) {			List ruleRefASTs = (List)altToRuleRefMap[outerAltNum].get(ref);			return ruleRefASTs;		}		return null;

⌨️ 快捷键说明

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