token: fix Token.str() for punctuation and operators (#8610)

pull/8615/head
Nick Treleaven 2021-02-07 02:40:00 +00:00 committed by GitHub
parent d284918554
commit b92f980274
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 5 deletions

View File

@ -250,11 +250,11 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
node = p.struct_init(true) // short_syntax: true node = p.struct_init(true) // short_syntax: true
} else if p.tok.kind == .name { } else if p.tok.kind == .name {
p.next() p.next()
s := if p.tok.lit != '' { '`$p.tok.lit`' } else { p.tok.kind.str() } p.error_with_pos('unexpected $p.tok, expecting `:` after struct field name',
p.error_with_pos('unexpected $s, expecting `:`', p.tok.position()) p.tok.position())
return ast.Expr{} return ast.Expr{}
} else { } 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()) p.tok.position())
return ast.Expr{} return ast.Expr{}
} }

View File

@ -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 5 | _ = fn (name string) flag.Flag
6 | _ = fn (name string) flag.Flag {return {}} 6 | _ = fn (name string) flag.Flag {return {}}
7 | _ = fn (name string) "hi" + name 7 | _ = fn (name string) "hi" + name

View File

@ -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 | }

View File

@ -0,0 +1,9 @@
enum TestEnum {
a b
}
x = {
.a : 'Alpha'
.b : 'Beta'
}

View File

@ -324,7 +324,16 @@ pub fn (t Kind) str() string {
} }
pub fn (t Token) 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 // Representation of highest and lowest precedence