Remove current iteration of bnf code

This commit is contained in:
Greg Shuflin 2022-10-23 00:25:20 -07:00
parent 502bfa9587
commit 4f807b991b
7 changed files with 3 additions and 114 deletions

View File

@ -1,6 +0,0 @@
#[derive(Debug, Clone, PartialEq)]
pub enum Bnf {
Production,
Choice(Vec<Bnf>),
Unknown,
}

View File

@ -1,4 +1,3 @@
use crate::bnf::Bnf;
use crate::parser::{ParseResult, Parser, ParserInput}; use crate::parser::{ParseResult, Parser, ParserInput};
pub fn choice2<P1, P2, I, O, E>(parser1: P1, parser2: P2) -> impl Parser<I, O, E> pub fn choice2<P1, P2, I, O, E>(parser1: P1, parser2: P2) -> impl Parser<I, O, E>
@ -10,19 +9,16 @@ where
choice((parser1, parser2)) choice((parser1, parser2))
} }
pub fn choice<C, I, O, E>(choices: C) -> (impl Parser<I, O, E>, Bnf) pub fn choice<C, I, O, E>(choices: C) -> impl Parser<I, O, E>
where where
C: Choice<I, O, E>, C: Choice<I, O, E>,
I: ParserInput + Clone, I: ParserInput + Clone,
{ {
let bnf = choices.bnf(); move |input| choices.parse(input)
(move |input| choices.parse(input), bnf)
} }
pub trait Choice<I: Clone, O, E> { pub trait Choice<I: Clone, O, E> {
fn parse(&self, input: I) -> ParseResult<I, O, E>; fn parse(&self, input: I) -> ParseResult<I, O, E>;
fn bnf(&self) -> Bnf;
} }
impl<I, O, E, P1, P2> Choice<I, O, E> for (P1, P2) impl<I, O, E, P1, P2> Choice<I, O, E> for (P1, P2)
@ -35,16 +31,6 @@ where
let parsers = vec![&self.0 as &dyn Parser<I, O, E>, &self.1]; let parsers = vec![&self.0 as &dyn Parser<I, O, E>, &self.1];
choice_loop(input, parsers) choice_loop(input, parsers)
} }
fn bnf(&self) -> Bnf {
let parsers = vec![&self.0 as &dyn Parser<I, O, E>, &self.1];
Bnf::Choice(
parsers
.into_iter()
.map(|p| p.bnf().unwrap_or(Bnf::Unknown))
.collect(),
)
}
} }
impl<I, O, E, P1, P2, P3> Choice<I, O, E> for (P1, P2, P3) impl<I, O, E, P1, P2, P3> Choice<I, O, E> for (P1, P2, P3)
@ -58,16 +44,6 @@ where
let parsers = vec![&self.0 as &dyn Parser<I, O, E>, &self.1, &self.2]; let parsers = vec![&self.0 as &dyn Parser<I, O, E>, &self.1, &self.2];
choice_loop(input, parsers) choice_loop(input, parsers)
} }
fn bnf(&self) -> Bnf {
let parsers = vec![&self.0 as &dyn Parser<I, O, E>, &self.1, &self.2];
Bnf::Choice(
parsers
.into_iter()
.map(|p| p.bnf().unwrap_or(Bnf::Unknown))
.collect(),
)
}
} }
impl<I, O, E, P1, P2, P3, P4> Choice<I, O, E> for (P1, P2, P3, P4) impl<I, O, E, P1, P2, P3, P4> Choice<I, O, E> for (P1, P2, P3, P4)
@ -82,16 +58,6 @@ where
let parsers = vec![&self.0 as &dyn Parser<I, O, E>, &self.1, &self.2, &self.3]; let parsers = vec![&self.0 as &dyn Parser<I, O, E>, &self.1, &self.2, &self.3];
choice_loop(input, parsers) choice_loop(input, parsers)
} }
fn bnf(&self) -> Bnf {
let parsers = vec![&self.0 as &dyn Parser<I, O, E>, &self.1, &self.2, &self.3];
Bnf::Choice(
parsers
.into_iter()
.map(|p| p.bnf().unwrap_or(Bnf::Unknown))
.collect(),
)
}
} }
impl<I, O, E, P1, P2, P3, P4, P5> Choice<I, O, E> for (P1, P2, P3, P4, P5) impl<I, O, E, P1, P2, P3, P4, P5> Choice<I, O, E> for (P1, P2, P3, P4, P5)
@ -113,22 +79,6 @@ where
]; ];
choice_loop(input, parsers) choice_loop(input, parsers)
} }
fn bnf(&self) -> Bnf {
let parsers = vec![
&self.0 as &dyn Parser<I, O, E>,
&self.1,
&self.2,
&self.3,
&self.4,
];
Bnf::Choice(
parsers
.into_iter()
.map(|p| p.bnf().unwrap_or(Bnf::Unknown))
.collect(),
)
}
} }
impl<I, O, E, P1, P2, P3, P4, P5, P6> Choice<I, O, E> for (P1, P2, P3, P4, P5, P6) impl<I, O, E, P1, P2, P3, P4, P5, P6> Choice<I, O, E> for (P1, P2, P3, P4, P5, P6)
@ -152,23 +102,6 @@ where
]; ];
choice_loop(input, parsers) choice_loop(input, parsers)
} }
fn bnf(&self) -> Bnf {
let parsers = vec![
&self.0 as &dyn Parser<I, O, E>,
&self.1,
&self.2,
&self.3,
&self.4,
&self.5,
];
Bnf::Choice(
parsers
.into_iter()
.map(|p| p.bnf().unwrap_or(Bnf::Unknown))
.collect(),
)
}
} }
fn choice_loop<I, O, E>(input: I, parsers: Vec<&dyn Parser<I, O, E>>) -> ParseResult<I, O, E> fn choice_loop<I, O, E>(input: I, parsers: Vec<&dyn Parser<I, O, E>>) -> ParseResult<I, O, E>

