Class ParserATNFactory

  • All Implemented Interfaces:
    ATNFactory
    Direct Known Subclasses:
    LexerATNFactory


    public class ParserATNFactory
    extends Object
    implements ATNFactory
    ATN construction routines triggered by ATNBuilder.g. No side-effects. It builds an ATN object and returns it.
    • Field Detail

      • atn

        public final ATN atn
      • currentRule

        public Rule currentRule
      • currentOuterAlt

        public int currentOuterAlt
      • preventEpsilonClosureBlocks

        protected final List<<any>> preventEpsilonClosureBlocks
      • preventEpsilonOptionalBlocks

        protected final List<<any>> preventEpsilonOptionalBlocks
    • Constructor Detail

      • ParserATNFactory

        public ParserATNFactory(Grammar g)
    • Method Detail

      • createATN

        public ATN createATN()
      • setCurrentRuleName

        public void setCurrentRuleName(String name)
      • setCurrentOuterAlt

        public void setCurrentOuterAlt(int alt)
      • set

        public ATNFactory.Handle set(GrammarAST associatedAST,
                                     List<GrammarAST> terminals,
                                     boolean invert)
        From set build single edge graph o->o-set->o. To conform to what an alt block looks like, must have extra state on left. This also handles ~A, converted to ~{A} set.
      • getTokenType

        protected int getTokenType(GrammarAST atom)
      • stringLiteral

        public ATNFactory.Handle stringLiteral(TerminalAST stringLiteralAST)
        For a non-lexer, just build a simple token reference atom.
      • ruleRef

        public ATNFactory.Handle ruleRef(GrammarAST node)
        For reference to rule r, build
          o->(r)  o
         
        where (r) is the start of rule r and the trailing o is not linked to from rule ref state directly (uses RuleTransition#followState).
      • addFollowLink

        public void addFollowLink(int ruleIndex,
                                  ATNState right)
      • action

        public ATNFactory.Handle action(ActionAST action)
        Build what amounts to an epsilon transition with an action. The action goes into ATN though it is ignored during prediction if actionIndex <0.
      • block

        public ATNFactory.Handle block(BlockAST blkAST,
                                       GrammarAST ebnfRoot,
                                       List<ATNFactory.Handle> alts)
        From A|B|..|Z alternative block build
          o->o-A->o->o (last ATNState is BlockEndState pointed to by all alts)
          |          ^
          |->o-B->o--|
          |          |
          ...        |
          |          |
          |->o-Z->o--|
         
        So start node points at every alternative with epsilon transition and every alt right side points at a block end ATNState.

        Special case: only one alternative: don't make a block with alt begin/end.

        Special case: if just a list of tokens/chars/sets, then collapse to a single edged o-set->o graph.

        TODO: Set alt number (1..n) in the states?
      • star

        public ATNFactory.Handle star(GrammarAST starAST,
                                      ATNFactory.Handle elem)
        From (blk)* build ( blk+ )? with *two* decisions, one for entry and one for choosing alts of blk.
           |-------------|
           v             |
           o--[o-blk-o]->o  o
           |                ^
           -----------------|
         
        Note that the optional bypass must jump outside the loop as (A|B)* is not the same thing as (A|B|)+.
      • epsilon

        protected void epsilon(ATNState a,
                               ATNState b)
      • epsilon

        protected void epsilon(ATNState a,
                               ATNState b,
                               boolean prepend)
      • addRuleFollowLinks

        public void addRuleFollowLinks()
      • addEOFTransitionToStartRules

        public int addEOFTransitionToStartRules()
        Add an EOF transition to any rule end ATNState that points to nothing (i.e., for all those rules not invoked by another rule). These are start symbols then. Return the number of grammar entry points; i.e., how many rules are not invoked by another rule (they can only be invoked from outside). These are the start rules.
      • newState

        public <T extends ATNState> T newState(Class<T> nodeType,
                                               GrammarAST node)
      • newState

        public ATNState newState(GrammarAST node)
      • newState

        public ATNState newState()
      • expectNonGreedy

        public boolean expectNonGreedy(BlockAST blkAST)
      • blockHasWildcardAlt

        public static boolean blockHasWildcardAlt(GrammarAST block)
        (BLOCK (ALT .)) or (BLOCK (ALT 'a') (ALT .)).