From fc8fce6facee9bd50958e78f3b177e16ed065c19 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 10 Sep 2019 13:55:35 +0200 Subject: Generalize parse_children and change its invocation --- pest_consume/src/lib.rs | 95 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 1 deletion(-) (limited to 'pest_consume/src') diff --git a/pest_consume/src/lib.rs b/pest_consume/src/lib.rs index 6e4b2e3..70aee56 100644 --- a/pest_consume/src/lib.rs +++ b/pest_consume/src/lib.rs @@ -5,7 +5,7 @@ use pest::Span; pub use pest_consume_macros::{make_parser, parse_children}; /// Carries a pest Pair alongside custom user data. -#[derive(Debug, Clone)] +#[derive(Debug)] pub struct ParseInput<'input, 'data, Rule, Data> where Rule: pest::RuleType, @@ -14,6 +14,16 @@ where user_data: &'data Data, } +/// Iterator over `ParseInput`s. It is created by `ParseInput::children`. +#[derive(Debug)] +pub struct ParseInputs<'input, 'data, Rule, Data> +where + Rule: pest::RuleType, +{ + input: ParseInput<'input, 'data, Rule, Data>, + pairs: pest::iterators::Pairs<'input, Rule>, +} + impl<'input, 'data, Rule, Data> ParseInput<'input, 'data, Rule, Data> where Rule: pest::RuleType, @@ -50,6 +60,15 @@ where } None } + /// Return an iterator over the children of this input + // Can't use `-> impl Iterator` because of weird lifetime limitations + // (see https://github.com/rust-lang/rust/issues/61997). + pub fn children(&self) -> ParseInputs<'input, 'data, Rule, Data> { + ParseInputs { + input: self.clone(), + pairs: self.as_pair().clone().into_inner(), + } + } pub fn user_data(&self) -> &'data Data { self.user_data @@ -66,6 +85,28 @@ where pub fn as_rule(&self) -> Rule { self.pair.as_rule() } + pub fn as_rule_alias(&self) -> String + where + T: PestConsumer, + { + T::rule_alias(self.as_rule()) + } +} + +impl<'input, 'data, Rule, Data> ParseInputs<'input, 'data, Rule, Data> +where + Rule: pest::RuleType, +{ + /// Create an error that points to the span of the input. + pub fn error(&self, message: String) -> Error { + self.input.error(message) + } + pub fn aliased_rules(&self) -> Vec + where + T: PestConsumer, + { + self.clone().map(|p| p.as_rule_alias::()).collect() + } } /// Used by the macros. @@ -121,3 +162,55 @@ fn debug_pair(pair: Pair) -> String { aux(&mut s, 0, "".into(), pair); s } + +impl<'input, 'data, Rule, Data> Iterator + for ParseInputs<'input, 'data, Rule, Data> +where + Rule: pest::RuleType, +{ + type Item = ParseInput<'input, 'data, Rule, Data>; + + fn next(&mut self) -> Option { + let child_pair = self.pairs.next()?; + let child = self.input.with_pair(child_pair); + Some(child) + } +} + +impl<'input, 'data, Rule, Data> DoubleEndedIterator + for ParseInputs<'input, 'data, Rule, Data> +where + Rule: pest::RuleType, +{ + fn next_back(&mut self) -> Option { + let child_pair = self.pairs.next_back()?; + let child = self.input.with_pair(child_pair); + Some(child) + } +} + +// Manual impl to avoid stupid `Data: Clone` trait bound +impl<'input, 'data, Rule, Data> Clone for ParseInput<'input, 'data, Rule, Data> +where + Rule: pest::RuleType, +{ + fn clone(&self) -> Self { + ParseInput { + pair: self.pair.clone(), + user_data: self.user_data, + } + } +} + +// Manual impl to avoid stupid `Data: Clone` trait bound +impl<'input, 'data, Rule, Data> Clone for ParseInputs<'input, 'data, Rule, Data> +where + Rule: pest::RuleType, +{ + fn clone(&self) -> Self { + ParseInputs { + input: self.input.clone(), + pairs: self.pairs.clone(), + } + } +} -- cgit v1.2.3