fix and improve parser logic to handle errors better

This commit is contained in:
mos 2024-07-28 11:17:25 +02:00
parent 3266726f16
commit c66728ab0e
2 changed files with 26 additions and 18 deletions

View File

@ -13,6 +13,9 @@ pub enum ParseError {
ExpectedParens, ExpectedParens,
ExpectedSym, ExpectedSym,
ExpectedTerm, ExpectedTerm,
BadInt,
BadStr,
BadSym(char),
} }
impl fmt::Display for ParseError { impl fmt::Display for ParseError {
@ -22,6 +25,9 @@ impl fmt::Display for ParseError {
Self::ExpectedParens => write!(f, "expected parenthesis"), Self::ExpectedParens => write!(f, "expected parenthesis"),
Self::ExpectedSym => write!(f, "expected symbol"), Self::ExpectedSym => write!(f, "expected symbol"),
Self::ExpectedTerm => write!(f, "expected semicolon"), Self::ExpectedTerm => write!(f, "expected semicolon"),
Self::BadInt => write!(f, "invalid integer"),
Self::BadStr => write!(f, "unterminated string"),
Self::BadSym(c) => write!(f, "invalid symbol {c}"),
} }
} }
} }
@ -75,22 +81,22 @@ impl Symbol {
fn take<T: Iterator<Item = char>>( fn take<T: Iterator<Item = char>>(
it: &mut Peekable<T>, it: &mut Peekable<T>,
predicate: fn(c: char) -> bool, predicate: fn(c: char) -> bool,
) -> Vec<char> { ) -> ParseResult<Vec<char>> {
let mut v = Vec::new(); let mut v = Vec::new();
while let Some(c) = it.peek() { while let Some(c) = it.peek() {
if !predicate(*c) { if !predicate(*c) {
break; return Ok(v);
} }
v.push(*c); v.push(*c);
it.next(); it.next();
} }
v Err(ParseError::Eof)
} }
pub fn parse(s: &str) -> Result<Vec<Self>, String> { pub fn parse(s: &str) -> ParseResult<Vec<Self>> {
let mut syms = Vec::new(); let mut syms = Vec::new();
let mut it = s.chars().peekable(); let mut it = s.chars().peekable();
@ -109,7 +115,8 @@ impl Symbol {
it.next(); it.next();
} }
let v = Symbol::take(&mut it, |c| c.is_numeric()); let v = Symbol::take(&mut it, |c| c.is_numeric())
.map_err(|_| ParseError::BadInt)?;
if let Ok(n) = v.iter().collect::<String>().parse::<u64>() { if let Ok(n) = v.iter().collect::<String>().parse::<u64>() {
syms.push(if sign > 0 { syms.push(if sign > 0 {
@ -118,7 +125,7 @@ impl Symbol {
Self::SInt((n as i64 * sign) as u64) Self::SInt((n as i64 * sign) as u64)
}); });
} else { } else {
return Err("invalid integer".to_string()); return Err(ParseError::BadInt);
} }
continue; continue;
@ -129,17 +136,18 @@ impl Symbol {
'"' => { '"' => {
it.next(); it.next();
let v = Symbol::take(&mut it, |c| c != '"'); let v = Symbol::take(&mut it, |c| c != '"').map_err(|_| ParseError::BadStr)?;
syms.push(Self::Str(v.iter().collect())); syms.push(Self::Str(v.iter().collect()));
} }
' ' | '\t' | '\r' => {} ' ' | '\t' | '\r' => {}
_ => { _ => {
let err = ParseError::BadSym(c.to_owned());
if !c.is_alphanumeric() { if !c.is_alphanumeric() {
return Err(format!("invalid symbol {}", c)); return Err(err);
} }
let v = Symbol::take(&mut it, |c| c.is_alphanumeric()); let v = Symbol::take(&mut it, |c| c.is_alphanumeric()).map_err(|_| err)?;
syms.push(Self::Id(Some(v.iter().collect()))); syms.push(Self::Id(Some(v.iter().collect())));
continue; continue;
} }
@ -195,11 +203,13 @@ impl<'a> Parser<'a> {
self.next(); self.next();
loop { loop {
if *self.peek()? == close { let sym = self.peek().map_err(|_| ParseError::ExpectedParens)?;
break;
}
node.entries.push(self.parse_expr()?); if *sym == close {
break;
} else {
node.entries.push(self.parse_expr()?);
}
} }
self.expect(close, ParseError::ExpectedParens)?; self.expect(close, ParseError::ExpectedParens)?;
self.next(); self.next();

View File

@ -8,7 +8,6 @@ pub type Result<T> = std::result::Result<T, Error>;
pub enum Error { pub enum Error {
ReadError, ReadError,
BadFile, BadFile,
BadRead(String),
BadWrite(std::io::Error), BadWrite(std::io::Error),
BadPath(std::io::Error), BadPath(std::io::Error),
BpError(bp::Error), BpError(bp::Error),
@ -20,7 +19,6 @@ impl fmt::Display for Error {
match self { match self {
Self::ReadError => write!(f, "errors found"), Self::ReadError => write!(f, "errors found"),
Self::BadFile => write!(f, "invalid text"), Self::BadFile => write!(f, "invalid text"),
Self::BadRead(e) => write!(f, "{e}"),
Self::BadWrite(e) => write!(f, "{e}"), Self::BadWrite(e) => write!(f, "{e}"),
Self::BadPath(e) => write!(f, "{e}"), Self::BadPath(e) => write!(f, "{e}"),
Self::BpError(e) => write!(f, "{e}"), Self::BpError(e) => write!(f, "{e}"),
@ -35,7 +33,7 @@ fn read_bip<R: BufRead>(buf: R) -> Result<Vec<bip::Node>> {
.lines() .lines()
.map(|l| { .map(|l| {
l.map_or(Err(Error::BadFile), |l| { l.map_or(Err(Error::BadFile), |l| {
bip::Symbol::parse(&l).map_err(Error::BadRead) bip::Symbol::parse(&l).map_err(Error::ParseError)
}) })
}) })
.filter_map(|r| r.map_err(|e| errors.push(e)).ok()) .filter_map(|r| r.map_err(|e| errors.push(e)).ok())
@ -48,7 +46,7 @@ fn read_bip<R: BufRead>(buf: R) -> Result<Vec<bip::Node>> {
}); });
if err_count > 0 { if err_count > 0 {
eprintln!("{err_count} errors"); eprintln!("{err_count} error{}", if err_count == 1 { "" } else { "s" });
Err(Error::ReadError) Err(Error::ReadError)
} else { } else {
bip::Parser::new(&syms).parse().map_err(Error::ParseError) bip::Parser::new(&syms).parse().map_err(Error::ParseError)