diff --git a/contributors.txt b/contributors.txt index be27d19da4..6f26f3ef5a 100644 --- a/contributors.txt +++ b/contributors.txt @@ -263,7 +263,8 @@ YYYY/MM/DD, github id, Full name, email 2020/09/06, ArthurSonzogni, Sonzogni Arthur, arthursonzogni@gmail.com 2020/09/12, Clcanny, Charles Ruan, a837940593@gmail.com 2020/09/15, rmcgregor1990, Robert McGregor, rmcgregor1990@gmail.com +2020/09/15, dmitrys99, Dmitry Solomennikov, dmitrys99/mail/ru 2020/09/16, trenki2, Markus Trenkwalder, trenki2[at]gmx[dot]net 2020/10/08, Marti2203, Martin Mirchev, mirchevmartin2203@gmail.com 2020/10/20, adamwojs, Adam Wójs, adam[at]wojs.pl -2020/10/24, cliid, Jiwu Jang, jiwujang@naver.com \ No newline at end of file +2020/10/24, cliid, Jiwu Jang, jiwujang@naver.com diff --git a/runtime/Go/antlr/atn_config.go b/runtime/Go/antlr/atn_config.go index 0535d5246c..a206ab3a3e 100644 --- a/runtime/Go/antlr/atn_config.go +++ b/runtime/Go/antlr/atn_config.go @@ -6,6 +6,7 @@ package antlr import ( "fmt" + "strconv" ) type comparable interface { @@ -22,7 +23,8 @@ type ATNConfig interface { hash() int - GetState() ATNState + GetState(a *ATN) ATNState + GetStateValue() int GetAlt() int GetSemanticContext() SemanticContext @@ -33,6 +35,7 @@ type ATNConfig interface { SetReachesIntoOuterContext(int) String() string + GetStateString() string getPrecedenceFilterSuppressed() bool setPrecedenceFilterSuppressed(bool) @@ -40,7 +43,7 @@ type ATNConfig interface { type BaseATNConfig struct { precedenceFilterSuppressed bool - state ATNState + state int alt int context PredictionContext semanticContext SemanticContext @@ -57,11 +60,11 @@ func NewBaseATNConfig7(old *BaseATNConfig) *BaseATNConfig { // TODO: Dup } } -func NewBaseATNConfig6(state ATNState, alt int, context PredictionContext) *BaseATNConfig { +func NewBaseATNConfig6(state int, alt int, context PredictionContext) *BaseATNConfig { return NewBaseATNConfig5(state, alt, context, SemanticContextNone) } -func NewBaseATNConfig5(state ATNState, alt int, context PredictionContext, semanticContext SemanticContext) *BaseATNConfig { +func NewBaseATNConfig5(state int, alt int, context PredictionContext, semanticContext SemanticContext) *BaseATNConfig { if semanticContext == nil { panic("semanticContext cannot be nil") // TODO: Necessary? } @@ -69,23 +72,23 @@ func NewBaseATNConfig5(state ATNState, alt int, context PredictionContext, seman return &BaseATNConfig{state: state, alt: alt, context: context, semanticContext: semanticContext} } -func NewBaseATNConfig4(c ATNConfig, state ATNState) *BaseATNConfig { +func NewBaseATNConfig4(c ATNConfig, state int) *BaseATNConfig { return NewBaseATNConfig(c, state, c.GetContext(), c.GetSemanticContext()) } -func NewBaseATNConfig3(c ATNConfig, state ATNState, semanticContext SemanticContext) *BaseATNConfig { +func NewBaseATNConfig3(c ATNConfig, state int, semanticContext SemanticContext) *BaseATNConfig { return NewBaseATNConfig(c, state, c.GetContext(), semanticContext) } func NewBaseATNConfig2(c ATNConfig, semanticContext SemanticContext) *BaseATNConfig { - return NewBaseATNConfig(c, c.GetState(), c.GetContext(), semanticContext) + return NewBaseATNConfig(c, c.GetStateValue(), c.GetContext(), semanticContext) } -func NewBaseATNConfig1(c ATNConfig, state ATNState, context PredictionContext) *BaseATNConfig { +func NewBaseATNConfig1(c ATNConfig, state int, context PredictionContext) *BaseATNConfig { return NewBaseATNConfig(c, state, context, c.GetSemanticContext()) } -func NewBaseATNConfig(c ATNConfig, state ATNState, context PredictionContext, semanticContext SemanticContext) *BaseATNConfig { +func NewBaseATNConfig(c ATNConfig, state int, context PredictionContext, semanticContext SemanticContext) *BaseATNConfig { if semanticContext == nil { panic("semanticContext cannot be nil") } @@ -108,10 +111,19 @@ func (b *BaseATNConfig) setPrecedenceFilterSuppressed(v bool) { b.precedenceFilterSuppressed = v } -func (b *BaseATNConfig) GetState() ATNState { +func (b *BaseATNConfig) GetStateValue() int { return b.state } +func (b *BaseATNConfig) GetState(a *ATN) ATNState { + if a != nil && a.states != nil && b.state >= 0 && b.state <= len(a.states) { + return a.states[b.state] + } else { + panic("GetState") + return nil + } +} + func (b *BaseATNConfig) GetAlt() int { return b.alt } @@ -157,7 +169,7 @@ func (b *BaseATNConfig) equals(o interface{}) bool { } var ( - nums = b.state.GetStateNumber() == other.state.GetStateNumber() + nums = b.state == other.state alts = b.alt == other.alt cons = b.semanticContext.equals(other.semanticContext) sups = b.precedenceFilterSuppressed == other.precedenceFilterSuppressed @@ -173,13 +185,17 @@ func (b *BaseATNConfig) hash() int { } h := murmurInit(7) - h = murmurUpdate(h, b.state.GetStateNumber()) + h = murmurUpdate(h, b.state) h = murmurUpdate(h, b.alt) h = murmurUpdate(h, c) h = murmurUpdate(h, b.semanticContext.hash()) return murmurFinish(h, 4) } +func (b *BaseATNConfig) GetStateString() string { + return strconv.Itoa(b.state) +} + func (b *BaseATNConfig) String() string { var s1, s2, s3 string @@ -205,19 +221,19 @@ type LexerATNConfig struct { } func NewLexerATNConfig6(state ATNState, alt int, context PredictionContext) *LexerATNConfig { - return &LexerATNConfig{BaseATNConfig: NewBaseATNConfig5(state, alt, context, SemanticContextNone)} + return &LexerATNConfig{BaseATNConfig: NewBaseATNConfig5(state.GetStateNumber(), alt, context, SemanticContextNone)} } func NewLexerATNConfig5(state ATNState, alt int, context PredictionContext, lexerActionExecutor *LexerActionExecutor) *LexerATNConfig { return &LexerATNConfig{ - BaseATNConfig: NewBaseATNConfig5(state, alt, context, SemanticContextNone), + BaseATNConfig: NewBaseATNConfig5(state.GetStateNumber(), alt, context, SemanticContextNone), lexerActionExecutor: lexerActionExecutor, } } func NewLexerATNConfig4(c *LexerATNConfig, state ATNState) *LexerATNConfig { return &LexerATNConfig{ - BaseATNConfig: NewBaseATNConfig(c, state, c.GetContext(), c.GetSemanticContext()), + BaseATNConfig: NewBaseATNConfig(c, state.GetStateNumber(), c.GetContext(), c.GetSemanticContext()), lexerActionExecutor: c.lexerActionExecutor, passedThroughNonGreedyDecision: checkNonGreedyDecision(c, state), } @@ -225,7 +241,7 @@ func NewLexerATNConfig4(c *LexerATNConfig, state ATNState) *LexerATNConfig { func NewLexerATNConfig3(c *LexerATNConfig, state ATNState, lexerActionExecutor *LexerActionExecutor) *LexerATNConfig { return &LexerATNConfig{ - BaseATNConfig: NewBaseATNConfig(c, state, c.GetContext(), c.GetSemanticContext()), + BaseATNConfig: NewBaseATNConfig(c, state.GetStateNumber(), c.GetContext(), c.GetSemanticContext()), lexerActionExecutor: lexerActionExecutor, passedThroughNonGreedyDecision: checkNonGreedyDecision(c, state), } @@ -233,14 +249,18 @@ func NewLexerATNConfig3(c *LexerATNConfig, state ATNState, lexerActionExecutor * func NewLexerATNConfig2(c *LexerATNConfig, state ATNState, context PredictionContext) *LexerATNConfig { return &LexerATNConfig{ - BaseATNConfig: NewBaseATNConfig(c, state, context, c.GetSemanticContext()), + BaseATNConfig: NewBaseATNConfig(c, state.GetStateNumber(), context, c.GetSemanticContext()), lexerActionExecutor: c.lexerActionExecutor, passedThroughNonGreedyDecision: checkNonGreedyDecision(c, state), } } func NewLexerATNConfig1(state ATNState, alt int, context PredictionContext) *LexerATNConfig { - return &LexerATNConfig{BaseATNConfig: NewBaseATNConfig5(state, alt, context, SemanticContextNone)} + return &LexerATNConfig{BaseATNConfig: NewBaseATNConfig5(state.GetStateNumber(), alt, context, SemanticContextNone)} +} + +func (b *LexerATNConfig) GetStateString() string { + return strconv.Itoa(b.state) } func (l *LexerATNConfig) hash() int { @@ -251,7 +271,7 @@ func (l *LexerATNConfig) hash() int { f = 0 } h := murmurInit(7) - h = murmurUpdate(h, l.state.hash()) + h = murmurUpdate(h, l.GetStateValue()) h = murmurUpdate(h, l.alt) h = murmurUpdate(h, l.context.hash()) h = murmurUpdate(h, l.semanticContext.hash()) diff --git a/runtime/Go/antlr/atn_config_set.go b/runtime/Go/antlr/atn_config_set.go index d9f74755fa..ada2916c29 100644 --- a/runtime/Go/antlr/atn_config_set.go +++ b/runtime/Go/antlr/atn_config_set.go @@ -149,7 +149,7 @@ func (b *BaseATNConfigSet) GetStates() *Set { states := NewSet(nil, nil) for i := 0; i < len(b.configs); i++ { - states.add(b.configs[i].GetState()) + states.add(b.configs[i].GetStateValue()) } return states @@ -379,7 +379,7 @@ func equalATNConfigs(a, b interface{}) bool { return false } - nums := ai.GetState().GetStateNumber() == bi.GetState().GetStateNumber() + nums := ai.GetStateValue() == bi.GetStateValue() alts := ai.GetAlt() == bi.GetAlt() cons := ai.GetSemanticContext().equals(bi.GetSemanticContext()) diff --git a/runtime/Go/antlr/bitset.go b/runtime/Go/antlr/bitset.go new file mode 100644 index 0000000000..1706fe5caa --- /dev/null +++ b/runtime/Go/antlr/bitset.go @@ -0,0 +1,208 @@ +package antlr + +import ( + "strconv" + "strings" +) + +/* + BitSet datatype is intentionally made without slice and map + to reduce GC presure by avoiding pointers since values of this + type created huge number of times. + + There is an assumption that bitset has no more than 1024 values. + Length (cardinality) of this set is queued very frequently during parsing, + so it is stored not calculated. +*/ + +type BitSet struct { + a00 uint64 + a01 uint64 + a02 uint64 + a03 uint64 + a04 uint64 + a05 uint64 + a06 uint64 + a07 uint64 + a08 uint64 + a09 uint64 + a10 uint64 + a11 uint64 + a12 uint64 + a13 uint64 + a14 uint64 + a15 uint64 + + _length int +} + +func NewBitSet() *BitSet { + return &BitSet{} +} + +func incLen(value uint64, set uint64, len *int) uint64 { + if value & set == 0 { + *len++ + } + return value | set +} + +func decLen(value uint64, set uint64, len *int) uint64 { + if value & set != 0 { + *len-- + } + return value & ^set +} + +const log2Word64 = 6 +const bitSetMaxIndex = 1024 + +func (b *BitSet) contains(value int) bool { + if value > bitSetMaxIndex { + panic("bitSetMaxIndex") + } + ind := uint64(value) >> log2Word64 + val := uint64(1 << (uint64(value) & 63)) + switch ind { + case 0: return b.a00 & val != 0 + case 1: return b.a01 & val != 0 + case 2: return b.a02 & val != 0 + case 3: return b.a03 & val != 0 + case 4: return b.a04 & val != 0 + case 5: return b.a05 & val != 0 + case 6: return b.a06 & val != 0 + case 7: return b.a07 & val != 0 + case 8: return b.a08 & val != 0 + case 9: return b.a09 & val != 0 + case 10: return b.a10 & val != 0 + case 11: return b.a11 & val != 0 + case 12: return b.a12 & val != 0 + case 13: return b.a13 & val != 0 + case 14: return b.a14 & val != 0 + case 15: return b.a15 & val != 0 + } + return false +} + +func (b *BitSet) remove(value int) { + if value > bitSetMaxIndex { + panic("bitSetMaxIndex") + } + ind := uint64(value) >> log2Word64 + val := uint64(1 << (uint64(value) & 63)) + switch ind { + case 0: b.a00 = decLen(b.a00, val, &b._length) + case 1: b.a01 = decLen(b.a01, val, &b._length) + case 2: b.a02 = decLen(b.a02, val, &b._length) + case 3: b.a03 = decLen(b.a03, val, &b._length) + case 4: b.a04 = decLen(b.a04, val, &b._length) + case 5: b.a05 = decLen(b.a05, val, &b._length) + case 6: b.a06 = decLen(b.a06, val, &b._length) + case 7: b.a07 = decLen(b.a07, val, &b._length) + case 8: b.a08 = decLen(b.a08, val, &b._length) + case 9: b.a09 = decLen(b.a09, val, &b._length) + case 10: b.a10 = decLen(b.a10, val, &b._length) + case 11: b.a11 = decLen(b.a11, val, &b._length) + case 12: b.a12 = decLen(b.a12, val, &b._length) + case 13: b.a13 = decLen(b.a13, val, &b._length) + case 14: b.a14 = decLen(b.a14, val, &b._length) + case 15: b.a15 = decLen(b.a15, val, &b._length) + } +} + +func (b *BitSet) length() int { + return b._length +} + +func (b *BitSet) add(value int) { + if value > bitSetMaxIndex { + panic("bitSetMaxIndex") + } + ind := uint64(value) >> log2Word64 + val := uint64(1 << (uint64(value) & 63)) + switch ind { + case 0: b.a00 = incLen(b.a00, val, &b._length) + case 1: b.a01 = incLen(b.a01, val, &b._length) + case 2: b.a02 = incLen(b.a02, val, &b._length) + case 3: b.a03 = incLen(b.a03, val, &b._length) + case 4: b.a04 = incLen(b.a04, val, &b._length) + case 5: b.a05 = incLen(b.a05, val, &b._length) + case 6: b.a06 = incLen(b.a06, val, &b._length) + case 7: b.a07 = incLen(b.a07, val, &b._length) + case 8: b.a08 = incLen(b.a08, val, &b._length) + case 9: b.a09 = incLen(b.a09, val, &b._length) + case 10: b.a10 = incLen(b.a10, val, &b._length) + case 11: b.a11 = incLen(b.a11, val, &b._length) + case 12: b.a12 = incLen(b.a12, val, &b._length) + case 13: b.a13 = incLen(b.a13, val, &b._length) + case 14: b.a14 = incLen(b.a14, val, &b._length) + case 15: b.a15 = incLen(b.a15, val, &b._length) + } +} + +func (b *BitSet) minValue() int { + for i := 0; i < bitSetMaxIndex; i++ { + if b.contains(i) { + return i + } + } + return 0 +} +func (b *BitSet) values() []int { + ks := make([]int, 0) + i := 0 + for i = 0; i < bitSetMaxIndex; i++ { + if b.contains(i) { + ks = append(ks, i) + } + } + return ks +} + +func (b *BitSet) String() string { + vals := b.values() + valsS := make([]string, len(vals)) + + for i, val := range vals { + valsS[i] = strconv.Itoa(val) + } + return "{" + strings.Join(valsS, ", ") + "}" +} + +func (b *BitSet) clear() { + b.a00 = 0 + b.a01 = 0 + b.a02 = 0 + b.a03 = 0 + b.a04 = 0 + b.a05 = 0 + b.a06 = 0 + b.a07 = 0 + b.a08 = 0 + b.a09 = 0 + b.a10 = 0 + b.a11 = 0 + b.a12 = 0 + b.a13 = 0 + b.a14 = 0 + b.a15 = 0 +} + +func (b *BitSet) or(o *BitSet) { + b.a00 |= o.a00 + b.a01 |= o.a01 + b.a02 |= o.a02 + b.a03 |= o.a03 + b.a04 |= o.a04 + b.a05 |= o.a05 + b.a06 |= o.a06 + b.a07 |= o.a07 + b.a08 |= o.a08 + b.a09 |= o.a09 + b.a10 |= o.a10 + b.a11 |= o.a11 + b.a12 |= o.a12 + b.a13 |= o.a13 + b.a14 |= o.a14 + b.a15 |= o.a15 +} diff --git a/runtime/Go/antlr/lexer_atn_simulator.go b/runtime/Go/antlr/lexer_atn_simulator.go index 131364f75c..a0c9ef334a 100644 --- a/runtime/Go/antlr/lexer_atn_simulator.go +++ b/runtime/Go/antlr/lexer_atn_simulator.go @@ -277,7 +277,7 @@ func (l *LexerATNSimulator) getReachableConfigSet(input CharStream, closure ATNC fmt.Printf("testing %s at %s\n", l.GetTokenName(t), cfg.String()) // l.recog, true)) } - for _, trans := range cfg.GetState().GetTransitions() { + for _, trans := range cfg.GetState(l.ATN()).GetTransitions() { target := l.getReachableTarget(trans, t) if target != nil { lexerActionExecutor := cfg.(*LexerATNConfig).lexerActionExecutor @@ -344,12 +344,12 @@ func (l *LexerATNSimulator) closure(input CharStream, config *LexerATNConfig, co fmt.Println("closure(" + config.String() + ")") // config.String(l.recog, true) + ")") } - _, ok := config.state.(*RuleStopState) + _, ok := config.GetState(l.ATN()).(*RuleStopState) if ok { if LexerATNSimulatorDebug { if l.recog != nil { - fmt.Printf("closure at %s rule stop %s\n", l.recog.GetRuleNames()[config.state.GetRuleIndex()], config) + fmt.Printf("closure at %s rule stop %s\n", l.recog.GetRuleNames()[config.GetState(l.ATN()).GetRuleIndex()], config) } else { fmt.Printf("closure at rule stop %s\n", config) } @@ -361,7 +361,7 @@ func (l *LexerATNSimulator) closure(input CharStream, config *LexerATNConfig, co return true } - configs.Add(NewLexerATNConfig2(config, config.state, BasePredictionContextEMPTY), nil) + configs.Add(NewLexerATNConfig2(config, config.GetState(l.ATN()), BasePredictionContextEMPTY), nil) currentAltReachedAcceptState = true } if config.context != nil && !config.context.isEmpty() { @@ -377,13 +377,13 @@ func (l *LexerATNSimulator) closure(input CharStream, config *LexerATNConfig, co return currentAltReachedAcceptState } // optimization - if !config.state.GetEpsilonOnlyTransitions() { + if !config.GetState(l.ATN()).GetEpsilonOnlyTransitions() { if !currentAltReachedAcceptState || !config.passedThroughNonGreedyDecision { configs.Add(config, nil) } } - for j := 0; j < len(config.state.GetTransitions()); j++ { - trans := config.state.GetTransitions()[j] + for j := 0; j < len(config.GetState(l.ATN()).GetTransitions()); j++ { + trans := config.GetState(l.ATN()).GetTransitions()[j] cfg := l.getEpsilonTarget(input, config, trans, configs, speculative, treatEOFAsEpsilon) if cfg != nil { currentAltReachedAcceptState = l.closure(input, cfg, configs, @@ -570,7 +570,7 @@ func (l *LexerATNSimulator) addDFAState(configs ATNConfigSet) *DFAState { for _, cfg := range configs.GetItems() { - _, ok := cfg.GetState().(*RuleStopState) + _, ok := cfg.GetState(l.ATN()).(*RuleStopState) if ok { firstConfigWithRuleStopState = cfg @@ -580,7 +580,7 @@ func (l *LexerATNSimulator) addDFAState(configs ATNConfigSet) *DFAState { if firstConfigWithRuleStopState != nil { proposed.isAcceptState = true proposed.lexerActionExecutor = firstConfigWithRuleStopState.(*LexerATNConfig).lexerActionExecutor - proposed.setPrediction(l.atn.ruleToTokenType[firstConfigWithRuleStopState.GetState().GetRuleIndex()]) + proposed.setPrediction(l.atn.ruleToTokenType[firstConfigWithRuleStopState.GetState(l.ATN()).GetRuleIndex()]) } hash := proposed.hash() dfa := l.decisionToDFA[l.mode] diff --git a/runtime/Go/antlr/ll1_analyzer.go b/runtime/Go/antlr/ll1_analyzer.go index f5afd09b39..475bb059d1 100644 --- a/runtime/Go/antlr/ll1_analyzer.go +++ b/runtime/Go/antlr/ll1_analyzer.go @@ -128,7 +128,7 @@ func (la *LL1Analyzer) look2(s, stopState ATNState, ctx PredictionContext, look func (la *LL1Analyzer) look1(s, stopState ATNState, ctx PredictionContext, look *IntervalSet, lookBusy *Set, calledRuleStack *BitSet, seeThruPreds, addEOF bool) { - c := NewBaseATNConfig6(s, 0, ctx) + c := NewBaseATNConfig6(s.GetStateNumber(), 0, ctx) if lookBusy.contains(c) { return diff --git a/runtime/Go/antlr/parser_atn_simulator.go b/runtime/Go/antlr/parser_atn_simulator.go index 128b9a96d4..84a0587ce2 100644 --- a/runtime/Go/antlr/parser_atn_simulator.go +++ b/runtime/Go/antlr/parser_atn_simulator.go @@ -337,7 +337,7 @@ func (p *ParserATNSimulator) computeTargetState(dfa *DFA, previousD *DFAState, t D.isAcceptState = true D.configs.SetUniqueAlt(predictedAlt) D.setPrediction(predictedAlt) - } else if PredictionModehasSLLConflictTerminatingPrediction(p.predictionMode, reach) { + } else if PredictionModehasSLLConflictTerminatingPrediction(p.ATN(), p.predictionMode, reach) { // MORE THAN ONE VIABLE ALTERNATIVE D.configs.SetConflictingAlts(p.getConflictingAlts(reach)) D.requiresFullContext = true @@ -511,7 +511,7 @@ func (p *ParserATNSimulator) computeReachSet(closure ATNConfigSet, t int, fullCt fmt.Println("testing " + p.GetTokenName(t) + " at " + c.String()) } - _, ok := c.GetState().(*RuleStopState) + _, ok := c.GetState(p.ATN()).(*RuleStopState) if ok { if fullCtx || t == TokenEOF { @@ -526,11 +526,11 @@ func (p *ParserATNSimulator) computeReachSet(closure ATNConfigSet, t int, fullCt continue } - for j := 0; j < len(c.GetState().GetTransitions()); j++ { - trans := c.GetState().GetTransitions()[j] + for j := 0; j < len(c.GetState(p.ATN()).GetTransitions()); j++ { + trans := c.GetState(p.ATN()).GetTransitions()[j] target := p.getReachableTarget(trans, t) if target != nil { - cfg := NewBaseATNConfig4(c, target) + cfg := NewBaseATNConfig4(c, target.GetStateNumber()) intermediate.Add(cfg, p.mergeCache) if ParserATNSimulatorDebug { fmt.Println("added " + cfg.String() + " to intermediate") @@ -602,7 +602,7 @@ func (p *ParserATNSimulator) computeReachSet(closure ATNConfigSet, t int, fullCt // chooses an alternative Matching the longest overall sequence when // multiple alternatives are viable. // - if SkippedStopStates != nil && ((!fullCtx) || (!PredictionModehasConfigInRuleStopState(reach))) { + if SkippedStopStates != nil && ((!fullCtx) || (!PredictionModehasConfigInRuleStopState(p.ATN(), reach))) { for l := 0; l < len(SkippedStopStates); l++ { reach.Add(SkippedStopStates[l], p.mergeCache) } @@ -635,23 +635,23 @@ func (p *ParserATNSimulator) computeReachSet(closure ATNConfigSet, t int, fullCt // the configurations from {@code configs} which are in a rule stop state // func (p *ParserATNSimulator) removeAllConfigsNotInRuleStopState(configs ATNConfigSet, lookToEndOfRule bool) ATNConfigSet { - if PredictionModeallConfigsInRuleStopStates(configs) { + if PredictionModeallConfigsInRuleStopStates(p.ATN(), configs) { return configs } result := NewBaseATNConfigSet(configs.FullContext()) for _, config := range configs.GetItems() { - _, ok := config.GetState().(*RuleStopState) + _, ok := config.GetState(p.ATN()).(*RuleStopState) if ok { result.Add(config, p.mergeCache) continue } - if lookToEndOfRule && config.GetState().GetEpsilonOnlyTransitions() { - NextTokens := p.atn.NextTokens(config.GetState(), nil) + if lookToEndOfRule && config.GetState(p.ATN()).GetEpsilonOnlyTransitions() { + NextTokens := p.atn.NextTokens(config.GetState(p.ATN()), nil) if NextTokens.contains(TokenEpsilon) { - endOfRuleState := p.atn.ruleToStopState[config.GetState().GetRuleIndex()] - result.Add(NewBaseATNConfig4(config, endOfRuleState), p.mergeCache) + endOfRuleState := p.atn.ruleToStopState[config.GetState(p.ATN()).GetRuleIndex()] + result.Add(NewBaseATNConfig4(config, endOfRuleState.GetStateNumber()), p.mergeCache) } } } @@ -664,7 +664,7 @@ func (p *ParserATNSimulator) computeStartState(a ATNState, ctx RuleContext, full configs := NewBaseATNConfigSet(fullCtx) for i := 0; i < len(a.GetTransitions()); i++ { target := a.GetTransitions()[i].getTarget() - c := NewBaseATNConfig6(target, i+1, initialContext) + c := NewBaseATNConfig6(target.GetStateNumber(), i+1, initialContext) closureBusy := NewSet(nil, nil) p.closure(c, configs, closureBusy, true, fullCtx, false) } @@ -742,7 +742,7 @@ func (p *ParserATNSimulator) applyPrecedenceFilter(configs ATNConfigSet) ATNConf // the configuration was eliminated continue } - statesFromAlt1[config.GetState().GetStateNumber()] = config.GetContext() + statesFromAlt1[config.GetStateValue()] = config.GetContext() if updatedContext != config.GetSemanticContext() { configSet.Add(NewBaseATNConfig2(config, updatedContext), p.mergeCache) } else { @@ -759,7 +759,7 @@ func (p *ParserATNSimulator) applyPrecedenceFilter(configs ATNConfigSet) ATNConf // filter the prediction context for alternatives predicting alt>1 // (basically a graph subtraction algorithm). if !config.getPrecedenceFilterSuppressed() { - context := statesFromAlt1[config.GetState().GetStateNumber()] + context := statesFromAlt1[config.GetStateValue()] if context != nil && context.equals(config.GetContext()) { // eliminated continue @@ -892,7 +892,7 @@ func (p *ParserATNSimulator) GetAltThatFinishedDecisionEntryRule(configs ATNConf alts := NewIntervalSet() for _, c := range configs.GetItems() { - _, ok := c.GetState().(*RuleStopState) + _, ok := c.GetState(p.ATN()).(*RuleStopState) if c.GetReachesIntoOuterContext() > 0 || (ok && c.GetContext().hasEmptyPath()) { alts.addOne(c.GetAlt()) @@ -988,7 +988,7 @@ func (p *ParserATNSimulator) closureCheckingStopState(config ATNConfig, configs } } - _, ok := config.GetState().(*RuleStopState) + _, ok := config.GetState(p.ATN()).(*RuleStopState) if ok { // We hit rule end. If we have context info, use it // run thru all possible stack tops in ctx @@ -996,12 +996,12 @@ func (p *ParserATNSimulator) closureCheckingStopState(config ATNConfig, configs for i := 0; i < config.GetContext().length(); i++ { if config.GetContext().getReturnState(i) == BasePredictionContextEmptyReturnState { if fullCtx { - configs.Add(NewBaseATNConfig1(config, config.GetState(), BasePredictionContextEMPTY), p.mergeCache) + configs.Add(NewBaseATNConfig1(config, config.GetStateValue(), BasePredictionContextEMPTY), p.mergeCache) continue } else { // we have no context info, just chase follow links (if greedy) if ParserATNSimulatorDebug { - fmt.Println("FALLING off rule " + p.getRuleName(config.GetState().GetRuleIndex())) + fmt.Println("FALLING off rule " + p.getRuleName(config.GetState(p.ATN()).GetRuleIndex())) } p.closureWork(config, configs, closureBusy, collectPredicates, fullCtx, depth, treatEOFAsEpsilon) } @@ -1010,7 +1010,7 @@ func (p *ParserATNSimulator) closureCheckingStopState(config ATNConfig, configs returnState := p.atn.states[config.GetContext().getReturnState(i)] newContext := config.GetContext().GetParent(i) // "pop" return state - c := NewBaseATNConfig5(returnState, config.GetAlt(), newContext, config.GetSemanticContext()) + c := NewBaseATNConfig5(returnState.GetStateNumber(), config.GetAlt(), newContext, config.GetSemanticContext()) // While we have context to pop back from, we may have // gotten that context AFTER having falling off a rule. // Make sure we track that we are now out of context. @@ -1025,7 +1025,7 @@ func (p *ParserATNSimulator) closureCheckingStopState(config ATNConfig, configs } else { // else if we have no context info, just chase follow links (if greedy) if ParserATNSimulatorDebug { - fmt.Println("FALLING off rule " + p.getRuleName(config.GetState().GetRuleIndex())) + fmt.Println("FALLING off rule " + p.getRuleName(config.GetState(p.ATN()).GetRuleIndex())) } } } @@ -1034,7 +1034,7 @@ func (p *ParserATNSimulator) closureCheckingStopState(config ATNConfig, configs // Do the actual work of walking epsilon edges// func (p *ParserATNSimulator) closureWork(config ATNConfig, configs ATNConfigSet, closureBusy *Set, collectPredicates, fullCtx bool, depth int, treatEOFAsEpsilon bool) { - state := config.GetState() + state := config.GetState(p.ATN()) // optimization if !state.GetEpsilonOnlyTransitions() { configs.Add(config, p.mergeCache) @@ -1053,7 +1053,7 @@ func (p *ParserATNSimulator) closureWork(config ATNConfig, configs ATNConfigSet, } newDepth := depth - if _, ok := config.GetState().(*RuleStopState); ok { + if _, ok := config.GetState(p.ATN()).(*RuleStopState); ok { // target fell off end of rule mark resulting c as having dipped into outer context // We can't get here if incoming config was rule stop and we had context @@ -1109,13 +1109,13 @@ func (p *ParserATNSimulator) getEpsilonTarget(config ATNConfig, t Transition, co case TransitionACTION: return p.actionTransition(config, t.(*ActionTransition)) case TransitionEPSILON: - return NewBaseATNConfig4(config, t.getTarget()) + return NewBaseATNConfig4(config, t.getTarget().GetStateNumber()) case TransitionATOM: // EOF transitions act like epsilon transitions after the first EOF // transition is traversed if treatEOFAsEpsilon { if t.Matches(TokenEOF, 0, 1) { - return NewBaseATNConfig4(config, t.getTarget()) + return NewBaseATNConfig4(config, t.getTarget().GetStateNumber()) } } return nil @@ -1124,7 +1124,7 @@ func (p *ParserATNSimulator) getEpsilonTarget(config ATNConfig, t Transition, co // transition is traversed if treatEOFAsEpsilon { if t.Matches(TokenEOF, 0, 1) { - return NewBaseATNConfig4(config, t.getTarget()) + return NewBaseATNConfig4(config, t.getTarget().GetStateNumber()) } } return nil @@ -1133,7 +1133,7 @@ func (p *ParserATNSimulator) getEpsilonTarget(config ATNConfig, t Transition, co // transition is traversed if treatEOFAsEpsilon { if t.Matches(TokenEOF, 0, 1) { - return NewBaseATNConfig4(config, t.getTarget()) + return NewBaseATNConfig4(config, t.getTarget().GetStateNumber()) } } return nil @@ -1146,7 +1146,7 @@ func (p *ParserATNSimulator) actionTransition(config ATNConfig, t *ActionTransit if ParserATNSimulatorDebug { fmt.Println("ACTION edge " + strconv.Itoa(t.ruleIndex) + ":" + strconv.Itoa(t.actionIndex)) } - return NewBaseATNConfig4(config, t.getTarget()) + return NewBaseATNConfig4(config, t.getTarget().GetStateNumber()) } func (p *ParserATNSimulator) precedenceTransition(config ATNConfig, @@ -1171,14 +1171,14 @@ func (p *ParserATNSimulator) precedenceTransition(config ATNConfig, predSucceeds := pt.getPredicate().evaluate(p.parser, p.outerContext) p.input.Seek(currentPosition) if predSucceeds { - c = NewBaseATNConfig4(config, pt.getTarget()) // no pred context + c = NewBaseATNConfig4(config, pt.getTarget().GetStateNumber()) // no pred context } } else { newSemCtx := SemanticContextandContext(config.GetSemanticContext(), pt.getPredicate()) - c = NewBaseATNConfig3(config, pt.getTarget(), newSemCtx) + c = NewBaseATNConfig3(config, pt.getTarget().GetStateNumber(), newSemCtx) } } else { - c = NewBaseATNConfig4(config, pt.getTarget()) + c = NewBaseATNConfig4(config, pt.getTarget().GetStateNumber()) } if ParserATNSimulatorDebug { fmt.Println("config from pred transition=" + c.String()) @@ -1207,14 +1207,14 @@ func (p *ParserATNSimulator) predTransition(config ATNConfig, pt *PredicateTrans predSucceeds := pt.getPredicate().evaluate(p.parser, p.outerContext) p.input.Seek(currentPosition) if predSucceeds { - c = NewBaseATNConfig4(config, pt.getTarget()) // no pred context + c = NewBaseATNConfig4(config, pt.getTarget().GetStateNumber()) // no pred context } } else { newSemCtx := SemanticContextandContext(config.GetSemanticContext(), pt.getPredicate()) - c = NewBaseATNConfig3(config, pt.getTarget(), newSemCtx) + c = NewBaseATNConfig3(config, pt.getTarget().GetStateNumber(), newSemCtx) } } else { - c = NewBaseATNConfig4(config, pt.getTarget()) + c = NewBaseATNConfig4(config, pt.getTarget().GetStateNumber()) } if ParserATNSimulatorDebug { fmt.Println("config from pred transition=" + c.String()) @@ -1228,7 +1228,7 @@ func (p *ParserATNSimulator) ruleTransition(config ATNConfig, t *RuleTransition) } returnState := t.followState newContext := SingletonBasePredictionContextCreate(config.GetContext(), returnState.GetStateNumber()) - return NewBaseATNConfig1(config, t.getTarget(), newContext) + return NewBaseATNConfig1(config, t.getTarget().GetStateNumber(), newContext) } func (p *ParserATNSimulator) getConflictingAlts(configs ATNConfigSet) *BitSet { diff --git a/runtime/Go/antlr/prediction_context.go b/runtime/Go/antlr/prediction_context.go index 99acb333fa..f9688509dc 100644 --- a/runtime/Go/antlr/prediction_context.go +++ b/runtime/Go/antlr/prediction_context.go @@ -175,15 +175,7 @@ func (b *BaseSingletonPredictionContext) equals(other PredictionContext) bool { } func (b *BaseSingletonPredictionContext) hash() int { - h := murmurInit(1) - - if b.parentCtx == nil { - return murmurFinish(h, 0) - } - - h = murmurUpdate(h, b.parentCtx.hash()) - h = murmurUpdate(h, b.returnState) - return murmurFinish(h, 2) + return b.cachedHash } func (b *BaseSingletonPredictionContext) String() string { diff --git a/runtime/Go/antlr/prediction_mode.go b/runtime/Go/antlr/prediction_mode.go index 15718f912b..d7cc9f7dad 100644 --- a/runtime/Go/antlr/prediction_mode.go +++ b/runtime/Go/antlr/prediction_mode.go @@ -162,13 +162,13 @@ const ( // the configurations to strip out all of the predicates so that a standard // {@link ATNConfigSet} will merge everything ignoring predicates.

// -func PredictionModehasSLLConflictTerminatingPrediction(mode int, configs ATNConfigSet) bool { +func PredictionModehasSLLConflictTerminatingPrediction(a *ATN, mode int, configs ATNConfigSet) bool { // Configs in rule stop states indicate reaching the end of the decision // rule (local context) or end of start rule (full context). If all // configs meet this condition, then none of the configurations is able // to Match additional input so we terminate prediction. // - if PredictionModeallConfigsInRuleStopStates(configs) { + if PredictionModeallConfigsInRuleStopStates(a, configs) { return true } // pure SLL mode parsing @@ -202,9 +202,9 @@ func PredictionModehasSLLConflictTerminatingPrediction(mode int, configs ATNConf // @param configs the configuration set to test // @return {@code true} if any configuration in {@code configs} is in a // {@link RuleStopState}, otherwise {@code false} -func PredictionModehasConfigInRuleStopState(configs ATNConfigSet) bool { +func PredictionModehasConfigInRuleStopState(a *ATN, configs ATNConfigSet) bool { for _, c := range configs.GetItems() { - if _, ok := c.GetState().(*RuleStopState); ok { + if _, ok := c.GetState(a).(*RuleStopState); ok { return true } } @@ -219,10 +219,10 @@ func PredictionModehasConfigInRuleStopState(configs ATNConfigSet) bool { // @param configs the configuration set to test // @return {@code true} if all configurations in {@code configs} are in a // {@link RuleStopState}, otherwise {@code false} -func PredictionModeallConfigsInRuleStopStates(configs ATNConfigSet) bool { +func PredictionModeallConfigsInRuleStopStates(a *ATN, configs ATNConfigSet) bool { for _, c := range configs.GetItems() { - if _, ok := c.GetState().(*RuleStopState); !ok { + if _, ok := c.GetState(a).(*RuleStopState); !ok { return false } } @@ -488,7 +488,7 @@ func PredictionModegetConflictingAltSubsets(configs ATNConfigSet) []*BitSet { configToAlts := make(map[int]*BitSet) for _, c := range configs.GetItems() { - key := 31 * c.GetState().GetStateNumber() + c.GetContext().hash() + key := 31 * c.GetStateValue() + c.GetContext().hash() alts, ok := configToAlts[key] if !ok { @@ -517,10 +517,10 @@ func PredictionModeGetStateToAltMap(configs ATNConfigSet) *AltDict { m := NewAltDict() for _, c := range configs.GetItems() { - alts := m.Get(c.GetState().String()) + alts := m.Get(c.GetStateString()) if alts == nil { alts = NewBitSet() - m.put(c.GetState().String(), alts) + m.put(c.GetStateString(), alts) } alts.(*BitSet).add(c.GetAlt()) } diff --git a/runtime/Go/antlr/utils.go b/runtime/Go/antlr/utils.go index bba2ffae79..5131e98b8a 100644 --- a/runtime/Go/antlr/utils.go +++ b/runtime/Go/antlr/utils.go @@ -8,8 +8,6 @@ import ( "bytes" "errors" "fmt" - "sort" - "strconv" "strings" ) @@ -48,7 +46,7 @@ func (s *IntStack) Push(e int) { } type Set struct { - data map[int][]interface{} + data map[int]*[]interface{} hashcodeFunction func(interface{}) int equalsFunction func(interface{}, interface{}) bool } @@ -59,7 +57,7 @@ func NewSet( s := new(Set) - s.data = make(map[int][]interface{}) + s.data = make(map[int]*[]interface{}) if hashcodeFunction != nil { s.hashcodeFunction = hashcodeFunction @@ -111,19 +109,20 @@ func (s *Set) add(value interface{}) interface{} { values := s.data[key] if s.data[key] != nil { - for i := 0; i < len(values); i++ { - if s.equalsFunction(value, values[i]) { - return values[i] + for i := 0; i < len(*values); i++ { + if s.equalsFunction(value, (*values)[i]) { + return (*values)[i] } } - s.data[key] = append(s.data[key], value) + r := append(*(s.data)[key], value) + s.data[key] = &r return value } v := make([]interface{}, 1, 10) v[0] = value - s.data[key] = v + s.data[key] = &v return value } @@ -135,8 +134,8 @@ func (s *Set) contains(value interface{}) bool { values := s.data[key] if s.data[key] != nil { - for i := 0; i < len(values); i++ { - if s.equalsFunction(value, values[i]) { + for i := 0; i < len(*values); i++ { + if s.equalsFunction(value, (*values)[i]) { return true } } @@ -148,7 +147,7 @@ func (s *Set) values() []interface{} { var l []interface{} for _, v := range s.data { - l = append(l, v...) + l = append(l, *v...) } return l @@ -158,7 +157,7 @@ func (s *Set) String() string { r := "" for _, av := range s.data { - for _, v := range av { + for _, v := range *av { r += fmt.Sprint(v) } } @@ -166,94 +165,6 @@ func (s *Set) String() string { return r } -type BitSet struct { - data map[int]bool -} - -func NewBitSet() *BitSet { - b := new(BitSet) - b.data = make(map[int]bool) - return b -} - -func (b *BitSet) add(value int) { - b.data[value] = true -} - -func (b *BitSet) clear(index int) { - delete(b.data, index) -} - -func (b *BitSet) or(set *BitSet) { - for k := range set.data { - b.add(k) - } -} - -func (b *BitSet) remove(value int) { - delete(b.data, value) -} - -func (b *BitSet) contains(value int) bool { - return b.data[value] -} - -func (b *BitSet) values() []int { - ks := make([]int, len(b.data)) - i := 0 - for k := range b.data { - ks[i] = k - i++ - } - sort.Ints(ks) - return ks -} - -func (b *BitSet) minValue() int { - min := 2147483647 - - for k := range b.data { - if k < min { - min = k - } - } - - return min -} - -func (b *BitSet) equals(other interface{}) bool { - otherBitSet, ok := other.(*BitSet) - if !ok { - return false - } - - if len(b.data) != len(otherBitSet.data) { - return false - } - - for k, v := range b.data { - if otherBitSet.data[k] != v { - return false - } - } - - return true -} - -func (b *BitSet) length() int { - return len(b.data) -} - -func (b *BitSet) String() string { - vals := b.values() - valsS := make([]string, len(vals)) - - for i, val := range vals { - valsS[i] = strconv.Itoa(val) - } - return "{" + strings.Join(valsS, ", ") + "}" -} - type AltDict struct { data map[string]interface{} } @@ -357,29 +268,6 @@ func PrintArrayJavaStyle(sa []string) string { const uintSize = 32 << (^uint(0) >> 32 & 1) // 32 or 64 -// rotateLeft returns the value of x rotated left by (k mod UintSize) bits. -// To rotate x right by k bits, call RotateLeft(x, -k). -func rotateLeft(x uint, k int) uint { - if uintSize == 32 { - return uint(rotateLeft32(uint32(x), k)) - } - return uint(rotateLeft64(uint64(x), k)) -} - -// rotateLeft32 returns the value of x rotated left by (k mod 32) bits. -func rotateLeft32(x uint32, k int) uint32 { - const n = 32 - s := uint(k) & (n - 1) - return x<>(n-s) -} - -// rotateLeft64 returns the value of x rotated left by (k mod 64) bits. -func rotateLeft64(x uint64, k int) uint64 { - const n = 64 - s := uint(k) & (n - 1) - return x<>(n-s) -} - // murmur hash const ( @@ -395,18 +283,21 @@ func murmurInit(seed int) int { func murmurUpdate(h1 int, k1 int) int { var k1u uint k1u = uint(k1) * c1_32 - k1u = rotateLeft(k1u, 15) + //k1u = rotateLeft(k1u, 15) + k1u = k1u << 15 | k1u >> (uintSize - 15) k1u *= c2_32 var h1u = uint(h1) ^ k1u - k1u = rotateLeft(k1u, 13) + //k1u = rotateLeft(k1u, 13) + k1u = k1u << 13 | k1u >> (uintSize - 13) + h1u = h1u*5 + 0xe6546b64 return int(h1u) } func murmurFinish(h1 int, numberOfWords int) int { var h1u uint = uint(h1) - h1u ^= uint(numberOfWords * 4) + h1u ^= uint(numberOfWords << 2) h1u ^= h1u >> 16 h1u *= uint(0x85ebca6b) h1u ^= h1u >> 13