Skip to content

Commit

Permalink
Added String requirement to Nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
RealA10N committed Aug 2, 2024
1 parent dd21fe9 commit f3d974f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions parse/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ type UnmanagedTokenView = view.UnmanagedView[lex.Token, uint32]
type TokenViewContext = view.ViewContext[lex.Token]

type Node interface {
// Return a reference to the node substring in the source code
View() source.UnmanagedSourceView

// Regenerate ("format") the code to a unique, single representation.
String(ctx source.SourceContext) string
}

type NodeParser[T any] interface {
Expand Down
4 changes: 4 additions & 0 deletions parse/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ func (n TypeNode) View() source.UnmanagedSourceView {
return n.view
}

func (n TypeNode) String(ctx source.SourceContext) string {
return string(n.view.Raw(ctx))
}

type TypeParser struct{}

func (TypeParser) Parse(v *TokenView) (node TypeNode, err ParsingError) {
Expand Down
9 changes: 9 additions & 0 deletions parse/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ import (
"github.com/stretchr/testify/assert"
)

func TestTypeNodeStringer(t *testing.T) {
typView, ctx := source.NewSourceView("$i32").Detach()
typTok := lex.Token{Type: lex.TypToken, View: typView}
tkns := view.NewView[lex.Token, uint32]([]lex.Token{typTok})
node, err := parse.TypeParser{}.Parse(&tkns)
assert.Nil(t, err)
assert.Equal(t, "$i32", node.String(ctx))
}

func TestTypeParserSimpleCase(t *testing.T) {
typView, ctx := source.NewSourceView("$i32").Detach()
typTkn := lex.Token{Type: lex.TypToken, View: typView}
Expand Down

0 comments on commit f3d974f

Please sign in to comment.