Give information on BLT syntax errors

This commit is contained in:
RunasSudo 2021-07-29 17:34:34 +10:00
parent 3801d30527
commit bfeec6f839
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 49 additions and 18 deletions

View File

@ -44,8 +44,10 @@ impl<N: Number> Election<N> {
/// Parse the given BLT file and return an [Election]
pub fn from_blt<I: Iterator<Item=char>>(input: Peekable<I>) -> 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

View File

@ -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<N: Number, I: Iterator<Item=char>> {
chars: Peekable<I>,
/// 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<N: Number, I: Iterator<Item=char>> {
}
/// 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<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
@ -85,8 +105,8 @@ impl<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
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<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
break;
}
} else {
self.buf_ballot_value.clear();
self.ballot_value_buf.clear();
self.ballot()?;
}
}
@ -123,7 +143,7 @@ impl<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
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<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
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<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
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<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
}
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<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
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<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
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<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
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<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
/// 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<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
pub fn new(chars: Peekable<I>) -> 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(),