View File

@ -1,4 +1,3 @@
mod bnf;
pub mod choice; pub mod choice;
pub mod combinators; pub mod combinators;
mod parser; mod parser;
@ -6,7 +5,6 @@ pub mod primitives;
pub mod sequence; pub mod sequence;
#[cfg(test)] #[cfg(test)]
use bnf::Bnf;
pub use parser::{ParseResult, Parser, ParserInput}; pub use parser::{ParseResult, Parser, ParserInput};
#[cfg(test)] #[cfg(test)]
@ -223,13 +221,4 @@ mod tests {
let parsed_json = json_object().parse(test_json); let parsed_json = json_object().parse(test_json);
assert!(parsed_json.is_ok()); assert!(parsed_json.is_ok());
} }
#[test]
fn bnf_representation() {
let bnf = json_value().bnf().unwrap();
assert_eq!(bnf, Bnf::Production);
let bnf = json_object().bnf().unwrap();
assert_eq!(bnf, Bnf::Production);
}
} }

View File

@ -1,4 +1,3 @@
use crate::bnf::Bnf;
use crate::parser::{ParseResult, Parser, ParserInput}; use crate::parser::{ParseResult, Parser, ParserInput};
pub struct BoxedParser<'a, I, O, E> pub struct BoxedParser<'a, I, O, E>
@ -27,10 +26,6 @@ impl<'a, I: ParserInput, O, E> Parser<I, O, E> for BoxedParser<'a, I, O, E> {
self.inner.parse(input) self.inner.parse(input)
} }
fn bnf(&self) -> Option<Bnf> {
self.inner.bnf()
}
fn boxed<'b>(self) -> BoxedParser<'b, I, O, E> fn boxed<'b>(self) -> BoxedParser<'b, I, O, E>
where where
Self: Sized + 'b, Self: Sized + 'b,

View File

@ -1,7 +1,6 @@
mod boxed_parser; mod boxed_parser;
mod named_parser; mod named_parser;
use crate::bnf::Bnf;
use std::rc::Rc; use std::rc::Rc;
pub use boxed_parser::BoxedParser; pub use boxed_parser::BoxedParser;
@ -19,9 +18,6 @@ where
I: ParserInput, I: ParserInput,
{ {
fn parse(&self, input: I) -> ParseResult<I, O, E>; fn parse(&self, input: I) -> ParseResult<I, O, E>;
fn bnf(&self) -> Option<Bnf> {
None
}
fn boxed<'a>(self) -> BoxedParser<'a, I, O, E> fn boxed<'a>(self) -> BoxedParser<'a, I, O, E>
where where
@ -150,19 +146,6 @@ where
} }
} }
impl<I: ParserInput, O, E, P> Parser<I, O, E> for (P, Bnf)
where
P: Parser<I, O, E>,
{
fn parse(&self, input: I) -> ParseResult<I, O, E> {
self.0.parse(input)
}
fn bnf(&self) -> Option<Bnf> {
Some(self.1.clone())
}
}
impl<I, O, E, T> Parser<I, O, E> for Rc<T> impl<I, O, E, T> Parser<I, O, E> for Rc<T>
where where
I: ParserInput, I: ParserInput,
@ -171,8 +154,4 @@ where
fn parse(&self, input: I) -> ParseResult<I, O, E> { fn parse(&self, input: I) -> ParseResult<I, O, E> {
self.as_ref().parse(input) self.as_ref().parse(input)
} }
fn bnf(&self) -> Option<Bnf> {
self.as_ref().bnf()
}
} }

View File

@ -1,5 +1,4 @@
use crate::parser::{ParseResult, Parser, ParserInput}; use crate::parser::{ParseResult, Parser, ParserInput};
//use crate::bnf::Bnf;
use super::boxed_parser::BoxedParser; use super::boxed_parser::BoxedParser;
pub struct NamedParser<'a, I, O, E> pub struct NamedParser<'a, I, O, E>

View File

@ -1,4 +1,4 @@
use crate::{ParseResult, Parser, ParserInput}; use crate::parser::{ParseResult, Parser, ParserInput};
pub fn tuple2<P1, P2, I, O1, O2, E>(parser1: P1, parser2: P2) -> impl Parser<I, (O1, O2), E> pub fn tuple2<P1, P2, I, O1, O2, E>(parser1: P1, parser2: P2) -> impl Parser<I, (O1, O2), E>
where where