From bfeec6f839cdd6d80e65283adc08412b94a4bc07 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Thu, 29 Jul 2021 17:34:34 +1000 Subject: [PATCH] Give information on BLT syntax errors --- src/election.rs | 6 +++-- src/parser/blt.rs | 61 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/src/election.rs b/src/election.rs index fafe41a..0182797 100644 --- a/src/election.rs +++ b/src/election.rs @@ -44,8 +44,10 @@ impl Election { /// Parse the given BLT file and return an [Election] pub fn from_blt>(input: Peekable) -> Self { let mut parser = BLTParser::new(input); - parser.parse_blt().expect("Syntax Error"); - return parser.as_election(); + match parser.parse_blt() { + Ok(_) => { return parser.as_election(); } + Err(e) => { panic!("Syntax Error: {}", e); } + } } /// Convert ballots with weight >1 to multiple ballots of weight 1 diff --git a/src/parser/blt.rs b/src/parser/blt.rs index 73baa8e..d64a36a 100644 --- a/src/parser/blt.rs +++ b/src/parser/blt.rs @@ -18,6 +18,7 @@ use crate::election::{Ballot, Candidate, Election}; use crate::numbers::Number; +use std::fmt; use std::iter::Peekable; /// Utility for parsing a BLT file @@ -26,7 +27,11 @@ pub struct BLTParser> { chars: Peekable, /// Temporary buffer for parsing ballot values - buf_ballot_value: String, + ballot_value_buf: String, + /// Current line number + line_no: u32, + /// Current column number + col_no: u32, /// Number of candidates num_candidates: usize, @@ -35,10 +40,25 @@ pub struct BLTParser> { } /// An error when parsing a BLT file -#[derive(Debug)] pub enum ParseError { - /// Invalid syntax - Invalid + /// Unexpected character + Unexpected(u32, u32, char), + /// Unexpected character, expected ... + Expected(u32, u32, char, &'static str), +} + +impl fmt::Display for ParseError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + ParseError::Unexpected(line_no, col_no, char) => { + f.write_fmt(format_args!("Line {} col {}, unexpected '{}'", line_no, col_no, char))?; + } + ParseError::Expected(line_no, col_no, char, expected) => { + f.write_fmt(format_args!("Line {} col {}, unexpected '{}', expected {}", line_no, col_no, char, expected))?; + } + } + return Ok(()); + } } impl> BLTParser { @@ -85,8 +105,8 @@ impl> BLTParser { if self.lookahead() == '.' { // Decimal - self.buf_ballot_value.clear(); - self.buf_ballot_value.push('0'); + self.ballot_value_buf.clear(); + self.ballot_value_buf.push('0'); self.ballot()?; } else { // End of ballots @@ -94,7 +114,7 @@ impl> BLTParser { break; } } else { - self.buf_ballot_value.clear(); + self.ballot_value_buf.clear(); self.ballot()?; } } @@ -123,7 +143,7 @@ impl> BLTParser { self.delimiter(); let ballot = Ballot { - orig_value: N::parse(&self.buf_ballot_value), + orig_value: N::parse(&self.ballot_value_buf), preferences: preferences, }; self.election.ballots.push(ballot); @@ -168,7 +188,7 @@ impl> BLTParser { loop { match self.ballot_value_element() { Err(_) => { break; } - Ok(d) => { self.buf_ballot_value.push(d); } + Ok(d) => { self.ballot_value_buf.push(d); } } } return Ok(()); @@ -184,7 +204,7 @@ impl> BLTParser { Ok(s) => { return Ok(s); } Err(_) => {} } - return Err(ParseError::Invalid); + return Err(ParseError::Expected(self.line_no, self.col_no, self.lookahead(), "string")); } /// Parse a quoted string @@ -202,7 +222,7 @@ impl> BLTParser { } return Ok(result); } else { - return Err(ParseError::Invalid); + return Err(ParseError::Expected(self.line_no, self.col_no, self.lookahead(), "'\"'")); } } @@ -268,7 +288,7 @@ impl> BLTParser { if self.lookahead() >= '1' && self.lookahead() <= '9' { return Ok(self.accept()); } else { - return Err(ParseError::Invalid); + return Err(ParseError::Expected(self.line_no, self.col_no, self.lookahead(), "nonzero digit")); } } @@ -277,7 +297,7 @@ impl> BLTParser { if self.lookahead() >= '0' && self.lookahead() <= '9' { return Ok(self.accept()); } else { - return Err(ParseError::Invalid); + return Err(ParseError::Expected(self.line_no, self.col_no, self.lookahead(), "digit")); } } @@ -286,7 +306,7 @@ impl> BLTParser { if (self.lookahead() >= '0' && self.lookahead() <= '9') || self.lookahead() == '.' || self.lookahead() == '/' { return Ok(self.accept()); } else { - return Err(ParseError::Invalid); + return Err(ParseError::Expected(self.line_no, self.col_no, self.lookahead(), "number")); } } @@ -305,7 +325,14 @@ impl> BLTParser { /// Read and return one character from the stream fn accept(&mut self) -> char { - return self.chars.next().expect("Unexpected EOF"); + let result = self.chars.next().expect("Unexpected EOF"); + if result == '\n' { + self.line_no += 1; + self.col_no = 1; + } else { + self.col_no += 1; + } + return result; } // PUBLIC API @@ -314,7 +341,9 @@ impl> BLTParser { pub fn new(chars: Peekable) -> Self { Self { chars, - buf_ballot_value: String::new(), + ballot_value_buf: String::new(), + line_no: 1, + col_no: 1, num_candidates: 0, election: Election { name: String::new(),