Skip to content

Commit 181b0f1

Browse files
author
Lukas Neubert
authored
parser: improve error message for missing db expr in ORM (#9890)
1 parent cafe382 commit 181b0f1

File tree

6 files changed

+36
-3
lines changed

6 files changed

+36
-3
lines changed

vlib/v/parser/pratt.v

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import v.vet
88
import v.token
99

1010
pub fn (mut p Parser) expr(precedence int) ast.Expr {
11+
return p.check_expr(precedence) or {
12+
p.error_with_pos('invalid expression: unexpected $p.tok', p.tok.position())
13+
}
14+
}
15+
16+
pub fn (mut p Parser) check_expr(precedence int) ?ast.Expr {
1117
$if trace_parser ? {
1218
tok_pos := p.tok.position()
1319
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 {
323329
else {
324330
if p.tok.kind != .eof && !(p.tok.kind == .rsbr && p.inside_asm) {
325331
// eof should be handled where it happens
326-
return p.error_with_pos('invalid expression: unexpected $p.tok', p.tok.position())
332+
return none
333+
// return p.error_with_pos('invalid expression: unexpected $p.tok', p.tok.position())
327334
}
328335
}
329336
}

vlib/v/parser/sql.v

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ fn (mut p Parser) sql_expr() ast.Expr {
99
// `sql db {`
1010
pos := p.tok.position()
1111
p.check_name()
12-
db_expr := p.expr(0)
12+
db_expr := p.check_expr(0) or {
13+
p.error_with_pos('invalid expression: unexpected $p.tok, expecting database',
14+
p.tok.position())
15+
}
1316
p.check(.lcbr)
1417
p.check(.key_select)
1518
n := p.check_name()
@@ -116,7 +119,10 @@ fn (mut p Parser) sql_stmt() ast.SqlStmt {
116119
}
117120
// `sql db {`
118121
p.check_name()
119-
db_expr := p.expr(0)
122+
db_expr := p.check_expr(0) or {
123+
p.error_with_pos('invalid expression: unexpected $p.tok, expecting database',
124+
p.tok.position())
125+
}
120126
// println(typeof(db_expr))
121127
p.check(.lcbr)
122128
// kind := ast.SqlExprKind.select_
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vlib/v/parser/tests/sql_no_db_expr_a.vv:3:6: error: invalid expression: unexpected token `:=`, expecting database
2+
1 | fn x() {
3+
2 | // SqlStmt
4+
3 | sql :=
5+
| ~~
6+
4 | }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn x() {
2+
// SqlStmt
3+
sql :=
4+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vlib/v/parser/tests/sql_no_db_expr_b.vv:3:11: error: invalid expression: unexpected token `:=`
2+
1 | fn x() {
3+
2 | // SqlExpr
4+
3 | x := sql :=
5+
| ~~
6+
4 | }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn x() {
2+
// SqlExpr
3+
x := sql :=
4+
}

0 commit comments

Comments
 (0)