From 9f41dc5a4680cba3697c64da28855c8d4f437191 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 10 Sep 2019 14:51:37 +0200 Subject: Implement parsing in PestConsumer --- pest_consume/src/lib.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'pest_consume') diff --git a/pest_consume/src/lib.rs b/pest_consume/src/lib.rs index 8f882a9..1579f81 100644 --- a/pest_consume/src/lib.rs +++ b/pest_consume/src/lib.rs @@ -1,9 +1,12 @@ use pest::error::{Error, ErrorVariant}; use pest::iterators::{Pair, Pairs}; +use pest::Parser as PestParser; use pest::{RuleType, Span}; pub use pest_consume_macros::{make_parser, match_inputs}; +static UNIT: () = (); + /// Carries a pest Pair alongside custom user data. #[derive(Debug)] pub struct ParseInput<'input, 'data, Rule: RuleType, Data> { @@ -76,6 +79,7 @@ impl<'i, 'd, R: RuleType, D> ParseInput<'i, 'd, R, D> { pub fn as_rule_alias(&self) -> String where C: PestConsumer, + ::Parser: PestParser, { C::rule_alias(self.as_rule()) } @@ -101,6 +105,7 @@ impl<'i, 'd, R: RuleType, D> ParseInputs<'i, 'd, R, D> { pub fn aliased_rules(&self) -> Vec where C: PestConsumer, + ::Parser: PestParser, { self.clone().map(|p| p.as_rule_alias::()).collect() } @@ -113,8 +118,26 @@ impl<'i, 'd, R: RuleType, D> ParseInputs<'i, 'd, R, D> { /// Used by the macros. pub trait PestConsumer { type Rule: RuleType; + type Parser: PestParser; fn rule_alias(rule: Self::Rule) -> String; fn allows_shortcut(rule: Self::Rule) -> bool; + + fn parse_with_userdata<'i, 'd, D>( + rule: Self::Rule, + input_str: &'i str, + user_data: &'d D, + ) -> Result, Error> { + let pairs = Self::Parser::parse(rule, input_str)?; + Ok(ParseInputs::new(input_str, pairs, user_data)) + } + + fn parse<'i>( + rule: Self::Rule, + input_str: &'i str, + ) -> Result, Error> + { + Self::parse_with_userdata(rule, input_str, &UNIT) + } } impl<'i, 'd, R: RuleType, D> Iterator for ParseInputs<'i, 'd, R, D> { -- cgit v1.2.3