Skip to content

Commit

Permalink
fix: Trivial format corrections
Browse files Browse the repository at this point in the history
Signed-off-by: Jim.Idle <jimi@idle.ws>
  • Loading branch information
jimidle authored and parrt committed Sep 4, 2023
1 parent 940a704 commit a5602db
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
4 changes: 1 addition & 3 deletions runtime/Go/antlr/v4/parser_atn_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"strings"
)

var ()

// ClosureBusy is a store of ATNConfigs and is a tiny abstraction layer over
// a standard JStore so that we can use Lazy instantiation of the JStore, mostly
// to avoid polluting the stats module with a ton of JStore instances with nothing in them.
Expand Down Expand Up @@ -883,7 +881,7 @@ func (p *ParserATNSimulator) getPredicatePredictions(ambigAlts *BitSet, altToPre
// the ERROR state was reached, outerContext as the initial parser context from the paper
// or the parser stack at the instant before prediction commences.
//
// Teh func returns the value to return from [AdaptivePredict], or
// The func returns the value to return from [AdaptivePredict], or
// [ATNInvalidAltNumber] if a suitable alternative was not
// identified and [AdaptivePredict] should report an error instead.
func (p *ParserATNSimulator) getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(configs *ATNConfigSet, outerContext ParserRuleContext) int {
Expand Down
48 changes: 24 additions & 24 deletions runtime/Go/antlr/v4/prediction_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func NewArrayPredictionContext(parents []*PredictionContext, returnStates []int)
hash = murmurUpdate(hash, returnState)
}
hash = murmurFinish(hash, len(parents)<<1)

nec := &PredictionContext{}
nec.cachedHash = hash
nec.pcType = PredictionContextArray
Expand Down Expand Up @@ -138,7 +138,7 @@ func (p *PredictionContext) ArrayEquals(o Collectable[*PredictionContext]) bool
if p.cachedHash != other.Hash() {
return false // can't be same if hash is different
}

// Must compare the actual array elements and not just the array address
//
return slices.Equal(p.returnStates, other.returnStates) &&
Expand All @@ -155,20 +155,20 @@ func (p *PredictionContext) SingletonEquals(other Collectable[*PredictionContext
if otherP == nil {
return false
}

if p.cachedHash != otherP.Hash() {
return false // Can't be same if hash is different
}

if p.returnState != otherP.getReturnState(0) {
return false
}

// Both parents must be nil if one is
if p.parentCtx == nil {
return otherP.parentCtx == nil
}

return p.parentCtx.Equals(otherP.parentCtx)
}

Expand Down Expand Up @@ -225,27 +225,27 @@ func (p *PredictionContext) String() string {
return "$"
case PredictionContextSingleton:
var up string

if p.parentCtx == nil {
up = ""
} else {
up = p.parentCtx.String()
}

if len(up) == 0 {
if p.returnState == BasePredictionContextEmptyReturnState {
return "$"
}

return strconv.Itoa(p.returnState)
}

return strconv.Itoa(p.returnState) + " " + up
case PredictionContextArray:
if p.isEmpty() {
return "[]"
}

s := "["
for i := 0; i < len(p.returnStates); i++ {
if i > 0 {
Expand All @@ -263,7 +263,7 @@ func (p *PredictionContext) String() string {
}
}
return s + "]"

default:
return "unknown"
}
Expand Down Expand Up @@ -309,18 +309,18 @@ func predictionContextFromRuleContext(a *ATN, outerContext RuleContext) *Predict
parent := predictionContextFromRuleContext(a, outerContext.GetParent().(RuleContext))
state := a.states[outerContext.GetInvokingState()]
transition := state.GetTransitions()[0]

return SingletonBasePredictionContextCreate(parent, transition.(*RuleTransition).followState.GetStateNumber())
}

func merge(a, b *PredictionContext, rootIsWildcard bool, mergeCache *JPCMap) *PredictionContext {

// Share same graph if both same
//
if a == b || a.Equals(b) {
return a
}

if a.pcType == PredictionContextSingleton && b.pcType == PredictionContextSingleton {
return mergeSingletons(a, b, rootIsWildcard, mergeCache)
}
Expand All @@ -334,7 +334,7 @@ func merge(a, b *PredictionContext, rootIsWildcard bool, mergeCache *JPCMap) *Pr
return b
}
}

// Convert either Singleton or Empty to arrays, so that we can merge them
//
ara := convertToArray(a)
Expand Down Expand Up @@ -395,7 +395,7 @@ func mergeSingletons(a, b *PredictionContext, rootIsWildcard bool, mergeCache *J
return previous
}
}

rootMerge := mergeRoot(a, b, rootIsWildcard)
if rootMerge != nil {
if mergeCache != nil {
Expand Down Expand Up @@ -564,7 +564,7 @@ func mergeArrays(a, b *PredictionContext, rootIsWildcard bool, mergeCache *JPCMa
i := 0 // walks a
j := 0 // walks b
k := 0 // walks target M array

mergedReturnStates := make([]int, len(a.returnStates)+len(b.returnStates))
mergedParents := make([]*PredictionContext, len(a.returnStates)+len(b.returnStates))
// walk and merge to yield mergedParents, mergedReturnStates
Expand Down Expand Up @@ -626,9 +626,9 @@ func mergeArrays(a, b *PredictionContext, rootIsWildcard bool, mergeCache *JPCMa
mergedParents = mergedParents[0:k]
mergedReturnStates = mergedReturnStates[0:k]
}

M := NewArrayPredictionContext(mergedParents, mergedReturnStates)

// if we created same array as a or b, return that instead
// TODO: JI track whether this is possible above during merge sort for speed and possibly avoid an allocation
if M.Equals(a) {
Expand All @@ -650,7 +650,7 @@ func mergeArrays(a, b *PredictionContext, rootIsWildcard bool, mergeCache *JPCMa
return b
}
combineCommonParents(&mergedParents)

if mergeCache != nil {
mergeCache.Put(a, b, M)
}
Expand All @@ -666,7 +666,7 @@ func mergeArrays(a, b *PredictionContext, rootIsWildcard bool, mergeCache *JPCMa
//goland:noinspection GoUnusedFunction
func combineCommonParents(parents *[]*PredictionContext) {
uniqueParents := NewJStore[*PredictionContext, Comparator[*PredictionContext]](pContextEqInst, PredictionContextCollection, "combineCommonParents for PredictionContext")

for p := 0; p < len(*parents); p++ {
parent := (*parents)[p]
_, _ = uniqueParents.Put(parent)
Expand All @@ -685,7 +685,7 @@ func getCachedBasePredictionContext(context *PredictionContext, contextCache *Pr
if present {
return existing
}

existing, present = contextCache.Get(context)
if present {
visited.Put(context, existing)
Expand Down Expand Up @@ -722,6 +722,6 @@ func getCachedBasePredictionContext(context *PredictionContext, contextCache *Pr
contextCache.add(updated)
visited.Put(updated, updated)
visited.Put(context, updated)

return updated
}

0 comments on commit a5602db

Please sign in to comment.