Compare commits

...

2 Commits

Author SHA1 Message Date
Greg Shuflin
4e813a7efd more parser annotation 2024-01-30 00:15:00 -08:00
Greg Shuflin
b042e06084 Add Representation 2024-01-30 00:05:20 -08:00
6 changed files with 35 additions and 6 deletions

View File

@ -1,6 +1,6 @@
use std::marker::PhantomData;
use crate::{ParseResult, Parser};
use crate::{representation::Representation, ParseResult, Parser};
pub struct AnnotatedParser<P, I, O, E>
where
@ -8,6 +8,7 @@ where
{
inner: P,
name: Option<String>,
repr: Option<Representation>,
phantom: PhantomData<(I, O, E)>,
}
@ -32,6 +33,7 @@ where
Self {
inner,
name: None,
repr: None,
phantom: PhantomData,
}
}
@ -42,4 +44,11 @@ where
..self
}
}
pub fn with_repr(self, repr: Representation) -> Self {
Self {
repr: Some(repr),
..self
}
}
}

View File

@ -5,6 +5,7 @@ mod combinators;
mod map;
mod parser;
mod primitives;
mod representation;
mod sequence;
#[cfg(test)]

View File

@ -28,6 +28,12 @@ pub trait ParserExtension<I, O, E>: Parser<I, O, E> {
fn then_ignore<O2, P: Parser<I, O2, E>>(self, next: P) -> impl Parser<I, O, E>;
fn ignore_then<O2, P: Parser<I, O2, E>>(self, next: P) -> impl Parser<I, O2, E>;
fn surrounded_by<O2>(self, surrounding: impl Parser<I, O2, E>) -> impl Parser<I, O, E>;
fn to_anno(self) -> AnnotatedParser<Self, I, O, E>
where
Self: Sized,
{
AnnotatedParser::new(self)
}
fn to_named(self, name: &str) -> AnnotatedParser<Self, I, O, E>
where
Self: Sized,

View File

@ -1,10 +1,11 @@
use crate::{ParseResult, Parser};
use crate::{representation::Representation, ParseResult, Parser, ParserExtension};
pub fn literal(expected: &'static str) -> impl Fn(&str) -> ParseResult<&str, &str, ()> {
move |input| match input.get(0..expected.len()) {
pub fn literal<'a>(expected: &'static str) -> impl Parser<&'a str, &'a str, ()> {
let p = move |input: &'a str| match input.get(0..expected.len()) {
Some(next) if next == expected => Ok((next, &input[expected.len()..])),
_ => Err(((), input)),
}
};
p.to_anno().with_repr(Representation::new())
}
pub fn literal_char<'a>(expected: char) -> impl Parser<&'a str, char, ()> {

12
src/representation.rs Normal file
View File

@ -0,0 +1,12 @@
#[derive(Debug)]
pub struct Representation {}
impl Representation {
pub fn show(&self) {
println!("Not done");
}
pub fn new() -> Self {
Self {}
}
}

View File

@ -4,7 +4,7 @@ use super::*;
#[test]
fn parsing() {
let (parsed, rest) = literal("a")("a yolo").unwrap();
let (parsed, rest) = literal("a").parse("a yolo").unwrap();
assert_eq!(parsed, "a");
assert_eq!(rest, " yolo");
}