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,
ExpectedSym,
ExpectedTerm,
BadInt,
BadStr,
BadSym(char),
}
impl fmt::Display for ParseError {
@ -22,6 +25,9 @@ impl fmt::Display for ParseError {
Self::ExpectedParens => write!(f, "expected parenthesis"),
Self::ExpectedSym => write!(f, "expected symbol"),
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>>(
it: &mut Peekable<T>,
predicate: fn(c: char) -> bool,
) -> Vec<char> {
) -> ParseResult<Vec<char>> {
let mut v = Vec::new();
while let Some(c) = it.peek() {
if !predicate(*c) {
break;
return Ok(v);
}
v.push(*c);
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 it = s.chars().peekable();
@ -109,7 +115,8 @@ impl Symbol {
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>() {
syms.push(if sign > 0 {
@ -118,7 +125,7 @@ impl Symbol {
Self::SInt((n as i64 * sign) as u64)
});
} else {
return Err("invalid integer".to_string());
return Err(ParseError::BadInt);
}
continue;
@ -129,17 +136,18 @@ impl Symbol {
'"' => {
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()));
}
' ' | '\t' | '\r' => {}
_ => {
let err = ParseError::BadSym(c.to_owned());
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())));
continue;
}
@ -195,12 +203,14 @@ impl<'a> Parser<'a> {
self.next();
loop {
if *self.peek()? == close {
break;
}
let sym = self.peek().map_err(|_| ParseError::ExpectedParens)?;
if *sym == close {
break;
} else {
node.entries.push(self.parse_expr()?);
}
}
self.expect(close, ParseError::ExpectedParens)?;
self.next();
Ok(node)

View File

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