Package org.antlr.v4.automata
Class ParserATNFactory
java.lang.Object
org.antlr.v4.automata.ParserATNFactory
- All Implemented Interfaces:
ATNFactory
- Direct Known Subclasses:
LexerATNFactory
ATN construction routines triggered by ATNBuilder.g.
No side-effects. It builds an
ATN
object and returns it.-
Nested Class Summary
Nested classes/interfaces inherited from interface org.antlr.v4.automata.ATNFactory
ATNFactory.Handle
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
_createATN
(Collection<Rule> rules) _ruleRef
(GrammarAST node) Build what amounts to an epsilon transition with an action.int
Add an EOF transition to any rule end ATNState that points to nothing (i.e., for all those rules not invoked by another rule).void
addFollowLink
(int ruleIndex, ATNState right) void
alt
(List<ATNFactory.Handle> els) block
(BlockAST blkAST, GrammarAST ebnfRoot, List<ATNFactory.Handle> alts) FromA|B|..|Z
alternative block buildstatic boolean
blockHasWildcardAlt
(GrammarAST block) (BLOCK (ALT .))
or(BLOCK (ALT 'a') (ALT .))
.charSetLiteral
(GrammarAST charSetAST) [Aa]
char sets not allowed in parserprotected void
(package private) void
Define all the rule begin/end ATNStates to solve forward reference issues.elemList
(List<ATNFactory.Handle> els) protected void
protected void
epsilon
(GrammarAST node) From an empty alternative buildo-e->o
.boolean
expectNonGreedy
(BlockAST blkAST) protected int
getTokenType
(GrammarAST atom) lexerAltCommands
(ATNFactory.Handle alt, ATNFactory.Handle cmds) lexerCallCommand
(GrammarAST ID, GrammarAST arg) protected ATNFactory.Handle
makeBlock
(BlockStartState start, BlockAST blkAST, List<ATNFactory.Handle> alts) newState()
<T extends ATNState>
TnewState
(Class<T> nodeType, GrammarAST node) newState
(GrammarAST node) optional
(GrammarAST optAST, ATNFactory.Handle blk) From(A)?
build either:plus
(GrammarAST plusAST, ATNFactory.Handle blk) From(blk)+
buildrange
(GrammarAST a, GrammarAST b) Not valid for non-lexers.rule
(GrammarAST ruleAST, String name, ATNFactory.Handle blk) ruleRef
(GrammarAST node) For reference to ruler
, buildBuild what amounts to an epsilon transition with a semantic predicate action.set
(GrammarAST associatedAST, List<GrammarAST> terminals, boolean invert) From set build single edge grapho->o-set->o
.void
setCurrentOuterAlt
(int alt) void
setCurrentRuleName
(String name) star
(GrammarAST starAST, ATNFactory.Handle elem) From(blk)*
build( blk+ )?
with *two* decisions, one for entry and one for choosing alts ofblk
.stringLiteral
(TerminalAST stringLiteralAST) For a non-lexer, just build a simple token reference atom.tokenRef
(TerminalAST node) From labelA
build grapho-A->o
.wildcard
(GrammarAST node) Build an atom with all possible values in its label.
-
Field Details
-
g
-
atn
-
currentRule
-
currentOuterAlt
public int currentOuterAlt -
preventEpsilonClosureBlocks
-
preventEpsilonOptionalBlocks
-
-
Constructor Details
-
ParserATNFactory
-
-
Method Details
-
createATN
- Specified by:
createATN
in interfaceATNFactory
-
checkEpsilonClosure
protected void checkEpsilonClosure() -
_createATN
-
setCurrentRuleName
- Specified by:
setCurrentRuleName
in interfaceATNFactory
-
setCurrentOuterAlt
public void setCurrentOuterAlt(int alt) - Specified by:
setCurrentOuterAlt
in interfaceATNFactory
-
rule
- Specified by:
rule
in interfaceATNFactory
-
tokenRef
From labelA
build grapho-A->o
.- Specified by:
tokenRef
in interfaceATNFactory
-
set
From set build single edge grapho->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.- Specified by:
set
in interfaceATNFactory
-
range
Not valid for non-lexers.- Specified by:
range
in interfaceATNFactory
-
getTokenType
-
stringLiteral
For a non-lexer, just build a simple token reference atom.- Specified by:
stringLiteral
in interfaceATNFactory
-
charSetLiteral
[Aa]
char sets not allowed in parser- Specified by:
charSetLiteral
in interfaceATNFactory
-
ruleRef
For reference to ruler
, buildo->(r) o
where(r)
is the start of ruler
and the trailingo
is not linked to from rule ref state directly (usesRuleTransition.followState
).- Specified by:
ruleRef
in interfaceATNFactory
- Parameters:
node
-
-
_ruleRef
-
addFollowLink
-
epsilon
From an empty alternative buildo-e->o
.- Specified by:
epsilon
in interfaceATNFactory
-
sempred
Build what amounts to an epsilon transition with a semantic predicate action. Thepred
is a pointer into the AST of theANTLRParser.SEMPRED
token.- Specified by:
sempred
in interfaceATNFactory
-
action
Build what amounts to an epsilon transition with an action. The action goes into ATN though it is ignored during prediction ifactionIndex
<0
.- Specified by:
action
in interfaceATNFactory
-
action
- Specified by:
action
in interfaceATNFactory
-
block
FromA|B|..|Z
alternative block buildo->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?
- Specified by:
block
in interfaceATNFactory
-
makeBlock
protected ATNFactory.Handle makeBlock(BlockStartState start, BlockAST blkAST, List<ATNFactory.Handle> alts) -
alt
- Specified by:
alt
in interfaceATNFactory
-
elemList
-
optional
From(A)?
build either:o--A->o | ^ o---->|
or, ifA
is a block, just add an empty alt to the end of the block- Specified by:
optional
in interfaceATNFactory
-
plus
From(blk)+
build|---------| v | [o-blk-o]->o->o
We add a decision for loop back node to the existing one atblk
start.- Specified by:
plus
in interfaceATNFactory
-
star
From(blk)*
build( blk+ )?
with *two* decisions, one for entry and one for choosing alts ofblk
.|-------------| 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|)+
.- Specified by:
star
in interfaceATNFactory
-
wildcard
Build an atom with all possible values in its label.- Specified by:
wildcard
in interfaceATNFactory
-
epsilon
-
epsilon
-
createRuleStartAndStopATNStates
void createRuleStartAndStopATNStates()Define all the rule begin/end ATNStates to solve forward reference issues. -
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. -
label
- Specified by:
label
in interfaceATNFactory
-
listLabel
- Specified by:
listLabel
in interfaceATNFactory
-
newState
-
newState
-
newState
- Specified by:
newState
in interfaceATNFactory
-
expectNonGreedy
-
blockHasWildcardAlt
(BLOCK (ALT .))
or(BLOCK (ALT 'a') (ALT .))
. -
lexerAltCommands
- Specified by:
lexerAltCommands
in interfaceATNFactory
-
lexerCallCommand
- Specified by:
lexerCallCommand
in interfaceATNFactory
-
lexerCommand
- Specified by:
lexerCommand
in interfaceATNFactory
-