Skip to content

Commit 289ea35

Browse files
Jim.Idleparrt
authored andcommitted
fix: Restore missing changes to v4 of go runtime
I had not though about this, but I guess some of the fix PRs did not have one fix that was in the legacy version of the runtime, copied in to the v4 branch. This commit fixes that. Signed-off-by: Jim.Idle <[email protected]>
1 parent 395a0cb commit 289ea35

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

runtime/Go/antlr/antlrdoc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ And the generate.sh file will look similar to this:
4848
#!/bin/sh
4949
5050
alias antlr4='java -Xmx500M -cp "./antlr4-4.11.1-complete.jar:$CLASSPATH" org.antlr.v4.Tool'
51-
antlr4 -Dlanguage=Go -no-visitor -package tgram *.g4
51+
antlr4 -Dlanguage=Go -no-visitor -package parser *.g4
5252
5353
depending on whether you want visitors or listeners or any other ANTLR options.
5454

runtime/Go/antlr/v4/prediction_context.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,20 @@ func predictionContextFromRuleContext(a *ATN, outerContext RuleContext) Predicti
376376
}
377377

378378
func merge(a, b PredictionContext, rootIsWildcard bool, mergeCache *DoubleDict) PredictionContext {
379-
// share same graph if both same
380-
if a == b {
379+
380+
// Share same graph if both same
381+
//
382+
if a == b || a.Equals(b) {
381383
return a
382384
}
383385

386+
// In Java, EmptyPredictionContext inherits from SingletonPredictionContext, and so the test
387+
// in java for SingletonPredictionContext will succeed and a new ArrayPredictionContext will be created
388+
// from it.
389+
// In go, EmptyPredictionContext does not equate to SingletonPredictionContext and so that conversion
390+
// will fail. We need to test for both Empty and Singleton and create an ArrayPredictionContext from
391+
// either of them.
392+
384393
ac, ok1 := a.(*BaseSingletonPredictionContext)
385394
bc, ok2 := b.(*BaseSingletonPredictionContext)
386395

@@ -397,14 +406,30 @@ func merge(a, b PredictionContext, rootIsWildcard bool, mergeCache *DoubleDict)
397406
return b
398407
}
399408
}
400-
// convert singleton so both are arrays to normalize
401-
if _, ok := a.(*BaseSingletonPredictionContext); ok {
402-
a = NewArrayPredictionContext([]PredictionContext{a.GetParent(0)}, []int{a.getReturnState(0)})
409+
410+
// Convert Singleton or Empty so both are arrays to normalize - We should not use the existing parameters
411+
// here.
412+
//
413+
// TODO: I think that maybe the Prediction Context structs should be redone as there is a chance we will see this mess again - maybe redo the logic here
414+
415+
var arp, arb *ArrayPredictionContext
416+
var ok bool
417+
if arp, ok = a.(*ArrayPredictionContext); ok {
418+
} else if _, ok = a.(*BaseSingletonPredictionContext); ok {
419+
arp = NewArrayPredictionContext([]PredictionContext{a.GetParent(0)}, []int{a.getReturnState(0)})
420+
} else if _, ok = a.(*EmptyPredictionContext); ok {
421+
arp = NewArrayPredictionContext([]PredictionContext{}, []int{})
403422
}
404-
if _, ok := b.(*BaseSingletonPredictionContext); ok {
405-
b = NewArrayPredictionContext([]PredictionContext{b.GetParent(0)}, []int{b.getReturnState(0)})
423+
424+
if arb, ok = b.(*ArrayPredictionContext); ok {
425+
} else if _, ok = b.(*BaseSingletonPredictionContext); ok {
426+
arb = NewArrayPredictionContext([]PredictionContext{b.GetParent(0)}, []int{b.getReturnState(0)})
427+
} else if _, ok = b.(*EmptyPredictionContext); ok {
428+
arb = NewArrayPredictionContext([]PredictionContext{}, []int{})
406429
}
407-
return mergeArrays(a.(*ArrayPredictionContext), b.(*ArrayPredictionContext), rootIsWildcard, mergeCache)
430+
431+
// Both arp and arb
432+
return mergeArrays(arp, arb, rootIsWildcard, mergeCache)
408433
}
409434

410435
// Merge two {@link SingletonBasePredictionContext} instances.
@@ -677,6 +702,7 @@ func mergeArrays(a, b *ArrayPredictionContext, rootIsWildcard bool, mergeCache *
677702

678703
// if we created same array as a or b, return that instead
679704
// TODO: track whether this is possible above during merge sort for speed
705+
// TODO: In go, I do not think we can just do M == xx as M is a brand new allocation. This could be causing allocation problems
680706
if M == a {
681707
if mergeCache != nil {
682708
mergeCache.set(a.Hash(), b.Hash(), a)

0 commit comments

Comments
 (0)