forked from jasder/antlr
Merge pull request #33 from willfaught/atn-runtime-cleanup
ATN runtime format, style, and comment cleanup
This commit is contained in:
commit
c58b3a9189
|
@ -2,86 +2,80 @@ package antlr
|
|||
|
||||
import "fmt"
|
||||
|
||||
// Temporary - for debugging purposes of the Go port
|
||||
const (
|
||||
PortDebug = false
|
||||
)
|
||||
// PortDebug prints debug information to standard out when true. TODO: Remove.
|
||||
const PortDebug = false
|
||||
|
||||
var ATNInvalidAltNumber = 0
|
||||
var ATNInvalidAltNumber int
|
||||
|
||||
type ATN struct {
|
||||
DecisionToState []DecisionState
|
||||
grammarType int
|
||||
maxTokenType int
|
||||
states []ATNState
|
||||
ruleToStartState []*RuleStartState
|
||||
ruleToStopState []*RuleStopState
|
||||
// DecisionToState is the decision points for all rules, subrules, optional
|
||||
// blocks, ()+, ()*, etc. Used to build DFA predictors for them.
|
||||
DecisionToState []DecisionState
|
||||
|
||||
// grammarType is the ATN type and is used for deserializing ATNs from strings.
|
||||
grammarType int
|
||||
|
||||
// lexerActions is referenced by action transitions in the ATN for lexer ATNs.
|
||||
lexerActions []LexerAction
|
||||
|
||||
// maxTokenType is the maximum value for any symbol recognized by a transition in the ATN.
|
||||
maxTokenType int
|
||||
|
||||
modeNameToStartState map[string]*TokensStartState
|
||||
modeToStartState []*TokensStartState
|
||||
ruleToTokenType []int
|
||||
lexerActions []LexerAction
|
||||
|
||||
modeToStartState []*TokensStartState
|
||||
|
||||
// ruleToStartState maps from rule index to starting state number.
|
||||
ruleToStartState []*RuleStartState
|
||||
|
||||
// ruleToStopState maps from rule index to stop state number.
|
||||
ruleToStopState []*RuleStopState
|
||||
|
||||
// ruleToTokenType maps the rule index to the resulting token type for lexer
|
||||
// ATNs. For parser ATNs, it maps the rule index to the generated bypass token
|
||||
// type if ATNDeserializationOptions.isGenerateRuleBypassTransitions was
|
||||
// specified, and otherwise is nil.
|
||||
ruleToTokenType []int
|
||||
|
||||
states []ATNState
|
||||
}
|
||||
|
||||
func NewATN(grammarType int, maxTokenType int) *ATN {
|
||||
|
||||
atn := new(ATN)
|
||||
|
||||
// Used for runtime deserialization of ATNs from strings///
|
||||
// The type of the ATN.
|
||||
atn.grammarType = grammarType
|
||||
// The maximum value for any symbol recognized by a transition in the ATN.
|
||||
atn.maxTokenType = maxTokenType
|
||||
atn.states = make([]ATNState, 0)
|
||||
// Each subrule/rule is a decision point and we must track them so we
|
||||
// can go back later and build DFA predictors for them. This includes
|
||||
// all the rules, subrules, optional blocks, ()+, ()* etc...
|
||||
atn.DecisionToState = make([]DecisionState, 0)
|
||||
// Maps from rule index to starting state number.
|
||||
atn.ruleToStartState = make([]*RuleStartState, 0)
|
||||
// Maps from rule index to stop state number.
|
||||
atn.ruleToStopState = nil
|
||||
atn.modeNameToStartState = make(map[string]*TokensStartState)
|
||||
// For lexer ATNs, atn.maps the rule index to the resulting token type.
|
||||
// For parser ATNs, atn.maps the rule index to the generated bypass token
|
||||
// type if the
|
||||
// {@link ATNDeserializationOptions//isGenerateRuleBypassTransitions}
|
||||
// deserialization option was specified otherwise, atn.is {@code nil}.
|
||||
atn.ruleToTokenType = nil
|
||||
// For lexer ATNs, atn.is an array of {@link LexerAction} objects which may
|
||||
// be referenced by action transitions in the ATN.
|
||||
atn.lexerActions = nil
|
||||
atn.modeToStartState = make([]*TokensStartState, 0)
|
||||
|
||||
return atn
|
||||
|
||||
return &ATN{
|
||||
grammarType: grammarType,
|
||||
maxTokenType: maxTokenType,
|
||||
modeNameToStartState: make(map[string]*TokensStartState),
|
||||
}
|
||||
}
|
||||
|
||||
// Compute the set of valid tokens that can occur starting in state {@code s}.
|
||||
// If {@code ctx} is nil, the set of tokens will not include what can follow
|
||||
// the rule surrounding {@code s}. In other words, the set will be
|
||||
// restricted to tokens reachable staying within {@code s}'s rule.
|
||||
// NextTokensInContext computes the set of valid tokens that can occur starting
|
||||
// in state s. If ctx is nil, the set of tokens will not include what can follow
|
||||
// the rule surrounding s. In other words, the set will be restricted to tokens
|
||||
// reachable staying within the rule of s.
|
||||
func (a *ATN) NextTokensInContext(s ATNState, ctx RuleContext) *IntervalSet {
|
||||
var anal = NewLL1Analyzer(a)
|
||||
var res = anal.Look(s, nil, ctx)
|
||||
return res
|
||||
return NewLL1Analyzer(a).Look(s, nil, ctx)
|
||||
}
|
||||
|
||||
// Compute the set of valid tokens that can occur starting in {@code s} and
|
||||
// staying in same rule. {@link Token//EPSILON} is in set if we reach end of
|
||||
// NextTokensNoContext computes the set of valid tokens that can occur starting
|
||||
// in s and staying in same rule. Token.EPSILON is in set if we reach end of
|
||||
// rule.
|
||||
func (a *ATN) NextTokensNoContext(s ATNState) *IntervalSet {
|
||||
if s.GetNextTokenWithinRule() != nil {
|
||||
if PortDebug {
|
||||
fmt.Println("DEBUG A")
|
||||
}
|
||||
|
||||
return s.GetNextTokenWithinRule()
|
||||
}
|
||||
|
||||
if PortDebug {
|
||||
fmt.Println("DEBUG 2")
|
||||
fmt.Println(a.NextTokensInContext(s, nil))
|
||||
}
|
||||
|
||||
s.SetNextTokenWithinRule(a.NextTokensInContext(s, nil))
|
||||
s.GetNextTokenWithinRule().readOnly = true
|
||||
|
||||
return s.GetNextTokenWithinRule()
|
||||
}
|
||||
|
||||
|
@ -98,16 +92,18 @@ func (a *ATN) addState(state ATNState) {
|
|||
state.SetATN(a)
|
||||
state.SetStateNumber(len(a.states))
|
||||
}
|
||||
|
||||
a.states = append(a.states, state)
|
||||
}
|
||||
|
||||
func (a *ATN) removeState(state ATNState) {
|
||||
a.states[state.GetStateNumber()] = nil // just free mem, don't shift states in list
|
||||
a.states[state.GetStateNumber()] = nil // Just free the memory; don't shift states in the slice
|
||||
}
|
||||
|
||||
func (a *ATN) defineDecisionState(s DecisionState) int {
|
||||
a.DecisionToState = append(a.DecisionToState, s)
|
||||
s.setDecision(len(a.DecisionToState) - 1)
|
||||
|
||||
return s.getDecision()
|
||||
}
|
||||
|
||||
|
@ -119,46 +115,48 @@ func (a *ATN) getDecisionState(decision int) DecisionState {
|
|||
return a.DecisionToState[decision]
|
||||
}
|
||||
|
||||
// Computes the set of input symbols which could follow ATN state number
|
||||
// {@code stateNumber} in the specified full {@code context}. This method
|
||||
// considers the complete parser context, but does not evaluate semantic
|
||||
// predicates (i.e. all predicates encountered during the calculation are
|
||||
// assumed true). If a path in the ATN exists from the starting state to the
|
||||
// {@link RuleStopState} of the outermost context without Matching any
|
||||
// symbols, {@link Token//EOF} is added to the returned set.
|
||||
// getExpectedTokens computes the set of input symbols which could follow ATN
|
||||
// state number stateNumber in the specified full parse context ctx and returns
|
||||
// the set of potentially valid input symbols which could follow the specified
|
||||
// state in the specified context. This method considers the complete parser
|
||||
// context, but does not evaluate semantic predicates (i.e. all predicates
|
||||
// encountered during the calculation are assumed true). If a path in the ATN
|
||||
// exists from the starting state to the RuleStopState of the outermost context
|
||||
// without Matching any symbols, Token.EOF is added to the returned set.
|
||||
//
|
||||
// <p>If {@code context} is {@code nil}, it is treated as
|
||||
// {@link ParserRuleContext//EMPTY}.</p>
|
||||
// A nil ctx defaults to ParserRuleContext.EMPTY.
|
||||
//
|
||||
// @param stateNumber the ATN state number
|
||||
// @param context the full parse context
|
||||
// @return The set of potentially valid input symbols which could follow the
|
||||
// specified state in the specified context.
|
||||
// @panics IllegalArgumentException if the ATN does not contain a state with
|
||||
// number {@code stateNumber}
|
||||
|
||||
// It panics if the ATN does not contain state stateNumber.
|
||||
func (a *ATN) getExpectedTokens(stateNumber int, ctx RuleContext) *IntervalSet {
|
||||
if stateNumber < 0 || stateNumber >= len(a.states) {
|
||||
panic("Invalid state number.")
|
||||
}
|
||||
|
||||
var s = a.states[stateNumber]
|
||||
var following = a.NextTokens(s, nil)
|
||||
|
||||
if !following.contains(TokenEpsilon) {
|
||||
return following
|
||||
}
|
||||
|
||||
var expected = NewIntervalSet()
|
||||
|
||||
expected.addSet(following)
|
||||
expected.removeOne(TokenEpsilon)
|
||||
|
||||
for ctx != nil && ctx.GetInvokingState() >= 0 && following.contains(TokenEpsilon) {
|
||||
var invokingState = a.states[ctx.GetInvokingState()]
|
||||
var rt = invokingState.GetTransitions()[0]
|
||||
|
||||
following = a.NextTokens(rt.(*RuleTransition).followState, nil)
|
||||
expected.addSet(following)
|
||||
expected.removeOne(TokenEpsilon)
|
||||
ctx = ctx.GetParent().(RuleContext)
|
||||
}
|
||||
|
||||
if following.contains(TokenEpsilon) {
|
||||
expected.addOne(TokenEOF)
|
||||
}
|
||||
|
||||
return expected
|
||||
}
|
||||
|
|
|
@ -2,29 +2,22 @@ package antlr
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
// "reflect"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// A tuple: (ATN state, predicted alt, syntactic, semantic context).
|
||||
// The syntactic context is a graph-structured stack node whose
|
||||
// path(s) to the root is the rule invocation(s)
|
||||
// chain used to arrive at the state. The semantic context is
|
||||
// the tree of semantic predicates encountered before reaching
|
||||
// an ATN state.
|
||||
//
|
||||
|
||||
type Comparable interface {
|
||||
equals(other interface{}) bool
|
||||
}
|
||||
|
||||
// ATNConfig is a tuple: (ATN state, predicted alt, syntactic, semantic
|
||||
// context). The syntactic context is a graph-structured stack node whose
|
||||
// path(s) to the root is the rule invocation(s) chain used to arrive at the
|
||||
// state. The semantic context is the tree of semantic predicates encountered
|
||||
// before reaching an ATN state.
|
||||
type ATNConfig interface {
|
||||
Hasher
|
||||
Comparable
|
||||
|
||||
getPrecedenceFilterSuppressed() bool
|
||||
setPrecedenceFilterSuppressed(bool)
|
||||
|
||||
GetState() ATNState
|
||||
GetAlt() int
|
||||
GetSemanticContext() SemanticContext
|
||||
|
@ -37,6 +30,9 @@ type ATNConfig interface {
|
|||
|
||||
String() string
|
||||
|
||||
getPrecedenceFilterSuppressed() bool
|
||||
setPrecedenceFilterSuppressed(bool)
|
||||
|
||||
shortHash() string
|
||||
}
|
||||
|
||||
|
@ -49,14 +45,14 @@ type BaseATNConfig struct {
|
|||
reachesIntoOuterContext int
|
||||
}
|
||||
|
||||
func NewBaseATNConfig7(old *BaseATNConfig) *BaseATNConfig { // dup
|
||||
a := new(BaseATNConfig)
|
||||
a.state = old.state
|
||||
a.alt = old.alt
|
||||
a.context = old.context
|
||||
a.semanticContext = old.semanticContext
|
||||
a.reachesIntoOuterContext = old.reachesIntoOuterContext
|
||||
return a
|
||||
func NewBaseATNConfig7(old *BaseATNConfig) *BaseATNConfig { // TODO: Dup
|
||||
return &BaseATNConfig{
|
||||
state: old.state,
|
||||
alt: old.alt,
|
||||
context: old.context,
|
||||
semanticContext: old.semanticContext,
|
||||
reachesIntoOuterContext: old.reachesIntoOuterContext,
|
||||
}
|
||||
}
|
||||
|
||||
func NewBaseATNConfig6(state ATNState, alt int, context PredictionContext) *BaseATNConfig {
|
||||
|
@ -64,18 +60,11 @@ func NewBaseATNConfig6(state ATNState, alt int, context PredictionContext) *Base
|
|||
}
|
||||
|
||||
func NewBaseATNConfig5(state ATNState, alt int, context PredictionContext, semanticContext SemanticContext) *BaseATNConfig {
|
||||
a := new(BaseATNConfig)
|
||||
|
||||
if semanticContext == nil {
|
||||
panic("SemanticContext cannot be null!")
|
||||
panic("semanticContext cannot be nil") // TODO: Necessary?
|
||||
}
|
||||
|
||||
a.state = state
|
||||
a.alt = alt
|
||||
a.context = context
|
||||
a.semanticContext = semanticContext
|
||||
|
||||
return a
|
||||
return &BaseATNConfig{state: state, alt: alt, context: context, semanticContext: semanticContext}
|
||||
}
|
||||
|
||||
func NewBaseATNConfig4(c ATNConfig, state ATNState) *BaseATNConfig {
|
||||
|
@ -95,19 +84,17 @@ func NewBaseATNConfig1(c ATNConfig, state ATNState, context PredictionContext) *
|
|||
}
|
||||
|
||||
func NewBaseATNConfig(c ATNConfig, state ATNState, context PredictionContext, semanticContext SemanticContext) *BaseATNConfig {
|
||||
a := new(BaseATNConfig)
|
||||
|
||||
if semanticContext == nil {
|
||||
panic("SemanticContext cannot be null!")
|
||||
panic("semanticContext cannot be nil")
|
||||
}
|
||||
|
||||
a.state = state
|
||||
a.alt = c.GetAlt()
|
||||
a.context = context
|
||||
a.semanticContext = semanticContext
|
||||
a.reachesIntoOuterContext = c.GetReachesIntoOuterContext()
|
||||
|
||||
return a
|
||||
return &BaseATNConfig{
|
||||
state: state,
|
||||
alt: c.GetAlt(),
|
||||
context: context,
|
||||
semanticContext: semanticContext,
|
||||
reachesIntoOuterContext: c.GetReachesIntoOuterContext(),
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BaseATNConfig) getPrecedenceFilterSuppressed() bool {
|
||||
|
@ -145,34 +132,35 @@ func (b *BaseATNConfig) SetReachesIntoOuterContext(v int) {
|
|||
b.reachesIntoOuterContext = v
|
||||
}
|
||||
|
||||
// An ATN configuration is equal to another if both have
|
||||
// the same state, they predict the same alternative, and
|
||||
// syntactic/semantic contexts are the same.
|
||||
///
|
||||
// An ATN configuration is equal to another if both have the same state, they
|
||||
// predict the same alternative, and syntactic/semantic contexts are the same.
|
||||
func (b *BaseATNConfig) equals(o interface{}) bool {
|
||||
|
||||
if b == o {
|
||||
return true
|
||||
}
|
||||
|
||||
other, ok := o.(*BaseATNConfig)
|
||||
var other, ok = o.(*BaseATNConfig)
|
||||
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
var equal bool
|
||||
|
||||
if b.context == nil {
|
||||
equal = other.context == nil
|
||||
} else {
|
||||
equal = b.context.equals(other.context)
|
||||
}
|
||||
|
||||
return b.state.GetStateNumber() == other.state.GetStateNumber() &&
|
||||
b.alt == other.alt &&
|
||||
b.semanticContext.equals(other.semanticContext) &&
|
||||
b.precedenceFilterSuppressed == other.precedenceFilterSuppressed &&
|
||||
equal
|
||||
var (
|
||||
nums = b.state.GetStateNumber() == other.state.GetStateNumber()
|
||||
alts = b.alt == other.alt
|
||||
cons = b.semanticContext.equals(other.semanticContext)
|
||||
sups = b.precedenceFilterSuppressed == other.precedenceFilterSuppressed
|
||||
)
|
||||
|
||||
return nums && alts && cons && sups && equal
|
||||
}
|
||||
|
||||
func (b *BaseATNConfig) shortHash() string {
|
||||
|
@ -180,8 +168,8 @@ func (b *BaseATNConfig) shortHash() string {
|
|||
}
|
||||
|
||||
func (b *BaseATNConfig) Hash() string {
|
||||
|
||||
var c string
|
||||
|
||||
if b.context == nil {
|
||||
c = ""
|
||||
} else {
|
||||
|
@ -192,93 +180,66 @@ func (b *BaseATNConfig) Hash() string {
|
|||
}
|
||||
|
||||
func (b *BaseATNConfig) String() string {
|
||||
var s1, s2, s3 string
|
||||
|
||||
var s1 string
|
||||
if b.context != nil {
|
||||
s1 = ",[" + fmt.Sprint(b.context) + "]"
|
||||
}
|
||||
|
||||
var s2 string
|
||||
if b.semanticContext != SemanticContextNone {
|
||||
s2 = "," + fmt.Sprint(b.semanticContext)
|
||||
}
|
||||
|
||||
var s3 string
|
||||
if b.reachesIntoOuterContext > 0 {
|
||||
s3 = ",up=" + fmt.Sprint(b.reachesIntoOuterContext)
|
||||
}
|
||||
|
||||
return "(" + fmt.Sprint(b.state) + "," + strconv.Itoa(b.alt) + s1 + s2 + s3 + ")"
|
||||
return fmt.Sprintf("(%v,%v%v%v%v)", b.state, b.alt, s1, s2, s3)
|
||||
}
|
||||
|
||||
type LexerATNConfig struct {
|
||||
*BaseATNConfig
|
||||
|
||||
lexerActionExecutor *LexerActionExecutor
|
||||
passedThroughNonGreedyDecision bool
|
||||
}
|
||||
|
||||
func NewLexerATNConfig6(state ATNState, alt int, context PredictionContext) *LexerATNConfig {
|
||||
|
||||
l := new(LexerATNConfig)
|
||||
|
||||
l.BaseATNConfig = NewBaseATNConfig5(state, alt, context, SemanticContextNone)
|
||||
|
||||
l.passedThroughNonGreedyDecision = false
|
||||
l.lexerActionExecutor = nil
|
||||
return l
|
||||
return &LexerATNConfig{BaseATNConfig: NewBaseATNConfig5(state, alt, context, SemanticContextNone)}
|
||||
}
|
||||
|
||||
func NewLexerATNConfig5(state ATNState, alt int, context PredictionContext, lexerActionExecutor *LexerActionExecutor) *LexerATNConfig {
|
||||
|
||||
l := new(LexerATNConfig)
|
||||
|
||||
l.BaseATNConfig = NewBaseATNConfig5(state, alt, context, SemanticContextNone)
|
||||
l.lexerActionExecutor = lexerActionExecutor
|
||||
l.passedThroughNonGreedyDecision = false
|
||||
return l
|
||||
return &LexerATNConfig{
|
||||
BaseATNConfig: NewBaseATNConfig5(state, alt, context, SemanticContextNone),
|
||||
lexerActionExecutor: lexerActionExecutor,
|
||||
}
|
||||
}
|
||||
|
||||
func NewLexerATNConfig4(c *LexerATNConfig, state ATNState) *LexerATNConfig {
|
||||
|
||||
l := new(LexerATNConfig)
|
||||
|
||||
l.BaseATNConfig = NewBaseATNConfig(c, state, c.GetContext(), c.GetSemanticContext())
|
||||
l.lexerActionExecutor = c.lexerActionExecutor
|
||||
l.passedThroughNonGreedyDecision = checkNonGreedyDecision(c, state)
|
||||
return l
|
||||
return &LexerATNConfig{
|
||||
BaseATNConfig: NewBaseATNConfig(c, state, c.GetContext(), c.GetSemanticContext()),
|
||||
lexerActionExecutor: c.lexerActionExecutor,
|
||||
passedThroughNonGreedyDecision: checkNonGreedyDecision(c, state),
|
||||
}
|
||||
}
|
||||
|
||||
func NewLexerATNConfig3(c *LexerATNConfig, state ATNState, lexerActionExecutor *LexerActionExecutor) *LexerATNConfig {
|
||||
|
||||
l := new(LexerATNConfig)
|
||||
|
||||
l.BaseATNConfig = NewBaseATNConfig(c, state, c.GetContext(), c.GetSemanticContext())
|
||||
l.lexerActionExecutor = lexerActionExecutor
|
||||
l.passedThroughNonGreedyDecision = checkNonGreedyDecision(c, state)
|
||||
return l
|
||||
return &LexerATNConfig{
|
||||
BaseATNConfig: NewBaseATNConfig(c, state, c.GetContext(), c.GetSemanticContext()),
|
||||
lexerActionExecutor: lexerActionExecutor,
|
||||
passedThroughNonGreedyDecision: checkNonGreedyDecision(c, state),
|
||||
}
|
||||
}
|
||||
|
||||
func NewLexerATNConfig2(c *LexerATNConfig, state ATNState, context PredictionContext) *LexerATNConfig {
|
||||
|
||||
l := new(LexerATNConfig)
|
||||
|
||||
l.BaseATNConfig = NewBaseATNConfig(c, state, context, c.GetSemanticContext())
|
||||
l.lexerActionExecutor = c.lexerActionExecutor
|
||||
l.passedThroughNonGreedyDecision = checkNonGreedyDecision(c, state)
|
||||
return l
|
||||
return &LexerATNConfig{
|
||||
BaseATNConfig: NewBaseATNConfig(c, state, context, c.GetSemanticContext()),
|
||||
lexerActionExecutor: c.lexerActionExecutor,
|
||||
passedThroughNonGreedyDecision: checkNonGreedyDecision(c, state),
|
||||
}
|
||||
}
|
||||
|
||||
func NewLexerATNConfig1(state ATNState, alt int, context PredictionContext) *LexerATNConfig {
|
||||
|
||||
l := new(LexerATNConfig)
|
||||
|
||||
l.BaseATNConfig = NewBaseATNConfig5(state, alt, context, SemanticContextNone)
|
||||
|
||||
l.lexerActionExecutor = nil
|
||||
l.passedThroughNonGreedyDecision = false
|
||||
|
||||
return l
|
||||
return &LexerATNConfig{BaseATNConfig: NewBaseATNConfig5(state, alt, context, SemanticContextNone)}
|
||||
}
|
||||
|
||||
func (l *LexerATNConfig) Hash() string {
|
||||
|
@ -290,13 +251,11 @@ func (l *LexerATNConfig) Hash() string {
|
|||
f = "0"
|
||||
}
|
||||
|
||||
return strconv.Itoa(l.state.GetStateNumber()) + strconv.Itoa(l.alt) + fmt.Sprint(l.context) +
|
||||
fmt.Sprint(l.semanticContext) + f + fmt.Sprint(l.lexerActionExecutor)
|
||||
return fmt.Sprintf("%v%v%v%v%v%v", l.state.GetStateNumber(), l.alt, l.context, l.semanticContext, f, l.lexerActionExecutor)
|
||||
}
|
||||
|
||||
func (l *LexerATNConfig) equals(other interface{}) bool {
|
||||
|
||||
othert, ok := other.(*LexerATNConfig)
|
||||
var othert, ok = other.(*LexerATNConfig)
|
||||
|
||||
if l == other {
|
||||
return true
|
||||
|
@ -307,6 +266,7 @@ func (l *LexerATNConfig) equals(other interface{}) bool {
|
|||
}
|
||||
|
||||
var b bool
|
||||
|
||||
if l.lexerActionExecutor != nil {
|
||||
b = !l.lexerActionExecutor.equals(othert.lexerActionExecutor)
|
||||
} else {
|
||||
|
@ -321,6 +281,7 @@ func (l *LexerATNConfig) equals(other interface{}) bool {
|
|||
}
|
||||
|
||||
func checkNonGreedyDecision(source *LexerATNConfig, target ATNState) bool {
|
||||
ds, ok := target.(DecisionState)
|
||||
var ds, ok = target.(DecisionState)
|
||||
|
||||
return source.passedThroughNonGreedyDecision || (ok && ds.getNonGreedy())
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package antlr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
import "fmt"
|
||||
|
||||
type ATNConfigSet interface {
|
||||
Hasher
|
||||
|
@ -43,114 +41,114 @@ type ATNConfigSet interface {
|
|||
SetDipsIntoOuterContext(bool)
|
||||
}
|
||||
|
||||
// Specialized {@link Set}{@code <}{@link ATNConfig}{@code >} that can track
|
||||
// info about the set, with support for combining similar configurations using a
|
||||
// BaseATNConfigSet is a specialized set of ATNConfig that tracks information
|
||||
// about its elements and can combine similar configurations using a
|
||||
// graph-structured stack.
|
||||
|
||||
type BaseATNConfigSet struct {
|
||||
readOnly bool
|
||||
fullCtx bool
|
||||
configLookup *Set
|
||||
conflictingAlts *BitSet
|
||||
cachedHashString string
|
||||
hasSemanticContext bool
|
||||
cachedHashString string
|
||||
|
||||
// configLookup is used to determine whether two BaseATNConfigSets are equal. We
|
||||
// need all configurations with the same (s, i, _, semctx) to be equal. A key
|
||||
// effectively doubles the number of objects associated with ATNConfigs. All
|
||||
// keys are hashed by (s, i, _, pi), not including the context. Wiped out when
|
||||
// read-only because a set becomes a DFA state.
|
||||
configLookup *Set
|
||||
|
||||
// configs is the added elements.
|
||||
configs []ATNConfig
|
||||
|
||||
// TODO: These fields make me pretty uncomfortable, but it is nice to pack up
|
||||
// info together because it saves recomputation. Can we track conflicts as they
|
||||
// are added to save scanning configs later?
|
||||
conflictingAlts *BitSet
|
||||
|
||||
// dipsIntoOuterContext is used by parsers and lexers. In a lexer, it indicates
|
||||
// we hit a pred while computing a closure operation. Do not make a DFA state
|
||||
// from the BaseATNConfigSet in this case. TODO: How is this used by parsers?
|
||||
dipsIntoOuterContext bool
|
||||
configs []ATNConfig
|
||||
uniqueAlt int
|
||||
}
|
||||
|
||||
func NewBaseATNConfigSet(fullCtx bool) *BaseATNConfigSet {
|
||||
|
||||
a := new(BaseATNConfigSet)
|
||||
|
||||
// The reason that we need a.is because we don't want the hash map to use
|
||||
// the standard hash code and equals. We need all configurations with the
|
||||
// same
|
||||
// {@code (s,i,_,semctx)} to be equal. Unfortunately, a.key effectively
|
||||
// doubles
|
||||
// the number of objects associated with ATNConfigs. The other solution is
|
||||
// to
|
||||
// use a hash table that lets us specify the equals/hashcode operation.
|
||||
// All configs but hashed by (s, i, _, pi) not including context. Wiped out
|
||||
// when we go readonly as a.set becomes a DFA state.
|
||||
a.configLookup = NewSet(hashATNConfig, equalATNConfigs)
|
||||
// Indicates that a.configuration set is part of a full context
|
||||
// LL prediction. It will be used to determine how to merge $. With SLL
|
||||
// it's a wildcard whereas it is not for LL context merge.
|
||||
a.fullCtx = fullCtx
|
||||
// Indicates that the set of configurations is read-only. Do not
|
||||
// allow any code to manipulate the set DFA states will point at
|
||||
// the sets and they must not change. a.does not protect the other
|
||||
// fields in particular, conflictingAlts is set after
|
||||
// we've made a.readonly.
|
||||
a.readOnly = false
|
||||
// Track the elements as they are added to the set supports Get(i)///
|
||||
a.configs = make([]ATNConfig, 0)
|
||||
|
||||
// TODO: these fields make me pretty uncomfortable but nice to pack up info
|
||||
// together, saves recomputation
|
||||
// TODO: can we track conflicts as they are added to save scanning configs
|
||||
// later?
|
||||
a.uniqueAlt = 0
|
||||
a.conflictingAlts = nil
|
||||
// fullCtx is whether it is part of a full context LL prediction. Used to
|
||||
// determine how to merge $. It is a wildcard with SLL, but not for an LL
|
||||
// context merge.
|
||||
fullCtx bool
|
||||
|
||||
// Used in parser and lexer. In lexer, it indicates we hit a pred
|
||||
// while computing a closure operation. Don't make a DFA state from a.
|
||||
a.hasSemanticContext = false
|
||||
a.dipsIntoOuterContext = false
|
||||
hasSemanticContext bool
|
||||
|
||||
a.cachedHashString = "-1"
|
||||
// readOnly is whether it is read-only. Do not
|
||||
// allow any code to manipulate the set if true because DFA states will point at
|
||||
// sets and those must not change. It not protect other fields; conflictingAlts
|
||||
// in particular, which is assigned after readOnly.
|
||||
readOnly bool
|
||||
|
||||
return a
|
||||
// TODO: These fields make me pretty uncomfortable, but it is nice to pack up
|
||||
// info together because it saves recomputation. Can we track conflicts as they
|
||||
// are added to save scanning configs later?
|
||||
uniqueAlt int
|
||||
}
|
||||
|
||||
// Adding a Newconfig means merging contexts with existing configs for
|
||||
// {@code (s, i, pi, _)}, where {@code s} is the
|
||||
// {@link ATNConfig//state}, {@code i} is the {@link ATNConfig//alt}, and
|
||||
// {@code pi} is the {@link ATNConfig//semanticContext}. We use
|
||||
// {@code (s,i,pi)} as key.
|
||||
//
|
||||
// <p>This method updates {@link //dipsIntoOuterContext} and
|
||||
// {@link //hasSemanticContext} when necessary.</p>
|
||||
// /
|
||||
func (b *BaseATNConfigSet) Add(config ATNConfig, mergeCache *DoubleDict) bool {
|
||||
|
||||
if b.readOnly {
|
||||
panic("This set is readonly")
|
||||
func NewBaseATNConfigSet(fullCtx bool) *BaseATNConfigSet {
|
||||
return &BaseATNConfigSet{
|
||||
cachedHashString: "-1",
|
||||
configLookup: NewSet(hashATNConfig, equalATNConfigs),
|
||||
fullCtx: fullCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// Add merges contexts with existing configs for (s, i, pi, _), where s is the
|
||||
// ATNConfig.state, i is the ATNConfig.alt, and pi is the
|
||||
// ATNConfig.semanticContext. We use (s,i,pi) as the key. Updates
|
||||
// dipsIntoOuterContext and hasSemanticContext when necessary.
|
||||
func (b *BaseATNConfigSet) Add(config ATNConfig, mergeCache *DoubleDict) bool {
|
||||
if b.readOnly {
|
||||
panic("set is read-only")
|
||||
}
|
||||
|
||||
if config.GetSemanticContext() != SemanticContextNone {
|
||||
b.hasSemanticContext = true
|
||||
}
|
||||
|
||||
if config.GetReachesIntoOuterContext() > 0 {
|
||||
b.dipsIntoOuterContext = true
|
||||
}
|
||||
|
||||
var existing = b.configLookup.add(config).(ATNConfig)
|
||||
|
||||
if existing == config {
|
||||
b.cachedHashString = "-1"
|
||||
b.configs = append(b.configs, config) // track order here
|
||||
b.configs = append(b.configs, config) // Track order here
|
||||
|
||||
return true
|
||||
}
|
||||
// a previous (s,i,pi,_), merge with it and save result
|
||||
|
||||
// Merge a previous (s, i, pi, _) with it and save the result
|
||||
var rootIsWildcard = !b.fullCtx
|
||||
var merged = merge(existing.GetContext(), config.GetContext(), rootIsWildcard, mergeCache)
|
||||
// no need to check for existing.context, config.context in cache
|
||||
// since only way to create Newgraphs is "call rule" and here. We
|
||||
// cache at both places.
|
||||
|
||||
// No need to check for existing.context because config.context is in the cache,
|
||||
// since the only way to create new graphs is the "call rule" and here. We cache
|
||||
// at both places.
|
||||
existing.SetReachesIntoOuterContext(intMax(existing.GetReachesIntoOuterContext(), config.GetReachesIntoOuterContext()))
|
||||
// make sure to preserve the precedence filter suppression during the merge
|
||||
|
||||
// Preserve the precedence filter suppression during the merge
|
||||
if config.getPrecedenceFilterSuppressed() {
|
||||
existing.setPrecedenceFilterSuppressed(true)
|
||||
}
|
||||
existing.SetContext(merged) // replace context no need to alt mapping
|
||||
|
||||
// Replace the context because there is no need to do alt mapping
|
||||
existing.SetContext(merged)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (b *BaseATNConfigSet) GetStates() *Set {
|
||||
var states = NewSet(nil, nil)
|
||||
|
||||
for i := 0; i < len(b.configs); i++ {
|
||||
states.add(b.configs[i].GetState())
|
||||
}
|
||||
|
||||
return states
|
||||
}
|
||||
|
||||
|
@ -164,12 +162,15 @@ func (b *BaseATNConfigSet) SetHasSemanticContext(v bool) {
|
|||
|
||||
func (b *BaseATNConfigSet) GetPredicates() []SemanticContext {
|
||||
var preds = make([]SemanticContext, 0)
|
||||
|
||||
for i := 0; i < len(b.configs); i++ {
|
||||
c := b.configs[i].GetSemanticContext()
|
||||
var c = b.configs[i].GetSemanticContext()
|
||||
|
||||
if c != SemanticContextNone {
|
||||
preds = append(preds, c)
|
||||
}
|
||||
}
|
||||
|
||||
return preds
|
||||
}
|
||||
|
||||
|
@ -179,13 +180,16 @@ func (b *BaseATNConfigSet) GetItems() []ATNConfig {
|
|||
|
||||
func (b *BaseATNConfigSet) OptimizeConfigs(interpreter *BaseATNSimulator) {
|
||||
if b.readOnly {
|
||||
panic("This set is readonly")
|
||||
panic("set is read-only")
|
||||
}
|
||||
|
||||
if b.configLookup.length() == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for i := 0; i < len(b.configs); i++ {
|
||||
var config = b.configs[i]
|
||||
|
||||
config.SetContext(interpreter.getCachedContext(config.GetContext()))
|
||||
}
|
||||
}
|
||||
|
@ -194,6 +198,7 @@ func (b *BaseATNConfigSet) AddAll(coll []ATNConfig) bool {
|
|||
for i := 0; i < len(coll); i++ {
|
||||
b.Add(coll[i], nil)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
|
@ -204,10 +209,10 @@ func (b *BaseATNConfigSet) Equals(other interface{}) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
other2 := other.(*BaseATNConfigSet)
|
||||
var other2 = other.(*BaseATNConfigSet)
|
||||
|
||||
return b.configs != nil &&
|
||||
// b.configs.equals(other2.configs) && // TODO is b necessary?
|
||||
// TODO: b.configs.equals(other2.configs) && // TODO: Is b necessary?
|
||||
b.fullCtx == other2.fullCtx &&
|
||||
b.uniqueAlt == other2.uniqueAlt &&
|
||||
b.conflictingAlts == other2.conflictingAlts &&
|
||||
|
@ -220,6 +225,7 @@ func (b *BaseATNConfigSet) Hash() string {
|
|||
if b.cachedHashString == "-1" {
|
||||
b.cachedHashString = b.hashConfigs()
|
||||
}
|
||||
|
||||
return b.cachedHashString
|
||||
}
|
||||
|
||||
|
@ -228,9 +234,11 @@ func (b *BaseATNConfigSet) Hash() string {
|
|||
|
||||
func (b *BaseATNConfigSet) hashConfigs() string {
|
||||
var s = ""
|
||||
|
||||
for _, c := range b.configs {
|
||||
s += fmt.Sprint(c)
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
|
@ -244,22 +252,25 @@ func (b *BaseATNConfigSet) IsEmpty() bool {
|
|||
|
||||
func (b *BaseATNConfigSet) Contains(item ATNConfig) bool {
|
||||
if b.configLookup == nil {
|
||||
panic("This method is not implemented for readonly sets.")
|
||||
panic("not implemented for read-only sets")
|
||||
}
|
||||
|
||||
return b.configLookup.contains(item)
|
||||
}
|
||||
|
||||
func (b *BaseATNConfigSet) ContainsFast(item ATNConfig) bool {
|
||||
if b.configLookup == nil {
|
||||
panic("This method is not implemented for readonly sets.")
|
||||
panic("not implemented for read-only sets")
|
||||
}
|
||||
return b.configLookup.contains(item) // TODO containsFast is not implemented for Set
|
||||
|
||||
return b.configLookup.contains(item) // TODO: containsFast is not implemented for Set
|
||||
}
|
||||
|
||||
func (b *BaseATNConfigSet) Clear() {
|
||||
if b.readOnly {
|
||||
panic("This set is readonly")
|
||||
panic("set is read-only")
|
||||
}
|
||||
|
||||
b.configs = make([]ATNConfig, 0)
|
||||
b.cachedHashString = "-1"
|
||||
b.configLookup = NewSet(hashATNConfig, equalATNConfigs)
|
||||
|
@ -299,8 +310,9 @@ func (b *BaseATNConfigSet) ReadOnly() bool {
|
|||
|
||||
func (b *BaseATNConfigSet) SetReadOnly(readOnly bool) {
|
||||
b.readOnly = readOnly
|
||||
|
||||
if readOnly {
|
||||
b.configLookup = nil // can't mod, no need for lookup cache
|
||||
b.configLookup = nil // Read only, so no need for the lookup cache
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -309,6 +321,7 @@ func (b *BaseATNConfigSet) String() string {
|
|||
|
||||
for i, c := range b.configs {
|
||||
s += c.String()
|
||||
|
||||
if i != len(b.configs)-1 {
|
||||
s += ", "
|
||||
}
|
||||
|
@ -340,13 +353,11 @@ type OrderedATNConfigSet struct {
|
|||
}
|
||||
|
||||
func NewOrderedATNConfigSet() *OrderedATNConfigSet {
|
||||
var b = NewBaseATNConfigSet(false)
|
||||
|
||||
o := new(OrderedATNConfigSet)
|
||||
b.configLookup = NewSet(nil, nil)
|
||||
|
||||
o.BaseATNConfigSet = NewBaseATNConfigSet(false)
|
||||
o.configLookup = NewSet(nil, nil)
|
||||
|
||||
return o
|
||||
return &OrderedATNConfigSet{BaseATNConfigSet: b}
|
||||
}
|
||||
|
||||
func hashATNConfig(c interface{}) string {
|
||||
|
@ -354,7 +365,6 @@ func hashATNConfig(c interface{}) string {
|
|||
}
|
||||
|
||||
func equalATNConfigs(a, b interface{}) bool {
|
||||
|
||||
if a == nil || b == nil {
|
||||
return false
|
||||
}
|
||||
|
@ -363,14 +373,16 @@ func equalATNConfigs(a, b interface{}) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
ai, ok := a.(ATNConfig)
|
||||
bi, ok1 := b.(ATNConfig)
|
||||
var ai, ok = a.(ATNConfig)
|
||||
var bi, ok1 = b.(ATNConfig)
|
||||
|
||||
if !ok || !ok1 {
|
||||
return false
|
||||
}
|
||||
|
||||
return ai.GetState().GetStateNumber() == bi.GetState().GetStateNumber() &&
|
||||
ai.GetAlt() == bi.GetAlt() &&
|
||||
ai.GetSemanticContext().equals(bi.GetSemanticContext())
|
||||
var nums = ai.GetState().GetStateNumber() == bi.GetState().GetStateNumber()
|
||||
var alts = ai.GetAlt() == bi.GetAlt()
|
||||
var cons = ai.GetSemanticContext().equals(bi.GetSemanticContext())
|
||||
|
||||
return nums && alts && cons
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package antlr
|
||||
|
||||
var ATNDeserializationOptionsdefaultOptions = &ATNDeserializationOptions{true, false, false}
|
||||
|
||||
type ATNDeserializationOptions struct {
|
||||
readOnly bool
|
||||
verifyATN bool
|
||||
|
@ -7,7 +9,7 @@ type ATNDeserializationOptions struct {
|
|||
}
|
||||
|
||||
func NewATNDeserializationOptions(CopyFrom *ATNDeserializationOptions) *ATNDeserializationOptions {
|
||||
o := new(ATNDeserializationOptions)
|
||||
var o = new(ATNDeserializationOptions)
|
||||
|
||||
if CopyFrom != nil {
|
||||
o.readOnly = CopyFrom.readOnly
|
||||
|
@ -17,5 +19,3 @@ func NewATNDeserializationOptions(CopyFrom *ATNDeserializationOptions) *ATNDeser
|
|||
|
||||
return o
|
||||
}
|
||||
|
||||
var ATNDeserializationOptionsdefaultOptions = &ATNDeserializationOptions{true, false, false}
|
||||
|
|
|
@ -39,16 +39,11 @@ type ATNDeserializer struct {
|
|||
}
|
||||
|
||||
func NewATNDeserializer(options *ATNDeserializationOptions) *ATNDeserializer {
|
||||
|
||||
if options == nil {
|
||||
options = ATNDeserializationOptionsdefaultOptions
|
||||
}
|
||||
|
||||
a := new(ATNDeserializer)
|
||||
|
||||
a.deserializationOptions = options
|
||||
|
||||
return a
|
||||
return &ATNDeserializer{deserializationOptions: options}
|
||||
}
|
||||
|
||||
func stringInSlice(a string, list []string) int {
|
||||
|
@ -57,60 +52,64 @@ func stringInSlice(a string, list []string) int {
|
|||
return i
|
||||
}
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
// Determines if a particular serialized representation of an ATN supports
|
||||
// a particular feature, identified by the {@link UUID} used for serializing
|
||||
// the ATN at the time the feature was first introduced.
|
||||
//
|
||||
// @param feature The {@link UUID} marking the first time the feature was
|
||||
// supported in the serialized ATN.
|
||||
// @param actualUuid The {@link UUID} of the actual serialized ATN which is
|
||||
// currently being deserialized.
|
||||
// @return {@code true} if the {@code actualUuid} value represents a
|
||||
// serialized ATN at or after the feature identified by {@code feature} was
|
||||
// introduced otherwise, {@code false}.
|
||||
|
||||
// isFeatureSupported determines if a particular serialized representation of an
|
||||
// ATN supports a particular feature, identified by the UUID used for
|
||||
// serializing the ATN at the time the feature was first introduced. Feature is
|
||||
// the UUID marking the first time the feature was supported in the serialized
|
||||
// ATN. ActualUuid is the UUID of the actual serialized ATN which is currently
|
||||
// being deserialized. It returns true if actualUuid represents a serialized ATN
|
||||
// at or after the feature identified by feature was introduced, and otherwise
|
||||
// false.
|
||||
func (a *ATNDeserializer) isFeatureSupported(feature, actualUUID string) bool {
|
||||
var idx1 = stringInSlice(feature, SupportedUUIDs)
|
||||
|
||||
if idx1 < 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
var idx2 = stringInSlice(actualUUID, SupportedUUIDs)
|
||||
|
||||
return idx2 >= idx1
|
||||
}
|
||||
|
||||
func (a *ATNDeserializer) DeserializeFromUInt16(data []uint16) *ATN {
|
||||
|
||||
a.reset(utf16.Decode(data))
|
||||
a.checkVersion()
|
||||
a.checkUUID()
|
||||
|
||||
var atn = a.readATN()
|
||||
|
||||
a.readStates(atn)
|
||||
a.readRules(atn)
|
||||
a.readModes(atn)
|
||||
|
||||
var sets = a.readSets(atn)
|
||||
|
||||
a.readEdges(atn, sets)
|
||||
a.readDecisions(atn)
|
||||
a.readLexerActions(atn)
|
||||
a.markPrecedenceDecisions(atn)
|
||||
a.verifyATN(atn)
|
||||
|
||||
if a.deserializationOptions.generateRuleBypassTransitions && atn.grammarType == ATNTypeParser {
|
||||
a.generateRuleBypassTransitions(atn)
|
||||
// re-verify after modification
|
||||
// Re-verify after modification
|
||||
a.verifyATN(atn)
|
||||
}
|
||||
|
||||
return atn
|
||||
|
||||
}
|
||||
|
||||
func (a *ATNDeserializer) reset(data []rune) {
|
||||
|
||||
temp := make([]rune, len(data))
|
||||
var temp = make([]rune, len(data))
|
||||
|
||||
for i, c := range data {
|
||||
// don't adjust the first value since that's the version number
|
||||
// Don't adjust the first value since that's the version number
|
||||
if i == 0 {
|
||||
temp[i] = c
|
||||
} else {
|
||||
|
@ -124,6 +123,7 @@ func (a *ATNDeserializer) reset(data []rune) {
|
|||
|
||||
func (a *ATNDeserializer) checkVersion() {
|
||||
var version = a.readInt()
|
||||
|
||||
if version != SerializedVersion {
|
||||
panic("Could not deserialize ATN with version " + strconv.Itoa(version) + " (expected " + strconv.Itoa(SerializedVersion) + ").")
|
||||
}
|
||||
|
@ -131,20 +131,22 @@ func (a *ATNDeserializer) checkVersion() {
|
|||
|
||||
func (a *ATNDeserializer) checkUUID() {
|
||||
var uuid = a.readUUID()
|
||||
|
||||
if stringInSlice(uuid, SupportedUUIDs) < 0 {
|
||||
panic("Could not deserialize ATN with UUID: " + uuid + " (expected " + SerializedUUID + " or a legacy UUID).")
|
||||
}
|
||||
|
||||
a.uuid = uuid
|
||||
}
|
||||
|
||||
func (a *ATNDeserializer) readATN() *ATN {
|
||||
var grammarType = a.readInt()
|
||||
var maxTokenType = a.readInt()
|
||||
|
||||
return NewATN(grammarType, maxTokenType)
|
||||
}
|
||||
|
||||
func (a *ATNDeserializer) readStates(atn *ATN) {
|
||||
|
||||
var loopBackStateNumbers = make([]LoopEndStateIntPair, 0)
|
||||
var endStateNumbers = make([]BlockStartStateIntPair, 0)
|
||||
|
||||
|
@ -152,72 +154,97 @@ func (a *ATNDeserializer) readStates(atn *ATN) {
|
|||
|
||||
for i := 0; i < nstates; i++ {
|
||||
var stype = a.readInt()
|
||||
// ignore bad type of states
|
||||
|
||||
// Ignore bad types of states
|
||||
if stype == ATNStateInvalidType {
|
||||
atn.addState(nil)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
var ruleIndex = a.readInt()
|
||||
|
||||
if ruleIndex == 0xFFFF {
|
||||
ruleIndex = -1
|
||||
}
|
||||
|
||||
var s = a.stateFactory(stype, ruleIndex)
|
||||
|
||||
if stype == ATNStateLoopEnd {
|
||||
var loopBackStateNumber = a.readInt()
|
||||
|
||||
loopBackStateNumbers = append(loopBackStateNumbers, LoopEndStateIntPair{s.(*LoopEndState), loopBackStateNumber})
|
||||
} else if s2, ok := s.(BlockStartState); ok {
|
||||
var endStateNumber = a.readInt()
|
||||
|
||||
endStateNumbers = append(endStateNumbers, BlockStartStateIntPair{s2, endStateNumber})
|
||||
}
|
||||
|
||||
atn.addState(s)
|
||||
}
|
||||
// delay the assignment of loop back and end states until we know all the
|
||||
// state instances have been initialized
|
||||
|
||||
// Delay the assignment of loop back and end states until we know all the state
|
||||
// instances have been initialized
|
||||
for j := 0; j < len(loopBackStateNumbers); j++ {
|
||||
pair := loopBackStateNumbers[j]
|
||||
var pair = loopBackStateNumbers[j]
|
||||
|
||||
pair.item0.loopBackState = atn.states[pair.item1]
|
||||
}
|
||||
|
||||
for j := 0; j < len(endStateNumbers); j++ {
|
||||
pair := endStateNumbers[j]
|
||||
var pair = endStateNumbers[j]
|
||||
|
||||
pair.item0.setEndState(atn.states[pair.item1].(*BlockEndState))
|
||||
}
|
||||
|
||||
var numNonGreedyStates = a.readInt()
|
||||
|
||||
for j := 0; j < numNonGreedyStates; j++ {
|
||||
stateNumber := a.readInt()
|
||||
var stateNumber = a.readInt()
|
||||
|
||||
atn.states[stateNumber].(DecisionState).setNonGreedy(true)
|
||||
}
|
||||
|
||||
var numPrecedenceStates = a.readInt()
|
||||
|
||||
for j := 0; j < numPrecedenceStates; j++ {
|
||||
stateNumber := a.readInt()
|
||||
var stateNumber = a.readInt()
|
||||
|
||||
atn.states[stateNumber].(*RuleStartState).isPrecedenceRule = true
|
||||
}
|
||||
}
|
||||
|
||||
func (a *ATNDeserializer) readRules(atn *ATN) {
|
||||
|
||||
var nrules = a.readInt()
|
||||
|
||||
if atn.grammarType == ATNTypeLexer {
|
||||
atn.ruleToTokenType = make([]int, nrules) // initIntArray(nrules, 0)
|
||||
atn.ruleToTokenType = make([]int, nrules) // TODO: initIntArray(nrules, 0)
|
||||
}
|
||||
atn.ruleToStartState = make([]*RuleStartState, nrules) // initIntArray(nrules, 0)
|
||||
|
||||
atn.ruleToStartState = make([]*RuleStartState, nrules) // TODO: initIntArray(nrules, 0)
|
||||
|
||||
for i := 0; i < nrules; i++ {
|
||||
var s = a.readInt()
|
||||
var startState = atn.states[s].(*RuleStartState)
|
||||
|
||||
atn.ruleToStartState[i] = startState
|
||||
|
||||
if atn.grammarType == ATNTypeLexer {
|
||||
var tokenType = a.readInt()
|
||||
|
||||
if tokenType == 0xFFFF {
|
||||
tokenType = TokenEOF
|
||||
}
|
||||
|
||||
atn.ruleToTokenType[i] = tokenType
|
||||
}
|
||||
}
|
||||
|
||||
atn.ruleToStopState = make([]*RuleStopState, nrules) //initIntArray(nrules, 0)
|
||||
|
||||
for i := 0; i < len(atn.states); i++ {
|
||||
var state = atn.states[i]
|
||||
|
||||
if s2, ok := state.(*RuleStopState); ok {
|
||||
atn.ruleToStopState[s2.ruleIndex] = s2
|
||||
atn.ruleToStartState[s2.ruleIndex].stopState = s2
|
||||
|
@ -227,8 +254,10 @@ func (a *ATNDeserializer) readRules(atn *ATN) {
|
|||
|
||||
func (a *ATNDeserializer) readModes(atn *ATN) {
|
||||
var nmodes = a.readInt()
|
||||
|
||||
for i := 0; i < nmodes; i++ {
|
||||
var s = a.readInt()
|
||||
|
||||
atn.modeToStartState = append(atn.modeToStartState, atn.states[s].(*TokensStartState))
|
||||
}
|
||||
}
|
||||
|
@ -236,81 +265,102 @@ func (a *ATNDeserializer) readModes(atn *ATN) {
|
|||
func (a *ATNDeserializer) readSets(atn *ATN) []*IntervalSet {
|
||||
var sets = make([]*IntervalSet, 0)
|
||||
var m = a.readInt()
|
||||
|
||||
for i := 0; i < m; i++ {
|
||||
var iset = NewIntervalSet()
|
||||
|
||||
sets = append(sets, iset)
|
||||
|
||||
var n = a.readInt()
|
||||
var containsEOF = a.readInt()
|
||||
|
||||
if containsEOF != 0 {
|
||||
iset.addOne(-1)
|
||||
}
|
||||
|
||||
for j := 0; j < n; j++ {
|
||||
var i1 = a.readInt()
|
||||
var i2 = a.readInt()
|
||||
|
||||
iset.addRange(i1, i2)
|
||||
}
|
||||
}
|
||||
|
||||
return sets
|
||||
}
|
||||
|
||||
func (a *ATNDeserializer) readEdges(atn *ATN, sets []*IntervalSet) {
|
||||
|
||||
var nedges = a.readInt()
|
||||
|
||||
for i := 0; i < nedges; i++ {
|
||||
var src = a.readInt()
|
||||
var trg = a.readInt()
|
||||
var ttype = a.readInt()
|
||||
var arg1 = a.readInt()
|
||||
var arg2 = a.readInt()
|
||||
var arg3 = a.readInt()
|
||||
trans := a.edgeFactory(atn, ttype, src, trg, arg1, arg2, arg3, sets)
|
||||
var srcState = atn.states[src]
|
||||
var (
|
||||
src = a.readInt()
|
||||
trg = a.readInt()
|
||||
ttype = a.readInt()
|
||||
arg1 = a.readInt()
|
||||
arg2 = a.readInt()
|
||||
arg3 = a.readInt()
|
||||
trans = a.edgeFactory(atn, ttype, src, trg, arg1, arg2, arg3, sets)
|
||||
srcState = atn.states[src]
|
||||
)
|
||||
|
||||
srcState.AddTransition(trans, -1)
|
||||
}
|
||||
// edges for rule stop states can be derived, so they aren't serialized
|
||||
|
||||
// Edges for rule stop states can be derived, so they are not serialized
|
||||
for i := 0; i < len(atn.states); i++ {
|
||||
state := atn.states[i]
|
||||
var state = atn.states[i]
|
||||
|
||||
for j := 0; j < len(state.GetTransitions()); j++ {
|
||||
var t, ok = state.GetTransitions()[j].(*RuleTransition)
|
||||
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
var outermostPrecedenceReturn = -1
|
||||
|
||||
if atn.ruleToStartState[t.getTarget().GetRuleIndex()].isPrecedenceRule {
|
||||
if t.precedence == 0 {
|
||||
outermostPrecedenceReturn = t.getTarget().GetRuleIndex()
|
||||
}
|
||||
}
|
||||
|
||||
trans := NewEpsilonTransition(t.followState, outermostPrecedenceReturn)
|
||||
var trans = NewEpsilonTransition(t.followState, outermostPrecedenceReturn)
|
||||
|
||||
atn.ruleToStopState[t.getTarget().GetRuleIndex()].AddTransition(trans, -1)
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < len(atn.states); i++ {
|
||||
state := atn.states[i]
|
||||
var state = atn.states[i]
|
||||
|
||||
if s2, ok := state.(*BaseBlockStartState); ok {
|
||||
// we need to know the end state to set its start state
|
||||
// We need to know the end state to set its start state
|
||||
if s2.endState == nil {
|
||||
panic("IllegalState")
|
||||
}
|
||||
// block end states can only be associated to a single block start
|
||||
// state
|
||||
|
||||
// Block end states can only be associated to a single block start state
|
||||
if s2.endState.startState != nil {
|
||||
panic("IllegalState")
|
||||
}
|
||||
|
||||
s2.endState.startState = state
|
||||
}
|
||||
|
||||
if s2, ok := state.(*PlusLoopbackState); ok {
|
||||
for j := 0; j < len(s2.GetTransitions()); j++ {
|
||||
target := s2.GetTransitions()[j].getTarget()
|
||||
var target = s2.GetTransitions()[j].getTarget()
|
||||
|
||||
if t2, ok := target.(*PlusBlockStartState); ok {
|
||||
t2.loopBackState = state
|
||||
}
|
||||
}
|
||||
} else if s2, ok := state.(*StarLoopbackState); ok {
|
||||
for j := 0; j < len(s2.GetTransitions()); j++ {
|
||||
target := s2.GetTransitions()[j].getTarget()
|
||||
var target = s2.GetTransitions()[j].getTarget()
|
||||
|
||||
if t2, ok := target.(*StarLoopEntryState); ok {
|
||||
t2.loopBackState = state
|
||||
}
|
||||
|
@ -321,9 +371,11 @@ func (a *ATNDeserializer) readEdges(atn *ATN, sets []*IntervalSet) {
|
|||
|
||||
func (a *ATNDeserializer) readDecisions(atn *ATN) {
|
||||
var ndecisions = a.readInt()
|
||||
|
||||
for i := 0; i < ndecisions; i++ {
|
||||
var s = a.readInt()
|
||||
var decState = atn.states[s].(DecisionState)
|
||||
|
||||
atn.DecisionToState = append(atn.DecisionToState, decState)
|
||||
decState.setDecision(i)
|
||||
}
|
||||
|
@ -332,18 +384,25 @@ func (a *ATNDeserializer) readDecisions(atn *ATN) {
|
|||
func (a *ATNDeserializer) readLexerActions(atn *ATN) {
|
||||
if atn.grammarType == ATNTypeLexer {
|
||||
var count = a.readInt()
|
||||
|
||||
atn.lexerActions = make([]LexerAction, count) // initIntArray(count, nil)
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
var actionType = a.readInt()
|
||||
var data1 = a.readInt()
|
||||
|
||||
if data1 == 0xFFFF {
|
||||
data1 = -1
|
||||
}
|
||||
|
||||
var data2 = a.readInt()
|
||||
|
||||
if data2 == 0xFFFF {
|
||||
data2 = -1
|
||||
}
|
||||
|
||||
var lexerAction = a.lexerActionFactory(actionType, data1, data2)
|
||||
|
||||
atn.lexerActions[i] = lexerAction
|
||||
}
|
||||
}
|
||||
|
@ -351,21 +410,24 @@ func (a *ATNDeserializer) readLexerActions(atn *ATN) {
|
|||
|
||||
func (a *ATNDeserializer) generateRuleBypassTransitions(atn *ATN) {
|
||||
var count = len(atn.ruleToStartState)
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
atn.ruleToTokenType[i] = atn.maxTokenType + i + 1
|
||||
}
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
a.generateRuleBypassTransition(atn, i)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *ATNDeserializer) generateRuleBypassTransition(atn *ATN, idx int) {
|
||||
|
||||
var bypassStart = NewBasicBlockStartState()
|
||||
|
||||
bypassStart.ruleIndex = idx
|
||||
atn.addState(bypassStart)
|
||||
|
||||
var bypassStop = NewBlockEndState()
|
||||
|
||||
bypassStop.ruleIndex = idx
|
||||
atn.addState(bypassStop)
|
||||
|
||||
|
@ -379,16 +441,20 @@ func (a *ATNDeserializer) generateRuleBypassTransition(atn *ATN, idx int) {
|
|||
var endState ATNState
|
||||
|
||||
if atn.ruleToStartState[idx].isPrecedenceRule {
|
||||
// wrap from the beginning of the rule to the StarLoopEntryState
|
||||
// Wrap from the beginning of the rule to the StarLoopEntryState
|
||||
endState = nil
|
||||
|
||||
for i := 0; i < len(atn.states); i++ {
|
||||
state := atn.states[i]
|
||||
var state = atn.states[i]
|
||||
|
||||
if a.stateIsEndStateFor(state, idx) != nil {
|
||||
endState = state
|
||||
excludeTransition = state.(*StarLoopEntryState).loopBackState.GetTransitions()[0]
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if excludeTransition == nil {
|
||||
panic("Couldn't identify final state of the precedence rule prefix section.")
|
||||
}
|
||||
|
@ -396,34 +462,39 @@ func (a *ATNDeserializer) generateRuleBypassTransition(atn *ATN, idx int) {
|
|||
endState = atn.ruleToStopState[idx]
|
||||
}
|
||||
|
||||
// all non-excluded transitions that currently target end state need to
|
||||
// target blockEnd instead
|
||||
// All non-excluded transitions that currently target end state need to target
|
||||
// blockEnd instead
|
||||
for i := 0; i < len(atn.states); i++ {
|
||||
state := atn.states[i]
|
||||
var state = atn.states[i]
|
||||
|
||||
for j := 0; j < len(state.GetTransitions()); j++ {
|
||||
var transition = state.GetTransitions()[j]
|
||||
|
||||
if transition == excludeTransition {
|
||||
continue
|
||||
}
|
||||
|
||||
if transition.getTarget() == endState {
|
||||
transition.setTarget(bypassStop)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// all transitions leaving the rule start state need to leave blockStart
|
||||
// instead
|
||||
// All transitions leaving the rule start state need to leave blockStart instead
|
||||
var ruleToStartState = atn.ruleToStartState[idx]
|
||||
var count = len(ruleToStartState.GetTransitions())
|
||||
|
||||
for count > 0 {
|
||||
bypassStart.AddTransition(ruleToStartState.GetTransitions()[count-1], -1)
|
||||
ruleToStartState.SetTransitions([]Transition{ruleToStartState.GetTransitions()[len(ruleToStartState.GetTransitions())-1]})
|
||||
}
|
||||
// link the new states
|
||||
|
||||
// Link the new states
|
||||
atn.ruleToStartState[idx].AddTransition(NewEpsilonTransition(bypassStart, -1), -1)
|
||||
bypassStop.AddTransition(NewEpsilonTransition(endState, -1), -1)
|
||||
|
||||
var MatchState = NewBasicState()
|
||||
|
||||
atn.addState(MatchState)
|
||||
MatchState.AddTransition(NewAtomTransition(bypassStop, atn.ruleToTokenType[idx]), -1)
|
||||
bypassStart.AddTransition(NewEpsilonTransition(MatchState, -1), -1)
|
||||
|
@ -433,15 +504,18 @@ func (a *ATNDeserializer) stateIsEndStateFor(state ATNState, idx int) ATNState {
|
|||
if state.GetRuleIndex() != idx {
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, ok := state.(*StarLoopEntryState); !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
var maybeLoopEndState = state.GetTransitions()[len(state.GetTransitions())-1].getTarget()
|
||||
|
||||
if _, ok := maybeLoopEndState.(*LoopEndState); !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, ok := maybeLoopEndState.GetTransitions()[0].getTarget().(*RuleStopState)
|
||||
var _, ok = maybeLoopEndState.GetTransitions()[0].getTarget().(*RuleStopState)
|
||||
|
||||
if maybeLoopEndState.(*LoopEndState).epsilonOnlyTransitions && ok {
|
||||
return state
|
||||
|
@ -450,29 +524,23 @@ func (a *ATNDeserializer) stateIsEndStateFor(state ATNState, idx int) ATNState {
|
|||
return nil
|
||||
}
|
||||
|
||||
//
|
||||
// Analyze the {@link StarLoopEntryState} states in the specified ATN to set
|
||||
// the {@link StarLoopEntryState//precedenceRuleDecision} field to the
|
||||
// correct value.
|
||||
//
|
||||
// @param atn The ATN.
|
||||
//
|
||||
// markPrecedenceDecisions analyzes the StarLoopEntryState states in the
|
||||
// specified ATN to set the StarLoopEntryState.precedenceRuleDecision field to
|
||||
// the correct value.
|
||||
func (a *ATNDeserializer) markPrecedenceDecisions(atn *ATN) {
|
||||
for _, state := range atn.states {
|
||||
if _, ok := state.(*StarLoopEntryState); !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
// We analyze the ATN to determine if a ATN decision state is the
|
||||
// decision for the closure block that determines whether a
|
||||
// precedence rule should continue or complete.
|
||||
//
|
||||
if atn.ruleToStartState[state.GetRuleIndex()].isPrecedenceRule {
|
||||
|
||||
var maybeLoopEndState = state.GetTransitions()[len(state.GetTransitions())-1].getTarget()
|
||||
|
||||
if s3, ok := maybeLoopEndState.(*LoopEndState); ok {
|
||||
|
||||
_, ok2 := maybeLoopEndState.GetTransitions()[0].getTarget().(*RuleStopState)
|
||||
var _, ok2 = maybeLoopEndState.GetTransitions()[0].getTarget().(*RuleStopState)
|
||||
|
||||
if s3.epsilonOnlyTransitions && ok2 {
|
||||
state.(*StarLoopEntryState).precedenceRuleDecision = true
|
||||
|
@ -486,53 +554,67 @@ func (a *ATNDeserializer) verifyATN(atn *ATN) {
|
|||
if !a.deserializationOptions.verifyATN {
|
||||
return
|
||||
}
|
||||
// verify assumptions
|
||||
for i := 0; i < len(atn.states); i++ {
|
||||
|
||||
// Verify assumptions
|
||||
for i := 0; i < len(atn.states); i++ {
|
||||
var state = atn.states[i]
|
||||
|
||||
if state == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
a.checkCondition(state.GetEpsilonOnlyTransitions() || len(state.GetTransitions()) <= 1, "")
|
||||
|
||||
switch s2 := state.(type) {
|
||||
|
||||
case *PlusBlockStartState:
|
||||
a.checkCondition(s2.loopBackState != nil, "")
|
||||
case *StarLoopEntryState:
|
||||
|
||||
case *StarLoopEntryState:
|
||||
a.checkCondition(s2.loopBackState != nil, "")
|
||||
a.checkCondition(len(s2.GetTransitions()) == 2, "")
|
||||
|
||||
switch s2 := state.(type) {
|
||||
case *StarBlockStartState:
|
||||
_, ok2 := s2.GetTransitions()[1].getTarget().(*LoopEndState)
|
||||
var _, ok2 = s2.GetTransitions()[1].getTarget().(*LoopEndState)
|
||||
|
||||
a.checkCondition(ok2, "")
|
||||
a.checkCondition(!s2.nonGreedy, "")
|
||||
|
||||
case *LoopEndState:
|
||||
s3, ok2 := s2.GetTransitions()[1].getTarget().(*StarBlockStartState)
|
||||
var s3, ok2 = s2.GetTransitions()[1].getTarget().(*StarBlockStartState)
|
||||
|
||||
a.checkCondition(ok2, "")
|
||||
a.checkCondition(s3.nonGreedy, "")
|
||||
|
||||
default:
|
||||
panic("IllegalState")
|
||||
}
|
||||
|
||||
case *StarLoopbackState:
|
||||
a.checkCondition(len(state.GetTransitions()) == 1, "")
|
||||
_, ok2 := state.GetTransitions()[0].getTarget().(*StarLoopEntryState)
|
||||
|
||||
var _, ok2 = state.GetTransitions()[0].getTarget().(*StarLoopEntryState)
|
||||
|
||||
a.checkCondition(ok2, "")
|
||||
|
||||
case *LoopEndState:
|
||||
a.checkCondition(s2.loopBackState != nil, "")
|
||||
|
||||
case *RuleStartState:
|
||||
a.checkCondition(s2.stopState != nil, "")
|
||||
|
||||
case *BaseBlockStartState:
|
||||
a.checkCondition(s2.endState != nil, "")
|
||||
|
||||
case *BlockEndState:
|
||||
a.checkCondition(s2.startState != nil, "")
|
||||
|
||||
case DecisionState:
|
||||
a.checkCondition(len(s2.GetTransitions()) <= 1 || s2.getDecision() >= 0, "")
|
||||
|
||||
default:
|
||||
_, ok := s2.(*RuleStopState)
|
||||
var _, ok = s2.(*RuleStopState)
|
||||
|
||||
a.checkCondition(len(s2.GetTransitions()) <= 1 || ok, "")
|
||||
}
|
||||
}
|
||||
|
@ -543,16 +625,20 @@ func (a *ATNDeserializer) checkCondition(condition bool, message string) {
|
|||
if message == "" {
|
||||
message = "IllegalState"
|
||||
}
|
||||
|
||||
panic(message)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *ATNDeserializer) readInt() int {
|
||||
v := a.data[a.pos]
|
||||
var v = a.data[a.pos]
|
||||
|
||||
a.pos++
|
||||
|
||||
return int(v)
|
||||
}
|
||||
|
||||
//TODO
|
||||
//func (a *ATNDeserializer) readLong() int64 {
|
||||
// panic("Not implemented")
|
||||
// var low = a.readInt32()
|
||||
|
@ -562,9 +648,11 @@ func (a *ATNDeserializer) readInt() int {
|
|||
|
||||
func createByteToHex() []string {
|
||||
var bth = make([]string, 256)
|
||||
|
||||
for i := 0; i < 256; i++ {
|
||||
bth[i] = strings.ToUpper(hex.EncodeToString([]byte{byte(i)}))
|
||||
}
|
||||
|
||||
return bth
|
||||
}
|
||||
|
||||
|
@ -572,11 +660,14 @@ var byteToHex = createByteToHex()
|
|||
|
||||
func (a *ATNDeserializer) readUUID() string {
|
||||
var bb = make([]int, 16)
|
||||
|
||||
for i := 7; i >= 0; i-- {
|
||||
var integer = a.readInt()
|
||||
|
||||
bb[(2*i)+1] = integer & 0xFF
|
||||
bb[2*i] = (integer >> 8) & 0xFF
|
||||
}
|
||||
|
||||
return byteToHex[bb[0]] + byteToHex[bb[1]] +
|
||||
byteToHex[bb[2]] + byteToHex[bb[3]] + "-" +
|
||||
byteToHex[bb[4]] + byteToHex[bb[5]] + "-" +
|
||||
|
@ -588,34 +679,44 @@ func (a *ATNDeserializer) readUUID() string {
|
|||
}
|
||||
|
||||
func (a *ATNDeserializer) edgeFactory(atn *ATN, typeIndex, src, trg, arg1, arg2, arg3 int, sets []*IntervalSet) Transition {
|
||||
|
||||
var target = atn.states[trg]
|
||||
|
||||
switch typeIndex {
|
||||
case TransitionEPSILON:
|
||||
return NewEpsilonTransition(target, -1)
|
||||
|
||||
case TransitionRANGE:
|
||||
if arg3 != 0 {
|
||||
return NewRangeTransition(target, TokenEOF, arg2)
|
||||
}
|
||||
|
||||
return NewRangeTransition(target, arg1, arg2)
|
||||
|
||||
case TransitionRULE:
|
||||
return NewRuleTransition(atn.states[arg1], arg2, arg3, target)
|
||||
|
||||
case TransitionPREDICATE:
|
||||
return NewPredicateTransition(target, arg1, arg2, arg3 != 0)
|
||||
|
||||
case TransitionPRECEDENCE:
|
||||
return NewPrecedencePredicateTransition(target, arg1)
|
||||
|
||||
case TransitionATOM:
|
||||
if arg3 != 0 {
|
||||
return NewAtomTransition(target, TokenEOF)
|
||||
}
|
||||
|
||||
return NewAtomTransition(target, arg1)
|
||||
|
||||
case TransitionACTION:
|
||||
return NewActionTransition(target, arg1, arg2, arg3 != 0)
|
||||
|
||||
case TransitionSET:
|
||||
return NewSetTransition(target, sets[arg1])
|
||||
|
||||
case TransitionNOTSET:
|
||||
return NewNotSetTransition(target, sets[arg1])
|
||||
|
||||
case TransitionWILDCARD:
|
||||
return NewWildcardTransition(target)
|
||||
}
|
||||
|
@ -624,41 +725,54 @@ func (a *ATNDeserializer) edgeFactory(atn *ATN, typeIndex, src, trg, arg1, arg2,
|
|||
}
|
||||
|
||||
func (a *ATNDeserializer) stateFactory(typeIndex, ruleIndex int) ATNState {
|
||||
|
||||
var s ATNState
|
||||
|
||||
switch typeIndex {
|
||||
case ATNStateInvalidType:
|
||||
return nil
|
||||
|
||||
case ATNStateBasic:
|
||||
s = NewBasicState()
|
||||
|
||||
case ATNStateRuleStart:
|
||||
s = NewRuleStartState()
|
||||
|
||||
case ATNStateBlockStart:
|
||||
s = NewBasicBlockStartState()
|
||||
|
||||
case ATNStatePlusBlockStart:
|
||||
s = NewPlusBlockStartState()
|
||||
|
||||
case ATNStateStarBlockStart:
|
||||
s = NewStarBlockStartState()
|
||||
|
||||
case ATNStateTokenStart:
|
||||
s = NewTokensStartState()
|
||||
|
||||
case ATNStateRuleStop:
|
||||
s = NewRuleStopState()
|
||||
|
||||
case ATNStateBlockEnd:
|
||||
s = NewBlockEndState()
|
||||
|
||||
case ATNStateStarLoopBack:
|
||||
s = NewStarLoopbackState()
|
||||
|
||||
case ATNStateStarLoopEntry:
|
||||
s = NewStarLoopEntryState()
|
||||
|
||||
case ATNStatePlusLoopBack:
|
||||
s = NewPlusLoopbackState()
|
||||
|
||||
case ATNStateLoopEnd:
|
||||
s = NewLoopEndState()
|
||||
|
||||
default:
|
||||
message := fmt.Sprintf("The specified state type %d is not valid.", typeIndex)
|
||||
panic(message)
|
||||
panic(fmt.Sprintf("state type %d is invalid", typeIndex))
|
||||
}
|
||||
|
||||
s.SetRuleIndex(ruleIndex)
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
|
@ -666,22 +780,29 @@ func (a *ATNDeserializer) lexerActionFactory(typeIndex, data1, data2 int) LexerA
|
|||
switch typeIndex {
|
||||
case LexerActionTypeChannel:
|
||||
return NewLexerChannelAction(data1)
|
||||
|
||||
case LexerActionTypeCustom:
|
||||
return NewLexerCustomAction(data1, data2)
|
||||
|
||||
case LexerActionTypeMode:
|
||||
return NewLexerModeAction(data1)
|
||||
|
||||
case LexerActionTypeMore:
|
||||
return LexerMoreActionINSTANCE
|
||||
|
||||
case LexerActionTypePopMode:
|
||||
return LexerPopModeActionINSTANCE
|
||||
|
||||
case LexerActionTypePushMode:
|
||||
return NewLexerPushModeAction(data1)
|
||||
|
||||
case LexerActionTypeSkip:
|
||||
return LexerSkipActionINSTANCE
|
||||
|
||||
case LexerActionTypeType:
|
||||
return NewLexerTypeAction(data1)
|
||||
|
||||
default:
|
||||
message := fmt.Sprintf("The specified lexer action typeIndex%d is not valid.", typeIndex)
|
||||
panic(message)
|
||||
panic(fmt.Sprintf("lexer action %d is invalid", typeIndex))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
package antlr
|
||||
|
||||
var ATNSimulatorError = NewDFAState(0x7FFFFFFF, NewBaseATNConfigSet(false))
|
||||
|
||||
type BaseATNSimulator struct {
|
||||
atn *ATN
|
||||
sharedContextCache *PredictionContextCache
|
||||
}
|
||||
|
||||
func NewBaseATNSimulator(atn *ATN, sharedContextCache *PredictionContextCache) *BaseATNSimulator {
|
||||
|
||||
b := new(BaseATNSimulator)
|
||||
var b = new(BaseATNSimulator)
|
||||
|
||||
b.atn = atn
|
||||
b.sharedContextCache = sharedContextCache
|
||||
|
@ -15,12 +16,12 @@ func NewBaseATNSimulator(atn *ATN, sharedContextCache *PredictionContextCache) *
|
|||
return b
|
||||
}
|
||||
|
||||
var ATNSimulatorError = NewDFAState(0x7FFFFFFF, NewBaseATNConfigSet(false))
|
||||
|
||||
func (b *BaseATNSimulator) getCachedContext(context PredictionContext) PredictionContext {
|
||||
if b.sharedContextCache == nil {
|
||||
return context
|
||||
}
|
||||
|
||||
var visited = make(map[PredictionContext]PredictionContext)
|
||||
|
||||
return getCachedBasePredictionContext(context, b.sharedContextCache, visited)
|
||||
}
|
||||
|
|
|
@ -2,8 +2,8 @@ package antlr
|
|||
|
||||
import "strconv"
|
||||
|
||||
// Constants for serialization.
|
||||
const (
|
||||
// constants for serialization
|
||||
ATNStateInvalidType = 0
|
||||
ATNStateBasic = 1
|
||||
ATNStateRuleStart = 2
|
||||
|
@ -48,34 +48,27 @@ type ATNState interface {
|
|||
}
|
||||
|
||||
type BaseATNState struct {
|
||||
// Which ATN are we in?
|
||||
atn *ATN
|
||||
stateNumber int
|
||||
stateType int
|
||||
ruleIndex int
|
||||
// NextTokenWithinRule caches lookahead during parsing. Not used during construction.
|
||||
NextTokenWithinRule *IntervalSet
|
||||
|
||||
// atn is the current ATN.
|
||||
atn *ATN
|
||||
|
||||
epsilonOnlyTransitions bool
|
||||
|
||||
// ruleIndex tracks the Rule index because there are no Rule objects at runtime.
|
||||
ruleIndex int
|
||||
|
||||
stateNumber int
|
||||
|
||||
stateType int
|
||||
|
||||
// Track the transitions emanating from this ATN state.
|
||||
transitions []Transition
|
||||
// Used to cache lookahead during parsing, not used during construction
|
||||
NextTokenWithinRule *IntervalSet
|
||||
}
|
||||
|
||||
func NewBaseATNState() *BaseATNState {
|
||||
|
||||
as := new(BaseATNState)
|
||||
|
||||
// Which ATN are we in?
|
||||
as.atn = nil
|
||||
as.stateNumber = ATNStateInvalidStateNumber
|
||||
as.stateType = ATNStateInvalidType
|
||||
as.ruleIndex = 0 // at runtime, we don't have Rule objects
|
||||
as.epsilonOnlyTransitions = false
|
||||
// Track the transitions emanating from this ATN state.
|
||||
as.transitions = make([]Transition, 0)
|
||||
// Used to cache lookahead during parsing, not used during construction
|
||||
as.NextTokenWithinRule = nil
|
||||
|
||||
return as
|
||||
return &BaseATNState{stateNumber: ATNStateInvalidStateNumber, stateType: ATNStateInvalidType}
|
||||
}
|
||||
|
||||
func (as *BaseATNState) GetRuleIndex() int {
|
||||
|
@ -147,11 +140,12 @@ func (as *BaseATNState) AddTransition(trans Transition, index int) {
|
|||
} else if as.epsilonOnlyTransitions != trans.getIsEpsilon() {
|
||||
as.epsilonOnlyTransitions = false
|
||||
}
|
||||
|
||||
if index == -1 {
|
||||
as.transitions = append(as.transitions, trans)
|
||||
} else {
|
||||
as.transitions = append(as.transitions[:index], append([]Transition{trans}, as.transitions[index:]...)...)
|
||||
// as.transitions.splice(index, 1, trans)
|
||||
// TODO: as.transitions.splice(index, 1, trans)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -160,11 +154,11 @@ type BasicState struct {
|
|||
}
|
||||
|
||||
func NewBasicState() *BasicState {
|
||||
b := new(BasicState)
|
||||
b.BaseATNState = NewBaseATNState()
|
||||
var b = NewBaseATNState()
|
||||
|
||||
b.stateType = ATNStateBasic
|
||||
return b
|
||||
|
||||
return &BasicState{BaseATNState: b}
|
||||
}
|
||||
|
||||
type DecisionState interface {
|
||||
|
@ -179,21 +173,12 @@ type DecisionState interface {
|
|||
|
||||
type BaseDecisionState struct {
|
||||
*BaseATNState
|
||||
|
||||
decision int
|
||||
nonGreedy bool
|
||||
}
|
||||
|
||||
func NewBaseDecisionState() *BaseDecisionState {
|
||||
|
||||
b := new(BaseDecisionState)
|
||||
|
||||
b.BaseATNState = NewBaseATNState()
|
||||
|
||||
b.decision = -1
|
||||
b.nonGreedy = false
|
||||
|
||||
return b
|
||||
return &BaseDecisionState{BaseATNState: NewBaseATNState(), decision: -1}
|
||||
}
|
||||
|
||||
func (s *BaseDecisionState) getDecision() int {
|
||||
|
@ -219,21 +204,14 @@ type BlockStartState interface {
|
|||
setEndState(*BlockEndState)
|
||||
}
|
||||
|
||||
// The start of a regular {@code (...)} block.
|
||||
// BaseBlockStartState is the start of a regular (...) block.
|
||||
type BaseBlockStartState struct {
|
||||
*BaseDecisionState
|
||||
|
||||
endState *BlockEndState
|
||||
}
|
||||
|
||||
func NewBlockStartState() *BaseBlockStartState {
|
||||
|
||||
b := new(BaseBlockStartState)
|
||||
|
||||
b.BaseDecisionState = NewBaseDecisionState()
|
||||
b.endState = nil
|
||||
|
||||
return b
|
||||
return &BaseBlockStartState{BaseDecisionState: NewBaseDecisionState()}
|
||||
}
|
||||
|
||||
func (s *BaseBlockStartState) getEndState() *BlockEndState {
|
||||
|
@ -249,123 +227,99 @@ type BasicBlockStartState struct {
|
|||
}
|
||||
|
||||
func NewBasicBlockStartState() *BasicBlockStartState {
|
||||
|
||||
b := new(BasicBlockStartState)
|
||||
|
||||
b.BaseBlockStartState = NewBlockStartState()
|
||||
var b = NewBlockStartState()
|
||||
|
||||
b.stateType = ATNStateBlockStart
|
||||
return b
|
||||
|
||||
return &BasicBlockStartState{BaseBlockStartState: b}
|
||||
}
|
||||
|
||||
// Terminal node of a simple {@code (a|b|c)} block.
|
||||
// BlockEndState is a terminal node of a simple (a|b|c) block.
|
||||
type BlockEndState struct {
|
||||
*BaseATNState
|
||||
|
||||
startState ATNState
|
||||
}
|
||||
|
||||
func NewBlockEndState() *BlockEndState {
|
||||
var b = NewBaseATNState()
|
||||
|
||||
b := new(BlockEndState)
|
||||
|
||||
b.BaseATNState = NewBaseATNState()
|
||||
b.stateType = ATNStateBlockEnd
|
||||
b.startState = nil
|
||||
|
||||
return b
|
||||
return &BlockEndState{BaseATNState: b}
|
||||
}
|
||||
|
||||
// The last node in the ATN for a rule, unless that rule is the start symbol.
|
||||
// In that case, there is one transition to EOF. Later, we might encode
|
||||
// references to all calls to this rule to compute FOLLOW sets for
|
||||
// error handling.
|
||||
//
|
||||
// RuleStopState is the last node in the ATN for a rule, unless that rule is the
|
||||
// start symbol. In that case, there is one transition to EOF. Later, we might
|
||||
// encode references to all calls to this rule to compute FOLLOW sets for error
|
||||
// handling.
|
||||
type RuleStopState struct {
|
||||
*BaseATNState
|
||||
}
|
||||
|
||||
func NewRuleStopState() *RuleStopState {
|
||||
r := new(RuleStopState)
|
||||
var b = NewBaseATNState()
|
||||
|
||||
r.BaseATNState = NewBaseATNState()
|
||||
r.stateType = ATNStateRuleStop
|
||||
return r
|
||||
b.stateType = ATNStateRuleStop
|
||||
|
||||
return &RuleStopState{BaseATNState: b}
|
||||
}
|
||||
|
||||
type RuleStartState struct {
|
||||
*BaseATNState
|
||||
|
||||
stopState ATNState
|
||||
isPrecedenceRule bool
|
||||
}
|
||||
|
||||
func NewRuleStartState() *RuleStartState {
|
||||
var b = NewBaseATNState()
|
||||
|
||||
r := new(RuleStartState)
|
||||
b.stateType = ATNStateRuleStart
|
||||
|
||||
r.BaseATNState = NewBaseATNState()
|
||||
r.stateType = ATNStateRuleStart
|
||||
r.stopState = nil
|
||||
r.isPrecedenceRule = false
|
||||
|
||||
return r
|
||||
return &RuleStartState{BaseATNState: b}
|
||||
}
|
||||
|
||||
// Decision state for {@code A+} and {@code (A|B)+}. It has two transitions:
|
||||
// one to the loop back to start of the block and one to exit.
|
||||
//
|
||||
// PlusLoopbackState is a decision state for A+ and (A|B)+. It has two
|
||||
// transitions: one to the loop back to start of the block, and one to exit.
|
||||
type PlusLoopbackState struct {
|
||||
*BaseDecisionState
|
||||
}
|
||||
|
||||
func NewPlusLoopbackState() *PlusLoopbackState {
|
||||
var b = NewBaseDecisionState()
|
||||
|
||||
p := new(PlusLoopbackState)
|
||||
b.stateType = ATNStatePlusLoopBack
|
||||
|
||||
p.BaseDecisionState = NewBaseDecisionState()
|
||||
|
||||
p.stateType = ATNStatePlusLoopBack
|
||||
return p
|
||||
return &PlusLoopbackState{BaseDecisionState: b}
|
||||
}
|
||||
|
||||
// Start of {@code (A|B|...)+} loop. Technically a decision state, but
|
||||
// we don't use for code generation somebody might need it, so I'm defining
|
||||
// it for completeness. In reality, the {@link PlusLoopbackState} node is the
|
||||
// real decision-making note for {@code A+}.
|
||||
//
|
||||
// PlusBlockStartState is the start of a (A|B|...)+ loop. Technically it is a
|
||||
// decision state; we don't use it for code generation. Somebody might need it,
|
||||
// it is included for completeness. In reality, PlusLoopbackState is the real
|
||||
// decision-making node for A+.
|
||||
type PlusBlockStartState struct {
|
||||
*BaseBlockStartState
|
||||
|
||||
loopBackState ATNState
|
||||
}
|
||||
|
||||
func NewPlusBlockStartState() *PlusBlockStartState {
|
||||
var b = NewBlockStartState()
|
||||
|
||||
p := new(PlusBlockStartState)
|
||||
b.stateType = ATNStatePlusBlockStart
|
||||
|
||||
p.BaseBlockStartState = NewBlockStartState()
|
||||
|
||||
p.stateType = ATNStatePlusBlockStart
|
||||
p.loopBackState = nil
|
||||
|
||||
return p
|
||||
return &PlusBlockStartState{BaseBlockStartState: b}
|
||||
}
|
||||
|
||||
// The block that begins a closure loop.
|
||||
// StarBlockStartState is the block that begins a closure loop.
|
||||
type StarBlockStartState struct {
|
||||
*BaseBlockStartState
|
||||
}
|
||||
|
||||
func NewStarBlockStartState() *StarBlockStartState {
|
||||
var b = NewBlockStartState()
|
||||
|
||||
s := new(StarBlockStartState)
|
||||
b.stateType = ATNStateStarBlockStart
|
||||
|
||||
s.BaseBlockStartState = NewBlockStartState()
|
||||
|
||||
s.stateType = ATNStateStarBlockStart
|
||||
|
||||
return s
|
||||
return &StarBlockStartState{BaseBlockStartState: b}
|
||||
}
|
||||
|
||||
type StarLoopbackState struct {
|
||||
|
@ -373,67 +327,51 @@ type StarLoopbackState struct {
|
|||
}
|
||||
|
||||
func NewStarLoopbackState() *StarLoopbackState {
|
||||
var b = NewBaseATNState()
|
||||
|
||||
s := new(StarLoopbackState)
|
||||
b.stateType = ATNStateStarLoopBack
|
||||
|
||||
s.BaseATNState = NewBaseATNState()
|
||||
|
||||
s.stateType = ATNStateStarLoopBack
|
||||
return s
|
||||
return &StarLoopbackState{BaseATNState: b}
|
||||
}
|
||||
|
||||
type StarLoopEntryState struct {
|
||||
*BaseDecisionState
|
||||
|
||||
loopBackState ATNState
|
||||
precedenceRuleDecision bool
|
||||
}
|
||||
|
||||
func NewStarLoopEntryState() *StarLoopEntryState {
|
||||
var b = NewBaseDecisionState()
|
||||
|
||||
s := new(StarLoopEntryState)
|
||||
b.stateType = ATNStateStarLoopEntry
|
||||
|
||||
s.BaseDecisionState = NewBaseDecisionState()
|
||||
|
||||
s.stateType = ATNStateStarLoopEntry
|
||||
s.loopBackState = nil
|
||||
|
||||
// Indicates whether s state can benefit from a precedence DFA during SLL decision making.
|
||||
s.precedenceRuleDecision = false
|
||||
|
||||
return s
|
||||
// False precedenceRuleDecision indicates whether s state can benefit from a precedence DFA during SLL decision making.
|
||||
return &StarLoopEntryState{BaseDecisionState: b}
|
||||
}
|
||||
|
||||
// Mark the end of a * or + loop.
|
||||
// LoopEndState marks the end of a * or + loop.
|
||||
type LoopEndState struct {
|
||||
*BaseATNState
|
||||
|
||||
loopBackState ATNState
|
||||
}
|
||||
|
||||
func NewLoopEndState() *LoopEndState {
|
||||
var b = NewBaseATNState()
|
||||
|
||||
l := new(LoopEndState)
|
||||
b.stateType = ATNStateLoopEnd
|
||||
|
||||
l.BaseATNState = NewBaseATNState()
|
||||
|
||||
l.stateType = ATNStateLoopEnd
|
||||
l.loopBackState = nil
|
||||
|
||||
return l
|
||||
return &LoopEndState{BaseATNState: b}
|
||||
}
|
||||
|
||||
// The Tokens rule start state linking to each lexer rule start state */
|
||||
// TokensStartState is the Tokens rule start state linking to each lexer rule start state.
|
||||
type TokensStartState struct {
|
||||
*BaseDecisionState
|
||||
}
|
||||
|
||||
func NewTokensStartState() *TokensStartState {
|
||||
var b = NewBaseDecisionState()
|
||||
|
||||
t := new(TokensStartState)
|
||||
b.stateType = ATNStateTokenStart
|
||||
|
||||
t.BaseDecisionState = NewBaseDecisionState()
|
||||
|
||||
t.stateType = ATNStateTokenStart
|
||||
return t
|
||||
return &TokensStartState{BaseDecisionState: b}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package antlr
|
||||
|
||||
// Represents the type of recognizer an ATN applies to.
|
||||
|
||||
// Represent the type of recognizer an ATN applies to.
|
||||
const (
|
||||
ATNTypeLexer = 0
|
||||
ATNTypeParser = 1
|
||||
|
|
|
@ -41,7 +41,7 @@ type BaseParser struct {
|
|||
|
||||
tracer *TraceListener
|
||||
parseListeners []ParseTreeListener
|
||||
_SyntaxErrors int
|
||||
_SyntaxErrors int
|
||||
}
|
||||
|
||||
// p.is all the parsing support code essentially most of it is error
|
||||
|
|
|
@ -26,7 +26,7 @@ type Recognizer interface {
|
|||
|
||||
type BaseRecognizer struct {
|
||||
listeners []ErrorListener
|
||||
state int
|
||||
state int
|
||||
|
||||
RuleNames []string
|
||||
LiteralNames []string
|
||||
|
|
|
@ -42,7 +42,7 @@ type BaseToken struct {
|
|||
tokenIndex int // from 0..n-1 of the token object in the input stream
|
||||
line int // line=1..n of the 1st character
|
||||
column int // beginning of the line at which it occurs, 0..n-1
|
||||
text string // text of the token.
|
||||
text string // text of the token.
|
||||
readOnly bool
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue