Representation work

This commit is contained in:
Greg Shuflin 2023-01-22 14:25:10 -08:00
parent 864246c24a
commit 2fd8b1be16
8 changed files with 75 additions and 12 deletions

View File

@ -1,5 +1,5 @@
use crate::combinators::separated_by::SeparatedBy;
use crate::parser::{BoxedParser, ParseResult, Parser, ParserInput};
use crate::parser::{BoxedParser, ParseResult, Parser, ParserInput, Representation};
pub fn repeated<'a, P, I, O>(parser: P) -> Repeated<'a, I, O>
where
@ -83,4 +83,8 @@ where
Ok((results, further_input))
}
fn representation(&self) -> Representation {
Representation::new("NOT IMPL'D")
}
}

View File

@ -1,5 +1,5 @@
use crate::combinators::repeated::Repeated;
use crate::parser::{BoxedParser, ParseResult, Parser, ParserInput};
use crate::parser::{BoxedParser, ParseResult, Parser, ParserInput, Representation};
pub struct SeparatedBy<'a, I, O>
where
@ -14,6 +14,10 @@ impl<'a, I, O> Parser<I, Vec<O>, I> for SeparatedBy<'a, I, O>
where
I: ParserInput + Clone + 'a,
{
fn representation(&self) -> Representation {
Representation::new("NOT IMPL'D")
}
fn parse(&self, input: I) -> ParseResult<I, Vec<O>, I> {
let at_least = self.inner_repeated.at_least.unwrap_or(0);
let at_most = self.inner_repeated.at_most.unwrap_or(u16::MAX);

View File

@ -1,4 +1,4 @@
use crate::parser::{ParseResult, Parser, ParserInput};
use crate::parser::{ParseResult, Parser, ParserInput, Representation};
pub struct BoxedParser<'a, I, O, E>
where
@ -22,6 +22,9 @@ where
}
impl<'a, I: ParserInput, O, E> Parser<I, O, E> for BoxedParser<'a, I, O, E> {
fn representation(&self) -> Representation {
Representation::new("NOT IMPL'D")
}
fn parse(&self, input: I) -> ParseResult<I, O, E> {
self.inner.parse(input)
}

View File

@ -1,12 +1,14 @@
mod boxed_parser;
mod named_parser;
mod parser_input;
mod representation;
use std::rc::Rc;
pub use boxed_parser::BoxedParser;
pub use named_parser::NamedParser;
pub use parser_input::ParserInput;
pub use representation::Representation;
pub type ParseResult<I, O, E> = Result<(O, I), E>;
@ -16,6 +18,8 @@ where
{
fn parse(&self, input: I) -> ParseResult<I, O, E>;
fn representation(&self) -> Representation;
fn boxed<'a>(self) -> BoxedParser<'a, I, O, E>
where
Self: Sized + 'a,
@ -141,6 +145,23 @@ where
fn parse(&self, input: I) -> ParseResult<I, O, E> {
self(input)
}
fn representation(&self) -> Representation {
Representation::new("NOT IMPL'D")
}
}
impl<I: ParserInput, O, E, F> Parser<I, O, E> for (F, Representation)
where
F: Fn(I) -> ParseResult<I, O, E>,
{
fn parse(&self, input: I) -> ParseResult<I, O, E> {
self.0(input)
}
fn representation(&self) -> Representation {
self.1.clone()
}
}
impl<I, O, E, T> Parser<I, O, E> for Rc<T>
@ -151,4 +172,8 @@ where
fn parse(&self, input: I) -> ParseResult<I, O, E> {
self.as_ref().parse(input)
}
fn representation(&self) -> Representation {
self.as_ref().representation()
}
}

View File

@ -1,5 +1,5 @@
use super::boxed_parser::BoxedParser;
use crate::parser::{ParseResult, Parser, ParserInput};
use crate::parser::{ParseResult, Parser, ParserInput, Representation};
pub struct NamedParser<'a, I, O, E>
where
@ -26,6 +26,10 @@ where
}
impl<'a, I: ParserInput, O, E> Parser<I, O, E> for NamedParser<'a, I, O, E> {
fn representation(&self) -> Representation {
Representation::new("NOT IMPL'D")
}
fn parse(&self, input: I) -> ParseResult<I, O, E> {
self.inner_parser.parse(input)
}

View File

@ -0,0 +1,12 @@
#[derive(Debug, Clone)]
pub struct Representation {
val: String
}
impl Representation {
pub fn new(from: &str) -> Self {
Self { val: from.to_string() }
}
}

View File

@ -1,4 +1,4 @@
use crate::parser::{ParseResult, Parser, ParserInput};
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() {
@ -7,11 +7,13 @@ pub fn literal_char(expected: char) -> impl Fn(&str) -> ParseResult<&str, char,
}
}
pub fn literal(expected: &'static str) -> impl Fn(&str) -> ParseResult<&str, &str, &str> {
move |input| match input.get(0..expected.len()) {
pub fn literal<'a>(expected: &'static str) -> impl Parser<&'a str, &'a str, &'a str> {
println!("literal call expected: {}", 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, Representation::new("yolo"))
}
pub fn any_char(input: &str) -> ParseResult<&str, char, &str> {
@ -22,7 +24,7 @@ pub fn any_char(input: &str) -> ParseResult<&str, char, &str> {
}
pub fn one_of<'a>(items: &'static str) -> impl Parser<&'a str, &'a str, &'a str> {
move |input: &'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);
@ -30,7 +32,15 @@ pub fn one_of<'a>(items: &'static str) -> impl Parser<&'a str, &'a str, &'a str>
}
}
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<P, F, I, O>(parser: P, pred_fn: F) -> impl Parser<I, O, I>
@ -39,7 +49,8 @@ where
P: Parser<I, O, I>,
F: Fn(&O) -> bool,
{
move |input| {
let orig_rep = parser.representation();
(move |input| {
parser.parse(input).and_then(|(result, rest)| {
if pred_fn(&result) {
Ok((result, rest))
@ -47,7 +58,7 @@ where
Err(rest)
}
})
}
}, Representation::new(&format!("{:?} if <PREDICATE>", orig_rep)))
}
/// Parses a standard identifier in a programming language

View File

@ -25,7 +25,7 @@ proptest! {
#[test]
fn test_parsing() {
let output = literal("a")("a yolo");
let output = literal("a").parse("a yolo");
assert_eq!(output.unwrap(), ("a", " yolo"));
}