parser: allow enum field names being the same as V keywords (#22456)

This commit is contained in:
yuyi 2024-10-10 01:57:00 +08:00 committed by GitHub
parent 3e6fc364a8
commit 4c82b0125e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 5 deletions

View File

@ -679,17 +679,28 @@ fn (mut p Parser) check_js_name() string {
return name return name
} }
fn is_ident_name(name string) bool {
if name.len == 0 || !util.name_char_table[name[0]] {
return false
}
for i in 1 .. name.len {
if !util.func_char_table[name[i]] {
return false
}
}
return true
}
fn (mut p Parser) check_name() string { fn (mut p Parser) check_name() string {
pos := p.tok.pos() pos := p.tok.pos()
name := p.tok.lit name := p.tok.lit
if p.tok.kind != .name && p.peek_tok.kind == .dot && name in p.imports { if p.tok.kind != .name && p.peek_tok.kind == .dot && name in p.imports {
p.register_used_import(name) p.register_used_import(name)
} }
match p.tok.kind { if !is_ident_name(name) {
.key_struct { p.check(.key_struct) } p.check(.name)
.key_enum { p.check(.key_enum) } } else {
.key_interface { p.check(.key_interface) } p.next()
else { p.check(.name) }
} }
if !p.inside_orm && !p.inside_attr_decl && name == 'sql' { if !p.inside_orm && !p.inside_attr_decl && name == 'sql' {
p.error_with_pos('unexpected keyword `sql`, expecting name', pos) p.error_with_pos('unexpected keyword `sql`, expecting name', pos)

View File

@ -0,0 +1,24 @@
enum Kind {
none
const
enum
struct
interface
sum_type
i32
f64
}
fn type_kind(kind Kind) string {
return '${kind}'
}
fn test_enum_field_name_same_as_keyword() {
mut ret := type_kind(Kind.none)
println(ret)
assert ret == 'none'
ret = type_kind(Kind.struct)
println(ret)
assert ret == 'struct'
}