From 72489793906c141524cd4ae69017871021b29150 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Ferr=C3=A0s?= Date: Sat, 31 Aug 2024 17:54:31 +0200 Subject: [PATCH] Fix parsing for initializer. --- server/internal/lsp/ast/convert_statement.go | 2 +- .../lsp/ast/convert_statement_test.go | 41 ++++++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/server/internal/lsp/ast/convert_statement.go b/server/internal/lsp/ast/convert_statement.go index 6afe8ca..76b8666 100644 --- a/server/internal/lsp/ast/convert_statement.go +++ b/server/internal/lsp/ast/convert_statement.go @@ -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"), diff --git a/server/internal/lsp/ast/convert_statement_test.go b/server/internal/lsp/ast/convert_statement_test.go index 6b2418e..bf6a0a5 100644 --- a/server/internal/lsp/ast/convert_statement_test.go +++ b/server/internal/lsp/ast/convert_statement_test.go @@ -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(), @@ -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 {