primitives mod
This commit is contained in:
parent
364ddeac00
commit
5e94769ed2
43
src/lib.rs
43
src/lib.rs
@ -2,17 +2,11 @@
|
|||||||
#![allow(dead_code)] //TODO eventually turn this off
|
#![allow(dead_code)] //TODO eventually turn this off
|
||||||
mod bnf;
|
mod bnf;
|
||||||
mod parser;
|
mod parser;
|
||||||
|
mod primitives;
|
||||||
mod sequence;
|
mod sequence;
|
||||||
|
|
||||||
use parser::{ParseResult, Parser};
|
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>
|
fn map<P, F, I, O1, O2, E>(parser: P, map_fn: F) -> impl Parser<I, O2, E>
|
||||||
where
|
where
|
||||||
P: Parser<I, O1, E>,
|
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> {
|
fn any_char(input: &str) -> ParseResult<&str, char, &str> {
|
||||||
match input.chars().next() {
|
match input.chars().next() {
|
||||||
Some(ch) => Ok((ch, &input[ch.len_utf8()..])),
|
Some(ch) => Ok((ch, &input[ch.len_utf8()..])),
|
||||||
@ -119,6 +91,7 @@ where
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::primitives::literal;
|
||||||
use std::assert_matches::assert_matches;
|
use std::assert_matches::assert_matches;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
@ -128,12 +101,6 @@ mod tests {
|
|||||||
assert_matches!(output.unwrap(), ("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]
|
#[test]
|
||||||
fn test_map() {
|
fn test_map() {
|
||||||
let lit_a = literal("a");
|
let lit_a = literal("a");
|
||||||
@ -150,12 +117,6 @@ mod tests {
|
|||||||
assert_matches!(p.parse(input), Ok((v, "ecks")) if v.len() == 1);
|
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]
|
#[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));
|
||||||
|
73
src/primitives.rs
Normal file
73
src/primitives.rs
Normal 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")));
|
||||||
|
}
|
||||||
|
}
|
@ -110,7 +110,8 @@ where
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
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;
|
use std::assert_matches::assert_matches;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user