Skip to content

Commit

Permalink
Merge pull request #3353 from KvanTTT/go-improvements
Browse files Browse the repository at this point in the history
Get rid of reflection in Go.stg
  • Loading branch information
parrt authored Dec 13, 2021
2 parents a850e96 + bd37cf0 commit 5d6a782
Showing 1 changed file with 30 additions and 8 deletions.
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

0 comments on commit 5d6a782

Please sign in to comment.