primitives mod

This commit is contained in:
Greg Shuflin 2022-10-16 19:07:58 -07:00
parent 364ddeac00
commit 5e94769ed2
3 changed files with 77 additions and 42 deletions

View File

@ -2,17 +2,11 @@
#![allow(dead_code)] //TODO eventually turn this off
mod bnf;
mod parser;
mod primitives;
mod sequence;
use parser::{ParseResult, Parser};
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<P, F, I, O1, O2, E>(parser: P, map_fn: F) -> impl Parser<I, O2, E>
where
P: Parser<I, O1, E>,
@ -75,28 +69,6 @@ where
})
}
/// 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()..])),
@ -119,6 +91,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::primitives::literal;
use std::assert_matches::assert_matches;
use std::collections::HashMap;
@ -128,12 +101,6 @@ mod tests {
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");
@ -150,12 +117,6 @@ mod tests {
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")));
}
#[test]
fn test_choice() {
let p = choice(literal("gnostika").to(1), one_or_more(literal(" ")).to(2));

73
src/primitives.rs Normal file
View File

@ -0,0 +1,73 @@
use crate::parser::{ParseResult, Parser};
pub 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),
}
}
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 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)
}
})
}
}
/// Parses a standard identifier in a programming language
pub 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..]))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_identifier() {
assert_eq!(
identifier("bongo1beans").unwrap(),
(("bongo1beans".to_string(), ""))
);
assert_eq!(identifier("2bongo1beans"), Err("2bongo1beans"));
}
#[test]
fn test_pred() {
let p = pred(any_char, |c| *c == 'f');
assert_eq!(p.parse("frog"), Ok(('f', "rog")));
}
}

View File

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