Package org.antlr.tool
Class Rule
java.lang.Object
org.antlr.tool.Rule
Combine the info associated with a rule.
-
Field Summary
FieldsModifier and TypeFieldDescriptionMap a name to an action for this rule.protected Map<String,
List<GrammarAST>>[] 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>>[] Each alt has a Map<tokenRefName,List<tokenRefAST>>; range 1..numberOfAlts.For convenience, track the argument def AST action node if anyA list of all LabelElementPair attached to single char literals like x='a'To which grammar does this belong?boolean
int
protected List
<GrammarAST> Track all executable actions other than named actions like @init.boolean
protected Map
<String, Grammar.LabelElementPair> All labels go in here (plus being split per the above lists) to catch dup label and label type mismatches.int
This rule's optionsboolean
Do not generate start, stop etc...The return values of a rule and predefined rule attributesA list of all LabelElementPair attached to rule references like f=fieldA list of all rule ref list LabelElementPair like ids+=exprthe attributes defined with "scope {...}" inside a rulestatic final boolean
Exceptions that this rule can throwA list of all LabelElementPair attached to tokens like id=IDA list of all Token list LabelElementPair like ids+=IDThe AST representing the whole ruleA list of scope names (String) used by this ruleA list of all LabelElementPair attached to tokens like x=.A list of all LabelElementPair attached to tokens like x+=. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
defineLabel
(Token label, GrammarAST elementRef, int type) void
defineNamedAction
(GrammarAST ampersandAST, GrammarAST nameAST, GrammarAST actionAST) Given @scope::name {action} define it for this grammar.For use with rewrite rules, we must track all rule AST results on the left-hand-side; so we need Lists.For use with rewrite rules, we must track all tokens matched on the left-hand-side; so we need Lists.getAttributeScope
(String name) Return the scope containing namegetElementLabel
(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.boolean
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.boolean
boolean
getLocalAttributeScope
(String name) Get the arg, return value, or predefined property for this rulegetRuleLabel
(String name) getRuleListLabel
(String name) getRuleRefsInAlt
(int outerAltNum) getRuleRefsInAlt
(String ref, int outerAltNum) static int
getRuleType
(String ruleName) getTokenLabel
(String name) getTokenListLabel
(String name) getTokenRefsInAlt
(int altNum) getTokenRefsInAlt
(String ref, int outerAltNum) boolean
hasRewrite
(int i) void
setActions
(Map<String, Object> actions) Save the option key/value pair and process it; return the key or null if invalid option.void
setOptions
(Map<String, Object> options, Token optionsStartToken) toString()
Used during grammar imports to see if sets of rules intersect...void
trackInlineAction
(GrammarAST actionAST) void
trackRuleReferenceInAlt
(GrammarAST refAST, int outerAltNum) 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.
-
Field Details
-
supportsLabelOptimization
public static final boolean supportsLabelOptimization -
name
-
index
public int index -
modifier
-
startState
-
stopState
-
options
This rule's options -
legalOptions
-
tree
The AST representing the whole rule -
grammar
To which grammar does this belong? -
argActionAST
For convenience, track the argument def AST action node if any -
EORNode
-
returnScope
The return values of a rule and predefined rule attributes -
parameterScope
-
ruleScope
the attributes defined with "scope {...}" inside a rule -
useScopes
A list of scope names (String) used by this rule -
throwsSpec
Exceptions that this rule can throw -
tokenLabels
A list of all LabelElementPair attached to tokens like id=ID -
wildcardTreeLabels
A list of all LabelElementPair attached to tokens like x=. in tree grammar -
wildcardTreeListLabels
A list of all LabelElementPair attached to tokens like x+=. in tree grammar -
charLabels
A list of all LabelElementPair attached to single char literals like x='a' -
ruleLabels
A list of all LabelElementPair attached to rule references like f=field -
tokenListLabels
A list of all Token list LabelElementPair like ids+=ID -
ruleListLabels
A list of all rule ref list LabelElementPair like ids+=expr -
labelNameSpace
All labels go in here (plus being split per the above lists) to catch dup label and label type mismatches. -
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. -
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... -
numberOfAlts
public int numberOfAlts -
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. -
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 -
referencedPredefinedRuleAttributes
public boolean referencedPredefinedRuleAttributesDo not generate start, stop etc... in a return value struct unless somebody references $r.start somewhere. -
isSynPred
public boolean isSynPred -
imported
public boolean imported
-
-
Constructor Details
-
Rule
-
-
Method Details
-
getRuleType
-
defineLabel
-
getLabel
-
getTokenLabel
-
getRuleLabels
-
getRuleListLabels
-
getRuleLabel
-
getTokenListLabel
-
getRuleListLabel
-
trackTokenReferenceInAlt
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. -
getTokenRefsInAlt
-
trackRuleReferenceInAlt
-
getRuleRefsInAlt
-
getTokenRefsInAlt
-
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. -
getRuleRefsInAlt
-
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. -
getInlineActions
-
hasRewrite
public boolean hasRewrite(int i) -
getAttributeScope
Return the scope containing name -
getLocalAttributeScope
Get the arg, return value, or predefined property for this rule -
getElementLabel
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. -
getHasMultipleReturnValues
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. -
getHasSingleReturnValue
public boolean getHasSingleReturnValue() -
getHasReturnValue
public boolean getHasReturnValue() -
getSingleValueReturnType
-
getSingleValueReturnName
-
defineNamedAction
Given @scope::name {action} define it for this grammar. Later, the code generator will ask for the actions table. -
trackInlineAction
-
getActions
-
setActions
-
setOption
Save the option key/value pair and process it; return the key or null if invalid option. -
setOptions
-
toString
Used during grammar imports to see if sets of rules intersect... public int hashCode() { return name.hashCode(); }
-