From 4e813a7efd60ef0cb6dbece852e1fb43e8e7461c Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Tue, 30 Jan 2024 00:15:00 -0800 Subject: [PATCH] more parser annotation --- src/parser.rs | 6 ++++++ src/primitives.rs | 9 +++++---- src/representation.rs | 10 ++++++++++ src/test/mod.rs | 2 +- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 2b4f9cf..a3a957f 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -28,6 +28,12 @@ pub trait ParserExtension: Parser { fn then_ignore>(self, next: P) -> impl Parser; fn ignore_then>(self, next: P) -> impl Parser; fn surrounded_by(self, surrounding: impl Parser) -> impl Parser; + fn to_anno(self) -> AnnotatedParser + where + Self: Sized, + { + AnnotatedParser::new(self) + } fn to_named(self, name: &str) -> AnnotatedParser where Self: Sized, diff --git a/src/primitives.rs b/src/primitives.rs index a6f74d8..9989818 100644 --- a/src/primitives.rs +++ b/src/primitives.rs @@ -1,10 +1,11 @@ -use crate::{ParseResult, Parser}; +use crate::{representation::Representation, ParseResult, Parser, ParserExtension}; -pub fn literal(expected: &'static str) -> impl Fn(&str) -> ParseResult<&str, &str, ()> { - move |input| match input.get(0..expected.len()) { +pub fn literal<'a>(expected: &'static str) -> impl Parser<&'a str, &'a str, ()> { + let p = move |input: &'a str| match input.get(0..expected.len()) { Some(next) if next == expected => Ok((next, &input[expected.len()..])), _ => Err(((), input)), - } + }; + p.to_anno().with_repr(Representation::new()) } pub fn literal_char<'a>(expected: char) -> impl Parser<&'a str, char, ()> { diff --git a/src/representation.rs b/src/representation.rs index dbd06b0..8516a0f 100644 --- a/src/representation.rs +++ b/src/representation.rs @@ -1,2 +1,12 @@ #[derive(Debug)] pub struct Representation {} + +impl Representation { + pub fn show(&self) { + println!("Not done"); + } + + pub fn new() -> Self { + Self {} + } +} diff --git a/src/test/mod.rs b/src/test/mod.rs index 9d5a67f..6204ef4 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -4,7 +4,7 @@ use super::*; #[test] fn parsing() { - let (parsed, rest) = literal("a")("a yolo").unwrap(); + let (parsed, rest) = literal("a").parse("a yolo").unwrap(); assert_eq!(parsed, "a"); assert_eq!(rest, " yolo"); }