Top Description Fields Constructors Methods
org.antlr.tool

public Class Rule

extends Object
Class Inheritance
Imports
org.antlr.analysis.NFAState, org.antlr.codegen.CodeGenerator, org.antlr.grammar.v3.ANTLRParser, org.antlr.runtime.CommonToken, .Token, java.util.*

Combine the info associated with a rule.

Field Summary

Modifier and TypeField and Description
protected Map<String, Object>
actions

Map a name to an action for this rule.

protected Map<String, List<GrammarAST>>[]
altToRuleRefMap

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.

protected Map<String, List<GrammarAST>>[]
altToTokenRefMap

Each alt has a Map<tokenRefName,List<tokenRefAST>>; range 1..numberOfAlts.

public GrammarAST
argActionAST

For convenience, track the argument def AST action node if any

public LinkedHashMap<String, Grammar.LabelElementPair>
charLabels

A list of all LabelElementPair attached to single char literals like x='a'

public GrammarAST
public Grammar
grammar

To which grammar does this belong?

public boolean
public int
protected List<GrammarAST>
inlineActions

Track all executable actions other than named actions like @init.

public boolean
protected Map<String, Grammar.LabelElementPair>
labelNameSpace

All labels go in here (plus being split per the above lists) to catch dup label and label type mismatches.

public static final Set<String>
public String
public String
public int
protected Map<String, Object>
options

This rule's options

public AttributeScope
public boolean
referencedPredefinedRuleAttributes

Do not generate start, stop etc...

public AttributeScope
returnScope

The return values of a rule and predefined rule attributes

public LinkedHashMap<String, Grammar.LabelElementPair>
ruleLabels

A list of all LabelElementPair attached to rule references like f=field

public LinkedHashMap<String, Grammar.LabelElementPair>
ruleListLabels

A list of all rule ref list LabelElementPair like ids+=expr

public AttributeScope
ruleScope

the attributes defined with "scope {...}" inside a rule

public NFAState
public NFAState
public static final boolean
public Set<String>
throwsSpec

Exceptions that this rule can throw

public LinkedHashMap<String, Grammar.LabelElementPair>
tokenLabels

A list of all LabelElementPair attached to tokens like id=ID

public LinkedHashMap<String, Grammar.LabelElementPair>
tokenListLabels

A list of all Token list LabelElementPair like ids+=ID

public GrammarAST
tree

The AST representing the whole rule

public List<String>
useScopes

A list of scope names (String) used by this rule

public LinkedHashMap<String, Grammar.LabelElementPair>
wildcardTreeLabels

A list of all LabelElementPair attached to tokens like x=.

public LinkedHashMap<String, Grammar.LabelElementPair>
wildcardTreeListLabels

A list of all LabelElementPair attached to tokens like x+=.

Constructor Summary

AccessConstructor and Description
public
Rule(Grammar grammar, String ruleName, int ruleIndex, int numberOfAlts)

Method Summary

Modifier and TypeMethod and Description
public void
defineLabel(Token label, GrammarAST elementRef, int type)

public void
defineNamedAction(GrammarAST ampersandAST, GrammarAST nameAST, GrammarAST actionAST)

Given @scope::name {action} define it for this grammar.

public Map<String, Object>
public Set<String>
getAllRuleRefsInAltsWithRewrites()

For use with rewrite rules, we must track all rule AST results on the left-hand-side; so we need Lists.

public Set<String>
getAllTokenRefsInAltsWithRewrites()

For use with rewrite rules, we must track all tokens matched on the left-hand-side; so we need Lists.

public AttributeScope
getAttributeScope(String name)

Return the scope containing name

public String
getElementLabel(String refdSymbol, int outerAltNum, CodeGenerator generator)

For references to tokens rather than by label such as $ID, we need to get the existing label for the ID ref or create a new one.

public boolean
getHasMultipleReturnValues()

If a rule has no user-defined return values and nobody references it's start/stop (predefined attributes), then there is no need to define a struct; otherwise for now we assume a struct.

public boolean
public boolean
public List<GrammarAST>
public Grammar.LabelElementPair
public AttributeScope
getLocalAttributeScope(String name)

Get the arg, return value, or predefined property for this rule

public Grammar.LabelElementPair
public Map<String, Grammar.LabelElementPair>
public Grammar.LabelElementPair
public Map<String, Grammar.LabelElementPair>
public List<GrammarAST>
getRuleRefsInAlt(String ref, int outerAltNum)

