Tear out old parser

This commit is contained in:
2025-05-06 08:24:36 -07:00
parent 5be13dca3e
commit e2eebc62ba
2 changed files with 11 additions and 61 deletions
+8 -10
View File
@@ -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")
}
+3 -51
View File
@@ -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<F>(&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<F>(&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>(T);
pub struct Star<T>(pub T);
impl<T> Rule for Star<T>
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");