allow short struct initialization with Config structs
parent
0b235cc893
commit
6b3f54692a
|
@ -902,6 +902,13 @@ fn (p mut Parser) factor() string {
|
||||||
if p.peek() == .str {
|
if p.peek() == .str {
|
||||||
return p.map_init()
|
return p.map_init()
|
||||||
}
|
}
|
||||||
|
peek2 := p.tokens[p.token_idx + 1]
|
||||||
|
if p.peek() == .name && peek2.tok == .colon {
|
||||||
|
if !p.expected_type.ends_with('Config') {
|
||||||
|
p.error('short struct initialization syntax only works with structs that end with `Config`')
|
||||||
|
}
|
||||||
|
return p.struct_init(p.expected_type)
|
||||||
|
}
|
||||||
// { user | name :'new name' }
|
// { user | name :'new name' }
|
||||||
return p.assoc()
|
return p.assoc()
|
||||||
}
|
}
|
||||||
|
|
|
@ -488,7 +488,9 @@ fn (p mut Parser) gen_struct_init(typ string, t &Type) bool {
|
||||||
if typ == 'tm' {
|
if typ == 'tm' {
|
||||||
p.cgen.lines[p.cgen.lines.len - 1] = ''
|
p.cgen.lines[p.cgen.lines.len - 1] = ''
|
||||||
}
|
}
|
||||||
p.next()
|
if p.tok != .lcbr {
|
||||||
|
p.next()
|
||||||
|
}
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
ptr := typ.contains('*')
|
ptr := typ.contains('*')
|
||||||
// `user := User{foo:bar}` => `User user = (User){ .foo = bar}`
|
// `user := User{foo:bar}` => `User user = (User){ .foo = bar}`
|
||||||
|
|
|
@ -337,6 +337,7 @@ fn (p mut Parser) struct_decl(generic_param_types []string) {
|
||||||
// p.fgenln('//kek')
|
// p.fgenln('//kek')
|
||||||
}
|
}
|
||||||
// `User{ foo: bar }`
|
// `User{ foo: bar }`
|
||||||
|
// tok == struct name
|
||||||
fn (p mut Parser) struct_init(typ_ string) string {
|
fn (p mut Parser) struct_init(typ_ string) string {
|
||||||
p.is_struct_init = true
|
p.is_struct_init = true
|
||||||
mut typ := typ_
|
mut typ := typ_
|
||||||
|
|
Loading…
Reference in New Issue