Skip to content

Commit

Permalink
feat: Corrects the test template when looking at parse tree correctness
Browse files Browse the repository at this point in the history
  o Uses reflection to check that the tree structure is all correct
  o Disables test that uses superClass as this basically doesn't work/isn't a concept in Go.

Signed-off-by: Jim.Idle <jimi@idle.ws>
  • Loading branch information
jimidle authored and parrt committed Apr 11, 2023
1 parent 449ee33 commit dd31a04
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ xyz
"""(a:3 x (b:2 y) z)
"""

[skip]
Go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package main
import (
"test/parser"
"github.com/antlr/antlr4/runtime/Go/antlr/v4"
"fmt"
"os"
"github.com/antlr/antlr4/runtime/Go/antlr/v4"
"os"
"test/parser"
)

<if(parserName)>

import "reflect"

type TreeShapeListener struct {
*parser.Base<grammarName>Listener
}
Expand All @@ -18,8 +21,14 @@ func NewTreeShapeListener() *TreeShapeListener {
func (this *TreeShapeListener) EnterEveryRule(ctx antlr.ParserRuleContext) {
for i := 0; i\<ctx.GetChildCount(); i++ {
child := ctx.GetChild(i)
parentR,ok := child.GetParent().(antlr.RuleNode)
if !ok || parentR.GetBaseRuleContext() != ctx.GetBaseRuleContext() {
parentR, ok := child.GetParent().(antlr.ParserRuleContext)

// Have to use reflect here - we need to compare the underlying pointers, but we
// do not know the types of the underlying structs, just that they will be the same
// type.
parPointer := reflect.ValueOf(parentR).Elem().Addr().Pointer()
ctxPointer := reflect.ValueOf(ctx).Elem().Addr().Pointer()
if !ok || parPointer != ctxPointer {
panic("Invalid parse tree shape detected.")
}
}
Expand Down

0 comments on commit dd31a04

Please sign in to comment.