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

Get rid of reflection in Go.stg #3353

Merged
merged 1 commit into from
Dec 13, 2021
Merged
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
38 changes: 30 additions & 8 deletions tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package parser // <file.grammarName>

import (
"fmt"
"reflect"
"strconv"

"github.com/antlr/antlr4/runtime/Go/antlr"
Expand All @@ -26,7 +25,6 @@ import (

// Suppress unused import errors
var _ = fmt.Printf
var _ = reflect.Copy
var _ = strconv.Itoa

<if(parser)>
Expand Down Expand Up @@ -932,7 +930,13 @@ ContextTokenListIndexedGetterDecl(t) ::= <<

ContextRuleGetterDecl(r) ::= <<
<r.name; format="cap">() I<r.ctxName> {
var t = s.GetTypedRuleContext(reflect.TypeOf((*I<r.ctxName>)(nil)).Elem(), 0)
var t antlr.RuleContext;
for _, ctx := range s.GetChildren() {
if _, ok := ctx.(I<r.ctxName>); ok {
t = ctx.(antlr.RuleContext);
break
}
}

if t == nil {
return nil
Expand All @@ -944,12 +948,20 @@ ContextRuleGetterDecl(r) ::= <<

ContextRuleListGetterDecl(r) ::= <<
All<r.name; format="cap">() []I<r.ctxName> {
var ts = s.GetTypedRuleContexts(reflect.TypeOf((*I<r.ctxName>)(nil)).Elem())
var tst = make([]I<r.ctxName>, len(ts))
children := s.GetChildren()
len := 0
for _, ctx := range children {
if _, ok := ctx.(I<r.ctxName>); ok {
len++
}
}

for i, t := range ts {
if t != nil {
tst := make([]I<r.ctxName>, len)
i := 0
for _, ctx := range children {
if t, ok := ctx.(I<r.ctxName>); ok {
tst[i] = t.(I<r.ctxName>)
i++
}
}

Expand All @@ -959,7 +971,17 @@ All<r.name; format="cap">() []I<r.ctxName> {

ContextRuleListIndexedGetterDecl(r) ::= <<
<r.name; format="cap">(i int) I<r.ctxName> {
var t = s.GetTypedRuleContext(reflect.TypeOf((*I<r.ctxName>)(nil)).Elem(), i)
var t antlr.RuleContext;
j := 0
for _, ctx := range s.GetChildren() {
if _, ok := ctx.(I<r.ctxName>); ok {
if j == i {
t = ctx.(antlr.RuleContext);
break
}
j++
}
}

if t == nil {
return nil
Expand Down