From 87d96378f4b02a9cc014a05cf0b07ad4de3349f3 Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Sun, 13 Mar 2022 12:41:23 +0700 Subject: [PATCH] hole span --- crates/hir/src/lib.rs | 11 ++++++++--- crates/parser/src/lib.rs | 7 ++++--- example/{err.hz => err/unknown_intrinsic.hz} | 0 3 files changed, 12 insertions(+), 6 deletions(-) rename example/{err.hz => err/unknown_intrinsic.hz} (100%) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index d936f91..8259bfc 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -118,7 +118,7 @@ pub fn expr_to_ir(expr: &Expr) -> (Option, Option) { // Remove all `Hole`(s) from the args let index = args.0.iter().position(|arg| match arg.0 { - Expr::Hole => true, + Expr::Hole(..) => true, _ => false }); @@ -152,7 +152,7 @@ pub fn expr_to_ir(expr: &Expr) -> (Option, Option) { let mut largs = Vec::new(); for arg in &args.0 { match arg.0 { - Expr::Hole => { + Expr::Hole(..) => { largs.push(lhs_ir.0.clone().unwrap()); }, _ => { @@ -270,7 +270,12 @@ pub fn expr_to_ir(expr: &Expr) -> (Option, Option) { Expr::Boolean(value) => (Some(IRKind::Value { value: Value::Boolean(*value) }), None), Expr::String(value) => (Some(IRKind::Value { value: Value::String(value.clone()) }), None), Expr::Identifier(value) => (Some(IRKind::Value { value: Value::Ident(value.clone()) }), None), - Expr::Hole => (None, None), + // Probably will never happen because it is catched in parser + Expr::Hole(start, end) => (None, Some(LoweringError { + span: *start..*end, + message: "Hole can only be used in piping, it is not allowed here.".to_string(), + note: None + })), _ => { dbg!(expr); todo!() } } } diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index 808806d..18a3cec 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -36,7 +36,8 @@ pub enum Expr { body: Vec> }, - Hole, + // Hole for positional argument(s) in piping + Hole(usize, usize), // The usize is the span of the hole (prob should be single but whatever) } fn expr_parser() -> impl Parser>, Error = Simple> + Clone { @@ -45,11 +46,11 @@ fn expr_parser() -> impl Parser>, Error = Simple _ => Err(Simple::expected_input_found(span, Vec::new(), Some(token))), }).labelled("identifier"); - let literal = filter_map(|span, token| match token { + let literal = filter_map(|span: std::ops::Range, token| match token { Token::Int(i) => Ok((Expr::Int(i), span)), Token::Boolean(b) => Ok((Expr::Boolean(b), span)), Token::String(s) => Ok((Expr::String(s), span)), - Token::Hole => Ok((Expr::Hole, span)), + Token::Hole => Ok((Expr::Hole(span.start, span.end), span)), _ => Err(Simple::expected_input_found(span, Vec::new(), Some(token))), }).labelled("literal"); diff --git a/example/err.hz b/example/err/unknown_intrinsic.hz similarity index 100% rename from example/err.hz rename to example/err/unknown_intrinsic.hz