Skip to content

Commit

Permalink
Merge branch 'feature/fix3875' of github.com:jimidle/antlr4 into jimi…
Browse files Browse the repository at this point in the history
…dle-feature/fix3875
  • Loading branch information
parrt committed Nov 20, 2022
2 parents 92adbf6 + 4b5d980 commit 1e377b4
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 15 deletions.
12 changes: 7 additions & 5 deletions runtime/Go/antlr/v4/atn_config_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

package antlr

import "fmt"
import (
"fmt"
)

type ATNConfigSet interface {
Hash() int
Expand Down Expand Up @@ -103,7 +105,7 @@ func (b *BaseATNConfigSet) Alts() *BitSet {
func NewBaseATNConfigSet(fullCtx bool) *BaseATNConfigSet {
return &BaseATNConfigSet{
cachedHash: -1,
configLookup: NewJStore[ATNConfig, Comparator[ATNConfig]](&ATNConfigComparator[ATNConfig]{}),
configLookup: NewJStore[ATNConfig, Comparator[ATNConfig]](aConfCompInst),
fullCtx: fullCtx,
}
}
Expand Down Expand Up @@ -159,7 +161,7 @@ func (b *BaseATNConfigSet) GetStates() *JStore[ATNState, Comparator[ATNState]] {

// states uses the standard comparator provided by the ATNState instance
//
states := NewJStore[ATNState, Comparator[ATNState]](&ObjEqComparator[ATNState]{})
states := NewJStore[ATNState, Comparator[ATNState]](aStateEqInst)

for i := 0; i < len(b.configs); i++ {
states.Put(b.configs[i].GetState())
Expand Down Expand Up @@ -314,7 +316,7 @@ func (b *BaseATNConfigSet) Clear() {

b.configs = make([]ATNConfig, 0)
b.cachedHash = -1
b.configLookup = NewJStore[ATNConfig, Comparator[ATNConfig]](&BaseATNConfigComparator[ATNConfig]{})
b.configLookup = NewJStore[ATNConfig, Comparator[ATNConfig]](atnConfCompInst)
}

func (b *BaseATNConfigSet) FullContext() bool {
Expand Down Expand Up @@ -397,7 +399,7 @@ func NewOrderedATNConfigSet() *OrderedATNConfigSet {
b := NewBaseATNConfigSet(false)

// This set uses the standard Hash() and Equals() from ATNConfig
b.configLookup = NewJStore[ATNConfig, Comparator[ATNConfig]](&ObjEqComparator[ATNConfig]{})
b.configLookup = NewJStore[ATNConfig, Comparator[ATNConfig]](aConfEqInst)

return &OrderedATNConfigSet{BaseATNConfigSet: b}
}
Expand Down
10 changes: 10 additions & 0 deletions runtime/Go/antlr/v4/comparators.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ package antlr
// allows us to use it in any collection instance that does nto require a special hash or equals implementation.
type ObjEqComparator[T Collectable[T]] struct{}

var (
aStateEqInst = &ObjEqComparator[ATNState]{}
aConfEqInst = &ObjEqComparator[ATNConfig]{}
aConfCompInst = &ATNConfigComparator[ATNConfig]{}
atnConfCompInst = &BaseATNConfigComparator[ATNConfig]{}
dfaStateEqInst = &ObjEqComparator[*DFAState]{}
semctxEqInst = &ObjEqComparator[SemanticContext]{}
atnAltCfgEqInst = &ATNAltConfigComparator[ATNConfig]{}
)

// Equals2 delegates to the Equals() method of type T
func (c *ObjEqComparator[T]) Equals2(o1, o2 T) bool {
return o1.Equals(o2)
Expand Down
4 changes: 2 additions & 2 deletions runtime/Go/antlr/v4/dfa.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewDFA(atnStartState DecisionState, decision int) *DFA {
dfa := &DFA{
atnStartState: atnStartState,
decision: decision,
states: NewJStore[*DFAState, *ObjEqComparator[*DFAState]](&ObjEqComparator[*DFAState]{}),
states: NewJStore[*DFAState, *ObjEqComparator[*DFAState]](dfaStateEqInst),
}
if s, ok := atnStartState.(*StarLoopEntryState); ok && s.precedenceRuleDecision {
dfa.precedenceDfa = true
Expand Down Expand Up @@ -95,7 +95,7 @@ func (d *DFA) getPrecedenceDfa() bool {
// true or nil otherwise, and d.precedenceDfa is updated.
func (d *DFA) setPrecedenceDfa(precedenceDfa bool) {
if d.getPrecedenceDfa() != precedenceDfa {
d.states = NewJStore[*DFAState, *ObjEqComparator[*DFAState]](&ObjEqComparator[*DFAState]{})
d.states = NewJStore[*DFAState, *ObjEqComparator[*DFAState]](dfaStateEqInst)
d.numstates = 0

if precedenceDfa {
Expand Down
5 changes: 4 additions & 1 deletion runtime/Go/antlr/v4/jcollect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package antlr
// Use of this file is governed by the BSD 3-clause license that
// can be found in the LICENSE.txt file in the project root.

import "sort"
import (
"sort"
)

// Collectable is an interface that a struct should implement if it is to be
// usable as a key in these collections.
Expand Down Expand Up @@ -149,6 +151,7 @@ func NewJMap[K, V any, C Comparator[K]](comparator Comparator[K]) *JMap[K, V, C]

func (m *JMap[K, V, C]) Put(key K, val V) {
kh := m.comparator.Hash1(key)

m.store[kh] = append(m.store[kh], &entry[K, V]{key, val})
m.len++
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/Go/antlr/v4/ll1_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (la *LL1Analyzer) getDecisionLookahead(s ATNState) []*IntervalSet {
look := make([]*IntervalSet, count)
for alt := 0; alt < count; alt++ {
look[alt] = NewIntervalSet()
lookBusy := NewJStore[ATNConfig, Comparator[ATNConfig]](&ObjEqComparator[ATNConfig]{})
lookBusy := NewJStore[ATNConfig, Comparator[ATNConfig]](aConfEqInst)
seeThruPreds := false // fail to get lookahead upon pred
la.look1(s.GetTransitions()[alt].getTarget(), nil, BasePredictionContextEMPTY, look[alt], lookBusy, NewBitSet(), seeThruPreds, false)
// Wipe out lookahead for la alternative if we found nothing
Expand Down Expand Up @@ -76,7 +76,7 @@ func (la *LL1Analyzer) Look(s, stopState ATNState, ctx RuleContext) *IntervalSet
if ctx != nil {
lookContext = predictionContextFromRuleContext(s.GetATN(), ctx)
}
la.look1(s, stopState, lookContext, r, NewJStore[ATNConfig, Comparator[ATNConfig]](&ObjEqComparator[ATNConfig]{}), NewBitSet(), seeThruPreds, true)
la.look1(s, stopState, lookContext, r, NewJStore[ATNConfig, Comparator[ATNConfig]](aConfEqInst), NewBitSet(), seeThruPreds, true)
return r
}

Expand Down
4 changes: 2 additions & 2 deletions runtime/Go/antlr/v4/parser_atn_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ func (p *ParserATNSimulator) computeReachSet(closure ATNConfigSet, t int, fullCt
//
if reach == nil {
reach = NewBaseATNConfigSet(fullCtx)
closureBusy := NewJStore[ATNConfig, Comparator[ATNConfig]](&ObjEqComparator[ATNConfig]{})
closureBusy := NewJStore[ATNConfig, Comparator[ATNConfig]](aConfEqInst)
treatEOFAsEpsilon := t == TokenEOF
amount := len(intermediate.configs)
for k := 0; k < amount; k++ {
Expand Down Expand Up @@ -669,7 +669,7 @@ func (p *ParserATNSimulator) computeStartState(a ATNState, ctx RuleContext, full
for i := 0; i < len(a.GetTransitions()); i++ {
target := a.GetTransitions()[i].getTarget()
c := NewBaseATNConfig6(target, i+1, initialContext)
closureBusy := NewJStore[ATNConfig, Comparator[ATNConfig]](&BaseATNConfigComparator[ATNConfig]{})
closureBusy := NewJStore[ATNConfig, Comparator[ATNConfig]](atnConfCompInst)
p.closure(c, configs, closureBusy, true, fullCtx, false)
}
return configs
Expand Down
2 changes: 1 addition & 1 deletion runtime/Go/antlr/v4/prediction_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ func PredictionModeGetAlts(altsets []*BitSet) *BitSet {
// alt and not pred
// </pre>
func PredictionModegetConflictingAltSubsets(configs ATNConfigSet) []*BitSet {
configToAlts := NewJMap[ATNConfig, *BitSet, *ATNAltConfigComparator[ATNConfig]](&ATNAltConfigComparator[ATNConfig]{})
configToAlts := NewJMap[ATNConfig, *BitSet, *ATNAltConfigComparator[ATNConfig]](atnAltCfgEqInst)

for _, c := range configs.GetItems() {

Expand Down
4 changes: 2 additions & 2 deletions runtime/Go/antlr/v4/semantic_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ type AND struct {

func NewAND(a, b SemanticContext) *AND {

operands := NewJStore[SemanticContext, Comparator[SemanticContext]](&ObjEqComparator[SemanticContext]{})
operands := NewJStore[SemanticContext, Comparator[SemanticContext]](semctxEqInst)
if aa, ok := a.(*AND); ok {
for _, o := range aa.opnds {
operands.Put(o)
Expand Down Expand Up @@ -349,7 +349,7 @@ type OR struct {

func NewOR(a, b SemanticContext) *OR {

operands := NewJStore[SemanticContext, Comparator[SemanticContext]](&ObjEqComparator[SemanticContext]{})
operands := NewJStore[SemanticContext, Comparator[SemanticContext]](semctxEqInst)
if aa, ok := a.(*OR); ok {
for _, o := range aa.opnds {
operands.Put(o)
Expand Down

0 comments on commit 1e377b4

Please sign in to comment.