Skip to content

Commit

Permalink
Improve initialization, condition and update nodes of for statement.
Browse files Browse the repository at this point in the history
  • Loading branch information
pherrymason committed Sep 1, 2024
1 parent 7248979 commit 23ff370
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 52 deletions.
10 changes: 5 additions & 5 deletions server/internal/lsp/ast/convert_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ $._base_expr,
func convert_expression(node *sitter.Node, source []byte) Expression {
//fmt.Print("convert_expression:\n")
//debugNode(node, source)
return anyOf([]NodeRule{
return anyOf("_expr", []NodeRule{
NodeOfType("assignment_expr"),
NodeOfType("ternary_expr"),
NodeChildWithSequenceOf([]NodeRule{
Expand All @@ -54,7 +54,7 @@ func convert_base_expression(node *sitter.Node, source []byte) Expression {
var expression Expression
//debugNode(node, source)

return anyOf([]NodeRule{
return anyOf("_base_expr", []NodeRule{
NodeOfType("string_literal"),
NodeOfType("char_literal"),
NodeOfType("raw_string_literal"),
Expand Down Expand Up @@ -177,7 +177,7 @@ func convert_ternary_expr(node *sitter.Node, source []byte) Expression {
NodeOfType("initializer_list"),
NodeOfType("_base_expr"),
}
condition := anyOf(expected, node.ChildByFieldName("condition"), source, false)
condition := anyOf("ternary_expr", expected, node.ChildByFieldName("condition"), source, false)

return TernaryExpression{
ASTBaseNode: NewBaseNodeFromSitterNode(node),
Expand Down Expand Up @@ -619,11 +619,11 @@ func convert_dummy(node *sitter.Node, source []byte) Expression {
return nil
}

func debugNode(node *sitter.Node, source []byte) {
func debugNode(node *sitter.Node, source []byte, tag string) {
if node == nil {
fmt.Printf("Node is nil\n")
return
}

fmt.Printf("%s: %s\n----- %s\n", node.Type(), node.Content(source), node)
fmt.Printf("%s: %s: %s\n----- %s\n", tag, node.Type(), node.Content(source), node)
}
47 changes: 24 additions & 23 deletions server/internal/lsp/ast/convert_rules_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,27 @@ func (c ConversionInfo) convert(node *sitter.Node, source []byte) Expression {

func nodeTypeConverterMap(nodeType string) (ConversionInfo, error) {
funcMap := map[string]ConversionInfo{
"assignment_expr": {method: convert_assignment_expr},
"at_ident": {method: convert_ident},
"binary_expr": {method: convert_binary_expr},
"break_stmt": {method: convert_break_stmt},
"bytes_expr": {method: convert_bytes_expr},
"builtin": {method: convert_as_literal},
"call_expr": {method: convert_call_expr},
"continue_stmt": {method: convert_continue_stmt},
"cast_expr": {method: convert_cast_expr},
"const_ident": {method: convert_ident},
"compound_stmt": {method: convert_compound_stmt},
"ct_ident": {method: convert_ident},
"declaration_stmt": {method: convert_declaration_stmt},
"elvis_orelse_expr": {method: convert_elvis_orelse_expr},
"expr_stmt": {method: convert_expression, goChild: true},
"for_stmt": {method: convert_for_stmt},
"hash_ident": {method: convert_ident},
"ident": {method: convert_ident},
"if_stmt": {method: convert_if_stmt},
"initializer_list": {method: convert_initializer_list},
"assignment_expr": {method: convert_assignment_expr},
"at_ident": {method: convert_ident},
"binary_expr": {method: convert_binary_expr},
"break_stmt": {method: convert_break_stmt},
"bytes_expr": {method: convert_bytes_expr},
"builtin": {method: convert_as_literal},
"call_expr": {method: convert_call_expr},
"continue_stmt": {method: convert_continue_stmt},
"cast_expr": {method: convert_cast_expr},
"const_ident": {method: convert_ident},
"compound_stmt": {method: convert_compound_stmt},
"ct_ident": {method: convert_ident},
"declaration_stmt": {method: convert_declaration_stmt},
"split_declaration_stmt": {method: convert_split_declaration_stmt},
"elvis_orelse_expr": {method: convert_elvis_orelse_expr},
"expr_stmt": {method: convert_expression, goChild: true},
"for_stmt": {method: convert_for_stmt},
"hash_ident": {method: convert_ident},
"ident": {method: convert_ident},
"if_stmt": {method: convert_if_stmt},
"initializer_list": {method: convert_initializer_list},

"lambda_declaration": {method: convert_lambda_declaration},
"lambda_expr": {method: convert_lambda_expr},
Expand Down Expand Up @@ -117,10 +118,10 @@ func nodeTypeConverterMap(nodeType string) (ConversionInfo, error) {
//panic(fmt.Sprintf("La función %s no existe\n", nodeType))
}

func anyOf(rules []NodeRule, node *sitter.Node, source []byte, debug bool) Expression {
func anyOf(name string, rules []NodeRule, node *sitter.Node, source []byte, debug bool) Expression {
//fmt.Printf("anyOf: ")
if debug {
debugNode(node, source)
debugNode(node, source, "AnyOf["+name+"]")
}
if node == nil {
panic("Nil node supplied!")
Expand All @@ -146,7 +147,7 @@ func anyOf(rules []NodeRule, node *sitter.Node, source []byte, debug bool) Expre
func commaSep(convert NodeConverter, node *sitter.Node, source []byte) []Expression {
expressions := []Expression{}
for {
debugNode(node, source)
debugNode(node, source, "commaSep")
condition := convert(node, source)

if condition != nil {
Expand Down
42 changes: 22 additions & 20 deletions server/internal/lsp/ast/convert_statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
)

func convert_statement(node *sitter.Node, source []byte) Expression {
debugNode(node, source)
return anyOf([]NodeRule{
return anyOf("statement", []NodeRule{
NodeOfType("compound_stmt"),
NodeOfType("expr_stmt"),
NodeOfType("declaration_stmt"),
Expand Down Expand Up @@ -66,6 +65,10 @@ func convert_return_stmt(node *sitter.Node, source []byte) Expression {
}
}

func convert_split_declaration_stmt(node *sitter.Node, source []byte) Expression {
return convert_declaration_stmt(node.Parent(), source)
}

func convert_declaration_stmt(node *sitter.Node, source []byte) Expression {
if node.Type() == "const_declaration" {
return convert_const_declaration(node, source)
Expand All @@ -78,6 +81,7 @@ func convert_declaration_stmt(node *sitter.Node, source []byte) Expression {
}
for i := 0; i < int(node.ChildCount()); i++ {
n := node.Child(i)
debugNode(n, source, "dd")

switch n.Type() {
case "local_decl_storage":
Expand Down Expand Up @@ -163,7 +167,6 @@ func convert_switch_stmt(node *sitter.Node, source []byte) Expression {
}

colon := conditionNode.NextSibling()
debugNode(colon, source)
ns := colon.NextSibling()
statements := []Statement{}
for {
Expand Down Expand Up @@ -203,7 +206,7 @@ func convert_nextcase_stmt(node *sitter.Node, source []byte) Expression {
var value Expression
targetNode := node.ChildByFieldName("target")
if targetNode != nil {
value = anyOf([]NodeRule{
value = anyOf("nextcase_stmt", []NodeRule{
NodeTryConversionFunc("_expr"),
NodeOfType("type"),
NodeOfType("default"),
Expand All @@ -225,8 +228,7 @@ func convert_nextcase_stmt(node *sitter.Node, source []byte) Expression {
}

func convert_if_stmt(node *sitter.Node, source []byte) Expression {
debugNode(node, source)
conditions := convert_paren_condition(node.ChildByFieldName("condition"), source)
conditions := convert_paren_conditions(node.ChildByFieldName("condition"), source)
//fmt.Printf("%s", reflect.TypeOf(conditions).String())
stmt := IfStatement{
ASTBaseNode: NewBaseNodeFromSitterNode(node),
Expand Down Expand Up @@ -276,13 +278,11 @@ func convert_if_stmt(node *sitter.Node, source []byte) Expression {
),
)
*/
func convert_paren_condition(node *sitter.Node, source []byte) []Expression {

debugNode(node, source)
return convert_condition(node.Child(1), source)
func convert_paren_conditions(node *sitter.Node, source []byte) []Expression {
return convert_conditions(node.Child(1), source)
}

func convert_condition(node *sitter.Node, source []byte) []Expression {
func convert_conditions(node *sitter.Node, source []byte) []Expression {
conditions := []Expression{}

// Option 1: try_unwrap_chain
Expand Down Expand Up @@ -335,33 +335,35 @@ func convert_for_stmt(node *sitter.Node, source []byte) Expression {
if n.Type() == "for_cond" {
initNode := n.ChildByFieldName("initializer")
if initNode != nil {
forStmt.Initializer = commaSep(convert_decl_or_expression, initNode, source)
forStmt.Initializer = convert_comma_decl_or_expression(initNode, source)
}

condNode := n.ChildByFieldName("condition")
if condNode != nil {
forStmt.Condition = convert_condition(condNode, source)[0]
forStmt.Condition = convert_conditions(condNode, source)[0]
}

updateNode := n.ChildByFieldName("update")
if updateNode != nil {
debugNode(updateNode, source)
forStmt.Update = commaSep(convert_decl_or_expression, updateNode.Child(0), source)
forStmt.Update = convert_comma_decl_or_expression(updateNode, source)
}
}
}

return forStmt
}

func convert_comma_decl_or_expression(node *sitter.Node, source []byte) []Expression {
return commaSep(convert_decl_or_expression, node.Child(0), source)
}

// This takes anon nodes
func convert_decl_or_expression(node *sitter.Node, source []byte) Expression {
declOrExpr := anyOf([]NodeRule{
return anyOf("decl_or_expression", []NodeRule{
NodeOfType("var_decl"),
NodeChildWithSequenceOf([]NodeRule{
NodeSiblingsWithSequenceOf([]NodeRule{
NodeOfType("type"), NodeOfType("local_decl_after_type"),
}, "declaration_stmt"),
}, "split_declaration_stmt"),
NodeAnonymous("_expr"),
}, node, source, true)

return declOrExpr
}
33 changes: 29 additions & 4 deletions server/internal/lsp/ast/convert_statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,9 @@ func TestConvertToAST_for_stmt(t *testing.T) {
expected ForStatement
}{
{
skip: false,
input: `
for (int i=0; true; i++) {}`,
for (int i=0; i<10; i++) {}`,
expected: ForStatement{
ASTBaseNode: NewBaseNodeBuilder().WithStartEnd(3, 3, 3, 30).Build(),
Label: option.None[string](),
Expand All @@ -550,7 +551,12 @@ func TestConvertToAST_for_stmt(t *testing.T) {
},
},
},
Condition: BoolLiteral{Value: true},
Condition: BinaryExpr{
ASTBaseNode: NewBaseNodeBuilder().WithStartEnd(3, 17, 3, 21).Build(),
Left: NewIdentifierBuilder().WithName("i").WithStartEnd(3, 17, 3, 18).Build(),
Right: IntegerLiteral{Value: "10"},
Operator: "<",
},
Update: []Expression{
UpdateExpression{
ASTBaseNode: NewBaseNodeBuilder().WithStartEnd(3, 23, 3, 26).Build(),
Expand All @@ -567,8 +573,27 @@ func TestConvertToAST_for_stmt(t *testing.T) {
expected: ForStatement{
ASTBaseNode: NewBaseNodeBuilder().WithStartEnd(3, 3, 3, 35).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, 28, 3, 31).Build(),
Expand Down

0 comments on commit 23ff370

Please sign in to comment.