Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix very minor code issues spotted by goreportcard.com #4276

Merged
merged 2 commits into from
Sep 4, 2023
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
2 changes: 1 addition & 1 deletion runtime/Go/antlr/v4/comparators.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package antlr
// type safety and avoid having to implement this for every type that we want to perform comparison on.
//
// This comparator works by using the standard Hash() and Equals() methods of the type T that is being compared. Which
// allows us to use it in any collection instance that does nto require a special hash or equals implementation.
// allows us to use it in any collection instance that does not require a special hash or equals implementation.
type ObjEqComparator[T Collectable[T]] struct{}

var (
Expand Down
3 changes: 1 addition & 2 deletions runtime/Go/antlr/v4/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,8 @@ func (b *BaseLexer) NextToken() Token {
continueOuter := false
for {
b.thetype = TokenInvalidType
ttype := LexerSkip

ttype = b.safeMatch()
ttype := b.safeMatch() // Defaults to LexerSkip

if b.input.LA(1) == TokenEOF {
b.hitEOF = true
Expand Down
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
}