parser: support bool values in attributes (#12750)
parent
f86af7237f
commit
c23ebec944
|
@ -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' }
|
||||
}
|
||||
|
|
|
@ -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 `:`')
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue