rust-parser-combinator/src/lib.rs

156 lines
3.8 KiB
Rust
Raw Normal View History

2022-10-15 23:36:04 -07:00
#![feature(assert_matches)]
#![allow(dead_code)] //TODO eventually turn this off
2022-10-16 00:30:06 -07:00
use std::rc::Rc;
2022-10-15 23:36:04 -07:00
type ParseResult<I, O, E> = Result<(O, I), E>;
trait Parser<I, O, E> {
fn parse(&self, input: I) -> ParseResult<I, O, E>;
}
2022-10-15 23:41:22 -07:00
impl<I, O, E, F> Parser<I, O, E> for F
where
F: Fn(I) -> ParseResult<I, O, E>,
{
2022-10-15 23:36:04 -07:00
fn parse(&self, input: I) -> ParseResult<I, O, E> {
self(input)
}
}
2022-10-16 00:30:06 -07:00
impl<I, O, E, T> Parser<I, O, E> for Rc<T>
where
T: Parser<I, O, E>,
{
fn parse(&self, input: I) -> ParseResult<I, O, E> {
self.as_ref().parse(input)
}
}
2022-10-15 23:41:22 -07:00
fn literal(expected: &'static str) -> impl Fn(&str) -> ParseResult<&str, &str, &str> {
2022-10-15 23:36:04 -07:00
move |input| match input.get(0..expected.len()) {
2022-10-15 23:41:22 -07:00
Some(next) if next == expected => Ok((expected, &input[expected.len()..])),
_ => Err(input),
2022-10-15 23:36:04 -07:00
}
2022-10-10 00:13:39 -07:00
}
2022-10-15 23:41:22 -07:00
fn map<P, F, I, O1, O2, E>(parser: P, map_fn: F) -> impl Parser<I, O2, E>
where
P: Parser<I, O1, E>,
F: Fn(O1) -> O2,
{
2022-10-15 23:58:05 -07:00
move |input| {
parser
.parse(input)
.map(|(result, rest)| (map_fn(result), rest))
}
}
fn seq<P1, P2, I, O1, O2, E>(parser1: P1, parser2: P2) -> impl Parser<I, (O1, O2), E>
where
P1: Parser<I, O1, E>,
P2: Parser<I, O2, E>,
{
move |input| {
parser1.parse(input).and_then(|(result1, rest1)| {
parser2
.parse(rest1)
.map(|(result2, rest2)| ((result1, result2), rest2))
})
}
}
2022-10-16 00:30:06 -07:00
fn zero_or_more<P, I, O>(parser: P) -> impl Parser<I, Vec<O>, I>
2022-10-16 00:23:01 -07:00
where
P: Parser<I, O, I>,
I: Copy,
{
move |mut input| {
let mut results = Vec::new();
while let Ok((item, rest)) = parser.parse(input) {
results.push(item);
input = rest;
}
Ok((results, input))
}
}
2022-10-16 00:30:06 -07:00
fn one_or_more<P, I, O>(parser: P) -> impl Parser<I, Vec<O>, I>
where
P: Parser<I, O, I>,
I: Copy,
{
let parser = std::rc::Rc::new(parser);
map(
seq(parser.clone(), zero_or_more(parser)),
|(first, rest)| {
let mut output = vec![first];
output.extend(rest.into_iter());
output
},
)
}
2022-10-15 23:58:05 -07:00
/// Parses a standard identifier in a programming language
fn identifier(input: &str) -> ParseResult<&str, String, &str> {
let mut chars = input.chars();
let mut buf = String::new();
match chars.next() {
Some(ch) if ch.is_alphabetic() => buf.push(ch),
_ => return Err(input),
}
while let Some(next) = chars.next() {
if next.is_alphanumeric() {
buf.push(next);
} else {
break;
}
}
let next_index = buf.len();
Ok((buf, &input[next_index..]))
2022-10-15 23:41:22 -07:00
}
2022-10-10 00:13:39 -07:00
#[cfg(test)]
mod tests {
use super::*;
2022-10-15 23:36:04 -07:00
use std::assert_matches::assert_matches;
2022-10-10 00:13:39 -07:00
#[test]
2022-10-15 23:41:22 -07:00
fn test_parsing() {
2022-10-15 23:36:04 -07:00
let output = literal("a")("a yolo");
2022-10-15 23:41:22 -07:00
assert_matches!(output.unwrap(), ("a", " yolo"));
}
2022-10-15 23:58:05 -07:00
#[test]
fn test_identifier() {
assert_matches!(identifier("bongo1beans"), Ok((s, "")) if s == "bongo1beans");
assert_matches!(identifier("2bongo1beans"), Err("2bongo1beans"));
}
2022-10-15 23:41:22 -07:00
#[test]
fn test_map() {
let lit_a = literal("a");
let output = map(lit_a, |s| s.to_uppercase()).parse("a yolo");
assert_matches!(output.unwrap(), (s, " yolo") if s == "A");
2022-10-10 00:13:39 -07:00
}
2022-10-15 23:58:05 -07:00
#[test]
fn test_seq() {
let p = seq(identifier, seq(literal(" "), literal("ruts")));
assert_matches!(p.parse("fort1 ruts"), Ok((r, "")) if r.0 == "fort1" && r.1 == (" ", "ruts") );
}
2022-10-16 00:23:01 -07:00
#[test]
fn test_one_or_more() {
let p = one_or_more(literal("bongo "));
let input = "bongo bongo bongo bongo bongo ";
assert_matches!(p.parse(input), Ok((v, "")) if v.len() == 5);
2022-10-16 00:30:06 -07:00
let input = "bongo ecks";
assert_matches!(p.parse(input), Ok((v, "ecks")) if v.len() == 1);
2022-10-16 00:23:01 -07:00
}
2022-10-10 00:13:39 -07:00
}