summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-07-08 18:56:52 +0200
committerLaurenz <laurmaedje@gmail.com>2021-07-08 18:56:52 +0200
commitc5635d8a3f45865619d66bc9e296da7d9e9efa5a (patch)
tree3dcfa1db01f0c129ebfd1ac66a255290b612d033
parent859275b17b14f7e558710cbdf27ace33ca591c66 (diff)
Handle missing arguments to with expr
-rw-r--r--src/parse/mod.rs27
-rw-r--r--tests/typ/code/ops-invalid.typ6
2 files changed, 22 insertions, 11 deletions
diff --git a/src/parse/mod.rs b/src/parse/mod.rs
index 793c4139..bdecc31f 100644
--- a/src/parse/mod.rs
+++ b/src/parse/mod.rs
@@ -256,7 +256,7 @@ fn expr_with(p: &mut Parser, atomic: bool, min_prec: usize) -> Option<Expr> {
}
if p.eat_if(Token::With) {
- lhs = with_expr(p, lhs);
+ lhs = with_expr(p, lhs)?;
}
if atomic {
@@ -574,16 +574,21 @@ fn args(p: &mut Parser) -> CallArgs {
}
/// Parse a with expression.
-fn with_expr(p: &mut Parser, callee: Expr) -> Expr {
- p.start_group(Group::Paren, TokenMode::Code);
- let args = args(p);
- p.end_group();
+fn with_expr(p: &mut Parser, callee: Expr) -> Option<Expr> {
+ if p.peek() == Some(Token::LeftParen) {
+ p.start_group(Group::Paren, TokenMode::Code);
+ let args = args(p);
+ p.end_group();
- Expr::With(WithExpr {
- span: p.span(callee.span().start),
- callee: Box::new(callee),
- args,
- })
+ Some(Expr::With(WithExpr {
+ span: p.span(callee.span().start),
+ callee: Box::new(callee),
+ args,
+ }))
+ } else {
+ p.expected("argument list");
+ None
+ }
}
/// Parse a let expression.
@@ -596,7 +601,7 @@ fn let_expr(p: &mut Parser) -> Option<Expr> {
let mut init = None;
if p.eat_if(Token::With) {
- init = Some(with_expr(p, Expr::Ident(binding.clone())));
+ init = with_expr(p, Expr::Ident(binding.clone()));
} else {
// If a parenthesis follows, this is a function definition.
let mut params = None;
diff --git a/tests/typ/code/ops-invalid.typ b/tests/typ/code/ops-invalid.typ
index 12d2a2c3..ab53dd97 100644
--- a/tests/typ/code/ops-invalid.typ
+++ b/tests/typ/code/ops-invalid.typ
@@ -38,6 +38,12 @@
// Error: 14-22 cannot apply '+=' to integer and string
{ let x = 1; x += "2" }
+// Error: 13-14 expected argument list, found integer
+{ test with 2 }
+
+// Error: 3-4 expected function, found integer
+{ 1 with () }
+
---
// Bad left-hand sides of assignment.