refactor(parser): Give more impl flexibility

This commit is contained in:
Ed Page 2021-04-27 21:26:05 -05:00
parent 9cbc7410a4
commit 287c4cbfe9

View file

@ -57,13 +57,8 @@ impl Tokenizer {
pub fn parse_str<'c>(&'c self, content: &'c str) -> impl Iterator<Item = Identifier<'c>> { pub fn parse_str<'c>(&'c self, content: &'c str) -> impl Iterator<Item = Identifier<'c>> {
parser::iter_literals(content).filter_map(move |identifier| { parser::iter_literals(content).filter_map(move |identifier| {
let case = Case::None;
let offset = offset(content.as_bytes(), identifier.as_bytes()); let offset = offset(content.as_bytes(), identifier.as_bytes());
if self.accept(identifier) { self.transform(identifier, offset)
Some(Identifier::new_unchecked(identifier, case, offset))
} else {
None
}
}) })
} }
@ -76,21 +71,22 @@ impl Tokenizer {
}) })
} }
fn accept(&self, contents: &str) -> bool { fn transform<'i>(&self, identifier: &'i str, offset: usize) -> Option<Identifier<'i>> {
debug_assert!(!contents.is_empty()); debug_assert!(!identifier.is_empty());
if self.leading_digits { if self.leading_digits {
if is_number(contents.as_bytes()) { if is_number(identifier.as_bytes()) {
return false; return None;
} }
if self.ignore_hex && is_hex(contents.as_bytes()) { if self.ignore_hex && is_hex(identifier.as_bytes()) {
return false; return None;
} }
} else if is_digit(contents.as_bytes()[0]) { } else if is_digit(identifier.as_bytes()[0]) {
return false; return None;
} }
true let case = Case::None;
Some(Identifier::new_unchecked(identifier, case, offset))
} }
} }