From 3ced970b176bb1f9c4c30d39752818f6aa274838 Mon Sep 17 00:00:00 2001 From: Ned Palacios <7358345+nedpals@users.noreply.github.com> Date: Tue, 30 Mar 2021 15:43:17 +0800 Subject: [PATCH] ast, parser: add additional info for CallExpr, StructInit nodes (#9526) --- vlib/v/ast/ast.v | 15 +++++++++++---- vlib/v/parser/fn.v | 1 + vlib/v/parser/struct.v | 6 +++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 9ea35f3eb8..95e362c993 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -28,8 +28,8 @@ pub type Stmt = AsmStmt | AssertStmt | AssignStmt | Block | BranchStmt | CompFor pub type ScopeObject = AsmRegister | ConstField | GlobalField | Var // TOOD: replace table.Param -pub type Node = ConstField | EnumField | Expr | Field | File | GlobalField | IfBranch | - MatchBranch | ScopeObject | SelectBranch | Stmt | StructField | StructInitField | +pub type Node = CallArg | ConstField | EnumField | Expr | Field | File | GlobalField | + IfBranch | MatchBranch | ScopeObject | SelectBranch | Stmt | StructField | StructInitField | table.Param pub struct Type { @@ -242,6 +242,7 @@ pub struct StructInitField { pub: expr Expr pos token.Position + name_pos token.Position comments []Comment next_comments []Comment pub mut: @@ -265,6 +266,7 @@ pub mut: pub struct StructInit { pub: pos token.Position + name_pos token.Position is_short bool pub mut: unresolved bool @@ -366,8 +368,9 @@ pub: // function or method call expr pub struct CallExpr { pub: - pos token.Position - mod string + pos token.Position + name_pos token.Position + mod string pub mut: name string // left.name() is_method bool @@ -1584,6 +1587,9 @@ pub fn (node Node) position() token.Position { } return pos } + CallArg { + return node.pos + } } } @@ -1609,6 +1615,7 @@ pub fn (node Node) children() []Node { } CallExpr { children << node.left + children << node.args.map(Node(it)) children << Expr(node.or_block) } InfixExpr { diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index af755c50fd..56051f1a60 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -91,6 +91,7 @@ pub fn (mut p Parser) call_expr(language table.Language, mod string) ast.CallExp pos.update_last_line(p.prev_tok.line_nr) return ast.CallExpr{ name: fn_name + name_pos: first_pos args: args mod: p.mod pos: pos diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 2371c3a948..4c4527897f 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -359,6 +359,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit { mut field_name := '' mut expr := ast.Expr{} mut field_pos := token.Position{} + mut first_field_pos := token.Position{} mut comments := []ast.Comment{} mut nline_comments := []ast.Comment{} is_update_expr := fields.len == 0 && p.tok.kind == .ellipsis @@ -366,6 +367,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit { // name will be set later in checker expr = p.expr(0) field_pos = expr.position() + first_field_pos = field_pos comments = p.eat_comments(same_line: true) } else if is_update_expr { // struct updating syntax; f2 := Foo{ ...f, name: 'f2' } @@ -374,7 +376,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit { update_expr_comments << p.eat_comments(same_line: true) has_update_expr = true } else { - first_field_pos := p.tok.position() + first_field_pos = p.tok.position() field_name = p.check_name() p.check(.colon) expr = p.expr(0) @@ -403,6 +405,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit { name: field_name expr: expr pos: field_pos + name_pos: first_field_pos comments: comments next_comments: nline_comments } @@ -419,6 +422,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit { update_expr: update_expr update_expr_comments: update_expr_comments has_update_expr: has_update_expr + name_pos: first_pos pos: first_pos.extend(p.prev_tok.position()) is_short: no_keys pre_comments: pre_comments