Give information on BLT syntax errors
This commit is contained in:
parent
3801d30527
commit
bfeec6f839
|
@ -44,8 +44,10 @@ impl<N: Number> Election<N> {
|
||||||
/// Parse the given BLT file and return an [Election]
|
/// Parse the given BLT file and return an [Election]
|
||||||
pub fn from_blt<I: Iterator<Item=char>>(input: Peekable<I>) -> Self {
|
pub fn from_blt<I: Iterator<Item=char>>(input: Peekable<I>) -> Self {
|
||||||
let mut parser = BLTParser::new(input);
|
let mut parser = BLTParser::new(input);
|
||||||
parser.parse_blt().expect("Syntax Error");
|
match parser.parse_blt() {
|
||||||
return parser.as_election();
|
Ok(_) => { return parser.as_election(); }
|
||||||
|
Err(e) => { panic!("Syntax Error: {}", e); }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert ballots with weight >1 to multiple ballots of weight 1
|
/// Convert ballots with weight >1 to multiple ballots of weight 1
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
use crate::election::{Ballot, Candidate, Election};
|
use crate::election::{Ballot, Candidate, Election};
|
||||||
use crate::numbers::Number;
|
use crate::numbers::Number;
|
||||||
|
|
||||||
|
use std::fmt;
|
||||||
use std::iter::Peekable;
|
use std::iter::Peekable;
|
||||||
|
|
||||||
/// Utility for parsing a BLT file
|
/// Utility for parsing a BLT file
|
||||||
|
@ -26,7 +27,11 @@ pub struct BLTParser<N: Number, I: Iterator<Item=char>> {
|
||||||
chars: Peekable<I>,
|
chars: Peekable<I>,
|
||||||
|
|
||||||
/// Temporary buffer for parsing ballot values
|
/// 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
|
/// Number of candidates
|
||||||
num_candidates: usize,
|
num_candidates: usize,
|
||||||
|
@ -35,10 +40,25 @@ pub struct BLTParser<N: Number, I: Iterator<Item=char>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An error when parsing a BLT file
|
/// An error when parsing a BLT file
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum ParseError {
|
pub enum ParseError {
|
||||||
/// Invalid syntax
|
/// Unexpected character
|
||||||
Invalid
|
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> {
|
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() == '.' {
|
if self.lookahead() == '.' {
|
||||||
// Decimal
|
// Decimal
|
||||||
self.buf_ballot_value.clear();
|
self.ballot_value_buf.clear();
|
||||||
self.buf_ballot_value.push('0');
|
self.ballot_value_buf.push('0');
|
||||||
self.ballot()?;
|
self.ballot()?;
|
||||||
} else {
|
} else {
|
||||||
// End of ballots
|
// End of ballots
|
||||||
|
@ -94,7 +114,7 @@ impl<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.buf_ballot_value.clear();
|
self.ballot_value_buf.clear();
|
||||||
self.ballot()?;
|
self.ballot()?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -123,7 +143,7 @@ impl<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
|
||||||
self.delimiter();
|
self.delimiter();
|
||||||
|
|
||||||
let ballot = Ballot {
|
let ballot = Ballot {
|
||||||
orig_value: N::parse(&self.buf_ballot_value),
|
orig_value: N::parse(&self.ballot_value_buf),
|
||||||
preferences: preferences,
|
preferences: preferences,
|
||||||
};
|
};
|
||||||
self.election.ballots.push(ballot);
|
self.election.ballots.push(ballot);
|
||||||
|
@ -168,7 +188,7 @@ impl<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
|
||||||
loop {
|
loop {
|
||||||
match self.ballot_value_element() {
|
match self.ballot_value_element() {
|
||||||
Err(_) => { break; }
|
Err(_) => { break; }
|
||||||
Ok(d) => { self.buf_ballot_value.push(d); }
|
Ok(d) => { self.ballot_value_buf.push(d); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
@ -184,7 +204,7 @@ impl<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
|
||||||
Ok(s) => { return Ok(s); }
|
Ok(s) => { return Ok(s); }
|
||||||
Err(_) => {}
|
Err(_) => {}
|
||||||
}
|
}
|
||||||
return Err(ParseError::Invalid);
|
return Err(ParseError::Expected(self.line_no, self.col_no, self.lookahead(), "string"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse a quoted string
|
/// Parse a quoted string
|
||||||
|
@ -202,7 +222,7 @@ impl<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
|
||||||
}
|
}
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
} else {
|
} 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' {
|
if self.lookahead() >= '1' && self.lookahead() <= '9' {
|
||||||
return Ok(self.accept());
|
return Ok(self.accept());
|
||||||
} else {
|
} 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' {
|
if self.lookahead() >= '0' && self.lookahead() <= '9' {
|
||||||
return Ok(self.accept());
|
return Ok(self.accept());
|
||||||
} else {
|
} 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() == '/' {
|
if (self.lookahead() >= '0' && self.lookahead() <= '9') || self.lookahead() == '.' || self.lookahead() == '/' {
|
||||||
return Ok(self.accept());
|
return Ok(self.accept());
|
||||||
} else {
|
} 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
|
/// Read and return one character from the stream
|
||||||
fn accept(&mut self) -> char {
|
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
|
// PUBLIC API
|
||||||
|
@ -314,7 +341,9 @@ impl<N: Number, I: Iterator<Item=char>> BLTParser<N, I> {
|
||||||
pub fn new(chars: Peekable<I>) -> Self {
|
pub fn new(chars: Peekable<I>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
chars,
|
chars,
|
||||||
buf_ballot_value: String::new(),
|
ballot_value_buf: String::new(),
|
||||||
|
line_no: 1,
|
||||||
|
col_no: 1,
|
||||||
num_candidates: 0,
|
num_candidates: 0,
|
||||||
election: Election {
|
election: Election {
|
||||||
name: String::new(),
|
name: String::new(),
|
||||||
|
|
Loading…
Reference in New Issue