From c23ebec94440f3f92b6753f3c3586e04bcdf31dd Mon Sep 17 00:00:00 2001 From: Tim Basel Date: Tue, 7 Dec 2021 10:11:29 +0100 Subject: [PATCH] parser: support bool values in attributes (#12750) --- vlib/v/ast/attr.v | 3 ++- vlib/v/parser/parser.v | 9 ++++++--- vlib/v/tests/attribute_test.v | 6 ++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/vlib/v/ast/attr.v b/vlib/v/ast/attr.v index f91cdb84ac..f836817461 100644 --- a/vlib/v/ast/attr.v +++ b/vlib/v/ast/attr.v @@ -9,6 +9,7 @@ pub enum AttrKind { plain // [name] string // ['name'] number // [123] + bool // [true] || [false] comptime_define // [if name] } @@ -41,7 +42,7 @@ pub fn (a Attr) str() string { a.name } s += match a.kind { - .plain, .number { arg } + .plain, .number, .bool { arg } .string { "'$arg'" } .comptime_define { 'if $arg' } } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 58b5a36cd2..4927613029 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1596,11 +1596,10 @@ fn (mut p Parser) parse_attr() ast.Attr { if p.tok.kind == .colon { has_arg = true p.next() - // `name: arg` - if p.tok.kind == .name { + if p.tok.kind == .name { // `name: arg` kind = .plain arg = p.check_name() - } else if p.tok.kind == .number { + } else if p.tok.kind == .number { // `name: 123` kind = .number arg = p.tok.lit p.next() @@ -1608,6 +1607,10 @@ fn (mut p Parser) parse_attr() ast.Attr { kind = .string arg = p.tok.lit p.next() + } else if p.tok.kind == .key_true || p.tok.kind == .key_false { // `name: true` + kind = .bool + arg = p.tok.kind.str() + p.next() } else { p.error('unexpected $p.tok, an argument is expected after `:`') } diff --git a/vlib/v/tests/attribute_test.v b/vlib/v/tests/attribute_test.v index 10dd6a682e..19d7c9278f 100644 --- a/vlib/v/tests/attribute_test.v +++ b/vlib/v/tests/attribute_test.v @@ -10,6 +10,12 @@ pub struct PubStructAttrTest { bar int } +struct StructFieldAttrTest { + foo string [attr: bar; attr0; attr1: 'foo'] + bar int [attr0: 123; attr1: true; attr2: false] + baz bool [prefix.attr0] = false +} + [testing] enum EnumAttrTest { one