ast, parser: add additional info for CallExpr, StructInit nodes (#9526)

pull/9531/head
Ned Palacios 2021-03-30 15:43:17 +08:00 committed by GitHub
parent c5302bfcf5
commit 3ced970b17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 5 deletions

View File

@ -28,8 +28,8 @@ pub type Stmt = AsmStmt | AssertStmt | AssignStmt | Block | BranchStmt | CompFor
pub type ScopeObject = AsmRegister | ConstField | GlobalField | Var pub type ScopeObject = AsmRegister | ConstField | GlobalField | Var
// TOOD: replace table.Param // TOOD: replace table.Param
pub type Node = ConstField | EnumField | Expr | Field | File | GlobalField | IfBranch | pub type Node = CallArg | ConstField | EnumField | Expr | Field | File | GlobalField |
MatchBranch | ScopeObject | SelectBranch | Stmt | StructField | StructInitField | IfBranch | MatchBranch | ScopeObject | SelectBranch | Stmt | StructField | StructInitField |
table.Param table.Param
pub struct Type { pub struct Type {
@ -242,6 +242,7 @@ pub struct StructInitField {
pub: pub:
expr Expr expr Expr
pos token.Position pos token.Position
name_pos token.Position
comments []Comment comments []Comment
next_comments []Comment next_comments []Comment
pub mut: pub mut:
@ -265,6 +266,7 @@ pub mut:
pub struct StructInit { pub struct StructInit {
pub: pub:
pos token.Position pos token.Position
name_pos token.Position
is_short bool is_short bool
pub mut: pub mut:
unresolved bool unresolved bool
@ -367,6 +369,7 @@ pub:
pub struct CallExpr { pub struct CallExpr {
pub: pub:
pos token.Position pos token.Position
name_pos token.Position
mod string mod string
pub mut: pub mut:
name string // left.name() name string // left.name()
@ -1584,6 +1587,9 @@ pub fn (node Node) position() token.Position {
} }
return pos return pos
} }
CallArg {
return node.pos
}
} }
} }
@ -1609,6 +1615,7 @@ pub fn (node Node) children() []Node {
} }
CallExpr { CallExpr {
children << node.left children << node.left
children << node.args.map(Node(it))
children << Expr(node.or_block) children << Expr(node.or_block)
} }
InfixExpr { InfixExpr {

View File

@ -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) pos.update_last_line(p.prev_tok.line_nr)
return ast.CallExpr{ return ast.CallExpr{
name: fn_name name: fn_name
name_pos: first_pos
args: args args: args
mod: p.mod mod: p.mod
pos: pos pos: pos

View File

@ -359,6 +359,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
mut field_name := '' mut field_name := ''
mut expr := ast.Expr{} mut expr := ast.Expr{}
mut field_pos := token.Position{} mut field_pos := token.Position{}
mut first_field_pos := token.Position{}
mut comments := []ast.Comment{} mut comments := []ast.Comment{}
mut nline_comments := []ast.Comment{} mut nline_comments := []ast.Comment{}
is_update_expr := fields.len == 0 && p.tok.kind == .ellipsis 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 // name will be set later in checker
expr = p.expr(0) expr = p.expr(0)
field_pos = expr.position() field_pos = expr.position()
first_field_pos = field_pos
comments = p.eat_comments(same_line: true) comments = p.eat_comments(same_line: true)
} else if is_update_expr { } else if is_update_expr {
// struct updating syntax; f2 := Foo{ ...f, name: 'f2' } // 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) update_expr_comments << p.eat_comments(same_line: true)
has_update_expr = true has_update_expr = true
} else { } else {
first_field_pos := p.tok.position() first_field_pos = p.tok.position()
field_name = p.check_name() field_name = p.check_name()
p.check(.colon) p.check(.colon)
expr = p.expr(0) expr = p.expr(0)
@ -403,6 +405,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
name: field_name name: field_name
expr: expr expr: expr
pos: field_pos pos: field_pos
name_pos: first_field_pos
comments: comments comments: comments
next_comments: nline_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: update_expr
update_expr_comments: update_expr_comments update_expr_comments: update_expr_comments
has_update_expr: has_update_expr has_update_expr: has_update_expr
name_pos: first_pos
pos: first_pos.extend(p.prev_tok.position()) pos: first_pos.extend(p.prev_tok.position())
is_short: no_keys is_short: no_keys
pre_comments: pre_comments pre_comments: pre_comments