#![feature(assert_matches)] #![allow(dead_code)] //TODO eventually turn this off use std::rc::Rc; type ParseResult = Result<(O, I), E>; trait Parser { fn parse(&self, input: I) -> ParseResult; } impl Parser for F where F: Fn(I) -> ParseResult, { fn parse(&self, input: I) -> ParseResult { self(input) } } impl Parser for Rc where T: Parser, { fn parse(&self, input: I) -> ParseResult { self.as_ref().parse(input) } } 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((expected, &input[expected.len()..])), _ => Err(input), } } fn map(parser: P, map_fn: F) -> impl Parser where P: Parser, F: Fn(O1) -> O2, { move |input| { parser .parse(input) .map(|(result, rest)| (map_fn(result), rest)) } } fn seq(parser1: P1, parser2: P2) -> impl Parser where P1: Parser, P2: Parser, { move |input| { parser1.parse(input).and_then(|(result1, rest1)| { parser2 .parse(rest1) .map(|(result2, rest2)| ((result1, result2), rest2)) }) } } fn pred(parser: P, pred_fn: F) -> impl Parser where P: Parser, 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(parser: P) -> impl Parser, I> where P: Parser, 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(parser: P) -> impl Parser, I> where P: Parser, 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 }, ) } /// 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..])) } fn any_char(input: &str) -> ParseResult<&str, char, &str> { match input.chars().next() { Some(ch) => Ok((ch, &input[ch.len_utf8()..])), None => Err(input), } } #[cfg(test)] mod tests { use super::*; use std::assert_matches::assert_matches; #[test] fn test_parsing() { let output = literal("a")("a yolo"); assert_matches!(output.unwrap(), ("a", " yolo")); } #[test] fn test_identifier() { assert_matches!(identifier("bongo1beans"), Ok((s, "")) if s == "bongo1beans"); assert_matches!(identifier("2bongo1beans"), Err("2bongo1beans")); } #[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"); } #[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") ); } #[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] fn test_pred() { let p = pred(any_char, |c| *c == 'f'); assert_eq!(p.parse("frog"), Ok(('f', "rog"))); } }