improve identifier parsing

This commit is contained in:
mos 2024-07-28 11:26:00 +02:00
parent c66728ab0e
commit eb99ba7119
1 changed files with 5 additions and 4 deletions

View File

@ -13,6 +13,7 @@ pub enum ParseError {
ExpectedParens, ExpectedParens,
ExpectedSym, ExpectedSym,
ExpectedTerm, ExpectedTerm,
BadID,
BadInt, BadInt,
BadStr, BadStr,
BadSym(char), BadSym(char),
@ -25,6 +26,7 @@ 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::BadID => write!(f, "invalid identifier"),
Self::BadInt => write!(f, "invalid integer"), Self::BadInt => write!(f, "invalid integer"),
Self::BadStr => write!(f, "unterminated string"), Self::BadStr => write!(f, "unterminated string"),
Self::BadSym(c) => write!(f, "invalid symbol {c}"), Self::BadSym(c) => write!(f, "invalid symbol {c}"),
@ -141,13 +143,12 @@ impl Symbol {
} }
' ' | '\t' | '\r' => {} ' ' | '\t' | '\r' => {}
_ => { _ => {
let err = ParseError::BadSym(c.to_owned());
if !c.is_alphanumeric() { if !c.is_alphanumeric() {
return Err(err); return Err(ParseError::BadSym(*c));
} }
let v = Symbol::take(&mut it, |c| c.is_alphanumeric()).map_err(|_| err)?; let v = Symbol::take(&mut it, |c| c.is_alphanumeric())
.map_err(|_| ParseError::BadID)?;
syms.push(Self::Id(Some(v.iter().collect()))); syms.push(Self::Id(Some(v.iter().collect())));
continue; continue;
} }