From eb99ba71192cecf29e61bc174926e83ae45fb351 Mon Sep 17 00:00:00 2001 From: mos Date: Sun, 28 Jul 2024 11:26:00 +0200 Subject: [PATCH] improve identifier parsing --- bip/src/lib.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bip/src/lib.rs b/bip/src/lib.rs index 4ec4b95..fdc6860 100644 --- a/bip/src/lib.rs +++ b/bip/src/lib.rs @@ -13,6 +13,7 @@ pub enum ParseError { ExpectedParens, ExpectedSym, ExpectedTerm, + BadID, BadInt, BadStr, BadSym(char), @@ -25,6 +26,7 @@ impl fmt::Display for ParseError { Self::ExpectedParens => write!(f, "expected parenthesis"), Self::ExpectedSym => write!(f, "expected symbol"), Self::ExpectedTerm => write!(f, "expected semicolon"), + Self::BadID => write!(f, "invalid identifier"), Self::BadInt => write!(f, "invalid integer"), Self::BadStr => write!(f, "unterminated string"), Self::BadSym(c) => write!(f, "invalid symbol {c}"), @@ -141,13 +143,12 @@ impl Symbol { } ' ' | '\t' | '\r' => {} _ => { - let err = ParseError::BadSym(c.to_owned()); - 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()))); continue; }