public Set<String>
getRuleRefsInAlt(int outerAltNum)

public static int
getRuleType(String ruleName)

public String
public String
public Grammar.LabelElementPair
public Grammar.LabelElementPair
public List<GrammarAST>
getTokenRefsInAlt(String ref, int outerAltNum)

public Set<String>
getTokenRefsInAlt(int altNum)

public boolean
hasRewrite(int i)

public void
public String
setOption(String key, Object value, Token optionsStartToken)

Save the option key/value pair and process it; return the key or null if invalid option.

public void
setOptions(Map<String, Object> options, Token optionsStartToken)

public String
toString()

Overrides java.lang.Object.toString.

Used during grammar imports to see if sets of rules intersect...
public void
public void
trackRuleReferenceInAlt(GrammarAST refAST, int outerAltNum)

public void
trackTokenReferenceInAlt(GrammarAST refAST, int outerAltNum)

Track a token ID or literal like '+' and "void" as having been referenced somewhere within the alts (not rewrite sections) of a rule.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

actionsback to summary
protected Map<String, Object> actions

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.

altToRuleRefMapback to summary
protected Map<String, List<GrammarAST>>[] altToRuleRefMap

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

altToTokenRefMapback to summary
protected Map<String, List<GrammarAST>>[] altToTokenRefMap

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.

argActionASTback to summary
public GrammarAST argActionAST

For convenience, track the argument def AST action node if any

charLabelsback to summary
public LinkedHashMap<String, Grammar.LabelElementPair> charLabels

A list of all LabelElementPair attached to single char literals like x='a'

EORNodeback to summary
public GrammarAST EORNode
grammarback to summary
public Grammar grammar

To which grammar does this belong?

importedback to summary
public boolean imported
indexback to summary
public int index
inlineActionsback to summary
protected List<GrammarAST> inlineActions

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...

isSynPredback to summary
public boolean isSynPred
labelNameSpaceback to summary
protected Map<String, Grammar.LabelElementPair> labelNameSpace

All labels go in here (plus being split per the above lists) to catch dup label and label type mismatches.

legalOptionsback to summary
public static final Set<String> legalOptions
modifierback to summary
public String modifier
nameback to summary
public String name
numberOfAltsback to summary
public int numberOfAlts
optionsback to summary
protected Map<String, Object> options

This rule's options

parameterScopeback to summary
public AttributeScope parameterScope
referencedPredefinedRuleAttributesback to summary
public boolean referencedPredefinedRuleAttributes

Do not generate start, stop etc... in a return value struct unless somebody references $r.start somewhere.

returnScopeback to summary
public AttributeScope returnScope

The return values of a rule and predefined rule attributes

ruleLabelsback to summary
public LinkedHashMap<String, Grammar.LabelElementPair> ruleLabels

A list of all LabelElementPair attached to rule references like f=field

ruleListLabelsback to summary
public LinkedHashMap<String, Grammar.LabelElementPair> ruleListLabels

A list of all rule ref list LabelElementPair like ids+=expr

ruleScopeback to summary
public AttributeScope ruleScope

the attributes defined with "scope {...}" inside a rule

startStateback to summary
public NFAState startState
stopStateback to summary
public NFAState stopState
supportsLabelOptimizationback to summary
public static final boolean supportsLabelOptimization
throwsSpecback to summary
public Set<String> throwsSpec

Exceptions that this rule can throw

tokenLabelsback to summary
public LinkedHashMap<String, Grammar.LabelElementPair> tokenLabels

A list of all LabelElementPair attached to tokens like id=ID

tokenListLabelsback to summary
public LinkedHashMap<String, Grammar.LabelElementPair> tokenListLabels

A list of all Token list LabelElementPair like ids+=ID

treeback to summary
public GrammarAST tree

The AST representing the whole rule

useScopesback to summary
public List<String> useScopes

A list of scope names (String) used by this rule

wildcardTreeLabelsback to summary
public LinkedHashMap<String, Grammar.LabelElementPair> wildcardTreeLabels

A list of all LabelElementPair attached to tokens like x=. in tree grammar

wildcardTreeListLabelsback to summary
public LinkedHashMap<String, Grammar.LabelElementPair> wildcardTreeListLabels

A list of all LabelElementPair attached to tokens like x+=. in tree grammar

