From e2eebc62ba03c020ff1707dd4bff6bb879114eb7 Mon Sep 17 00:00:00 2001 From: Chris Mounce Date: Tue, 6 May 2025 08:24:36 -0700 Subject: [PATCH] Tear out old parser --- src/labels/labels.rs | 18 +++++++-------- src/preprocess/peg.rs | 54 +++---------------------------------------- 2 files changed, 11 insertions(+), 61 deletions(-) diff --git a/src/labels/labels.rs b/src/labels/labels.rs index 4bc8c1d..6a5412a 100644 --- a/src/labels/labels.rs +++ b/src/labels/labels.rs @@ -1,14 +1,12 @@ -use super::super::preprocess::peg::Parser; -use anyhow::Result; +use crate::{ + preprocess::peg::{EOF, Rule, Star}, + star, +}; -fn parse_program(p: &mut Parser) -> Result<()> { - p.star(|p| parse_line(p))?; - p.eof()?; - Ok(()) +fn parse_program() -> impl Rule { + (star!(parse_line), EOF) } -fn parse_line(p: &mut Parser) -> Result<()> { - p.star(|p| p.char(|c| c != '\n'))?; - p.literal("\n")?; - Ok(()) +fn parse_line() -> impl Rule { + (star!("foo"), "\n") } diff --git a/src/preprocess/peg.rs b/src/preprocess/peg.rs index 9f9bc19..68b4226 100644 --- a/src/preprocess/peg.rs +++ b/src/preprocess/peg.rs @@ -37,51 +37,9 @@ impl Parser { self.offset = sp.offset; self.output.truncate(sp.num_tags); } - - pub fn eof(&mut self) -> Result<()> { - if self.input.len() == self.offset { - Ok(()) - } else { - bail!("No match") - } - } - - pub fn literal(&mut self, str: &str) -> Result<()> { - if self.input[self.offset..].starts_with(str) { - self.offset += str.len(); - Ok(()) - } else { - bail!("No match") - } - } - - pub fn char(&mut self, f: F) -> Result<()> - where - F: Fn(char) -> bool, - { - if let Some(c) = self.input[self.offset..].chars().next() { - if f(c) { - self.offset += c.len_utf8(); - return Ok(()); - } - } - bail!("No match") - } - - pub fn star(&mut self, mut f: F) -> Result<()> - where - F: FnMut(&mut Self) -> Result<()>, - { - let mut sp = self.save(); - while let Ok(_) = f(self) { - sp = self.save(); - } - self.restore(sp); - Ok(()) - } } -trait Rule { +pub trait Rule { fn parse(&self, p: &mut Parser) -> Result<()>; } @@ -161,7 +119,7 @@ macro_rules! impl_rule_for_alt { }; } -struct Star(T); +pub struct Star(pub T); impl Rule for Star where @@ -173,6 +131,7 @@ where } } +#[macro_export] macro_rules! star { ($($item:tt),+ $(,)?) => { Star(( @@ -212,13 +171,6 @@ mod test { rule.parse(&mut p) } - #[test] - fn test_hello() { - let mut p = Parser::new("foo"); - p.literal("f").unwrap(); - p.star(|p| p.literal("o")).unwrap(); - } - #[test] fn test_combinator_tuples() { let foo = ("f", "o", "o");