Class CompositeGrammar

java.lang.Object
org.antlr.tool.CompositeGrammar

public class CompositeGrammar extends 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 Details

    • MIN_RULE_INDEX

      public static final int MIN_RULE_INDEX
      See Also:
    • delegateGrammarTreeRoot

      public CompositeGrammarTree delegateGrammarTreeRoot
    • refClosureBusy

      protected 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 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 Map tokenIDToTypeMap
      Map token like ID (but not literals like "while") to its token type
    • stringLiteralToTypeMap

      public Map<String,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 Vector<String> typeToStringLiteralList
      Reverse index for stringLiteralToTypeMap
    • typeToTokenList

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

      protected Set<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 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 Details

    • CompositeGrammar

      public CompositeGrammar()
    • CompositeGrammar

      public CompositeGrammar(Grammar g)
  • Method Details

    • initTokenSymbolTables

      protected void initTokenSymbolTables()
    • setDelegationRoot

      public void setDelegationRoot(Grammar root)
    • getRule

      public Rule getRule(String ruleName)
    • getOption

      public Object getOption(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 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 List<Grammar> getDirectDelegates(Grammar g)
    • getIndirectDelegates

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

      public 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 Set<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 Set<Rule> getAllImportedRules(Grammar g)
      Get all rule definitions from all direct/indirect delegate grammars of g.
    • getRootGrammar

      public Grammar getRootGrammar()
    • getGrammar

      public Grammar getGrammar(String grammarName)
    • getNewNFAStateNumber

      public int getNewNFAStateNumber()
    • addState

      public void addState(NFAState state)
    • getState

      public NFAState getState(int s)
    • assignTokenTypes

      public void assignTokenTypes() throws antlr.RecognitionException
      Throws:
      antlr.RecognitionException
    • defineGrammarSymbols

      public void defineGrammarSymbols()
    • createNFAs

      public void createNFAs()
    • minimizeRuleSet

      public void minimizeRuleSet()
    • _minimizeRuleSet

      public void _minimizeRuleSet(Set<String> ruleDefs, CompositeGrammarTree p)