Skip to content

Commit

Permalink
Fix parsing for initializer.
Browse files Browse the repository at this point in the history
  • Loading branch information
pherrymason committed Aug 31, 2024
1 parent 64d7f0c commit 7248979
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion server/internal/lsp/ast/convert_statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func convert_for_stmt(node *sitter.Node, source []byte) Expression {
func convert_decl_or_expression(node *sitter.Node, source []byte) Expression {
declOrExpr := anyOf([]NodeRule{
NodeOfType("var_decl"),
NodeSiblingsWithSequenceOf([]NodeRule{
NodeChildWithSequenceOf([]NodeRule{
NodeOfType("type"), NodeOfType("local_decl_after_type"),
}, "declaration_stmt"),
NodeAnonymous("_expr"),
Expand Down
41 changes: 39 additions & 2 deletions server/internal/lsp/ast/convert_statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,27 @@ func TestConvertToAST_for_stmt(t *testing.T) {
expected: ForStatement{
ASTBaseNode: NewBaseNodeBuilder().WithStartEnd(3, 3, 3, 30).Build(),
Label: option.None[string](),
Initializer: []Expression{},
Condition: BoolLiteral{Value: true},
Initializer: []Expression{
VariableDecl{
ASTBaseNode: NewBaseNodeBuilder().WithStartEnd(3, 8, 3, 15).Build(),
Names: []Identifier{
NewIdentifierBuilder().
WithName("i").
WithStartEnd(3, 12, 3, 13).
Build(),
},
Type: NewTypeInfoBuilder().
WithName("int").
WithStartEnd(3, 8, 3, 11).
WithNameStartEnd(3, 8, 3, 11).
IsBuiltin().
Build(),
Initializer: IntegerLiteral{
Value: "0",
},
},
},
Condition: BoolLiteral{Value: true},
Update: []Expression{
UpdateExpression{
ASTBaseNode: NewBaseNodeBuilder().WithStartEnd(3, 23, 3, 26).Build(),
Expand All @@ -541,6 +560,24 @@ func TestConvertToAST_for_stmt(t *testing.T) {
},
},
},
{
skip: true,
input: `
for (int i=0, j=0; true; i++) {}`,
expected: ForStatement{
ASTBaseNode: NewBaseNodeBuilder().WithStartEnd(3, 3, 3, 35).Build(),
Label: option.None[string](),
Initializer: []Expression{},
Condition: BoolLiteral{Value: true},
Update: []Expression{
UpdateExpression{
ASTBaseNode: NewBaseNodeBuilder().WithStartEnd(3, 28, 3, 31).Build(),
Operator: "++",
Argument: NewIdentifierBuilder().WithName("i").WithStartEnd(3, 28, 3, 29).Build(),
},
},
},
},
}

for _, tt := range cases {
Expand Down

0 comments on commit 7248979

Please sign in to comment.