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 anATNobject 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 ATNatnintcurrentOuterAltRulecurrentRuleGrammargprotected List<Triple<Rule,ATNState,ATNState>>preventEpsilonClosureBlocksprotected 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.Handleaction(String action)ATNFactory.Handleaction(ActionAST action)Build what amounts to an epsilon transition with an action.intaddEOFTransitionToStartRules()Add an EOF transition to any rule end ATNState that points to nothing (i.e., for all those rules not invoked by another rule).voidaddFollowLink(int ruleIndex, ATNState right)voidaddRuleFollowLinks()ATNFactory.Handlealt(List<ATNFactory.Handle> els)ATNFactory.Handleblock(BlockAST blkAST, GrammarAST ebnfRoot, List<ATNFactory.Handle> alts)FromA|B|..|Zalternative block buildstatic booleanblockHasWildcardAlt(GrammarAST block)(BLOCK (ALT .))or(BLOCK (ALT 'a') (ALT .)).ATNFactory.HandlecharSetLiteral(GrammarAST charSetAST)[Aa]char sets not allowed in parserprotected voidcheckEpsilonClosure()ATNcreateATN()ATNFactory.HandleelemList(List<ATNFactory.Handle> els)protected voidepsilon(ATNState a, ATNState b)protected voidepsilon(ATNState a, ATNState b, boolean prepend)ATNFactory.Handleepsilon(GrammarAST node)From an empty alternative buildo-e->o.booleanexpectNonGreedy(BlockAST blkAST)protected intgetTokenType(GrammarAST atom)ATNFactory.Handlelabel(ATNFactory.Handle t)ATNFactory.HandlelexerAltCommands(ATNFactory.Handle alt, ATNFactory.Handle cmds)ATNFactory.HandlelexerCallCommand(GrammarAST ID, GrammarAST arg)ATNFactory.HandlelexerCommand(GrammarAST ID)ATNFactory.HandlelistLabel(ATNFactory.Handle t)protected ATNFactory.HandlemakeBlock(BlockStartState start, BlockAST blkAST, List<ATNFactory.Handle> alts)ATNStatenewState()<T extends ATNState>
TnewState(Class<T> nodeType, GrammarAST node)ATNStatenewState(GrammarAST node)ATNFactory.Handleoptional(GrammarAST optAST, ATNFactory.Handle blk)From(A)?build either:ATNFactory.Handleplus(GrammarAST plusAST, ATNFactory.Handle blk)From(blk)+buildATNFactory.Handlerange(GrammarAST a, GrammarAST b)Not valid for non-lexers.ATNFactory.Handlerule(GrammarAST ruleAST, String name, ATNFactory.Handle blk)ATNFactory.HandleruleRef(GrammarAST node)For reference to ruler, buildATNFactory.Handlesempred(PredAST pred)Build what amounts to an epsilon transition with a semantic predicate action.ATNFactory.Handleset(GrammarAST associatedAST, List<GrammarAST> terminals, boolean invert)From set build single edge grapho->o-set->o.voidsetCurrentOuterAlt(int alt)voidsetCurrentRuleName(String name)ATNFactory.Handlestar(GrammarAST starAST, ATNFactory.Handle elem)From(blk)*build( blk+ )?with *two* decisions, one for entry and one for choosing alts ofblk.ATNFactory.HandlestringLiteral(TerminalAST stringLiteralAST)For a non-lexer, just build a simple token reference atom.ATNFactory.HandletokenRef(TerminalAST node)From labelAbuild grapho-A->o.ATNFactory.Handlewildcard(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:
createATNin interfaceATNFactory
-
checkEpsilonClosure
protected void checkEpsilonClosure()
-
_createATN
protected void _createATN(Collection<Rule> rules)
-
setCurrentRuleName
public void setCurrentRuleName(String name)
- Specified by:
setCurrentRuleNamein interfaceATNFactory
-
setCurrentOuterAlt
public void setCurrentOuterAlt(int alt)
- Specified by:
setCurrentOuterAltin interfaceATNFactory
-
rule
public ATNFactory.Handle rule(GrammarAST ruleAST, String name, ATNFactory.Handle blk)
- Specified by:
rulein interfaceATNFactory
-
tokenRef
public ATNFactory.Handle tokenRef(TerminalAST node)
From labelAbuild grapho-A->o.- Specified by:
tokenRefin 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:
setin interfaceATNFactory
-
range
public ATNFactory.Handle range(GrammarAST a, GrammarAST b)
Not valid for non-lexers.- Specified by:
rangein 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:
stringLiteralin interfaceATNFactory
-
charSetLiteral
public ATNFactory.Handle charSetLiteral(GrammarAST charSetAST)
[Aa]char sets not allowed in parser- Specified by:
charSetLiteralin interfaceATNFactory
-
ruleRef
public ATNFactory.Handle ruleRef(GrammarAST node)
For reference to ruler, buildo->(r) o
where(r)is the start of rulerand the trailingois not linked to from rule ref state directly (usesRuleTransition.followState).- Specified by:
ruleRefin 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:
epsilonin interfaceATNFactory
-
sempred
public ATNFactory.Handle sempred(PredAST pred)
Build what amounts to an epsilon transition with a semantic predicate action. Thepredis a pointer into the AST of theANTLRParser.SEMPREDtoken.- Specified by:
sempredin 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:
actionin interfaceATNFactory
-
action
public ATNFactory.Handle action(String action)
- Specified by:
actionin interfaceATNFactory
-
block
public ATNFactory.Handle block(BlockAST blkAST, GrammarAST ebnfRoot, List<ATNFactory.Handle> alts)
FromA|B|..|Zalternative 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:
blockin 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:
altin 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, ifAis a block, just add an empty alt to the end of the block- Specified by:
optionalin 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 atblkstart.- Specified by:
plusin 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:
starin interfaceATNFactory
-
wildcard
public ATNFactory.Handle wildcard(GrammarAST node)
Build an atom with all possible values in its label.- Specified by:
wildcardin 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:
labelin interfaceATNFactory
-
listLabel
public ATNFactory.Handle listLabel(ATNFactory.Handle t)
- Specified by:
listLabelin 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:
newStatein 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:
lexerAltCommandsin interfaceATNFactory
-
lexerCallCommand
public ATNFactory.Handle lexerCallCommand(GrammarAST ID, GrammarAST arg)
- Specified by:
lexerCallCommandin interfaceATNFactory
-
lexerCommand
public ATNFactory.Handle lexerCommand(GrammarAST ID)
- Specified by:
lexerCommandin interfaceATNFactory
-
-