Class Rule


  • public class Rule
    extends Object
    Combine the info associated with a rule.
    • Field Detail

      • supportsLabelOptimization

        public static final boolean supportsLabelOptimization
      • index

        public int index
      • modifier

        public String modifier
      • startState

        public NFAState startState
      • legalOptions

        public static final Set<String> 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
      • returnScope

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

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

        public List<String> useScopes
        A list of scope names (String) used by this rule
      • throwsSpec

        public Set<String> throwsSpec
        Exceptions that this rule can throw
      • labelNameSpace

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

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

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

        public int numberOfAlts
      • altToTokenRefMap

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

        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
      • 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,
                    String ruleName,
                    int ruleIndex,
                    int numberOfAlts)
    • Method Detail

      • getRuleType

        public static int getRuleType​(String ruleName)
      • defineLabel

        public void defineLabel​(org.antlr.runtime.Token label,
                                GrammarAST elementRef,
                                int type)
      • 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.
      • trackRuleReferenceInAlt

        public void trackRuleReferenceInAlt​(GrammarAST refAST,
                                            int outerAltNum)
      • getTokenRefsInAlt

        public Set<String> getTokenRefsInAlt​(int altNum)
      • getAllTokenRefsInAltsWithRewrites

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

        public Set<String> getRuleRefsInAlt​(int outerAltNum)
      • getAllRuleRefsInAltsWithRewrites

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

        public boolean hasRewrite​(int i)
      • getAttributeScope

        public AttributeScope getAttributeScope​(String name)
        Return the scope containing name
      • getLocalAttributeScope

        public AttributeScope getLocalAttributeScope​(String name)
        Get the arg, return value, or predefined property for this rule
      • getElementLabel

        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.
      • 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 String getSingleValueReturnType()
      • getSingleValueReturnName

        public 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)
      • setOption

        public String setOption​(String key,
                                Object value,
                                org.antlr.runtime.Token optionsStartToken)
        Save the option key/value pair and process it; return the key or null if invalid option.
      • setOptions

        public void setOptions​(Map<String,​Object> options,
                               org.antlr.runtime.Token optionsStartToken)
      • toString

        public String toString()
        Used during grammar imports to see if sets of rules intersect... public int hashCode() { return name.hashCode(); }
        Overrides:
        toString in class Object