forked from jasder/antlr
Ported ATNStates
This commit is contained in:
parent
e96e55e66d
commit
5ef465e8e2
|
@ -168,13 +168,13 @@ func (bt *BufferedTokenStream) fetch(n int) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get all tokens from start..stop inclusively///
|
// Get all tokens from start..stop inclusively///
|
||||||
func (bt *BufferedTokenStream) getTokens(start int, stop int, types []int) []Token {
|
func (bt *BufferedTokenStream) getTokens(start int, stop int, types []int) []*Token {
|
||||||
|
|
||||||
if (start < 0 || stop < 0) {
|
if (start < 0 || stop < 0) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
bt.lazyInit()
|
bt.lazyInit()
|
||||||
var subset = []
|
var subset = (make[]*Token)
|
||||||
if (stop >= len(bt.tokens)) {
|
if (stop >= len(bt.tokens)) {
|
||||||
stop = len(bt.tokens) - 1
|
stop = len(bt.tokens) - 1
|
||||||
}
|
}
|
||||||
|
@ -295,7 +295,12 @@ func (bt *BufferedTokenStream) getHiddenTokensToRight(tokenIndex, channel int) {
|
||||||
var nextOnChannel = bt.nextTokenOnChannel(tokenIndex + 1, LexerDefaultTokenChannel)
|
var nextOnChannel = bt.nextTokenOnChannel(tokenIndex + 1, LexerDefaultTokenChannel)
|
||||||
var from_ = tokenIndex + 1
|
var from_ = tokenIndex + 1
|
||||||
// if none onchannel to right, nextOnChannel=-1 so set to = last token
|
// if none onchannel to right, nextOnChannel=-1 so set to = last token
|
||||||
var to = nextOnChannel == -1 ? len(bt.tokens) - 1 : nextOnChannel
|
var to int
|
||||||
|
if (nextOnChannel == -1){
|
||||||
|
to = len(bt.tokens) - 1
|
||||||
|
} else {
|
||||||
|
to = nextOnChannel
|
||||||
|
}
|
||||||
return bt.filterForChannel(from_, to, channel)
|
return bt.filterForChannel(from_, to, channel)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -305,7 +310,7 @@ func (bt *BufferedTokenStream) getHiddenTokensToRight(tokenIndex, channel int) {
|
||||||
func (bt *BufferedTokenStream) getHiddenTokensToLeft(tokenIndex, channel int) {
|
func (bt *BufferedTokenStream) getHiddenTokensToLeft(tokenIndex, channel int) {
|
||||||
bt.lazyInit()
|
bt.lazyInit()
|
||||||
if (tokenIndex < 0 || tokenIndex >= len(bt.tokens)) {
|
if (tokenIndex < 0 || tokenIndex >= len(bt.tokens)) {
|
||||||
panic( "" + tokenIndex + " not in 0.." + len(bt.tokens) - 1
|
panic( "" + tokenIndex + " not in 0.." + len(bt.tokens) - 1 )
|
||||||
}
|
}
|
||||||
var prevOnChannel = bt.previousTokenOnChannel(tokenIndex - 1, LexerDefaultTokenChannel)
|
var prevOnChannel = bt.previousTokenOnChannel(tokenIndex - 1, LexerDefaultTokenChannel)
|
||||||
if (prevOnChannel == tokenIndex - 1) {
|
if (prevOnChannel == tokenIndex - 1) {
|
||||||
|
|
|
@ -30,21 +30,20 @@ const (
|
||||||
//
|
//
|
||||||
// @param s the ATN state
|
// @param s the ATN state
|
||||||
// @return the expected symbols for each outgoing transition of {@code s}.
|
// @return the expected symbols for each outgoing transition of {@code s}.
|
||||||
///
|
func (la *LL1Analyzer) getDecisionLookahead(s *atn.ATNState) []*IntervalSet {
|
||||||
func (la *LL1Analyzer) getDecisionLookahead(s) {
|
|
||||||
if (s == nil) {
|
if (s == nil) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var count = len(s.transitions)
|
var count = len(s.transitions)
|
||||||
var look = []
|
var look = make([]*IntervalSet)
|
||||||
for alt := 0; alt < count; alt++ {
|
for alt := 0; alt < count; alt++ {
|
||||||
look[alt] = NewIntervalSet()
|
look[alt] = NewIntervalSet()
|
||||||
var lookBusy = NewSet()
|
var lookBusy = NewSet(nil,nil)
|
||||||
var seeThruPreds = false // fail to get lookahead upon pred
|
var seeThruPreds = false // fail to get lookahead upon pred
|
||||||
la._LOOK(s.transition(alt).target, nil, PredictionContext.EMPTY, look[alt], lookBusy, NewBitSet(), seeThruPreds, false)
|
la._LOOK(s.transitions(alt).target, nil, PredictionContextEMPTY, look[alt], lookBusy, NewBitSet(), seeThruPreds, false)
|
||||||
// Wipe out lookahead for la alternative if we found nothing
|
// Wipe out lookahead for la alternative if we found nothing
|
||||||
// or we had a predicate when we !seeThruPreds
|
// or we had a predicate when we !seeThruPreds
|
||||||
if (look[alt].length==0 || look[alt].contains(LL1Analyzer.HIT_PRED)) {
|
if (look[alt].length==0 || look[alt].contains(LL1AnalyzerHIT_PRED)) {
|
||||||
look[alt] = nil
|
look[alt] = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -101,7 +101,7 @@ func (p *Parser) reset() {
|
||||||
p._precedenceStack = make([]int, 0)
|
p._precedenceStack = make([]int, 0)
|
||||||
p._precedenceStack.Push(0)
|
p._precedenceStack.Push(0)
|
||||||
if (p._interp != nil) {
|
if (p._interp != nil) {
|
||||||
p._interp.reset()
|
*p._interp.reset()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,7 +154,7 @@ func (p *Parser) match(ttype int) *Token {
|
||||||
// a wildcard and the error strategy could not recover from the mismatched
|
// a wildcard and the error strategy could not recover from the mismatched
|
||||||
// symbol
|
// symbol
|
||||||
|
|
||||||
func (p *Parser) matchWildcard() {
|
func (p *Parser) matchWildcard() *Token {
|
||||||
var t = p.getCurrentToken()
|
var t = p.getCurrentToken()
|
||||||
if (t.tokenType > 0) {
|
if (t.tokenType > 0) {
|
||||||
p._errHandler.reportMatch(p)
|
p._errHandler.reportMatch(p)
|
||||||
|
@ -171,8 +171,11 @@ func (p *Parser) matchWildcard() {
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) getParseListeners() {
|
func (p *Parser) getParseListeners() []*tree.ParseTreeListener {
|
||||||
return p._parseListeners || []
|
if (p._parseListeners == nil){
|
||||||
|
return make([]*tree.ParseTreeListener)
|
||||||
|
}
|
||||||
|
return p._parseListeners
|
||||||
}
|
}
|
||||||
|
|
||||||
// Registers {@code listener} to receive events during the parsing process.
|
// Registers {@code listener} to receive events during the parsing process.
|
||||||
|
@ -392,19 +395,19 @@ func (p *Parser) consume() {
|
||||||
if (o.tokenType != TokenEOF) {
|
if (o.tokenType != TokenEOF) {
|
||||||
p.getInputStream().consume()
|
p.getInputStream().consume()
|
||||||
}
|
}
|
||||||
var hasListener = p._parseListeners != nil && p._parseListeners.length > 0
|
var hasListener = p._parseListeners != nil && len(p._parseListeners) > 0
|
||||||
if (p.buildParseTrees || hasListener) {
|
if (p.buildParseTrees || hasListener) {
|
||||||
var node
|
var node *tree.ErrorNodeImpl
|
||||||
if (p._errHandler.inErrorRecoveryMode(p.) {
|
if (p._errHandler.inErrorRecoveryMode(p)) {
|
||||||
node = p._ctx.addErrorNode(o)
|
node = p._ctx.addErrorNode(o)
|
||||||
} else {
|
} else {
|
||||||
node = p._ctx.addTokenNode(o)
|
node = p._ctx.addTokenNode(o)
|
||||||
}
|
}
|
||||||
node.invokingState = p.state
|
node.invokingState = p.state
|
||||||
if (hasListener) {
|
if (hasListener) {
|
||||||
p._parseListeners.map(function(listener) {
|
for _, l := range p._parseListeners {
|
||||||
listener.visitTerminal(node)
|
l.visitTerminal(node)
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return o
|
return o
|
||||||
|
@ -420,7 +423,7 @@ func (p *Parser) addContextToParseTree() {
|
||||||
// Always called by generated parsers upon entry to a rule. Access field
|
// Always called by generated parsers upon entry to a rule. Access field
|
||||||
// {@link //_ctx} get the current context.
|
// {@link //_ctx} get the current context.
|
||||||
|
|
||||||
func (p *Parser) enterRule(localctx, state, ruleIndex int) {
|
func (p *Parser) enterRule(localctx *ParserRuleContext, state, ruleIndex int) {
|
||||||
p.state = state
|
p.state = state
|
||||||
p._ctx = localctx
|
p._ctx = localctx
|
||||||
p._ctx.start = p._input.LT(1)
|
p._ctx.start = p._input.LT(1)
|
||||||
|
@ -442,7 +445,7 @@ func (p *Parser) exitRule() {
|
||||||
p._ctx = p._ctx.parentCtx
|
p._ctx = p._ctx.parentCtx
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) enterOuterAlt(localctx, altNum) {
|
func (p *Parser) enterOuterAlt(localctx *ParserRuleContext, altNum int) {
|
||||||
// if we have Newlocalctx, make sure we replace existing ctx
|
// if we have Newlocalctx, make sure we replace existing ctx
|
||||||
// that is previous child of parse tree
|
// that is previous child of parse tree
|
||||||
if (p.buildParseTrees && p._ctx != localctx) {
|
if (p.buildParseTrees && p._ctx != localctx) {
|
||||||
|
@ -459,17 +462,17 @@ func (p *Parser) enterOuterAlt(localctx, altNum) {
|
||||||
// @return The precedence level for the top-most precedence rule, or -1 if
|
// @return The precedence level for the top-most precedence rule, or -1 if
|
||||||
// the parser context is not nested within a precedence rule.
|
// the parser context is not nested within a precedence rule.
|
||||||
|
|
||||||
func (p *Parser) getPrecedence() {
|
func (p *Parser) getPrecedence() int {
|
||||||
if (p._precedenceStack.length == 0) {
|
if ( len(p._precedenceStack) == 0) {
|
||||||
return -1
|
return -1
|
||||||
} else {
|
} else {
|
||||||
return p._precedenceStack[p._precedenceStack.length-1]
|
return p._precedenceStack[ len(p._precedenceStack) -1]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) enterRecursionRule(localctx, state, ruleIndex, precedence) {
|
func (p *Parser) enterRecursionRule(localctx *ParserRuleContext, state, ruleIndex, precedence int) {
|
||||||
p.state = state
|
p.state = state
|
||||||
p._precedenceStack.push(precedence)
|
p._precedenceStack.Push(precedence)
|
||||||
p._ctx = localctx
|
p._ctx = localctx
|
||||||
p._ctx.start = p._input.LT(1)
|
p._ctx.start = p._input.LT(1)
|
||||||
if (p._parseListeners != nil) {
|
if (p._parseListeners != nil) {
|
||||||
|
@ -481,7 +484,7 @@ func (p *Parser) enterRecursionRule(localctx, state, ruleIndex, precedence) {
|
||||||
//
|
//
|
||||||
// Like {@link //enterRule} but for recursive rules.
|
// Like {@link //enterRule} but for recursive rules.
|
||||||
|
|
||||||
func (p *Parser) pushNewRecursionContext(localctx, state, ruleIndex) {
|
func (p *Parser) pushNewRecursionContext(localctx *ParserRuleContext, state, ruleIndex int) {
|
||||||
var previous = p._ctx
|
var previous = p._ctx
|
||||||
previous.parentCtx = localctx
|
previous.parentCtx = localctx
|
||||||
previous.invokingState = state
|
previous.invokingState = state
|
||||||
|
@ -498,8 +501,8 @@ func (p *Parser) pushNewRecursionContext(localctx, state, ruleIndex) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) unrollRecursionContexts(parentCtx) {
|
func (p *Parser) unrollRecursionContexts(parentCtx *ParserRuleContext) {
|
||||||
p._precedenceStack.pop()
|
p._precedenceStack.Pop()
|
||||||
p._ctx.stop = p._input.LT(-1)
|
p._ctx.stop = p._input.LT(-1)
|
||||||
var retCtx = p._ctx // save current ctx (return value)
|
var retCtx = p._ctx // save current ctx (return value)
|
||||||
// unroll so _ctx is as it was before call to recursive method
|
// unroll so _ctx is as it was before call to recursive method
|
||||||
|
@ -519,7 +522,7 @@ func (p *Parser) unrollRecursionContexts(parentCtx) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) getInvokingContext(ruleIndex) {
|
func (p *Parser) getInvokingContext(ruleIndex int) *ParserRuleContext {
|
||||||
var ctx = p._ctx
|
var ctx = p._ctx
|
||||||
for (ctx != nil) {
|
for (ctx != nil) {
|
||||||
if (ctx.ruleIndex == ruleIndex) {
|
if (ctx.ruleIndex == ruleIndex) {
|
||||||
|
@ -530,11 +533,11 @@ func (p *Parser) getInvokingContext(ruleIndex) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) precpred(localctx, precedence) {
|
func (p *Parser) precpred(localctx, precedence int) {
|
||||||
return precedence >= p._precedenceStack[p._precedenceStack.length-1]
|
return precedence >= p._precedenceStack[ len(p._precedenceStack) -1]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) inContext(context) {
|
func (p *Parser) inContext(context *ParserRuleContext) bool {
|
||||||
// TODO: useful in parser?
|
// TODO: useful in parser?
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -553,8 +556,8 @@ func (p *Parser) inContext(context) {
|
||||||
// @return {@code true} if {@code symbol} can follow the current state in
|
// @return {@code true} if {@code symbol} can follow the current state in
|
||||||
// the ATN, otherwise {@code false}.
|
// the ATN, otherwise {@code false}.
|
||||||
|
|
||||||
func (p *Parser) isExpectedToken(symbol) {
|
func (p *Parser) isExpectedToken(symbol *Token) bool {
|
||||||
var atn = p._interp.atn
|
var atn *atn.ATN = p._interp.atn
|
||||||
var ctx = p._ctx
|
var ctx = p._ctx
|
||||||
var s = atn.states[p.state]
|
var s = atn.states[p.state]
|
||||||
var following = atn.nextTokens(s)
|
var following = atn.nextTokens(s)
|
||||||
|
@ -586,11 +589,11 @@ func (p *Parser) isExpectedToken(symbol) {
|
||||||
//
|
//
|
||||||
// @see ATN//getExpectedTokens(int, RuleContext)
|
// @see ATN//getExpectedTokens(int, RuleContext)
|
||||||
//
|
//
|
||||||
func (p *Parser) getExpectedTokens() {
|
func (p *Parser) getExpectedTokens() []*Token {
|
||||||
return p._interp.atn.getExpectedTokens(p.state, p._ctx)
|
return p._interp.atn.getExpectedTokens(p.state, p._ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) getExpectedTokensWithinCurrentRule() {
|
func (p *Parser) getExpectedTokensWithinCurrentRule() []*Token {
|
||||||
var atn = p._interp.atn
|
var atn = p._interp.atn
|
||||||
var s = atn.states[p.state]
|
var s = atn.states[p.state]
|
||||||
return atn.nextTokens(s)
|
return atn.nextTokens(s)
|
||||||
|
@ -611,31 +614,31 @@ func (p *Parser) getRuleIndex(ruleName string) int {
|
||||||
// you want more details such as the file/line info of where
|
// you want more details such as the file/line info of where
|
||||||
// in the ATN a rule is invoked.
|
// in the ATN a rule is invoked.
|
||||||
//
|
//
|
||||||
// p.is very useful for error messages.
|
// this very useful for error messages.
|
||||||
//
|
|
||||||
func (p *Parser) getRuleInvocationStack(p) {
|
func (this *Parser) getRuleInvocationStack(p *ParserRuleContext) []string {
|
||||||
p = p || nil
|
|
||||||
if (p == nil) {
|
if (p == nil) {
|
||||||
p = p._ctx
|
p = this._ctx;
|
||||||
}
|
}
|
||||||
var stack = []
|
var stack = make([]string)
|
||||||
for (p != nil) {
|
for (p != nil) {
|
||||||
// compute what follows who invoked us
|
// compute what follows who invoked us
|
||||||
var ruleIndex = p.ruleIndex
|
var ruleIndex = p.ruleIndex;
|
||||||
if (ruleIndex < 0) {
|
if (ruleIndex < 0) {
|
||||||
stack.push("n/a")
|
stack = append(stack, "n/a")
|
||||||
} else {
|
} else {
|
||||||
stack.push(p.ruleNames[ruleIndex])
|
stack = append(stack, this.getRuleNames()[ruleIndex]);
|
||||||
}
|
}
|
||||||
p = p.parentCtx
|
p = p.parentCtx;
|
||||||
}
|
}
|
||||||
return stack
|
return stack;
|
||||||
}
|
};
|
||||||
|
|
||||||
// For debugging and other purposes.//
|
// For debugging and other purposes.//
|
||||||
func (p *Parser) getDFAStrings() {
|
func (p *Parser) getDFAStrings() {
|
||||||
return p._interp.decisionToDFA.toString()
|
return p._interp.decisionToDFA.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
// For debugging and other purposes.//
|
// For debugging and other purposes.//
|
||||||
func (p *Parser) dumpDFA() {
|
func (p *Parser) dumpDFA() {
|
||||||
var seenOne = false
|
var seenOne = false
|
||||||
|
|
|
@ -32,6 +32,7 @@ import (
|
||||||
|
|
||||||
type ParserRuleContext struct {
|
type ParserRuleContext struct {
|
||||||
RuleContext
|
RuleContext
|
||||||
|
|
||||||
ruleIndex int
|
ruleIndex int
|
||||||
children []RuleContext
|
children []RuleContext
|
||||||
start, stop *Token
|
start, stop *Token
|
||||||
|
@ -225,8 +226,7 @@ type InterpreterRuleContext struct {
|
||||||
func NewInterpreterRuleContext(parent *InterpreterRuleContext, invokingStateNumber, ruleIndex int) {
|
func NewInterpreterRuleContext(parent *InterpreterRuleContext, invokingStateNumber, ruleIndex int) {
|
||||||
|
|
||||||
prc := new(InterpreterRuleContext)
|
prc := new(InterpreterRuleContext)
|
||||||
|
prc.initParserRuleContext( parent, invokingStateNumber )
|
||||||
prc.init(parent, invokingStateNumber)
|
|
||||||
|
|
||||||
prc.ruleIndex = ruleIndex
|
prc.ruleIndex = ruleIndex
|
||||||
|
|
||||||
|
|
|
@ -36,8 +36,6 @@ type RuleContext struct {
|
||||||
func NewRuleContext(parent *RuleContext, invokingState int) *RuleContext {
|
func NewRuleContext(parent *RuleContext, invokingState int) *RuleContext {
|
||||||
|
|
||||||
rn := &RuleContext{tree.RuleNode{}}
|
rn := &RuleContext{tree.RuleNode{}}
|
||||||
|
|
||||||
rn.initRuleNode()
|
|
||||||
rn.initRuleContext(parent, invokingState)
|
rn.initRuleContext(parent, invokingState)
|
||||||
|
|
||||||
return rn
|
return rn
|
||||||
|
|
|
@ -35,6 +35,8 @@ func arrayToString(a []interface{}) string{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func hashCode(s string) int {
|
func hashCode(s string) int {
|
||||||
h := fnv.New32a()
|
h := fnv.New32a()
|
||||||
h.Write([]byte((s)))
|
h.Write([]byte((s)))
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
package atn
|
package atn
|
||||||
|
|
||||||
//var DFAState = require('./../dfa/DFAState').DFAState
|
import (
|
||||||
//var ATNConfigSet = require('./ATNConfigSet').ATNConfigSet
|
"antlr4"
|
||||||
//var getCachedPredictionContext = require('./../PredictionContext').getCachedPredictionContext
|
"antlr4/dfa"
|
||||||
|
)
|
||||||
|
|
||||||
type ATNSimulator struct {
|
type ATNSimulator struct {
|
||||||
atn *ATN
|
atn *ATN
|
||||||
sharedContextCache
|
sharedContextCache *antlr4.PredictionContextCache
|
||||||
}
|
}
|
||||||
|
|
||||||
func ATNSimulator(atn, sharedContextCache) {
|
func ATNSimulator(atn *ATN, sharedContextCache *antlr4.PredictionContextCache) *ATNSimulator {
|
||||||
|
|
||||||
// The context cache maps all PredictionContext objects that are ==
|
// The context cache maps all PredictionContext objects that are ==
|
||||||
// to a single cached copy. This cache is shared across all contexts
|
// to a single cached copy. This cache is shared across all contexts
|
||||||
|
@ -30,21 +31,23 @@ func ATNSimulator(atn, sharedContextCache) {
|
||||||
// whacked after each adaptivePredict(). It cost a little bit
|
// whacked after each adaptivePredict(). It cost a little bit
|
||||||
// more time I think and doesn't save on the overall footprint
|
// more time I think and doesn't save on the overall footprint
|
||||||
// so it's not worth the complexity.</p>
|
// so it's not worth the complexity.</p>
|
||||||
///
|
|
||||||
|
this := new(ATNSimulator)
|
||||||
|
|
||||||
this.atn = atn
|
this.atn = atn
|
||||||
this.sharedContextCache = sharedContextCache
|
this.sharedContextCache = sharedContextCache
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
// Must distinguish between missing edge and edge we know leads nowhere///
|
// Must distinguish between missing edge and edge we know leads nowhere///
|
||||||
ATNSimulator.ERROR = NewDFAState(0x7FFFFFFF, NewATNConfigSet())
|
var ATNSimulatorERROR = dfa.NewDFAState(0x7FFFFFFF, NewATNConfigSet())
|
||||||
|
|
||||||
|
func (this *ATNSimulator) getCachedContext(context *antlr4.PredictionContext) *antlr4.PredictionContext {
|
||||||
func (this *ATNSimulator) getCachedContext(context) {
|
if (this.sharedContextCache == nil) {
|
||||||
if (this.sharedContextCache ==nil) {
|
|
||||||
return context
|
return context
|
||||||
}
|
}
|
||||||
var visited = {}
|
var visited = make(map[*antlr4.PredictionContext]*antlr4.PredictionContext)
|
||||||
return getCachedPredictionContext(context, this.sharedContextCache, visited)
|
return getCachedPredictionContext(context, this.sharedContextCache, visited)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
package atn
|
package atn
|
||||||
|
import "antlr4"
|
||||||
|
|
||||||
// The following images show the relation of states and
|
// The following images show the relation of states and
|
||||||
// {@link ATNState//transitions} for various grammar constructs.
|
// {@link ATNState//transitions} for various grammar constructs.
|
||||||
|
@ -70,14 +71,20 @@ type ATNState struct {
|
||||||
ruleIndex int
|
ruleIndex int
|
||||||
epsilonOnlyTransitions bool
|
epsilonOnlyTransitions bool
|
||||||
// Track the transitions emanating from this ATN state.
|
// Track the transitions emanating from this ATN state.
|
||||||
transitions []Transition
|
transitions []*Transition
|
||||||
// Used to cache lookahead during parsing, not used during construction
|
// Used to cache lookahead during parsing, not used during construction
|
||||||
nextTokenWithinRule *Token
|
nextTokenWithinRule *antlr4.Token
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewATNState() *ATNState {
|
func NewATNState() *ATNState {
|
||||||
|
|
||||||
as := new(ATNState)
|
as := new(ATNState)
|
||||||
|
as.initATNState()
|
||||||
|
|
||||||
|
return as
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *ATNState) initATNState(){
|
||||||
|
|
||||||
// Which ATN are we in?
|
// Which ATN are we in?
|
||||||
as.atn = nil
|
as.atn = nil
|
||||||
|
@ -90,7 +97,6 @@ func NewATNState() *ATNState {
|
||||||
// Used to cache lookahead during parsing, not used during construction
|
// Used to cache lookahead during parsing, not used during construction
|
||||||
as.nextTokenWithinRule = nil
|
as.nextTokenWithinRule = nil
|
||||||
|
|
||||||
return as
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -111,21 +117,21 @@ const (
|
||||||
|
|
||||||
ATNStateINVALID_STATE_NUMBER = -1
|
ATNStateINVALID_STATE_NUMBER = -1
|
||||||
)
|
)
|
||||||
|
//
|
||||||
var ATNState.serializationNames = [
|
//var ATNState.serializationNames = [
|
||||||
"INVALID",
|
// "INVALID",
|
||||||
"BASIC",
|
// "BASIC",
|
||||||
"RULE_START",
|
// "RULE_START",
|
||||||
"BLOCK_START",
|
// "BLOCK_START",
|
||||||
"PLUS_BLOCK_START",
|
// "PLUS_BLOCK_START",
|
||||||
"STAR_BLOCK_START",
|
// "STAR_BLOCK_START",
|
||||||
"TOKEN_START",
|
// "TOKEN_START",
|
||||||
"RULE_STOP",
|
// "RULE_STOP",
|
||||||
"BLOCK_END",
|
// "BLOCK_END",
|
||||||
"STAR_LOOP_BACK",
|
// "STAR_LOOP_BACK",
|
||||||
"STAR_LOOP_ENTRY",
|
// "STAR_LOOP_ENTRY",
|
||||||
"PLUS_LOOP_BACK",
|
// "PLUS_LOOP_BACK",
|
||||||
"LOOP_END" ]
|
// "LOOP_END" ]
|
||||||
|
|
||||||
func (this *ATNState) toString() string {
|
func (this *ATNState) toString() string {
|
||||||
return this.stateNumber
|
return this.stateNumber
|
||||||
|
@ -143,71 +149,109 @@ func (this *ATNState) isNonGreedyExitState() {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *ATNState) addTransition(trans *Transition, index int) {
|
||||||
func (this *ATNState) addTransition(trans int, index int) {
|
|
||||||
if ( len(this.transitions) == 0 ) {
|
if ( len(this.transitions) == 0 ) {
|
||||||
this.epsilonOnlyTransitions = trans.isEpsilon
|
this.epsilonOnlyTransitions = trans.isEpsilon
|
||||||
} else if(this.epsilonOnlyTransitions != trans.isEpsilon) {
|
} else if(this.epsilonOnlyTransitions != trans.isEpsilon) {
|
||||||
this.epsilonOnlyTransitions = false
|
this.epsilonOnlyTransitions = false
|
||||||
}
|
}
|
||||||
if (index==-1) {
|
if (index==-1) {
|
||||||
this.transitions.push(trans)
|
this.transitions = append(this.transitions, trans)
|
||||||
} else {
|
} else {
|
||||||
this.transitions.splice(index, 1, trans)
|
this.transitions.splice(index, 1, trans)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type BasicState struct {
|
type BasicState struct {
|
||||||
|
ATNState
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBasicState() *BasicState {
|
func NewBasicState() *BasicState {
|
||||||
ATNState.call(this)
|
this := new(BasicState)
|
||||||
|
this.initATNState()
|
||||||
|
|
||||||
this.stateType = ATNStateBASIC
|
this.stateType = ATNStateBASIC
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
type DecisionState struct {
|
type DecisionState struct {
|
||||||
|
ATNState
|
||||||
|
|
||||||
|
decision int
|
||||||
|
nonGreedy bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDecisionState() *DecisionState {
|
func NewDecisionState() *DecisionState {
|
||||||
ATNState.call(this)
|
|
||||||
|
this := new(DecisionState)
|
||||||
|
|
||||||
|
this.initATNState()
|
||||||
|
this.initDecisionState()
|
||||||
|
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *DecisionState) initDecisionState() {
|
||||||
|
|
||||||
this.decision = -1
|
this.decision = -1
|
||||||
this.nonGreedy = false
|
this.nonGreedy = false
|
||||||
return this
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// The start of a regular {@code (...)} block.
|
// The start of a regular {@code (...)} block.
|
||||||
type BlockStartState struct {
|
type BlockStartState struct {
|
||||||
|
DecisionState
|
||||||
|
|
||||||
|
endState *ATNState
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBlockStartState() *BlockStartState {
|
func NewBlockStartState() *BlockStartState {
|
||||||
DecisionState.call(this)
|
|
||||||
this.endState = nil
|
this := new(BlockStartState)
|
||||||
|
|
||||||
|
this.initATNState()
|
||||||
|
this.initDecisionState()
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
type BasicBlockStartState struct {
|
func (this *BlockStartState) initBlockStartState() {
|
||||||
|
|
||||||
|
this.endState = nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type BasicBlockStartState struct {
|
||||||
|
BlockStartState
|
||||||
|
}
|
||||||
|
|
||||||
func NewBasicBlockStartState() *BasicBlockStartState {
|
func NewBasicBlockStartState() *BasicBlockStartState {
|
||||||
BlockStartState.call(this)
|
|
||||||
|
this := new(BasicBlockStartState)
|
||||||
|
|
||||||
|
this.initATNState()
|
||||||
|
this.initDecisionState()
|
||||||
|
this.initBlockStartState()
|
||||||
|
|
||||||
this.stateType = ATNStateBLOCK_START
|
this.stateType = ATNStateBLOCK_START
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
// Terminal node of a simple {@code (a|b|c)} block.
|
// Terminal node of a simple {@code (a|b|c)} block.
|
||||||
type BlockEndState struct {
|
type BlockEndState struct {
|
||||||
|
ATNState
|
||||||
|
|
||||||
|
startState *ATNState
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBlockEndState() *BlockEndState {
|
func NewBlockEndState() *BlockEndState {
|
||||||
ATNState.call(this)
|
|
||||||
|
this := new(BlockEndState)
|
||||||
|
|
||||||
|
this.initATNState()
|
||||||
this.stateType = ATNStateBLOCK_END
|
this.stateType = ATNStateBLOCK_END
|
||||||
this.startState = nil
|
this.startState = nil
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -217,24 +261,33 @@ func NewBlockEndState() *BlockEndState {
|
||||||
// error handling.
|
// error handling.
|
||||||
//
|
//
|
||||||
type RuleStopState struct {
|
type RuleStopState struct {
|
||||||
|
ATNState
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRuleStopState() *RuleStopState {
|
func NewRuleStopState() *RuleStopState {
|
||||||
ATNState.call(this)
|
this := new(RuleStopState)
|
||||||
|
|
||||||
|
this.initATNState()
|
||||||
this.stateType = ATNStateRULE_STOP
|
this.stateType = ATNStateRULE_STOP
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
type RuleStartState struct {
|
type RuleStartState struct {
|
||||||
|
ATNState
|
||||||
|
|
||||||
|
stopState *ATNState
|
||||||
|
isPrecedenceRule bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRuleStartState() *RuleStartState {
|
func NewRuleStartState() *RuleStartState {
|
||||||
ATNState.call(this)
|
|
||||||
|
this := new(RuleStartState)
|
||||||
|
|
||||||
|
this.initATNState()
|
||||||
this.stateType = ATNStateRULE_START
|
this.stateType = ATNStateRULE_START
|
||||||
this.stopState = nil
|
this.stopState = nil
|
||||||
this.isPrecedenceRule = false
|
this.isPrecedenceRule = false
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -242,11 +295,17 @@ func NewRuleStartState() *RuleStartState {
|
||||||
// one to the loop back to start of the block and one to exit.
|
// one to the loop back to start of the block and one to exit.
|
||||||
//
|
//
|
||||||
type PlusLoopbackState struct {
|
type PlusLoopbackState struct {
|
||||||
|
BlockStartState
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPlusLoopbackState() *PlusLoopbackState {
|
func NewPlusLoopbackState() *PlusLoopbackState {
|
||||||
DecisionState.call(this)
|
|
||||||
|
this := new(PlusLoopbackState)
|
||||||
|
|
||||||
|
this.initATNState()
|
||||||
|
this.initDecisionState()
|
||||||
|
this.initBlockStartState()
|
||||||
|
|
||||||
this.stateType = ATNStatePLUS_LOOP_BACK
|
this.stateType = ATNStatePLUS_LOOP_BACK
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
@ -257,72 +316,113 @@ func NewPlusLoopbackState() *PlusLoopbackState {
|
||||||
// real decision-making note for {@code A+}.
|
// real decision-making note for {@code A+}.
|
||||||
//
|
//
|
||||||
type PlusBlockStartState struct {
|
type PlusBlockStartState struct {
|
||||||
|
BlockStartState
|
||||||
|
|
||||||
|
loopBackState *ATNState
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPlusBlockStartState() *PlusBlockStartState {
|
func NewPlusBlockStartState() *PlusBlockStartState {
|
||||||
BlockStartState.call(this)
|
|
||||||
|
this := new(PlusBlockStartState)
|
||||||
|
|
||||||
|
this.initATNState()
|
||||||
|
this.initDecisionState()
|
||||||
|
this.initBlockStartState()
|
||||||
|
|
||||||
this.stateType = ATNStatePLUS_BLOCK_START
|
this.stateType = ATNStatePLUS_BLOCK_START
|
||||||
this.loopBackState = nil
|
this.loopBackState = nil
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
// The block that begins a closure loop.
|
// The block that begins a closure loop.
|
||||||
type StarBlockStartState struct {
|
type StarBlockStartState struct {
|
||||||
|
BlockStartState
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStarBlockStartState() *StarBlockStartState {
|
func NewStarBlockStartState() *StarBlockStartState {
|
||||||
BlockStartState.call(this)
|
|
||||||
|
this := new(StarBlockStartState)
|
||||||
|
|
||||||
|
this.initATNState()
|
||||||
|
this.initDecisionState()
|
||||||
|
this.initBlockStartState()
|
||||||
|
|
||||||
this.stateType = ATNStateSTAR_BLOCK_START
|
this.stateType = ATNStateSTAR_BLOCK_START
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type StarLoopbackState struct {
|
type StarLoopbackState struct {
|
||||||
|
ATNState
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStarLoopbackState() *StarLoopbackState {
|
func NewStarLoopbackState() *StarLoopbackState {
|
||||||
ATNState.call(this)
|
|
||||||
|
this := new(StarLoopbackState)
|
||||||
|
|
||||||
|
this.initATNState()
|
||||||
|
|
||||||
this.stateType = ATNStateSTAR_LOOP_BACK
|
this.stateType = ATNStateSTAR_LOOP_BACK
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type StarLoopEntryState struct {
|
type StarLoopEntryState struct {
|
||||||
|
DecisionState
|
||||||
|
|
||||||
|
loopBackState *ATNState
|
||||||
|
precedenceRuleDecision bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStarLoopEntryState() *StarLoopEntryState {
|
func NewStarLoopEntryState() *StarLoopEntryState {
|
||||||
DecisionState.call(this)
|
|
||||||
|
this := new(StarLoopEntryState)
|
||||||
|
|
||||||
|
this.initATNState()
|
||||||
|
this.initDecisionState()
|
||||||
|
|
||||||
this.stateType = ATNStateSTAR_LOOP_ENTRY
|
this.stateType = ATNStateSTAR_LOOP_ENTRY
|
||||||
this.loopBackState = nil
|
this.loopBackState = nil
|
||||||
|
|
||||||
// Indicates whether this state can benefit from a precedence DFA during SLL decision making.
|
// Indicates whether this state can benefit from a precedence DFA during SLL decision making.
|
||||||
this.precedenceRuleDecision = nil
|
this.precedenceRuleDecision = false
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Mark the end of a * or + loop.
|
// Mark the end of a * or + loop.
|
||||||
type LoopEndState struct {
|
type LoopEndState struct {
|
||||||
|
ATNState
|
||||||
|
loopBackState *ATNState
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLoopEndState() *LoopEndState {
|
func NewLoopEndState() *LoopEndState {
|
||||||
ATNState.call(this)
|
|
||||||
|
this := new(LoopEndState)
|
||||||
|
|
||||||
|
this.initATNState()
|
||||||
|
|
||||||
this.stateType = ATNStateLOOP_END
|
this.stateType = ATNStateLOOP_END
|
||||||
this.loopBackState = nil
|
this.loopBackState = nil
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
// The Tokens rule start state linking to each lexer rule start state */
|
// The Tokens rule start state linking to each lexer rule start state */
|
||||||
type TokensStartState struct {
|
type TokensStartState struct {
|
||||||
|
DecisionState
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTokensStartState() *TokensStartState {
|
func NewTokensStartState() *TokensStartState {
|
||||||
DecisionState.call(this)
|
|
||||||
|
this := new(TokensStartState)
|
||||||
|
|
||||||
|
this.initATNState()
|
||||||
|
this.initDecisionState()
|
||||||
|
|
||||||
this.stateType = ATNStateTOKEN_START
|
this.stateType = ATNStateTOKEN_START
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
|
@ -174,7 +174,7 @@ const (
|
||||||
// the configurations to strip out all of the predicates so that a standard
|
// the configurations to strip out all of the predicates so that a standard
|
||||||
// {@link ATNConfigSet} will merge everything ignoring predicates.</p>
|
// {@link ATNConfigSet} will merge everything ignoring predicates.</p>
|
||||||
//
|
//
|
||||||
PredictionModehasSLLConflictTerminatingPrediction = function( mode, configs) {
|
func PredictionModehasSLLConflictTerminatingPrediction( mode, configs ) {
|
||||||
// Configs in rule stop states indicate reaching the end of the decision
|
// Configs in rule stop states indicate reaching the end of the decision
|
||||||
// rule (local context) or end of start rule (full context). If all
|
// rule (local context) or end of start rule (full context). If all
|
||||||
// configs meet this condition, then none of the configurations is able
|
// configs meet this condition, then none of the configurations is able
|
||||||
|
|
|
@ -35,7 +35,7 @@ type DiagnosticErrorListener struct {
|
||||||
|
|
||||||
func DiagnosticErrorListener(exactOnly bool) {
|
func DiagnosticErrorListener(exactOnly bool) {
|
||||||
|
|
||||||
n = new(DiagnosticErrorListener)
|
n := new(DiagnosticErrorListener)
|
||||||
|
|
||||||
// whether all ambiguities or only exact ambiguities are reported.
|
// whether all ambiguities or only exact ambiguities are reported.
|
||||||
n.exactOnly = exactOnly
|
n.exactOnly = exactOnly
|
||||||
|
@ -75,7 +75,7 @@ func (this *DiagnosticErrorListener) reportContextSensitivity(recognizer *antlr4
|
||||||
recognizer.notifyErrorListeners(msg)
|
recognizer.notifyErrorListeners(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *DiagnosticErrorListener) getDecisionDescription(recognizer, dfa *dfa.DFA) {
|
func (this *DiagnosticErrorListener) getDecisionDescription(recognizer *antlr4.Parser, dfa *dfa.DFA) {
|
||||||
var decision = dfa.decision
|
var decision = dfa.decision
|
||||||
var ruleIndex = dfa.atnStartState.ruleIndex
|
var ruleIndex = dfa.atnStartState.ruleIndex
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue