Fix bug in BaseATNConfigSet HashCode

This commit is contained in:
Peter Boyer 2017-03-12 14:10:53 -04:00
parent 86bf2fe089
commit 6ff55d0bfe
4 changed files with 12 additions and 17 deletions

View File

@ -48,7 +48,6 @@ type ATNConfigSet interface {
// about its elements and can combine similar configurations using a
// graph-structured stack.
type BaseATNConfigSet struct {
cachedHashString string
cachedHash int
// configLookup is used to determine whether two BaseATNConfigSets are equal. We
@ -94,7 +93,7 @@ type BaseATNConfigSet struct {
func NewBaseATNConfigSet(fullCtx bool) *BaseATNConfigSet {
return &BaseATNConfigSet{
cachedHashString: "-1",
cachedHash: -1,
configLookup: NewSet(nil, equalATNConfigs),
fullCtx: fullCtx,
}
@ -120,7 +119,7 @@ func (b *BaseATNConfigSet) Add(config ATNConfig, mergeCache *DoubleDict) bool {
existing := b.configLookup.add(config).(ATNConfig)
if existing == config {
b.cachedHashString = "-1"
b.cachedHash = -1
b.configs = append(b.configs, config) // Track order here
return true
@ -237,17 +236,13 @@ func (b *BaseATNConfigSet) HashCode() int {
}
func (b *BaseATNConfigSet) hashCodeConfigs() int {
h := 1
h := initHash(1)
for _, c := range b.configs {
h += 31 * h
if c != nil {
h += c.HashCode()
h = update(h, c.HashCode())
}
}
return h
return finish(h, len(b.configs))
}
func (b *BaseATNConfigSet) Length() int {
@ -280,7 +275,7 @@ func (b *BaseATNConfigSet) Clear() {
}
b.configs = make([]ATNConfig, 0)
b.cachedHashString = "-1"
b.cachedHash = -1
b.configLookup = NewSet(nil, equalATNConfigs)
}

View File

@ -14,7 +14,7 @@ type DFA struct {
// states is all the DFA states. Use Map to get the old state back; Set can only
// indicate whether it is there.
states map[string]*DFAState
states map[int]*DFAState
s0 *DFAState
@ -27,7 +27,7 @@ func NewDFA(atnStartState DecisionState, decision int) *DFA {
return &DFA{
atnStartState: atnStartState,
decision: decision,
states: make(map[string]*DFAState),
states: make(map[int]*DFAState),
}
}
@ -76,7 +76,7 @@ func (d *DFA) setPrecedenceStartState(precedence int, startState *DFAState) {
// true or nil otherwise, and d.precedenceDfa is updated.
func (d *DFA) setPrecedenceDfa(precedenceDfa bool) {
if d.precedenceDfa != precedenceDfa {
d.states = make(map[string]*DFAState)
d.states = make(map[int]*DFAState)
if precedenceDfa {
precedenceState := NewDFAState(-1, NewBaseATNConfigSet(false))
@ -93,7 +93,7 @@ func (d *DFA) setPrecedenceDfa(precedenceDfa bool) {
}
}
func (d *DFA) GetStates() map[string]*DFAState {
func (d *DFA) GetStates() map[int]*DFAState {
return d.states
}

View File

@ -582,7 +582,7 @@ func (l *LexerATNSimulator) addDFAState(configs ATNConfigSet) *DFAState {
proposed.lexerActionExecutor = firstConfigWithRuleStopState.(*LexerATNConfig).lexerActionExecutor
proposed.setPrediction(l.atn.ruleToTokenType[firstConfigWithRuleStopState.GetState().GetRuleIndex()])
}
hash := proposed.Hash()
hash := proposed.HashCode()
dfa := l.decisionToDFA[l.mode]
existing := dfa.GetStates()[hash]
if existing != nil {

View File

@ -1420,7 +1420,7 @@ func (p *ParserATNSimulator) addDFAState(dfa *DFA, D *DFAState) *DFAState {
if D == ATNSimulatorError {
return D
}
hash := D.Hash()
hash := D.HashCode()
var existing, ok = dfa.GetStates()[hash]
if ok {
return existing