Tear out old parser
This commit is contained in:
+8
-10
@@ -1,14 +1,12 @@
|
|||||||
use super::super::preprocess::peg::Parser;
|
use crate::{
|
||||||
use anyhow::Result;
|
preprocess::peg::{EOF, Rule, Star},
|
||||||
|
star,
|
||||||
|
};
|
||||||
|
|
||||||
fn parse_program(p: &mut Parser) -> Result<()> {
|
fn parse_program() -> impl Rule {
|
||||||
p.star(|p| parse_line(p))?;
|
(star!(parse_line), EOF)
|
||||||
p.eof()?;
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_line(p: &mut Parser) -> Result<()> {
|
fn parse_line() -> impl Rule {
|
||||||
p.star(|p| p.char(|c| c != '\n'))?;
|
(star!("foo"), "\n")
|
||||||
p.literal("\n")?;
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-51
@@ -37,51 +37,9 @@ impl Parser {
|
|||||||
self.offset = sp.offset;
|
self.offset = sp.offset;
|
||||||
self.output.truncate(sp.num_tags);
|
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<()>;
|
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>
|
impl<T> Rule for Star<T>
|
||||||
where
|
where
|
||||||
@@ -173,6 +131,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
macro_rules! star {
|
macro_rules! star {
|
||||||
($($item:tt),+ $(,)?) => {
|
($($item:tt),+ $(,)?) => {
|
||||||
Star((
|
Star((
|
||||||
@@ -212,13 +171,6 @@ mod test {
|
|||||||
rule.parse(&mut p)
|
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]
|
#[test]
|
||||||
fn test_combinator_tuples() {
|
fn test_combinator_tuples() {
|
||||||
let foo = ("f", "o", "o");
|
let foo = ("f", "o", "o");
|
||||||
|
|||||||
Reference in New Issue
Block a user