diff --git a/vlib/v/checker/tests/trailing_comma_struct_attr.out b/vlib/v/checker/tests/trailing_comma_struct_attr.out index 0de5808839..eed55bdeb3 100644 --- a/vlib/v/checker/tests/trailing_comma_struct_attr.out +++ b/vlib/v/checker/tests/trailing_comma_struct_attr.out @@ -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 { 2 | name string 3 | jobs []string [json:jobss;] diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 9b54d838aa..5fe81c0682 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -527,7 +527,7 @@ fn (mut p Parser) anon_fn() ast.AnonFn { no_body := p.tok.kind != .lcbr same_line = p.tok.line_nr == p.prev_tok.line_nr 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()) } mut label_names := []string{} diff --git a/vlib/v/parser/lock.v b/vlib/v/parser/lock.v index f3ef1c82ea..222951d1d3 100644 --- a/vlib/v/parser/lock.v +++ b/vlib/v/parser/lock.v @@ -15,7 +15,7 @@ fn (mut p Parser) lock_expr() ast.LockExpr { } is_rlock := p.tok.kind == .key_rlock 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() for p.tok.kind == .name { diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 692812fc48..ddd4126432 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -424,14 +424,16 @@ fn (mut p Parser) check(expected token.Kind) { // } if p.tok.kind == expected { p.next() - } else if p.tok.kind == .name { - p.error('unexpected name `$p.tok.lit`, expecting `$expected.str()`') } else { if expected == .name { p.name_error = true } - label := if token.is_key(p.tok.lit) { 'keyword ' } else { '' } - p.error('unexpected $label`$p.tok.kind.str()`, expecting `$expected.str()`') + mut s := 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() break } - p.error('unexpected `$p.tok.kind.str()`, expecting `;`') + p.error('unexpected $p.tok, expecting `;`') return } p.next() diff --git a/vlib/v/parser/pratt.v b/vlib/v/parser/pratt.v index 49192b3fed..7640fc8d47 100644 --- a/vlib/v/parser/pratt.v +++ b/vlib/v/parser/pratt.v @@ -297,8 +297,7 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr { else { if p.tok.kind != .eof { // eof should be handled where it happens - p.error_with_pos('invalid expression: unexpected `$p.tok.kind.str()` token', - p.tok.position()) + p.error_with_pos('invalid expression: unexpected $p.tok', p.tok.position()) return ast.Expr{} } } diff --git a/vlib/v/parser/tests/postfix_inc.out b/vlib/v/parser/tests/postfix_inc.out index 91cbf89b4d..189a4132ab 100644 --- a/vlib/v/parser/tests/postfix_inc.out +++ b/vlib/v/parser/tests/postfix_inc.out @@ -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 2 | _ = v 3 | ++v diff --git a/vlib/v/parser/tests/struct_field_expected.out b/vlib/v/parser/tests/struct_field_expected.out index 6d2841aa97..9fa06a2d95 100644 --- a/vlib/v/parser/tests/struct_field_expected.out +++ b/vlib/v/parser/tests/struct_field_expected.out @@ -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 | 5 | x = { 6 | .a : 'Alpha' diff --git a/vlib/v/parser/tests/struct_update_err.out b/vlib/v/parser/tests/struct_update_err.out index 4146854c88..ee72f49f96 100644 --- a/vlib/v/parser/tests/struct_update_err.out +++ b/vlib/v/parser/tests/struct_update_err.out @@ -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{ 14 | name: 'f2' 15 | ...f diff --git a/vlib/v/parser/tests/uncomplete_module_call_err.out b/vlib/v/parser/tests/uncomplete_module_call_err.out index d33b0ae043..8acb09de41 100644 --- a/vlib/v/parser/tests/uncomplete_module_call_err.out +++ b/vlib/v/parser/tests/uncomplete_module_call_err.out @@ -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() { 6 | os. 7 | } diff --git a/vlib/v/parser/tests/unexpected_expr.out b/vlib/v/parser/tests/unexpected_expr.out new file mode 100644 index 0000000000..46c1610a9f --- /dev/null +++ b/vlib/v/parser/tests/unexpected_expr.out @@ -0,0 +1,3 @@ +vlib/v/parser/tests/unexpected_expr.vv:1:5: error: invalid expression: unexpected keyword `break` + 1 | _ = break + | ~~~~~ diff --git a/vlib/v/parser/tests/unexpected_expr.vv b/vlib/v/parser/tests/unexpected_expr.vv new file mode 100644 index 0000000000..25b06901e0 --- /dev/null +++ b/vlib/v/parser/tests/unexpected_expr.vv @@ -0,0 +1 @@ +_ = break diff --git a/vlib/v/token/token.v b/vlib/v/token/token.v index 64976eb2ae..d69e1a56a1 100644 --- a/vlib/v/token/token.v +++ b/vlib/v/token/token.v @@ -319,6 +319,7 @@ pub fn (t Kind) is_assign() bool { return t in token.assign_tokens } +// note: used for some code generation, so no quoting pub fn (t Kind) str() string { return token.token_str[int(t)] } @@ -327,12 +328,17 @@ pub fn (t Token) str() string { mut s := t.kind.str() if s.len == 0 { eprintln('missing token kind string') - } else if !s[0].is_letter() { + } else if !s[0].is_letter() { // punctuation, operators - return '`$s`' + return 'token `$s`' + } + if is_key(t.lit) { + s = 'keyword' + } + if t.lit != '' { + // string contents etc + s += ' `$t.lit`' } - // string contents etc - if t.lit != '' { s += ' `$t.lit`' } return s }