mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parser: allow '' in attributes
This commit is contained in:
parent
689003454b
commit
bb9d95e9aa
@ -802,7 +802,15 @@ fn (p mut Parser) struct_decl() {
|
|||||||
attr = p.check_name()
|
attr = p.check_name()
|
||||||
if p.tok == .colon {
|
if p.tok == .colon {
|
||||||
p.check(.colon)
|
p.check(.colon)
|
||||||
attr += ':' + p.check_name()
|
mut val := ''
|
||||||
|
match p.tok {
|
||||||
|
.name => { val = p.check_name() }
|
||||||
|
.str => { val = p.check_string() }
|
||||||
|
else => {
|
||||||
|
p.error('attribute value should be either name or string')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
attr += ':' + val
|
||||||
}
|
}
|
||||||
p.check(.rsbr)
|
p.check(.rsbr)
|
||||||
}
|
}
|
||||||
@ -3770,6 +3778,8 @@ fn (p mut Parser) match_statement(is_expr bool) string {
|
|||||||
p.gen('if (')
|
p.gen('if (')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ph := p.cgen.add_placeholder()
|
||||||
|
|
||||||
// Multiple checks separated by comma
|
// Multiple checks separated by comma
|
||||||
mut got_comma := false
|
mut got_comma := false
|
||||||
|
|
||||||
@ -3778,31 +3788,35 @@ fn (p mut Parser) match_statement(is_expr bool) string {
|
|||||||
p.gen(') || (')
|
p.gen(') || (')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mut got_string := false
|
||||||
|
|
||||||
if typ == 'string' {
|
if typ == 'string' {
|
||||||
// TODO: use tmp variable
|
got_string = true
|
||||||
// p.gen('string_eq($tmp_var, ')
|
|
||||||
p.gen('string_eq($tmp_var, ')
|
p.gen('string_eq($tmp_var, ')
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// TODO: use tmp variable
|
p.gen('$tmp_var == ')
|
||||||
// p.gen('($tmp_var == ')
|
|
||||||
p.gen('($tmp_var == ')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
p.expected_type = typ
|
p.expected_type = typ
|
||||||
p.check_types(p.bool_expression(), typ)
|
p.check_types(p.bool_expression(), typ)
|
||||||
p.expected_type = ''
|
p.expected_type = ''
|
||||||
|
|
||||||
|
if got_string {
|
||||||
|
p.gen(')')
|
||||||
|
}
|
||||||
|
|
||||||
if p.tok != .comma {
|
if p.tok != .comma {
|
||||||
if got_comma {
|
if got_comma {
|
||||||
p.gen(') ')
|
p.gen(') ')
|
||||||
|
p.cgen.set_placeholder(ph, '(')
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
p.check(.comma)
|
p.check(.comma)
|
||||||
got_comma = true
|
got_comma = true
|
||||||
}
|
}
|
||||||
p.gen(') )')
|
p.gen(')')
|
||||||
|
|
||||||
p.check(.arrow)
|
p.check(.arrow)
|
||||||
|
|
||||||
|
@ -5,10 +5,11 @@ struct User {
|
|||||||
nums []int
|
nums []int
|
||||||
last_name string [json:lastName]
|
last_name string [json:lastName]
|
||||||
is_registered bool [json:IsRegistered]
|
is_registered bool [json:IsRegistered]
|
||||||
|
typ int [json:'type']
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_parse_user() {
|
fn test_parse_user() {
|
||||||
s := '{"age": 10, "nums": [1,2,3], "lastName": "Johnson", "IsRegistered": true}'
|
s := '{"age": 10, "nums": [1,2,3], "type": 0, "lastName": "Johnson", "IsRegistered": true}'
|
||||||
u := json.decode(User, s) or {
|
u := json.decode(User, s) or {
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
@ -19,11 +20,12 @@ fn test_parse_user() {
|
|||||||
assert u.nums[0] == 1
|
assert u.nums[0] == 1
|
||||||
assert u.nums[1] == 2
|
assert u.nums[1] == 2
|
||||||
assert u.nums[2] == 3
|
assert u.nums[2] == 3
|
||||||
|
assert u.typ == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_encode_user(){
|
fn test_encode_user(){
|
||||||
usr := User{ age: 10, nums: [1,2,3], last_name: 'Johnson', is_registered: true}
|
usr := User{ age: 10, nums: [1,2,3], last_name: 'Johnson', is_registered: true, typ: 0}
|
||||||
expected := '{"age":10,"nums":[1,2,3],"lastName":"Johnson","IsRegistered":true}'
|
expected := '{"age":10,"nums":[1,2,3],"lastName":"Johnson","IsRegistered":true,"type":0}'
|
||||||
out := json.encode(usr)
|
out := json.encode(usr)
|
||||||
assert out == expected
|
assert out == expected
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user