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: #2016 Fix Go template list reference, go runtime and got test te… #3848

Merged
merged 1 commit into from
Sep 2, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ expression
a and b

[skip]
Go
Cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ AppendStr(a,b) ::= "<a> + <b>"

Concat(a,b) ::= "<a><b>"

AssertIsList(v) ::= "TODO!!"
AssertIsList(v) ::= <<
// A noddy range over the list will not compile if it is not getting a slice
// however, Go will not compile the generated code if the slice vs single value is wrong.
// Makes the Java based tests suite work though.
parrt marked this conversation as resolved.
Show resolved Hide resolved
j1__ := make([]interface{}, len(<v>))
j2__ := <v>
for j3__ := range j2__ {
j1__[j3__] = j2__[j3__]
}
>>

AssignLocal(s, v) ::= "<s> = <v>;"

Expand Down
42 changes: 34 additions & 8 deletions runtime/Go/antlr/prediction_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,20 @@ func predictionContextFromRuleContext(a *ATN, outerContext RuleContext) Predicti
}

func merge(a, b PredictionContext, rootIsWildcard bool, mergeCache *DoubleDict) PredictionContext {
// share same graph if both same
if a == b {

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

// In Java, EmptyPredictionContext inherits from SingletonPredictionContext, and so the test
// in java for SingletonPredictionContext will succeed and a new ArrayPredictionContext will be created
// from it.
// In go, EmptyPredictionContext does not equate to SingletonPredictionContext and so that conversion
// will fail. We need to test for both Empty and Singleton and create an ArrayPredictionContext from
// either of them.

ac, ok1 := a.(*BaseSingletonPredictionContext)
bc, ok2 := b.(*BaseSingletonPredictionContext)

Expand All @@ -397,14 +406,30 @@ func merge(a, b PredictionContext, rootIsWildcard bool, mergeCache *DoubleDict)
return b
}
}
// convert singleton so both are arrays to normalize
if _, ok := a.(*BaseSingletonPredictionContext); ok {
a = NewArrayPredictionContext([]PredictionContext{a.GetParent(0)}, []int{a.getReturnState(0)})

// Convert Singleton or Empty so both are arrays to normalize - We should not use the existing parameters
// here.
//
// 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

var arp, arb *ArrayPredictionContext
var ok bool
if arp, ok = a.(*ArrayPredictionContext); ok {
} else if _, ok = a.(*BaseSingletonPredictionContext); ok {
arp = NewArrayPredictionContext([]PredictionContext{a.GetParent(0)}, []int{a.getReturnState(0)})
} else if _, ok = a.(*EmptyPredictionContext); ok {
arp = NewArrayPredictionContext([]PredictionContext{}, []int{})
}
if _, ok := b.(*BaseSingletonPredictionContext); ok {
b = NewArrayPredictionContext([]PredictionContext{b.GetParent(0)}, []int{b.getReturnState(0)})

if arb, ok = b.(*ArrayPredictionContext); ok {
} else if _, ok = b.(*BaseSingletonPredictionContext); ok {
arb = NewArrayPredictionContext([]PredictionContext{b.GetParent(0)}, []int{b.getReturnState(0)})
} else if _, ok = b.(*EmptyPredictionContext); ok {
arb = NewArrayPredictionContext([]PredictionContext{}, []int{})
}
return mergeArrays(a.(*ArrayPredictionContext), b.(*ArrayPredictionContext), rootIsWildcard, mergeCache)

// Both arp and arb
return mergeArrays(arp, arb, rootIsWildcard, mergeCache)
}

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

// if we created same array as a or b, return that instead
// TODO: track whether this is possible above during merge sort for speed
// 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
if M == a {
if mergeCache != nil {
mergeCache.set(a.Hash(), b.Hash(), a)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ QRetValueRef(a) ::= "<ctx(a)>.Get<a.dict;format={cap}>().Get<a.escapedName;forma
/** How to translate $tokenLabel */
TokenRef(t) ::= "<ctx(t)>.Get<t.escapedName;format={cap}>()"
LabelRef(t) ::= "<ctx(t)>.Get<t.escapedName;format={cap}>()"
ListLabelRef(t) ::= "<ctx(t)>.Get<ListLabelName(t.escapedName);format={cap}>"
ListLabelRef(t) ::= "<ctx(t)>.Get<t.escapedName;format={cap}>()"

SetAttr(s, rhsChunks) ::= "<ctx(s)>.Set<s.escapedName; format={cap}>(<rhsChunks>)"

Expand Down