This commit is contained in:
Greg Shuflin 2024-01-25 15:38:29 -08:00
parent 194e23a5be
commit 8f00b77c2c

View File

@ -1,35 +1,77 @@
#![feature(assert_matches)]
#![allow(dead_code)] //TODO eventually turn this off
type ParseResult<I, O, E> = Result<(O, I), E>;
trait Parser<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> {
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()) {
Some(next) if next == expected =>
Ok(((), &input[expected.len()..])),
_ => Err(input)
Some(next) if next == expected => Ok((next, &input[expected.len()..])),
_ => 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)]
mod tests {
use super::*;
use std::assert_matches::assert_matches;
#[test]
fn parsing() {
let output = literal("a")("a yolo");
assert_matches!(output.unwrap(), ((), " yolo"));
let (parsed, rest) = literal("a")("a yolo").unwrap();
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, "");
}
}