mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parser: improve printing of unexpected tokens (#8654)
This commit is contained in:
parent
0f92485698
commit
d37fb5641f
@ -1,4 +1,4 @@
|
|||||||
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 token `]`, expecting name
|
||||||
1 | struct User {
|
1 | struct User {
|
||||||
2 | name string
|
2 | name string
|
||||||
3 | jobs []string [json:jobss;]
|
3 | jobs []string [json:jobss;]
|
||||||
|
@ -527,7 +527,7 @@ fn (mut p Parser) anon_fn() ast.AnonFn {
|
|||||||
no_body := p.tok.kind != .lcbr
|
no_body := p.tok.kind != .lcbr
|
||||||
same_line = p.tok.line_nr == p.prev_tok.line_nr
|
same_line = p.tok.line_nr == p.prev_tok.line_nr
|
||||||
if no_body && same_line {
|
if no_body && same_line {
|
||||||
p.error_with_pos('unexpected `$p.tok.kind` after anonymous function signature, expecting `{`',
|
p.error_with_pos('unexpected $p.tok after anonymous function signature, expecting `{`',
|
||||||
p.tok.position())
|
p.tok.position())
|
||||||
}
|
}
|
||||||
mut label_names := []string{}
|
mut label_names := []string{}
|
||||||
|
@ -15,7 +15,7 @@ fn (mut p Parser) lock_expr() ast.LockExpr {
|
|||||||
}
|
}
|
||||||
is_rlock := p.tok.kind == .key_rlock
|
is_rlock := p.tok.kind == .key_rlock
|
||||||
if !is_rlock && p.tok.kind != .key_lock {
|
if !is_rlock && p.tok.kind != .key_lock {
|
||||||
p.error_with_pos('unexpected `$p.tok.lit`; `lock` or `rlock` expected', p.tok.position())
|
p.error_with_pos('unexpected $p.tok, expected `lock` or `rlock`', p.tok.position())
|
||||||
}
|
}
|
||||||
p.next()
|
p.next()
|
||||||
for p.tok.kind == .name {
|
for p.tok.kind == .name {
|
||||||
|
@ -424,14 +424,16 @@ fn (mut p Parser) check(expected token.Kind) {
|
|||||||
// }
|
// }
|
||||||
if p.tok.kind == expected {
|
if p.tok.kind == expected {
|
||||||
p.next()
|
p.next()
|
||||||
} else if p.tok.kind == .name {
|
|
||||||
p.error('unexpected name `$p.tok.lit`, expecting `$expected.str()`')
|
|
||||||
} else {
|
} else {
|
||||||
if expected == .name {
|
if expected == .name {
|
||||||
p.name_error = true
|
p.name_error = true
|
||||||
}
|
}
|
||||||
label := if token.is_key(p.tok.lit) { 'keyword ' } else { '' }
|
mut s := expected.str()
|
||||||
p.error('unexpected $label`$p.tok.kind.str()`, expecting `$expected.str()`')
|
// quote keywords, punctuation, operators
|
||||||
|
if token.is_key(s) || (s.len > 0 && !s[0].is_letter()) {
|
||||||
|
s = '`$s`'
|
||||||
|
}
|
||||||
|
p.error('unexpected $p.tok, expecting $s')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -841,7 +843,7 @@ fn (mut p Parser) attributes() {
|
|||||||
p.next()
|
p.next()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
p.error('unexpected `$p.tok.kind.str()`, expecting `;`')
|
p.error('unexpected $p.tok, expecting `;`')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
p.next()
|
p.next()
|
||||||
|
@ -297,8 +297,7 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
|
|||||||
else {
|
else {
|
||||||
if p.tok.kind != .eof {
|
if p.tok.kind != .eof {
|
||||||
// eof should be handled where it happens
|
// eof should be handled where it happens
|
||||||
p.error_with_pos('invalid expression: unexpected `$p.tok.kind.str()` token',
|
p.error_with_pos('invalid expression: unexpected $p.tok', p.tok.position())
|
||||||
p.tok.position())
|
|
||||||
return ast.Expr{}
|
return ast.Expr{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
vlib/v/parser/tests/postfix_inc.vv:3:1: error: `++` must be on the same line as the previous token
|
vlib/v/parser/tests/postfix_inc.vv:3:1: error: token `++` must be on the same line as the previous token
|
||||||
1 | mut v := 4
|
1 | mut v := 4
|
||||||
2 | _ = v
|
2 | _ = v
|
||||||
3 | ++v
|
3 | ++v
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
vlib/v/parser/tests/struct_field_expected.vv:6:2: error: unexpected `.`, expecting struct field name
|
vlib/v/parser/tests/struct_field_expected.vv:6:2: error: unexpected token `.`, expecting struct field name
|
||||||
4 |
|
4 |
|
||||||
5 | x = {
|
5 | x = {
|
||||||
6 | .a : 'Alpha'
|
6 | .a : 'Alpha'
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
vlib/v/parser/tests/struct_update_err.vv:15:3: error: unexpected `...`, expecting `name`
|
vlib/v/parser/tests/struct_update_err.vv:15:3: error: unexpected token `...`, expecting name
|
||||||
13 | f2 := Foo{
|
13 | f2 := Foo{
|
||||||
14 | name: 'f2'
|
14 | name: 'f2'
|
||||||
15 | ...f
|
15 | ...f
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
vlib/v/parser/tests/uncomplete_module_call_err.vv:7:1: error: unexpected `}`, expecting `name`
|
vlib/v/parser/tests/uncomplete_module_call_err.vv:7:1: error: unexpected token `}`, expecting name
|
||||||
5 | fn main() {
|
5 | fn main() {
|
||||||
6 | os.
|
6 | os.
|
||||||
7 | }
|
7 | }
|
||||||
|
3
vlib/v/parser/tests/unexpected_expr.out
Normal file
3
vlib/v/parser/tests/unexpected_expr.out
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
vlib/v/parser/tests/unexpected_expr.vv:1:5: error: invalid expression: unexpected keyword `break`
|
||||||
|
1 | _ = break
|
||||||
|
| ~~~~~
|
1
vlib/v/parser/tests/unexpected_expr.vv
Normal file
1
vlib/v/parser/tests/unexpected_expr.vv
Normal file
@ -0,0 +1 @@
|
|||||||
|
_ = break
|
@ -319,6 +319,7 @@ pub fn (t Kind) is_assign() bool {
|
|||||||
return t in token.assign_tokens
|
return t in token.assign_tokens
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// note: used for some code generation, so no quoting
|
||||||
pub fn (t Kind) str() string {
|
pub fn (t Kind) str() string {
|
||||||
return token.token_str[int(t)]
|
return token.token_str[int(t)]
|
||||||
}
|
}
|
||||||
@ -329,10 +330,15 @@ pub fn (t Token) str() string {
|
|||||||
eprintln('missing token kind string')
|
eprintln('missing token kind string')
|
||||||
} else if !s[0].is_letter() {
|
} else if !s[0].is_letter() {
|
||||||
// punctuation, operators
|
// punctuation, operators
|
||||||
return '`$s`'
|
return 'token `$s`'
|
||||||
}
|
}
|
||||||
|
if is_key(t.lit) {
|
||||||
|
s = 'keyword'
|
||||||
|
}
|
||||||
|
if t.lit != '' {
|
||||||
// string contents etc
|
// string contents etc
|
||||||
if t.lit != '' { s += ' `$t.lit`' }
|
s += ' `$t.lit`'
|
||||||
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user