Skip to content

Commit

Permalink
Update parsing initializer_list_assignment test
Browse files Browse the repository at this point in the history
  • Loading branch information
pherrymason committed Jan 28, 2025
1 parent de8d995 commit 6421c1d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 39 deletions.
90 changes: 52 additions & 38 deletions server/internal/lsp/ast/factory/convert_expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package factory

import (
"fmt"
"github.com/pherrymason/c3-lsp/internal/lsp"
"github.com/pherrymason/c3-lsp/internal/lsp/ast"
"strings"
"testing"
Expand Down Expand Up @@ -301,12 +302,15 @@ func TestConvertToAST_declaration_with_assignment(t *testing.T) {
}
}

func TestConvertToAST_declaration_with_initializer_list_assingment(t *testing.T) {
func TestConvertToAST_declaration_with_initializer_list_assignment(t *testing.T) {
cases := []struct {
literal string
expected ast.Node
incomplete bool
name string
literal string
expected ast.Node
}{
{
name: "testing arg: param_path = literal",
literal: "{[0] = 1, [2] = 2}",
expected: &ast.InitializerList{
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(2, 13, 2, 13+18).Build(),
Expand All @@ -330,32 +334,36 @@ func TestConvertToAST_declaration_with_initializer_list_assingment(t *testing.T)
},
},
{
literal: "{[0] = TypeDescription}",
name: "testing arg: param_path [x..y] = literal",
literal: "{[0..2] = 2}",
expected: &ast.InitializerList{
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(2, 13, 2, 36).Build(),
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(2, 13, 2, 25).Build(),
Args: []ast.Expression{
&ast.ArgParamPathSet{
Path: "[0]",
Expr: ast.NewTypeInfoBuilder().
WithStartEnd(2, 13+7, 2, 35).
WithName("TypeDescription").
WithNameStartEnd(2, 13+7, 2, 35).
Build(),
Path: "[0..2]",
Expr: &ast.BasicLit{
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(2, 23, 2, 24).Build(),
Kind: ast.INT,
Value: "2"},
},
},
},
},
{
literal: "{[0..2] = 2}",
// TODO: define what AST should be generated in ArgParamPathSet.Expr
incomplete: true,
name: "testing arg: param_path = expr",
literal: "{[0] = TypeDescription{0}}",
expected: &ast.InitializerList{
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(2, 13, 2, 25).Build(),
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(2, 13, 2, 36).Build(),
Args: []ast.Expression{
&ast.ArgParamPathSet{
Path: "[0..2]",
Expr: &ast.BasicLit{
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(2, 23, 2, 24).Build(),
Kind: ast.INT,
Value: "2"},
Path: "[0]",
Expr: ast.NewTypeInfoBuilder().
WithStartEnd(2, 13+7, 2, 35).
WithName("TypeDescription").
WithNameStartEnd(2, 13+7, 2, 35).
Build(),
},
},
},
Expand All @@ -376,7 +384,9 @@ func TestConvertToAST_declaration_with_initializer_list_assingment(t *testing.T)
},
},
{
literal: "{TypeDescription}",
// TODO: define what AST
incomplete: true,
literal: "{TypeDescription}",
expected: &ast.InitializerList{
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(2, 13, 2, 30).Build(),
Args: []ast.Expression{
Expand All @@ -389,40 +399,41 @@ func TestConvertToAST_declaration_with_initializer_list_assingment(t *testing.T)
},
},
{
literal: "{$vasplat()}",
expected: &ast.InitializerList{
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(2, 13, 2, 25).Build(),
Args: []ast.Expression{
&ast.BasicLit{
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(2, 14, 2, 24).Build(),
Kind: ast.STRING,
Value: "$vasplat()"},
},
},
},
{
literal: "{$vasplat(0..1)}",
literal: "{$vasplat(0)}",
expected: &ast.InitializerList{
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(2, 13, 2, 29).Build(),
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(2, 13, 2, 26).Build(),
Args: []ast.Expression{
&ast.BasicLit{
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(2, 14, 2, 28).Build(),
Kind: ast.STRING,
Value: "$vasplat(0..1)"},
&ast.FunctionCall{
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(2, 14, 2, 25).Build(),
Identifier: ast.NewIdentifierBuilder().
WithName("$vasplat").
IsCompileTime(true).
WithRange(lsp.NewRange(2, 14, 2, 22)).
Build(),
Arguments: []ast.Expression{&ast.BasicLit{
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(2, 23, 2, 24).Build(),
Kind: ast.INT,
Value: "0"},
},
},
},
},
},

/* TODO move this test to call_arg testing
{
// No longer valid!
literal: "{...id}",
expected: &ast.InitializerList{
NodeAttributes: ast.NewNodeAttributesBuilder().WithRangePositions(2, 13, 2, 20).Build(),
Args: []ast.Expression{
ast.NewIdentifierBuilder().
WithName("id").
WithStartEnd(2, 13+4, 2, 13+6).BuildPtr(),
WithStartEnd(2, 13+4, 2, 13+6).Build(),
},
},
},
*/
/*{
literal: "{[0..2] = TypeDescription}",
expected: Literal{Value: "{[0..2] = TypeDescription}"},
Expand All @@ -446,6 +457,9 @@ func TestConvertToAST_declaration_with_initializer_list_assingment(t *testing.T)
}

for _, tt := range cases {
if tt.incomplete {
continue
}
t.Run(fmt.Sprintf("compile_time_call: %s", tt.literal), func(t *testing.T) {
source := `
module foo;
Expand Down
2 changes: 1 addition & 1 deletion server/pkg/parser/node_to_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (p *Parser) typeNodeToType(node *sitter.Node, currentModule *symbols.Module
parsedType := symbols.Type{}

tailChild := node.Child(int(node.ChildCount()) - 1)
isOptional := !tailChild.IsNamed() && tailChild.Content(sourceCode) == "!"
isOptional = !tailChild.IsNamed() && tailChild.Content(sourceCode) == "!"

//fmt.Println(node.Type(), node.Content(sourceCode), node.ChildCount())
isCollection := false
Expand Down

0 comments on commit 6421c1d

Please sign in to comment.