Class CompositeGrammar


  • public class CompositeGrammar
    extends java.lang.Object
    A tree of component (delegate) grammars. Rules defined in delegates are "inherited" like multi-inheritance so you can override them. All token types must be consistent across rules from all delegate grammars, so they must be stored here in one central place. We have to start out assuming a composite grammar situation as we can't look into the grammar files a priori to see if there is a delegate statement. Because of this, and to avoid duplicating token type tracking in each grammar, even single noncomposite grammars use one of these objects to track token types.
    • Field Detail

      • refClosureBusy

        protected java.util.Set<NFAState> refClosureBusy
        Used during getRuleReferenceClosure to detect computation cycles
      • stateCounter

        public int stateCounter
        Used to assign state numbers; all grammars in composite share common NFA space. This NFA tracks state numbers number to state mapping.
      • numberToStateList

        protected java.util.Vector<NFAState> numberToStateList
        The NFA states in the NFA built from rules across grammars in composite. Maps state number to NFAState object. This is a Vector instead of a List because I need to be able to grow this properly. After talking to Josh Bloch, Collections guy at Sun, I decided this was easiest solution.
      • maxTokenType

        protected int maxTokenType
        Token names and literal tokens like "void" are uniquely indexed. with -1 implying EOF. Characters are different; they go from -1 (EOF) to ￾. For example, 0 could be a binary byte you want to lexer. Labels of DFA/NFA transitions can be both tokens and characters. I use negative numbers for bookkeeping labels like EPSILON. Char/String literals and token types overlap in the same space, however.
      • tokenIDToTypeMap

        public java.util.Map<java.lang.String,​java.lang.Integer> tokenIDToTypeMap
        Map token like ID (but not literals like "while") to its token type
      • stringLiteralToTypeMap

        public java.util.Map<java.lang.String,​java.lang.Integer> stringLiteralToTypeMap
        Map token literals like "while" to its token type. It may be that WHILE="while"=35, in which case both tokenIDToTypeMap and this field will have entries both mapped to 35.
      • typeToStringLiteralList

        public java.util.Vector<java.lang.String> typeToStringLiteralList
        Reverse index for stringLiteralToTypeMap
      • typeToTokenList

        public java.util.Vector<java.lang.String> typeToTokenList
        Map a token type to its token name. Must subtract MIN_TOKEN_TYPE from index.
      • lexerRules

        protected java.util.Set<java.lang.String> lexerRules
        If combined or lexer grammar, track the rules. Track lexer rules so we can warn about undefined tokens. This is combined set of lexer rules from all lexer grammars seen in all imports.
      • ruleIndex

        protected int ruleIndex
        Rules are uniquely labeled from 1..n among all grammars
      • ruleIndexToRuleList

        protected java.util.Vector<Rule> ruleIndexToRuleList
        Map a rule index to its name; use a Vector on purpose as new collections stuff won't let me setSize and make it grow. :( I need a specific guaranteed index, which the Collections stuff won't let me have.
      • watchNFAConversion

        public boolean watchNFAConversion
    • Constructor Detail

      • CompositeGrammar

        public CompositeGrammar()
      • CompositeGrammar

        public CompositeGrammar​(Grammar g)
    • Method Detail

      • initTokenSymbolTables

        protected void initTokenSymbolTables()
      • setDelegationRoot

        public void setDelegationRoot​(Grammar root)
      • getRule

        public Rule getRule​(java.lang.String ruleName)
      • getOption

        public java.lang.Object getOption​(java.lang.String key)
      • addGrammar

        public void addGrammar​(Grammar delegator,
                               Grammar delegate)
        Add delegate grammar as child of delegator
      • getDelegator

        public Grammar getDelegator​(Grammar g)
        Get parent of this grammar
      • getDelegates

        public java.util.List<Grammar> getDelegates​(Grammar g)
        Get list of all delegates from all grammars in the delegate subtree of g. The grammars are in delegation tree preorder. Don't include g itself in list as it is not a delegate of itself.
      • getDirectDelegates

        public java.util.List<Grammar> getDirectDelegates​(Grammar g)
      • getIndirectDelegates

        public java.util.List<Grammar> getIndirectDelegates​(Grammar g)
        Get delegates below direct delegates of g
      • getDelegators

        public java.util.List<Grammar> getDelegators​(Grammar g)
        Return list of delegate grammars from root down to g. Order is root, ..., g.parent. (g not included).
      • getDelegatedRules

        public java.util.Set<? extends Rule> getDelegatedRules​(Grammar g)
        Get set of rules for grammar g that need to have manual delegation methods. This is the list of rules collected from all direct/indirect delegates minus rules overridden in grammar g. This returns null except for the delegate root because it is the only one that has to have a complete grammar rule interface. The delegates should not be instantiated directly for use as parsers (you can create them to pass to the root parser's ctor as arguments).
      • getAllImportedRules

        public java.util.Set<? extends Rule> getAllImportedRules​(Grammar g)
        Get all rule definitions from all direct/indirect delegate grammars of g.
      • getRootGrammar

        public Grammar getRootGrammar()
      • getGrammar

        public Grammar getGrammar​(java.lang.String grammarName)
      • getNewNFAStateNumber

        public int getNewNFAStateNumber()
      • addState

        public void addState​(NFAState state)
      • getState

        public NFAState getState​(int s)
      • assignTokenTypes

        public void assignTokenTypes()
                              throws org.antlr.runtime.RecognitionException
        Throws:
        org.antlr.runtime.RecognitionException
      • translateLeftRecursiveRules

        public void translateLeftRecursiveRules()
      • defineGrammarSymbols

        public void defineGrammarSymbols()
      • createNFAs

        public void createNFAs()
      • minimizeRuleSet

        public void minimizeRuleSet()
      • _minimizeRuleSet

        public void _minimizeRuleSet​(java.util.Set<java.lang.String> ruleDefs,
                                     CompositeGrammarTree p)