parser: add an error for an invalid string prefix (#7019)

pull/7035/head
Swastik Baranwal 2020-11-30 14:28:00 +05:30 committed by GitHub
parent 39b46e95a0
commit e3fab7e076
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 2 deletions

View File

@ -1037,8 +1037,13 @@ pub fn (mut p Parser) name_expr() ast.Expr {
} }
} }
// Raw string (`s := r'hello \n ') // Raw string (`s := r'hello \n ')
if p.tok.lit in ['r', 'c', 'js'] && p.peek_tok.kind == .string && !p.inside_str_interp { if p.peek_tok.kind == .string && !p.inside_str_interp && p.peek_tok2.kind != .colon {
return p.string_expr() if p.tok.lit in ['r', 'c', 'js'] && p.tok.kind == .name {
return p.string_expr()
} else {
// don't allow any other string prefix except `r`, `js` and `c`
p.error('only `c`, `r`, `js` are recognized string prefixes, but you tried to use `$p.tok.lit`')
}
} }
// don't allow r`byte` and c`byte` // don't allow r`byte` and c`byte`
if p.tok.lit in ['r', 'c'] && p.peek_tok.kind == .chartoken { if p.tok.lit in ['r', 'c'] && p.peek_tok.kind == .chartoken {

View File

@ -0,0 +1,6 @@
vlib/v/parser/tests/string_invalid_prefix_err.vv:2:12: error: only `c`, `r`, `js` are recognized string prefixes, but you tried to use `w`
1 | fn main() {
2 | why := w'why'
| ^
3 | println(why)
4 | }

View File

@ -0,0 +1,4 @@
fn main() {
why := w'why'
println(why)
}