use crate::parser::{ParseResult, Parser, ParserInput, Representation}; pub fn literal_char(expected: char) -> impl Fn(&str) -> ParseResult<&str, char, &str> { move |input| match input.chars().next() { Some(ch) if ch == expected => Ok((expected, &input[ch.len_utf8()..])), _ => Err(input), } } pub fn literal<'a>(expected: &'static str) -> impl Parser<&'a str, &'a str, &'a str> { println!("literal call expected: {}", expected); let rep = Representation::new(expected); let p = move |input: &'a str| match input.get(0..expected.len()) { Some(next) if next == expected => Ok((expected, &input[expected.len()..])), _ => Err(input), }; (p, rep) } pub fn any_char(input: &str) -> ParseResult<&str, char, &str> { match input.chars().next() { Some(ch) => Ok((ch, &input[ch.len_utf8()..])), None => Err(input), } } pub fn one_of<'a>(items: &'static str) -> impl Parser<&'a str, &'a str, &'a str> { let p = move |input: &'a str| { if let Some(ch) = input.chars().next() { if items.contains(ch) { let (first, rest) = input.split_at(1); return Ok((first, rest)); } } Err(input) }; let mut s = String::new(); for ch in items.chars() { s.push(ch); s.push_str(" | "); } let rep = Representation::new(&s); (p, rep) } pub fn pred
(parser: P, pred_fn: F) -> impl Parser
where
I: ParserInput,
P: Parser,
F: Fn(&O) -> bool,
{
let orig_rep = parser.representation();
(
move |input| {
parser.parse(input).and_then(|(result, rest)| {
if pred_fn(&result) {
Ok((result, rest))
} else {
Err(rest)
}
})
},
Representation::new(&format!("{:?} if