From 22f2750853790f4ece4f14c641c638bc5ea72639 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 5 Jan 2019 17:40:05 -0800 Subject: [PATCH] More use of Token in error messages --- schala-lang/language/src/parsing.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/schala-lang/language/src/parsing.rs b/schala-lang/language/src/parsing.rs index cce6c6e..73c5680 100644 --- a/schala-lang/language/src/parsing.rs +++ b/schala-lang/language/src/parsing.rs @@ -121,13 +121,16 @@ macro_rules! print_token_pattern { } macro_rules! expect { - ($self:expr, $token_type:pat) => { expect!($self, $token_type if true) }; - ($self:expr, $token_type:pat if $cond:expr) => { - match $self.peek() { - $token_type if $cond => $self.next(), - tok => { - let msg = format!("Expected {}, got {:?}", print_token_pattern!($token_type), tok); - return ParseError::new(&msg); + ($self:expr, $token_kind:pat) => { expect!($self, $token_kind if true) }; + ($self:expr, $expected_kind:pat if $cond:expr) => { + { + let tok = $self.token_handler.peek_token(); + match tok.get_kind() { + $expected_kind if $cond => $self.next(), + actual_kind => { + let msg = format!("Expected {}, got {:?}", print_token_pattern!($expected_kind), actual_kind); + return ParseError::new_with_token(&msg, tok); + } } } } @@ -982,7 +985,8 @@ impl Parser { #[recursive_descent_method] fn for_expr_body(&mut self) -> ParseResult { use self::ForBody::*; - Ok(match self.peek() { + let tok = self.token_handler.peek_token(); + Ok(match tok.get_kind() { LCurlyBrace => { let statements = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict); StatementBlock(statements.into_iter().map(|s| Node::new(s)).collect()) @@ -991,7 +995,7 @@ impl Parser { self.next(); MonadicReturn(self.expression()?) }, - _ => return ParseError::new("for expressions must end in a block or 'return'"), + _ => return ParseError::new_with_token("for expressions must end in a block or 'return'", tok), }) }