From b92f9802743cfed4aac0d57aa1eb04a7658e1006 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Sun, 7 Feb 2021 02:40:00 +0000 Subject: [PATCH] token: fix Token.str() for punctuation and operators (#8610) --- vlib/v/parser/pratt.v | 6 +++--- vlib/v/parser/tests/anon_fn_return_type.out | 2 +- vlib/v/parser/tests/struct_field_expected.out | 7 +++++++ vlib/v/parser/tests/struct_field_expected.vv | 9 +++++++++ vlib/v/token/token.v | 11 ++++++++++- 5 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 vlib/v/parser/tests/struct_field_expected.out create mode 100644 vlib/v/parser/tests/struct_field_expected.vv diff --git a/vlib/v/parser/pratt.v b/vlib/v/parser/pratt.v index d40cc0fa45..f3cb27ab45 100644 --- a/vlib/v/parser/pratt.v +++ b/vlib/v/parser/pratt.v @@ -250,11 +250,11 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr { node = p.struct_init(true) // short_syntax: true } else if p.tok.kind == .name { p.next() - s := if p.tok.lit != '' { '`$p.tok.lit`' } else { p.tok.kind.str() } - p.error_with_pos('unexpected $s, expecting `:`', p.tok.position()) + p.error_with_pos('unexpected $p.tok, expecting `:` after struct field name', + p.tok.position()) return ast.Expr{} } else { - p.error_with_pos('unexpected `$p.tok.lit`, expecting struct key', + p.error_with_pos('unexpected $p.tok, expecting struct field name', p.tok.position()) return ast.Expr{} } diff --git a/vlib/v/parser/tests/anon_fn_return_type.out b/vlib/v/parser/tests/anon_fn_return_type.out index bbc18cabd5..c787e54752 100644 --- a/vlib/v/parser/tests/anon_fn_return_type.out +++ b/vlib/v/parser/tests/anon_fn_return_type.out @@ -1,4 +1,4 @@ -vlib/v/parser/tests/anon_fn_return_type.vv:7:22: error: expected return type, not string "hi" for anonymous function +vlib/v/parser/tests/anon_fn_return_type.vv:7:22: error: expected return type, not string `hi` for anonymous function 5 | _ = fn (name string) flag.Flag 6 | _ = fn (name string) flag.Flag {return {}} 7 | _ = fn (name string) "hi" + name diff --git a/vlib/v/parser/tests/struct_field_expected.out b/vlib/v/parser/tests/struct_field_expected.out new file mode 100644 index 0000000000..6d2841aa97 --- /dev/null +++ b/vlib/v/parser/tests/struct_field_expected.out @@ -0,0 +1,7 @@ +vlib/v/parser/tests/struct_field_expected.vv:6:2: error: unexpected `.`, expecting struct field name + 4 | + 5 | x = { + 6 | .a : 'Alpha' + | ^ + 7 | .b : 'Beta' + 8 | } diff --git a/vlib/v/parser/tests/struct_field_expected.vv b/vlib/v/parser/tests/struct_field_expected.vv new file mode 100644 index 0000000000..bf4929cded --- /dev/null +++ b/vlib/v/parser/tests/struct_field_expected.vv @@ -0,0 +1,9 @@ +enum TestEnum { + a b +} + +x = { + .a : 'Alpha' + .b : 'Beta' +} + diff --git a/vlib/v/token/token.v b/vlib/v/token/token.v index 5229da2e81..64976eb2ae 100644 --- a/vlib/v/token/token.v +++ b/vlib/v/token/token.v @@ -324,7 +324,16 @@ pub fn (t Kind) str() string { } pub fn (t Token) str() string { - return '$t.kind.str() "$t.lit"' + mut s := t.kind.str() + if s.len == 0 { + eprintln('missing token kind string') + } else if !s[0].is_letter() { + // punctuation, operators + return '`$s`' + } + // string contents etc + if t.lit != '' { s += ' `$t.lit`' } + return s } // Representation of highest and lowest precedence