Package org.antlr.v4.automata
Class ParserATNFactory
- java.lang.Object
-
- org.antlr.v4.automata.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 anATN
object and returns it.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.antlr.v4.automata.ATNFactory
ATNFactory.Handle
-
-
Field Summary
Fields Modifier and Type Field Description ATN
atn
int
currentOuterAlt
Rule
currentRule
Grammar
g
protected List<Triple<Rule,ATNState,ATNState>>
preventEpsilonClosureBlocks
protected List<Triple<Rule,ATNState,ATNState>>
preventEpsilonOptionalBlocks
-
Constructor Summary
Constructors Constructor Description ParserATNFactory(Grammar g)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
_createATN(Collection<Rule> rules)
ATNFactory.Handle
_ruleRef(GrammarAST node)
ATNFactory.Handle
action(String action)
ATNFactory.Handle
action(ActionAST action)
Build what amounts to an epsilon transition with an action.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).void
addFollowLink(int ruleIndex, ATNState right)
void
addRuleFollowLinks()
ATNFactory.Handle
alt(List<ATNFactory.Handle> els)
ATNFactory.Handle
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 .))
.ATNFactory.Handle
charSetLiteral(GrammarAST charSetAST)
[Aa]
char sets not allowed in parserprotected void
checkEpsilonClosure()
ATN
createATN()
ATNFactory.Handle
elemList(List<ATNFactory.Handle> els)
protected void
epsilon(ATNState a, ATNState b)
protected void
epsilon(ATNState a, ATNState b, boolean prepend)
ATNFactory.Handle
epsilon(GrammarAST node)
From an empty alternative buildo-e->o
.boolean
expectNonGreedy(BlockAST blkAST)
protected int
getTokenType(GrammarAST atom)
ATNFactory.Handle
label(ATNFactory.Handle t)
ATNFactory.Handle
lexerAltCommands(ATNFactory.Handle alt, ATNFactory.Handle cmds)
ATNFactory.Handle
lexerCallCommand(GrammarAST ID, GrammarAST arg)
ATNFactory.Handle
lexerCommand(GrammarAST ID)
ATNFactory.Handle
listLabel(ATNFactory.Handle t)
protected ATNFactory.Handle
makeBlock(BlockStartState start, BlockAST blkAST, List<ATNFactory.Handle> alts)
ATNState
newState()
<T extends ATNState>
TnewState(Class<T> nodeType, GrammarAST node)
ATNState
newState(GrammarAST node)
ATNFactory.Handle
optional(GrammarAST optAST, ATNFactory.Handle blk)
From(A)?
build either:ATNFactory.Handle
plus(GrammarAST plusAST, ATNFactory.Handle blk)
From(blk)+
buildATNFactory.Handle
range(GrammarAST a, GrammarAST b)
Not valid for non-lexers.ATNFactory.Handle
rule(GrammarAST ruleAST, String name, ATNFactory.Handle blk)
ATNFactory.Handle
ruleRef(GrammarAST node)
For reference to ruler
, buildATNFactory.Handle
sempred(PredAST pred)
Build what amounts to an epsilon transition with a semantic predicate action.ATNFactory.Handle
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)
ATNFactory.Handle
star(GrammarAST starAST, ATNFactory.Handle elem)
From(blk)*
build( blk+ )?
with *two* decisions, one for entry and one for choosing alts ofblk
.ATNFactory.Handle
stringLiteral(TerminalAST stringLiteralAST)
For a non-lexer, just build a simple token reference atom.ATNFactory.Handle
tokenRef(TerminalAST node)
From labelA
build grapho-A->o
.ATNFactory.Handle
wildcard(GrammarAST node)
Build an atom with all possible values in its label.
-
-
-
Constructor Detail
-
ParserATNFactory
public ParserATNFactory(Grammar g)
-
-
Method Detail
-
createATN
public ATN createATN()
- Specified by:
createATN
in interfaceATNFactory
-
checkEpsilonClosure
protected void checkEpsilonClosure()
-
_createATN
protected void _createATN(Collection<Rule> rules)
-
setCurrentRuleName
public void setCurrentRuleName(String name)
- Specified by:
setCurrentRuleName
in interfaceATNFactory
-
setCurrentOuterAlt
public void setCurrentOuterAlt(int alt)
- Specified by:
setCurrentOuterAlt
in interfaceATNFactory
-
rule
public ATNFactory.Handle rule(GrammarAST ruleAST, String name, ATNFactory.Handle blk)
- Specified by:
rule
in interfaceATNFactory
-
tokenRef
public ATNFactory.Handle tokenRef(TerminalAST node)
From labelA
build grapho-A->o
.- Specified by:
tokenRef
in interfaceATNFactory
-
set
public ATNFactory.Handle set(GrammarAST associatedAST, List<GrammarAST> terminals, boolean invert)
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
public ATNFactory.Handle range(GrammarAST a, GrammarAST b)
Not valid for non-lexers.- Specified by:
range
in interfaceATNFactory
-
getTokenType
protected int getTokenType(GrammarAST atom)
-
stringLiteral
public ATNFactory.Handle stringLiteral(TerminalAST stringLiteralAST)
For a non-lexer, just build a simple token reference atom.- Specified by:
stringLiteral
in interfaceATNFactory
-
charSetLiteral
public ATNFactory.Handle charSetLiteral(GrammarAST charSetAST)
[Aa]
char sets not allowed in parser- Specified by:
charSetLiteral
in interfaceATNFactory
-
ruleRef
public ATNFactory.Handle ruleRef(GrammarAST node)
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
-
_ruleRef
public ATNFactory.Handle _ruleRef(GrammarAST node)
-
addFollowLink
public void addFollowLink(int ruleIndex, ATNState right)
-
epsilon
public ATNFactory.Handle epsilon(GrammarAST node)
From an empty alternative buildo-e->o
.- Specified by:
epsilon
in interfaceATNFactory
-
sempred
public ATNFactory.Handle sempred(PredAST pred)
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
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 ifactionIndex
<0
.- Specified by:
action
in interfaceATNFactory
-
action
public ATNFactory.Handle action(String action)
- Specified by:
action
in interfaceATNFactory
-
block
public ATNFactory.Handle block(BlockAST blkAST, GrammarAST ebnfRoot, List<ATNFactory.Handle> alts)
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
public ATNFactory.Handle alt(List<ATNFactory.Handle> els)
- Specified by:
alt
in interfaceATNFactory
-
elemList
public ATNFactory.Handle elemList(List<ATNFactory.Handle> els)
-
optional
public ATNFactory.Handle optional(GrammarAST optAST, ATNFactory.Handle blk)
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
public ATNFactory.Handle plus(GrammarAST plusAST, ATNFactory.Handle blk)
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
public ATNFactory.Handle star(GrammarAST starAST, ATNFactory.Handle elem)
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
public ATNFactory.Handle wildcard(GrammarAST node)
Build an atom with all possible values in its label.- Specified by:
wildcard
in interfaceATNFactory
-
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
public ATNFactory.Handle label(ATNFactory.Handle t)
- Specified by:
label
in interfaceATNFactory
-
listLabel
public ATNFactory.Handle listLabel(ATNFactory.Handle t)
- Specified by:
listLabel
in interfaceATNFactory
-
newState
public <T extends ATNState> T newState(Class<T> nodeType, GrammarAST node)
-
newState
public ATNState newState(GrammarAST node)
-
newState
public ATNState newState()
- Specified by:
newState
in interfaceATNFactory
-
expectNonGreedy
public boolean expectNonGreedy(BlockAST blkAST)
-
blockHasWildcardAlt
public static boolean blockHasWildcardAlt(GrammarAST block)
(BLOCK (ALT .))
or(BLOCK (ALT 'a') (ALT .))
.
-
lexerAltCommands
public ATNFactory.Handle lexerAltCommands(ATNFactory.Handle alt, ATNFactory.Handle cmds)
- Specified by:
lexerAltCommands
in interfaceATNFactory
-
lexerCallCommand
public ATNFactory.Handle lexerCallCommand(GrammarAST ID, GrammarAST arg)
- Specified by:
lexerCallCommand
in interfaceATNFactory
-
lexerCommand
public ATNFactory.Handle lexerCommand(GrammarAST ID)
- Specified by:
lexerCommand
in interfaceATNFactory
-
-