parser: improve error message for missing db expr in ORM (#9890)

pull/9895/head
Lukas Neubert 2021-04-26 18:51:51 +02:00 committed by GitHub
parent cafe382424
commit 181b0f1035
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 3 deletions

View File

@ -8,6 +8,12 @@ import v.vet
import v.token import v.token
pub fn (mut p Parser) expr(precedence int) ast.Expr { pub fn (mut p Parser) expr(precedence int) ast.Expr {
return p.check_expr(precedence) or {
p.error_with_pos('invalid expression: unexpected $p.tok', p.tok.position())
}
}
pub fn (mut p Parser) check_expr(precedence int) ?ast.Expr {
$if trace_parser ? { $if trace_parser ? {
tok_pos := p.tok.position() tok_pos := p.tok.position()
eprintln('parsing file: ${p.file_name:-30} | tok.kind: ${p.tok.kind:-10} | tok.lit: ${p.tok.lit:-10} | tok_pos: ${tok_pos.str():-45} | expr($precedence)') eprintln('parsing file: ${p.file_name:-30} | tok.kind: ${p.tok.kind:-10} | tok.lit: ${p.tok.lit:-10} | tok_pos: ${tok_pos.str():-45} | expr($precedence)')
@ -323,7 +329,8 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
else { else {
if p.tok.kind != .eof && !(p.tok.kind == .rsbr && p.inside_asm) { if p.tok.kind != .eof && !(p.tok.kind == .rsbr && p.inside_asm) {
// eof should be handled where it happens // eof should be handled where it happens
return p.error_with_pos('invalid expression: unexpected $p.tok', p.tok.position()) return none
// return p.error_with_pos('invalid expression: unexpected $p.tok', p.tok.position())
} }
} }
} }

View File

@ -9,7 +9,10 @@ fn (mut p Parser) sql_expr() ast.Expr {
// `sql db {` // `sql db {`
pos := p.tok.position() pos := p.tok.position()
p.check_name() p.check_name()
db_expr := p.expr(0) db_expr := p.check_expr(0) or {
p.error_with_pos('invalid expression: unexpected $p.tok, expecting database',
p.tok.position())
}
p.check(.lcbr) p.check(.lcbr)
p.check(.key_select) p.check(.key_select)
n := p.check_name() n := p.check_name()
@ -116,7 +119,10 @@ fn (mut p Parser) sql_stmt() ast.SqlStmt {
} }
// `sql db {` // `sql db {`
p.check_name() p.check_name()
db_expr := p.expr(0) db_expr := p.check_expr(0) or {
p.error_with_pos('invalid expression: unexpected $p.tok, expecting database',
p.tok.position())
}
// println(typeof(db_expr)) // println(typeof(db_expr))
p.check(.lcbr) p.check(.lcbr)
// kind := ast.SqlExprKind.select_ // kind := ast.SqlExprKind.select_

View File

@ -0,0 +1,6 @@
vlib/v/parser/tests/sql_no_db_expr_a.vv:3:6: error: invalid expression: unexpected token `:=`, expecting database
1 | fn x() {
2 | // SqlStmt
3 | sql :=
| ~~
4 | }

View File

@ -0,0 +1,4 @@
fn x() {
// SqlStmt
sql :=
}

View File

@ -0,0 +1,6 @@
vlib/v/parser/tests/sql_no_db_expr_b.vv:3:11: error: invalid expression: unexpected token `:=`
1 | fn x() {
2 | // SqlExpr
3 | x := sql :=
| ~~
4 | }

View File

@ -0,0 +1,4 @@
fn x() {
// SqlExpr
x := sql :=
}