combinators module

This commit is contained in:
Greg Shuflin 2022-10-16 19:16:21 -07:00
parent 5e94769ed2
commit e584668efc
4 changed files with 79 additions and 87 deletions

75
src/combinators.rs Normal file
View File

@ -0,0 +1,75 @@
use crate::parser::Parser;
pub 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,
{
move |input| {
parser
.parse(input)
.map(|(result, rest)| (map_fn(result), rest))
}
}
pub fn zero_or_more<P, I, O>(parser: P) -> impl Parser<I, Vec<O>, I>
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))
}
}
pub fn one_or_more<P, I, O>(parser: P) -> impl Parser<I, Vec<O>, I>
where
P: Parser<I, O, I> + 'static,
I: Copy + 'static,
O: 'static,
{
let parser = std::rc::Rc::new(parser);
parser
.clone()
.then(zero_or_more(parser))
.map(|(first, rest)| {
let mut output = vec![first];
output.extend(rest.into_iter());
output
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::combinators::one_or_more;
use crate::primitives::literal;
#[test]
fn test_map() {
let lit_a = literal("a");
let output = lit_a.map(|s| s.to_uppercase()).parse("a yolo");
assert_eq!(output.unwrap(), ("A".to_string(), " yolo"));
}
#[test]
fn test_one_or_more() {
let p = one_or_more(literal("bongo "));
let input = "bongo bongo bongo bongo bongo ";
let (output, rest) = p.parse(input).unwrap();
assert_eq!(rest, "");
assert_eq!(output.len(), 5);
let (output, rest) = p.parse("bongo ecks").unwrap();
assert_eq!(output.len(), 1);
assert_eq!(rest, "ecks");
}
}

View File

@ -1,81 +1,13 @@
#![feature(assert_matches)] #![feature(assert_matches)]
#![allow(dead_code)] //TODO eventually turn this off #![allow(dead_code)] //TODO eventually turn this off
mod bnf; mod bnf;
mod combinators;
mod parser; mod parser;
mod primitives; mod primitives;
mod sequence; mod sequence;
use parser::{ParseResult, Parser}; use parser::{ParseResult, Parser};
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,
{
move |input| {
parser
.parse(input)
.map(|(result, rest)| (map_fn(result), rest))
}
}
fn pred<P, F, I, O>(parser: P, pred_fn: F) -> impl Parser<I, O, I>
where
P: Parser<I, O, I>,
F: Fn(&O) -> bool,
{
move |input| {
parser.parse(input).and_then(|(result, rest)| {
if pred_fn(&result) {
Ok((result, rest))
} else {
Err(rest)
}
})
}
}
fn zero_or_more<P, I, O>(parser: P) -> impl Parser<I, Vec<O>, I>
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))
}
}
fn one_or_more<P, I, O>(parser: P) -> impl Parser<I, Vec<O>, I>
where
P: Parser<I, O, I> + 'static,
I: Copy + 'static,
O: 'static,
{
let parser = std::rc::Rc::new(parser);
parser
.clone()
.then(zero_or_more(parser))
.map(|(first, rest)| {
let mut output = vec![first];
output.extend(rest.into_iter());
output
})
}
fn any_char(input: &str) -> ParseResult<&str, char, &str> {
match input.chars().next() {
Some(ch) => Ok((ch, &input[ch.len_utf8()..])),
None => Err(input),
}
}
fn choice<P1, P2, I, O, E>(parser1: P1, parser2: P2) -> impl Parser<I, O, E> fn choice<P1, P2, I, O, E>(parser1: P1, parser2: P2) -> impl Parser<I, O, E>
where where
P1: Parser<I, O, E>, P1: Parser<I, O, E>,
@ -91,6 +23,7 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::combinators::one_or_more;
use crate::primitives::literal; use crate::primitives::literal;
use std::assert_matches::assert_matches; use std::assert_matches::assert_matches;
use std::collections::HashMap; use std::collections::HashMap;
@ -101,22 +34,6 @@ mod tests {
assert_matches!(output.unwrap(), ("a", " yolo")); assert_matches!(output.unwrap(), ("a", " yolo"));
} }
#[test]
fn test_map() {
let lit_a = literal("a");
let output = lit_a.map(|s| s.to_uppercase()).parse("a yolo");
assert_matches!(output.unwrap(), (s, " yolo") if s == "A");
}
#[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);
let input = "bongo ecks";
assert_matches!(p.parse(input), Ok((v, "ecks")) if v.len() == 1);
}
#[test] #[test]
fn test_choice() { fn test_choice() {
let p = choice(literal("gnostika").to(1), one_or_more(literal(" ")).to(2)); let p = choice(literal("gnostika").to(1), one_or_more(literal(" ")).to(2));

View File

@ -18,7 +18,7 @@ pub trait Parser<I, O, E> {
O2: 'a, O2: 'a,
F: Fn(O) -> O2 + 'a, F: Fn(O) -> O2 + 'a,
{ {
BoxedParser::new(crate::map(self, map_fn)) BoxedParser::new(crate::combinators::map(self, map_fn))
} }
fn to<'a, O2>(self, item: O2) -> BoxedParser<'a, I, O2, E> fn to<'a, O2>(self, item: O2) -> BoxedParser<'a, I, O2, E>

View File

@ -110,8 +110,8 @@ where
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use crate::combinators::zero_or_more;
use crate::primitives::{identifier, literal}; use crate::primitives::{identifier, literal};
use crate::zero_or_more;
use std::assert_matches::assert_matches; use std::assert_matches::assert_matches;
#[test] #[test]