rust-parser-combinator/src/annotated.rs

56 lines
1.1 KiB
Rust

use std::marker::PhantomData;
use crate::{representation::Representation, ParseResult, Parser};
pub struct AnnotatedParser<P, I, O, E>
where
P: Parser<I, O, E>,
{
inner: P,
name: Option<String>,
repr: Representation,
phantom: PhantomData<(I, O, E)>,
}
impl<P, I, O, E> Parser<I, O, E> for AnnotatedParser<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> {
self.name.clone()
}
fn representation(&self) -> Representation {
self.repr.clone()
}
}
impl<P, I, O, E> AnnotatedParser<P, I, O, E>
where
P: Parser<I, O, E>,
{
pub fn new(inner: P) -> Self {
Self {
inner,
name: None,
repr: Representation::new(),
phantom: PhantomData,
}
}
pub fn with_name(self, name: &str) -> Self {
Self {
name: Some(name.to_string()),
..self
}
}
pub fn with_repr(self, repr: Representation) -> Self {
Self { repr, ..self }
}
}