Constructor Detail

Ruleback to summary
public Rule(Grammar grammar, String ruleName, int ruleIndex, int numberOfAlts)
Annotations
@SuppressWarnings:unchecked

Method Detail

defineLabelback to summary
public void defineLabel(Token label, GrammarAST elementRef, int type)
defineNamedActionback to summary
public void defineNamedAction(GrammarAST ampersandAST, GrammarAST nameAST, GrammarAST actionAST)

Given @scope::name {action} define it for this grammar. Later, the code generator will ask for the actions table.

getActionsback to summary
public Map<String, Object> getActions()
getAllRuleRefsInAltsWithRewritesback to summary
public Set<String> getAllRuleRefsInAltsWithRewrites()

For use with rewrite rules, we must track all rule AST results on the left-hand-side; so we need Lists. This is a unique list of all rule results for which the rule needs a list of results.

getAllTokenRefsInAltsWithRewritesback to summary
public Set<String> getAllTokenRefsInAltsWithRewrites()

For use with rewrite rules, we must track all tokens matched on the left-hand-side; so we need Lists. This is a unique list of all token types for which the rule needs a list of tokens. This is called from the rule template not directly by the code generator.

getAttributeScopeback to summary
public AttributeScope getAttributeScope(String name)

Return the scope containing name

getElementLabelback to summary
public String getElementLabel(String refdSymbol, int outerAltNum, CodeGenerator generator)

For references to tokens rather than by label such as $ID, we need to get the existing label for the ID ref or create a new one.

getHasMultipleReturnValuesback to summary
public boolean getHasMultipleReturnValues()

If a rule has no user-defined return values and nobody references it's start/stop (predefined attributes), then there is no need to define a struct; otherwise for now we assume a struct. A rule also has multiple return values if you are building trees or templates.

getHasReturnValueback to summary
public boolean getHasReturnValue()
getHasSingleReturnValueback to summary
public boolean getHasSingleReturnValue()
getInlineActionsback to summary
public List<GrammarAST> getInlineActions()
getLabelback to summary
public Grammar.LabelElementPair getLabel(String name)
getLocalAttributeScopeback to summary
public AttributeScope getLocalAttributeScope(String name)

Get the arg, return value, or predefined property for this rule

getRuleLabelback to summary
public Grammar.LabelElementPair getRuleLabel(String name)
getRuleLabelsback to summary
public Map<String, Grammar.LabelElementPair> getRuleLabels()
getRuleListLabelback to summary
public Grammar.LabelElementPair getRuleListLabel(String name)
getRuleListLabelsback to summary
public Map<String, Grammar.LabelElementPair> getRuleListLabels()
getRuleRefsInAltback to summary
public List<GrammarAST> getRuleRefsInAlt(String ref, int outerAltNum)
getRuleRefsInAltback to summary
public Set<String> getRuleRefsInAlt(int outerAltNum)
getRuleTypeback to summary
public static int getRuleType(String ruleName)
getSingleValueReturnNameback to summary
public String getSingleValueReturnName()
getSingleValueReturnTypeback to summary
public String getSingleValueReturnType()
getTokenLabelback to summary
public Grammar.LabelElementPair getTokenLabel(String name)
getTokenListLabelback to summary
public Grammar.LabelElementPair getTokenListLabel(String name)
getTokenRefsInAltback to summary
public List<GrammarAST> getTokenRefsInAlt(String ref, int outerAltNum)
getTokenRefsInAltback to summary
public Set<String> getTokenRefsInAlt(int altNum)
hasRewriteback to summary
public boolean hasRewrite(int i)
setActionsback to summary
public void setActions(Map<String, Object> actions)
setOptionback to summary
public String setOption(String key, Object value, Token optionsStartToken)

Save the option key/value pair and process it; return the key or null if invalid option.

setOptionsback to summary
public void setOptions(Map<String, Object> options, Token optionsStartToken)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Used during grammar imports to see if sets of rules intersect... public int hashCode() { return name.hashCode(); }

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object

Annotations
@Override
trackInlineActionback to summary
public void trackInlineAction(GrammarAST actionAST)
trackRuleReferenceInAltback to summary
public void trackRuleReferenceInAlt(GrammarAST refAST, int outerAltNum)
trackTokenReferenceInAltback to summary
public void trackTokenReferenceInAlt(GrammarAST refAST, int outerAltNum)

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.