Compare commits

..

No commits in common. "dfb151e2a3fad5e49fc14c8f22dd28bc0707a40a" and "116e2fc361a0c3fdd8c11114f60e677f11a6fd0b" have entirely different histories.

8 changed files with 25 additions and 122 deletions

View File

@ -1,4 +1,4 @@
use crate::parser::{ParseResult, Parser, ParserInput, Representation};
use crate::parser::{ParseResult, Parser, ParserInput};
pub fn choice2<P1, P2, I, O, E>(parser1: P1, parser2: P2) -> impl Parser<I, O, E>
where
@ -14,13 +14,11 @@ where
C: Choice<I, O, E>,
I: ParserInput + Clone,
{
let rep = choices.representation();
(move |input| choices.parse(input), rep)
move |input| choices.parse(input)
}
pub trait Choice<I: Clone, O, E> {
fn parse(&self, input: I) -> ParseResult<I, O, E>;
fn representation(&self) -> Representation;
}
impl<I, O, E, P1, P2> Choice<I, O, E> for (P1, P2)
@ -33,11 +31,6 @@ where
let parsers = vec![&self.0 as &dyn Parser<I, O, E>, &self.1];
choice_loop(input, parsers)
}
fn representation(&self) -> Representation {
let parsers = vec![&self.0 as &dyn Parser<I, O, E>, &self.1];
repr_loop(parsers)
}
}
impl<I, O, E, P1, P2, P3> Choice<I, O, E> for (P1, P2, P3)
@ -51,11 +44,6 @@ where
let parsers = vec![&self.0 as &dyn Parser<I, O, E>, &self.1, &self.2];
choice_loop(input, parsers)
}
fn representation(&self) -> Representation {
let parsers = vec![&self.0 as &dyn Parser<I, O, E>, &self.1, &self.2];
repr_loop(parsers)
}
}
impl<I, O, E, P1, P2, P3, P4> Choice<I, O, E> for (P1, P2, P3, P4)
@ -70,11 +58,6 @@ where
let parsers = vec![&self.0 as &dyn Parser<I, O, E>, &self.1, &self.2, &self.3];
choice_loop(input, parsers)
}
fn representation(&self) -> Representation {
let parsers = vec![&self.0 as &dyn Parser<I, O, E>, &self.1, &self.2, &self.3];
repr_loop(parsers)
}
}
impl<I, O, E, P1, P2, P3, P4, P5> Choice<I, O, E> for (P1, P2, P3, P4, P5)
@ -96,17 +79,6 @@ where
];
choice_loop(input, parsers)
}
fn representation(&self) -> Representation {
let parsers = vec![
&self.0 as &dyn Parser<I, O, E>,
&self.1,
&self.2,
&self.3,
&self.4,
];
repr_loop(parsers)
}
}
impl<I, O, E, P1, P2, P3, P4, P5, P6> Choice<I, O, E> for (P1, P2, P3, P4, P5, P6)
@ -130,17 +102,6 @@ where
];
choice_loop(input, parsers)
}
fn representation(&self) -> Representation {
let parsers = vec![
&self.0 as &dyn Parser<I, O, E>,
&self.1,
&self.2,
&self.3,
&self.4,
&self.5,
];
repr_loop(parsers)
}
}
fn choice_loop<I, O, E>(input: I, parsers: Vec<&dyn Parser<I, O, E>>) -> ParseResult<I, O, E>
@ -161,14 +122,6 @@ where
Err(err.unwrap())
}
fn repr_loop<I, O, E>(parsers: Vec<&dyn Parser<I, O, E>>) -> Representation
where
I: ParserInput + Clone,
{
let mut iter = parsers.iter().map(|p| p.representation());
Representation::from_choice(&mut iter)
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -6,11 +6,9 @@ where
P: Parser<I, O1, E>,
F: Fn(O1) -> O2,
{
let rep = parser.representation();
let p = move |input| {
move |input| {
parser
.parse(input)
.map(|(result, rest)| (map_fn(result), rest))
};
(p, rep)
}
}

View File

@ -4,4 +4,4 @@ mod parser;
pub mod primitives;
pub mod sequence;
pub use parser::{ParseResult, Parser, ParserInput, Representation};
pub use parser::{ParseResult, Parser, ParserInput};

View File

@ -23,7 +23,7 @@ where
impl<'a, I: ParserInput, O, E> Parser<I, O, E> for BoxedParser<'a, I, O, E> {
fn representation(&self) -> Representation {
self.inner.representation()
Representation::new("NOT IMPL'D")
}
fn parse(&self, input: I) -> ParseResult<I, O, E> {
self.inner.parse(input)

View File

@ -1,34 +1,12 @@
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone)]
pub struct Representation {
val: String,
val: String
}
impl Representation {
pub fn new(from: &str) -> Self {
Self {
val: from.to_string(),
}
}
pub fn from_choice(choice_parser_reps: &mut impl Iterator<Item = Representation>) -> Self {
let mut buf = String::new();
let mut iter = choice_parser_reps.peekable();
loop {
let rep = match iter.next() {
Some(r) => r,
None => break,
};
buf.push_str(&rep.val);
match iter.peek() {
Some(_) => {
buf.push_str(" | ");
}
None => {
break;
}
}
}
Representation::new(&buf)
Self { val: from.to_string() }
}
}

View File

@ -9,12 +9,11 @@ pub fn literal_char(expected: char) -> impl Fn(&str) -> ParseResult<&str, char,
pub fn literal<'a>(expected: &'static str) -> impl Parser<&'a str, &'a str, &'a str> {
println!("literal call expected: {}", expected);
let rep = Representation::new(expected);
let p = move |input: &'a str| match input.get(0..expected.len()) {
Some(next) if next == expected => Ok((expected, &input[expected.len()..])),
_ => Err(input),
};
(p, rep)
(p, Representation::new("yolo"))
}
pub fn any_char(input: &str) -> ParseResult<&str, char, &str> {
@ -51,8 +50,7 @@ where
F: Fn(&O) -> bool,
{
let orig_rep = parser.representation();
(
move |input| {
(move |input| {
parser.parse(input).and_then(|(result, rest)| {
if pred_fn(&result) {
Ok((result, rest))
@ -60,9 +58,7 @@ where
Err(rest)
}
})
},
Representation::new(&format!("{:?} if <PREDICATE>", orig_rep)),
)
}, Representation::new(&format!("{:?} if <PREDICATE>", orig_rep)))
}
/// Parses a standard identifier in a programming language

View File

@ -1,4 +1,4 @@
use crate::parser::{ParseResult, Parser, ParserInput, Representation};
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>
where
@ -14,9 +14,7 @@ where
I: ParserInput,
T: Sequence<I, O, E>,
{
let rep = sequence.representation();
let p = move |input| sequence.parse(input);
(p, rep)
move |input| sequence.parse(input)
}
/* TODO - eventually rewrite this parser combinator in Schala. Seeing what this
@ -26,9 +24,6 @@ where
pub trait Sequence<I, O, E> {
fn parse(&self, input: I) -> ParseResult<I, O, E>;
fn representation(&self) -> Representation {
Representation::new("SEQ NOT DONE YET")
}
}
impl<I, O1, O2, E, P1, P2> Sequence<I, (O1, O2), E> for (P1, P2)

View File

@ -3,7 +3,6 @@ use parser_combinator::combinators::repeated;
use parser_combinator::primitives::{any_char, literal, literal_char, one_of, pred};
use parser_combinator::sequence::seq;
use parser_combinator::Parser;
use parser_combinator::Representation;
use proptest::prelude::*;
@ -54,7 +53,6 @@ impl<'a, T, P> JsonParser<'a, T> for P where P: Parser<&'a str, T, &'a str> {}
fn json_null<'a>() -> impl JsonParser<'a, JsonValue> {
literal("null").to(JsonValue::Null)
}
fn json_bool<'a>() -> impl JsonParser<'a, JsonValue> {
choice((
literal("true").to(JsonValue::Bool(true)),
@ -63,13 +61,8 @@ fn json_bool<'a>() -> impl JsonParser<'a, JsonValue> {
}
fn json_number<'a>() -> impl JsonParser<'a, JsonValue> {
fn digit<'a>() -> impl JsonParser<'a, &'a str> {
one_of("1234567890")
}
fn digits<'a>() -> impl JsonParser<'a, Vec<&'a str>> {
repeated(digit()).at_least(1)
}
let digit = || one_of("1234567890");
let digits = || repeated(digit()).at_least(1);
let json_number_inner = choice((
seq((digits(), literal(".").ignore_then(digits()).optional())).map(
@ -236,13 +229,3 @@ fn parse_json_document() {
let parsed_json = json_object().parse(test_json);
assert!(parsed_json.is_ok());
}
#[test]
fn test_representations() {
assert_eq!(json_null().representation(), Representation::new("null"));
assert_eq!(
json_bool().representation(),
Representation::new("true | false")
);
assert_eq!(json_number().representation(), Representation::new("null"));
}