parser: check enum name and field name errors

pull/4799/head
yuyi 2020-05-09 18:43:10 +08:00 committed by GitHub
parent 85763d0539
commit 5f435fa1cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 1 deletions

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/enum_field_name_err.v:2:5: error: field name `Green` must be all lowercase
1 | enum Color {
2 | Green
| ~~~~~
3 | yellow
4 | }

View File

@ -0,0 +1,7 @@
enum Color {
Green
yellow
}
fn main(){
println('hello')
}

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/enum_name_err.v:1:6: error: enum name `color` must begin with a capital letter
1 | enum color {
| ~~~~~
2 | green
3 | yellow

View File

@ -0,0 +1,7 @@
enum color {
green
yellow
}
fn main(){
println('hello')
}

View File

@ -1146,7 +1146,7 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
end_pos := p.tok.position()
enum_name := p.check_name()
if enum_name.len > 0 && !enum_name[0].is_capital() {
verror('enum name `$enum_name` must begin with a capital letter')
p.error_with_pos('enum name `$enum_name` must begin with a capital letter', end_pos)
}
name := p.prepend_mod(enum_name)
p.check(.lcbr)
@ -1156,6 +1156,9 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
for p.tok.kind != .eof && p.tok.kind != .rcbr {
pos := p.tok.position()
val := p.check_name()
if !val.is_lower() {
p.error_with_pos('field name `$val` must be all lowercase', pos)
}
vals << val
mut expr := ast.Expr{}
mut has_expr := false