named parser

This commit is contained in:
Greg Shuflin 2024-01-29 20:14:53 -08:00
parent 33899ae66e
commit 2909cdf296
2 changed files with 43 additions and 0 deletions

View File

@ -1,9 +1,14 @@
use std::marker::PhantomData;
use crate::{map, seq2, surrounded_by}; use crate::{map, seq2, surrounded_by};
pub type ParseResult<I, O, E> = Result<(O, I), (E, I)>; pub type ParseResult<I, O, E> = Result<(O, I), (E, I)>;
pub trait Parser<I, O, E> { pub trait Parser<I, O, E> {
fn parse(&self, input: I) -> ParseResult<I, O, E>; fn parse(&self, input: I) -> ParseResult<I, O, E>;
fn name(&self) -> Option<String> {
None
}
} }
impl<I, O, E, F> Parser<I, O, E> for F impl<I, O, E, F> Parser<I, O, E> for F
@ -15,6 +20,28 @@ where
} }
} }
pub struct NamedParser<P, I, O, E>
where
P: Parser<I, O, E>,
{
inner: P,
name: String,
phantom: PhantomData<(I, O, E)>,
}
impl<P, I, O, E> Parser<I, O, E> for NamedParser<P, I, O, E>
where
P: Parser<I, O, E>,
{
fn parse(&self, input: I) -> ParseResult<I, O, E> {
self.inner.parse(input)
}
fn name(&self) -> Option<String> {
Some(self.name.clone())
}
}
pub trait ParserExtension<I, O, E>: Parser<I, O, E> { pub trait ParserExtension<I, O, E>: Parser<I, O, E> {
fn map<F, O2>(self, map_fn: F) -> impl Parser<I, O2, E> fn map<F, O2>(self, map_fn: F) -> impl Parser<I, O2, E>
where where
@ -25,6 +52,16 @@ 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 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 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 surrounded_by<O2>(self, surrounding: impl Parser<I, O2, E>) -> impl Parser<I, O, E>;
fn to_named(self, name: &str) -> NamedParser<Self, I, O, E>
where
Self: Sized,
{
NamedParser {
inner: self,
name: name.to_string(),
phantom: PhantomData,
}
}
} }
impl<T, I, O, E> ParserExtension<I, O, E> for T impl<T, I, O, E> ParserExtension<I, O, E> for T

View File

@ -73,3 +73,9 @@ fn test_repeated() {
assert_eq!(output.0.len(), 0); assert_eq!(output.0.len(), 0);
assert_eq!(output.1, "tra la la"); assert_eq!(output.1, "tra la la");
} }
#[test]
fn test_named_parser() {
let parser = literal("yokel").to_named("yokelparser");
assert_eq!(parser.name(), Some("yokelparser".to_string()));
}