Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ Python3
JavaScript
TypeScript
PHP
Go

4 changes: 2 additions & 2 deletions runtime/Go/antlr/v4/array_prediction_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (a *ArrayPredictionContext) getReturnState(index int) int {

// Equals is the default comparison function for ArrayPredictionContext when no specialized
// implementation is needed for a collection
func (a *ArrayPredictionContext) Equals(o interface{}) bool {
func (a *ArrayPredictionContext) Equals(o Collectable[PredictionContext]) bool {
if a == o {
return true
}
Expand All @@ -76,7 +76,7 @@ func (a *ArrayPredictionContext) Equals(o interface{}) bool {
}

// Must compare the actual array elements and not just the array address
//
// TODO: The hash hashes in all the return states anyway, to we maybe don't need to compare them here?
return slices.Equal(a.returnStates, other.returnStates) &&
slices.EqualFunc(a.parents, other.parents, func(x, y PredictionContext) bool {
return x.Equals(y)
Expand Down
77 changes: 36 additions & 41 deletions runtime/Go/antlr/v4/atn_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@ import (
// state. The semantic context is the tree of semantic predicates encountered
// before reaching an ATN state.
type ATNConfig interface {

// Equals compares this ATNConfig to another for equality
Equals(o Collectable[ATNConfig]) bool

// Hash returns the hash code for this ATNConfig for use in maps and comparisons
Hash() int

// GetState returns the ATN state associated with this configuration
GetState() ATNState
// GetAlt returns the alternative associated with this configuration
GetAlt() int
// GetSemanticContext returns the semantic context associated with this configuration
GetSemanticContext() SemanticContext

// GetContext returns the rule invocation stack associated with this configuration
GetContext() PredictionContext
// SetContext sets the rule invocation stack associated with this configuration
SetContext(PredictionContext)

// GetReachesIntoOuterContext returns the count of references to an outer context from this configuration
GetReachesIntoOuterContext() int
// SetReachesIntoOuterContext sets the count of references to an outer context from this configuration
SetReachesIntoOuterContext(int)

// String returns a string representation of the configuration
String() string

getPrecedenceFilterSuppressed() bool
setPrecedenceFilterSuppressed(bool)
}
Expand Down Expand Up @@ -78,7 +78,7 @@ func NewBaseATNConfig5(state ATNState, alt int, context PredictionContext, seman
if semanticContext == nil {
panic("semanticContext cannot be nil") // TODO: Necessary?
}

return &BaseATNConfig{state: state, alt: alt, context: context, semanticContext: semanticContext}
}

Expand Down Expand Up @@ -108,15 +108,15 @@ func NewBaseATNConfig(c ATNConfig, state ATNState, context PredictionContext, se
if semanticContext == nil {
panic("semanticContext cannot be nil") // TODO: Remove this - probably put here for some bug that is now fixed
}

b := &BaseATNConfig{}
b.InitBaseATNConfig(c, state, c.GetAlt(), context, semanticContext)

return b
}

func (b *BaseATNConfig) InitBaseATNConfig(c ATNConfig, state ATNState, alt int, context PredictionContext, semanticContext SemanticContext) {

b.state = state
b.alt = alt
b.context = context
Expand Down Expand Up @@ -179,28 +179,28 @@ func (b *BaseATNConfig) Equals(o Collectable[ATNConfig]) bool {
} else if o == nil {
return false
}

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)
}

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
}

Expand All @@ -211,7 +211,7 @@ func (b *BaseATNConfig) Hash() int {
if b.context != nil {
c = b.context.Hash()
}

h := murmurInit(7)
h = murmurUpdate(h, b.state.GetStateNumber())
h = murmurUpdate(h, b.alt)
Expand All @@ -223,19 +223,19 @@ func (b *BaseATNConfig) Hash() int {
// String returns a string representation of the BaseATNConfig, usually used for debugging purposes
func (b *BaseATNConfig) String() string {
var s1, s2, s3 string

if b.context != nil {
s1 = ",[" + fmt.Sprint(b.context) + "]"
}

if b.semanticContext != SemanticContextNone {
s2 = "," + fmt.Sprint(b.semanticContext)
}

if b.reachesIntoOuterContext > 0 {
s3 = ",up=" + fmt.Sprint(b.reachesIntoOuterContext)
}

return fmt.Sprintf("(%v,%v%v%v%v)", b.state, b.alt, s1, s2, s3)
}

Expand All @@ -249,7 +249,7 @@ type LexerATNConfig struct {
}

func NewLexerATNConfig6(state ATNState, alt int, context PredictionContext) *LexerATNConfig {

return &LexerATNConfig{
BaseATNConfig: BaseATNConfig{
state: state,
Expand All @@ -274,7 +274,7 @@ func NewLexerATNConfig5(state ATNState, alt int, context PredictionContext, lexe

func NewLexerATNConfig4(c *LexerATNConfig, state ATNState) *LexerATNConfig {
lac := &LexerATNConfig{

lexerActionExecutor: c.lexerActionExecutor,
passedThroughNonGreedyDecision: checkNonGreedyDecision(c, state),
}
Expand Down Expand Up @@ -340,32 +340,27 @@ func (l *LexerATNConfig) Equals(other Collectable[ATNConfig]) bool {
return true
}
var otherT, ok = other.(*LexerATNConfig)

if l == other {
return true
} else if !ok {
if !ok {
return false
} else if l.passedThroughNonGreedyDecision != otherT.passedThroughNonGreedyDecision {
return false
}

var b bool

if l.lexerActionExecutor != nil {
b = !l.lexerActionExecutor.Equals(otherT.lexerActionExecutor)
} else {
b = otherT.lexerActionExecutor != nil

switch {
case l.lexerActionExecutor == nil && otherT.lexerActionExecutor == nil:
case l.lexerActionExecutor != nil && otherT.lexerActionExecutor != nil:
if !l.lexerActionExecutor.Equals(otherT.lexerActionExecutor) {
return false
}
default:
return false // One but not both, are nil
}

if b {
return false
}


return l.BaseATNConfig.Equals(&otherT.BaseATNConfig)
}

func checkNonGreedyDecision(source *LexerATNConfig, target ATNState) bool {
var ds, ok = target.(DecisionState)

return source.passedThroughNonGreedyDecision || (ok && ds.getNonGreedy())
}
Loading