Rewrite
This commit is contained in:
parent
194e23a5be
commit
8f00b77c2c
62
src/lib.rs
62
src/lib.rs
@ -1,35 +1,77 @@
|
|||||||
#![feature(assert_matches)]
|
|
||||||
#![allow(dead_code)] //TODO eventually turn this off
|
#![allow(dead_code)] //TODO eventually turn this off
|
||||||
|
|
||||||
|
|
||||||
type ParseResult<I, O, E> = Result<(O, I), E>;
|
type ParseResult<I, O, E> = Result<(O, I), E>;
|
||||||
|
|
||||||
trait Parser<I, O, E> {
|
trait Parser<I, O, E> {
|
||||||
fn parse(&self, input: I) -> ParseResult<I, O, E>;
|
fn parse(&self, input: I) -> ParseResult<I, O, E>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I, O, E, F> Parser<I, O, E> for F where F: Fn(I) -> ParseResult<I, O, E> {
|
impl<I, O, E, F> Parser<I, O, E> for F
|
||||||
|
where
|
||||||
|
F: Fn(I) -> ParseResult<I, O, E>,
|
||||||
|
{
|
||||||
fn parse(&self, input: I) -> ParseResult<I, O, E> {
|
fn parse(&self, input: I) -> ParseResult<I, O, E> {
|
||||||
self(input)
|
self(input)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn literal(expected: &'static str) -> impl Fn(&str) -> ParseResult<&str, (), &str> {
|
fn literal(expected: &'static str) -> impl Fn(&str) -> ParseResult<&str, &str, &str> {
|
||||||
move |input| match input.get(0..expected.len()) {
|
move |input| match input.get(0..expected.len()) {
|
||||||
Some(next) if next == expected =>
|
Some(next) if next == expected => Ok((next, &input[expected.len()..])),
|
||||||
Ok(((), &input[expected.len()..])),
|
_ => Err(input),
|
||||||
_ => Err(input)
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sequence<I, O1, O2, E>(
|
||||||
|
first: impl Parser<I, O1, E>,
|
||||||
|
second: impl Parser<I, O2, E>,
|
||||||
|
) -> impl Parser<I, (O1, O2), E> {
|
||||||
|
move |input| -> ParseResult<I, (O1, O2), E> {
|
||||||
|
first.parse(input).and_then(|(result1, rest)| {
|
||||||
|
second
|
||||||
|
.parse(rest)
|
||||||
|
.map(|(result2, rest2)| ((result1, result2), rest2))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn choice<'a, I, O, E>(parsers: &'a [&'a dyn Parser<I, O, E>]) -> impl Parser<I, O, E> + 'a
|
||||||
|
where
|
||||||
|
I: Clone,
|
||||||
|
{
|
||||||
|
move |input: I| {
|
||||||
|
//TODO need a more principled way to return an error when no choices work
|
||||||
|
let mut err = None;
|
||||||
|
|
||||||
|
for parser in parsers.iter() {
|
||||||
|
match parser.parse(input.clone()) {
|
||||||
|
Ok(res) => return Ok(res),
|
||||||
|
Err(e) => {
|
||||||
|
err = Some(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err.unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use std::assert_matches::assert_matches;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parsing() {
|
fn parsing() {
|
||||||
let output = literal("a")("a yolo");
|
let (parsed, rest) = literal("a")("a yolo").unwrap();
|
||||||
assert_matches!(output.unwrap(), ((), " yolo"));
|
assert_eq!(parsed, "a");
|
||||||
|
assert_eq!(rest, " yolo");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sequence() {
|
||||||
|
let parser = sequence(literal("bongo"), sequence(literal(" "), literal("jonzzz")));
|
||||||
|
let output = parser.parse("bongo jonzzz").unwrap();
|
||||||
|
assert_eq!(output.0 .0, "bongo");
|
||||||
|
assert_eq!(output.0 .1, (" ", "jonzzz"));
|
||||||
|
assert_eq!(output.1, "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user