From 09e2d8579decdb442593b3a21d56722699e10284 Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 8 Jan 2019 01:00:40 -0800 Subject: [PATCH] Remove all ParseErrors that don't return the failed token --- schala-lang/language/src/parsing.rs | 32 ++++++++++++----------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/schala-lang/language/src/parsing.rs b/schala-lang/language/src/parsing.rs index db90762..41b4cb3 100644 --- a/schala-lang/language/src/parsing.rs +++ b/schala-lang/language/src/parsing.rs @@ -17,13 +17,6 @@ pub struct ParseError { } impl ParseError { - fn new(msg: &str) -> ParseResult { - Err(ParseError { - msg: msg.to_string(), - token: None - }) - } - fn new_with_token(msg: &str, t: Token) -> ParseResult{ Err(ParseError { msg: msg.to_string(), @@ -506,7 +499,7 @@ impl Parser { _ => None }; if let Some(_) = expr_body.1 { - return ParseError::new("Bad parse state"); + return ParseError::new_with_token("Bad parse state encountered", self.token_handler.peek()); } expr_body.1 = type_anno; Ok(expr_body) @@ -637,7 +630,7 @@ impl Parser { #[recursive_descent_method] fn curly_brace_expr(&mut self) -> ParseResult { - ParseError::new("Not implemented") + ParseError::new_with_token("Not implemented", self.token_handler.peek()) } #[recursive_descent_method] @@ -1034,12 +1027,12 @@ impl Parser { match tok.get_kind() { BinNumberSigil => { let digits = self.digits()?; - let n = parse_binary(digits)?; + let n = parse_binary(digits, tok)?; Ok(Expression(NatLiteral(n), None)) }, HexLiteral(text) => { let digits: String = text.chars().filter(|c| c.is_digit(16)).collect(); - let n = parse_hex(digits)?; + let n = parse_hex(digits, tok)?; Ok(Expression(NatLiteral(n), None)) }, _ => return ParseError::new_with_token("Expected '0x' or '0b'", tok), @@ -1049,6 +1042,7 @@ impl Parser { #[recursive_descent_method] fn float_literal(&mut self) -> ParseResult { use self::ExpressionType::*; + let tok = self.token_handler.peek(); let mut digits = self.digits()?; if let Period = self.token_handler.peek_kind() { self.token_handler.next(); @@ -1056,13 +1050,13 @@ impl Parser { digits.push_str(&self.digits()?); match digits.parse::() { Ok(f) => Ok(Expression(FloatLiteral(f), None)), - Err(e) => ParseError::new(&format!("Float failed to parse with error: {}", e)), + Err(e) => ParseError::new_with_token(&format!("Float failed to parse with error: {}", e), tok), } } else { match digits.parse::() { Ok(d) => Ok(Expression(NatLiteral(d), None)), - Err(e) => ParseError::new(&format!("Integer failed to parse with error: {}", e)), + Err(e) => ParseError::new_with_token(&format!("Integer failed to parse with error: {}", e), tok), } } } @@ -1081,34 +1075,34 @@ impl Parser { } } -fn parse_binary(digits: String) -> ParseResult { +fn parse_binary(digits: String, tok: Token) -> ParseResult { let mut result: u64 = 0; let mut multiplier = 1; for d in digits.chars().rev() { match d { '1' => result += multiplier, '0' => (), - _ => return ParseError::new("Encountered a character not '1' or '0 while parsing a binary literal"), + _ => return ParseError::new_with_token("Encountered a character not '1' or '0 while parsing a binary literal", tok), } multiplier = match multiplier.checked_mul(2) { Some(m) => m, - None => return ParseError::new("This binary expression will overflow") + None => return ParseError::new_with_token("This binary expression will overflow", tok) } } Ok(result) } -fn parse_hex(digits: String) -> ParseResult { +fn parse_hex(digits: String, tok: Token) -> ParseResult { let mut result: u64 = 0; let mut multiplier: u64 = 1; for d in digits.chars().rev() { match d.to_digit(16) { Some(n) => result += n as u64 * multiplier, - None => return ParseError::new("Encountered a non-hex digit in a hex literal"), + None => return ParseError::new_with_token("Encountered a non-hex digit in a hex literal", tok), } multiplier = match multiplier.checked_mul(16) { Some(m) => m, - None => return ParseError::new("This hex expression will overflow") + None => return ParseError::new_with_token("This hex expression will overflow", tok) } } Ok(result)