parser: short struct init syntax

pull/4722/head
Alexander Medvednikov 2020-05-05 02:12:40 +02:00
parent a4b6c3fa5d
commit 621cb7b914
3 changed files with 8 additions and 4 deletions

View File

@ -704,6 +704,9 @@ pub fn (mut p Parser) name_expr() ast.Expr {
pos: p.tok.position() pos: p.tok.position()
mod: mod mod: mod
} }
} else if p.peek_tok.kind == .colon && p.prev_tok.kind != .str_dollar {
// `foo(key:val, key2:val2)`
return p.struct_init(true) // short_syntax:true
} else { } else {
node = p.parse_ident(is_c, is_js) node = p.parse_ident(is_c, is_js)
} }

View File

@ -200,12 +200,12 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
} }
mut fields := []ast.StructInitField{} mut fields := []ast.StructInitField{}
mut i := 0 mut i := 0
is_short_syntax := p.peek_tok.kind != .colon && p.tok.kind != .rcbr // `Vec{a,b,c} no_keys := p.peek_tok.kind != .colon && p.tok.kind != .rcbr // `Vec{a,b,c}
// p.warn(is_short_syntax.str()) // p.warn(is_short_syntax.str())
for p.tok.kind != .rcbr { for p.tok.kind != .rcbr && p.tok.kind != .rpar {
p.check_comment() p.check_comment()
mut field_name := '' mut field_name := ''
if is_short_syntax { if no_keys {
expr := p.expr(0) expr := p.expr(0)
// name will be set later in checker // name will be set later in checker
fields << ast.StructInitField{ fields << ast.StructInitField{
@ -247,7 +247,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
pos: first_pos.pos pos: first_pos.pos
len: last_pos.pos - first_pos.pos + last_pos.len len: last_pos.pos - first_pos.pos + last_pos.len
} }
is_short: is_short_syntax is_short: no_keys
} }
return node return node
} }

View File

@ -244,6 +244,7 @@ fn test_config() {
foo2({ foo2({
name: 'Peter' name: 'Peter'
}) })
foo2(name: 'Peter')
} }
struct City { struct City {