org.antlr.tool
Class Rule

java.lang.Object
  extended by org.antlr.tool.Rule

public class Rule
extends java.lang.Object

Combine the info associated with a rule.


Field Summary
protected  java.util.Map<java.lang.String,GrammarAST> actions
          Map a name to an action for this rule.
protected  boolean[] altsWithRewrites
          Track which alts have rewrite rules associated with them.
protected  java.util.Map<java.lang.String,java.util.List<GrammarAST>>[] altToRuleRefMap
          Each alt has a Map>; 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  java.util.Map<java.lang.String,java.util.List<GrammarAST>>[] altToTokenRefMap
          Each alt has a Map>; range 1..numberOfAlts.
 GrammarAST argActionAST
          For convenience, track the argument def AST action node if any
 java.util.LinkedHashMap charLabels
          A list of all LabelElementPair attached to single char literals like x='a'
 GrammarAST EORNode
           
 LookaheadSet FIRST
          The set of all tokens reachable from the start state w/o leaving via the accept state.
 Grammar grammar
          To which grammar does this belong?
 boolean imported
           
 int index
           
protected  java.util.List<GrammarAST> inlineActions
          Track all executable actions other than named actions like @init.
 boolean isSynPred
           
protected  java.util.Map<java.lang.String,Grammar.LabelElementPair> labelNameSpace
          All labels go in here (plus being split per the above lists) to catch dup label and label type mismatches.
static java.util.Set legalOptions
           
 java.lang.String modifier
           
 java.lang.String name
           
 int numberOfAlts
           
protected  java.util.Map options
          This rule's options
 AttributeScope parameterScope
           
 boolean referencedPredefinedRuleAttributes
          Do not generate start, stop etc...
 AttributeScope returnScope
          The return values of a rule and predefined rule attributes
 java.util.LinkedHashMap ruleLabels
          A list of all LabelElementPair attached to rule references like f=field
 java.util.LinkedHashMap ruleListLabels
          A list of all rule ref list LabelElementPair like ids+=expr
 AttributeScope ruleScope
          the attributes defined with "scope {...}" inside a rule
 NFAState startState
           
 NFAState stopState
           
 java.util.LinkedHashMap tokenLabels
          A list of all LabelElementPair attached to tokens like id=ID
 java.util.LinkedHashMap tokenListLabels
          A list of all Token list LabelElementPair like ids+=ID
 GrammarAST tree
          The AST representing the whole rule
 java.util.List useScopes
          A list of scope names (String) used by this rule
 java.util.LinkedHashMap wildcardTreeLabels
          A list of all LabelElementPair attached to tokens like x=.
 java.util.LinkedHashMap wildcardTreeListLabels
          A list of all LabelElementPair attached to tokens like x+=.
 
