1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: improve the error message for unexpected keywords

This commit is contained in:
Delyan Angelov 2021-01-25 15:03:12 +02:00
parent 444d49da75
commit a5b10b5539
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
6 changed files with 25 additions and 5 deletions

View File

@ -1,6 +1,6 @@
vlib/v/checker/tests/trailing_comma_struct_attr.vv:3:31: error: unexpected `]`, expecting `name`
vlib/v/checker/tests/trailing_comma_struct_attr.vv:3:31: error: unexpected `]`, expecting `name`
1 | struct User {
2 | name string
3 | jobs []string [json:jobss;]
| ^
4 | }
4 | }

View File

@ -425,7 +425,8 @@ fn (mut p Parser) check(expected token.Kind) {
if expected == .name {
p.name_error = true
}
p.error('unexpected `$p.tok.kind.str()`, expecting `$expected.str()`')
label := if token.is_key(p.tok.lit) { 'keyword ' } else { '' }
p.error('unexpected $label`$p.tok.kind.str()`, expecting `$expected.str()`')
}
}

View File

@ -4,4 +4,4 @@ vlib/v/parser/tests/struct_update_err.vv:15:3: error: unexpected `...`, expectin
15 | ...f
| ~~~
16 | }
17 | }
17 | }

View File

@ -2,4 +2,4 @@ vlib/v/parser/tests/uncomplete_module_call_err.vv:7:1: error: unexpected `}`, ex
5 | fn main() {
6 | os.
7 | }
| ^
| ^

View File

@ -0,0 +1,7 @@
vlib/v/parser/tests/unexpected_keyword.vv:5:12: error: unexpected keyword `import`, expecting `(`
3 | }
4 |
5 | fn (s Abc) import(name string) {
| ~~~~~~
6 | println(name)
7 | }

View File

@ -0,0 +1,12 @@
struct Abc {
x int
}
fn (s Abc) import(name string) {
println(name)
}
fn main() {
s := Abc{}
s.import('lib')
}