Remove all ParseErrors that don't return the failed token

This commit is contained in:
greg 2019-01-08 01:00:40 -08:00
parent ee7861cbd0
commit 09e2d8579d
1 changed files with 13 additions and 19 deletions

View File

@ -17,13 +17,6 @@ pub struct ParseError {
}
impl ParseError {
fn new<T>(msg: &str) -> ParseResult<T> {
Err(ParseError {
msg: msg.to_string(),
token: None
})
}
fn new_with_token<T>(msg: &str, t: Token) -> ParseResult<T>{
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<Expression> {
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<Expression> {
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::<f64>() {
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::<u64>() {
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<u64> {
fn parse_binary(digits: String, tok: Token) -> ParseResult<u64> {
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<u64> {
fn parse_hex(digits: String, tok: Token) -> ParseResult<u64> {
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)