parser: support bool values in attributes (#12750)

pull/12756/head
Tim Basel 2021-12-07 10:11:29 +01:00 committed by GitHub
parent f86af7237f
commit c23ebec944
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 4 deletions

View File

@ -9,6 +9,7 @@ pub enum AttrKind {
plain // [name] plain // [name]
string // ['name'] string // ['name']
number // [123] number // [123]
bool // [true] || [false]
comptime_define // [if name] comptime_define // [if name]
} }
@ -41,7 +42,7 @@ pub fn (a Attr) str() string {
a.name a.name
} }
s += match a.kind { s += match a.kind {
.plain, .number { arg } .plain, .number, .bool { arg }
.string { "'$arg'" } .string { "'$arg'" }
.comptime_define { 'if $arg' } .comptime_define { 'if $arg' }
} }

View File

@ -1596,11 +1596,10 @@ fn (mut p Parser) parse_attr() ast.Attr {
if p.tok.kind == .colon { if p.tok.kind == .colon {
has_arg = true has_arg = true
p.next() p.next()
// `name: arg` if p.tok.kind == .name { // `name: arg`
if p.tok.kind == .name {
kind = .plain kind = .plain
arg = p.check_name() arg = p.check_name()
} else if p.tok.kind == .number { } else if p.tok.kind == .number { // `name: 123`
kind = .number kind = .number
arg = p.tok.lit arg = p.tok.lit
p.next() p.next()
@ -1608,6 +1607,10 @@ fn (mut p Parser) parse_attr() ast.Attr {
kind = .string kind = .string
arg = p.tok.lit arg = p.tok.lit
p.next() 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 { } else {
p.error('unexpected $p.tok, an argument is expected after `:`') p.error('unexpected $p.tok, an argument is expected after `:`')
} }

View File

@ -10,6 +10,12 @@ pub struct PubStructAttrTest {
bar int 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] [testing]
enum EnumAttrTest { enum EnumAttrTest {
one one