Constructor Summary
Rule(Grammar grammar, java.lang.String ruleName, int ruleIndex, int numberOfAlts)
           
 
Method Summary
 void defineLabel(antlr.Token label, GrammarAST elementRef, int type)
           
 void defineNamedAction(GrammarAST ampersandAST, GrammarAST nameAST, GrammarAST actionAST)
          Given @scope::name {action} define it for this grammar.
 java.util.Map<java.lang.String,GrammarAST> getActions()
           
 java.util.Set getAllRuleRefsInAltsWithRewrites()
          For use with rewrite rules, we must track all rule AST results on the left-hand-side; so we need Lists.
 java.util.Set getAllTokenRefsInAltsWithRewrites()
          For use with rewrite rules, we must track all tokens matched on the left-hand-side; so we need Lists.
 AttributeScope getAttributeScope(java.lang.String name)
          Return the scope containing name
 java.lang.String getElementLabel(java.lang.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 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.
 boolean getHasReturnValue()
           
 boolean getHasSingleReturnValue()
           
 java.util.List<GrammarAST> getInlineActions()
           
 Grammar.LabelElementPair getLabel(java.lang.String name)
           
 AttributeScope getLocalAttributeScope(java.lang.String name)
          Get the arg, return value, or predefined property for this rule
 Grammar.LabelElementPair getRuleLabel(java.lang.String name)
           
 java.util.Map getRuleLabels()
           
 Grammar.LabelElementPair getRuleListLabel(java.lang.String name)
           
 java.util.Map getRuleListLabels()
           
 java.util.Set getRuleRefsInAlt(int outerAltNum)
           
 java.util.List getRuleRefsInAlt(java.lang.String ref, int outerAltNum)
           
 java.lang.String getSingleValueReturnName()
           
 java.lang.String getSingleValueReturnType()
           
 Grammar.LabelElementPair getTokenLabel(java.lang.String name)
           
 Grammar.LabelElementPair getTokenListLabel(java.lang.String name)
           
 java.util.Set getTokenRefsInAlt(int altNum)
           
 java.util.List getTokenRefsInAlt(java.lang.String ref, int outerAltNum)
           
 boolean hasRewrite(int i)
           
 void setActions(java.util.Map<java.lang.String,GrammarAST> actions)
           
 java.lang.String setOption(java.lang.String key, java.lang.Object value, antlr.Token optionsStartToken)
          Save the option key/value pair and process it; return the key or null if invalid option.
 void setOptions(java.util.Map options, antlr.Token optionsStartToken)
           
 java.lang.String toString()
          Used during grammar imports to see if sets of rules intersect...
 void trackAltsWithRewrites(GrammarAST altAST, int outerAltNum)
          Track which rules have rewrite rules.
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

name

public java.lang.String name

index

public int index

modifier

public java.lang.String modifier

startState

public NFAState startState

stopState

public NFAState stopState

options

protected java.util.Map options
This rule's options


legalOptions

public static final java.util.Set legalOptions

tree

public GrammarAST tree
The AST representing the whole rule


grammar

public Grammar grammar
To which grammar does this belong?


argActionAST

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


EORNode

public GrammarAST EORNode

FIRST

public LookaheadSet FIRST
The set of all tokens reachable from the start state w/o leaving via the accept state. If it reaches the accept state, FIRST includes EOR_TOKEN_TYPE.


returnScope

public AttributeScope returnScope
The return values of a rule and predefined rule attributes


parameterScope

public AttributeScope parameterScope

ruleScope

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


useScopes

public java.util.List useScopes
A list of scope names (String) used by this rule


tokenLabels

public java.util.LinkedHashMap tokenLabels
A list of all LabelElementPair attached to tokens like id=ID


wildcardTreeLabels

public java.util.LinkedHashMap wildcardTreeLabels
A list of all LabelElementPair attached to tokens like x=. in tree grammar


wildcardTreeListLabels

public java.util.LinkedHashMap wildcardTreeListLabels
A list of all LabelElementPair attached to tokens like x+=. in tree grammar


charLabels

public java.util.LinkedHashMap charLabels
A list of all LabelElementPair attached to single char literals like x='a'


ruleLabels

public java.util.LinkedHashMap ruleLabels
A list of all LabelElementPair attached to rule references like f=field


tokenListLabels

public java.util.LinkedHashMap tokenListLabels
A list of all Token list LabelElementPair like ids+=ID


ruleListLabels

public java.util.LinkedHashMap ruleListLabels
A list of all rule ref list LabelElementPair like ids+=expr


labelNameSpace

protected java.util.Map<java.lang.String,Grammar.LabelElementPair> labelNameSpace
All labels go in here (plus being split per the above lists) to catch dup label and label type mismatches.


actions

protected java.util.Map<java.lang.String,GrammarAST> 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

protected java.util.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...


numberOfAlts

public int numberOfAlts

altToTokenRefMap

protected java.util.Map<java.lang.String,java.util.List<GrammarAST>>[] altToTokenRefMap
Each alt has a Map>; 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

protected java.util.Map<java.lang.String,java.util.List<GrammarAST>>[] altToRuleRefMap
Each alt has a Map>; 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


altsWithRewrites

protected boolean[] altsWithRewrites
Track which alts have rewrite rules associated with them. 1..n


referencedPredefinedRuleAttributes

public boolean referencedPredefinedRuleAttributes
Do 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 Detail

Rule

public Rule(Grammar grammar,
            java.lang.String ruleName,
            int ruleIndex,
            int numberOfAlts)
Method Detail

defineLabel

public void defineLabel(antlr.Token label,
                        GrammarAST elementRef,
                        int type)

getLabel

public Grammar.LabelElementPair getLabel(java.lang.String name)

getTokenLabel

public Grammar.LabelElementPair getTokenLabel(java.lang.String name)

getRuleLabels

public java.util.Map getRuleLabels()

getRuleListLabels

public java.util.Map getRuleListLabels()

getRuleLabel

public Grammar.LabelElementPair getRuleLabel(java.lang.String name)

getTokenListLabel

public Grammar.LabelElementPair getTokenListLabel(java.lang.String name)

getRuleListLabel

public Grammar.LabelElementPair getRuleListLabel(java.lang.String name)

trackTokenReferenceInAlt

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.


getTokenRefsInAlt

public java.util.List getTokenRefsInAlt(java.lang.String ref,
                                        int outerAltNum)

trackRuleReferenceInAlt

public void trackRuleReferenceInAlt(GrammarAST refAST,
                                    int outerAltNum)

getRuleRefsInAlt

public java.util.List getRuleRefsInAlt(java.lang.String ref,
                                       int outerAltNum)

getTokenRefsInAlt

public java.util.Set getTokenRefsInAlt(int altNum)

getAllTokenRefsInAltsWithRewrites

public java.util.Set 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

public java.util.Set getRuleRefsInAlt(int outerAltNum)

getAllRuleRefsInAltsWithRewrites

public java.util.Set 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

public java.util.List<GrammarAST> getInlineActions()

hasRewrite

public boolean hasRewrite(int i)

trackAltsWithRewrites

public void trackAltsWithRewrites(GrammarAST altAST,
                                  int outerAltNum)
Track which rules have rewrite rules. Pass in the ALT node for the alt so we can check for problems when output=template, rewrite=true, and grammar type is tree parser.


getAttributeScope

public AttributeScope getAttributeScope(java.lang.String name)
Return the scope containing name


getLocalAttributeScope

public AttributeScope getLocalAttributeScope(java.lang.String name)
Get the arg, return value, or predefined property for this rule


getElementLabel

public java.lang.String getElementLabel(java.lang.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.


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

public java.lang.String getSingleValueReturnType()

getSingleValueReturnName

public java.lang.String getSingleValueReturnName()

defineNamedAction

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.


trackInlineAction

public void trackInlineAction(GrammarAST actionAST)

getActions

public java.util.Map<java.lang.String,GrammarAST> getActions()

setActions

public void setActions(java.util.Map<java.lang.String,GrammarAST> actions)

setOption

public java.lang.String setOption(java.lang.String key,
                                  java.lang.Object value,
                                  antlr.Token optionsStartToken)
Save the option key/value pair and process it; return the key or null if invalid option.


setOptions

public void setOptions(java.util.Map options,
                       antlr.Token optionsStartToken)

toString

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

Overrides:
toString in class java.lang.Object


Copyright © 2013. All Rights Reserved.