From feb02a8048a31a46ab57fa268d3f22b40af73c61 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 10 Jan 2022 01:54:21 -0500 Subject: refactor the grammar a bit --- src/parse/ast.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'src/parse') diff --git a/src/parse/ast.rs b/src/parse/ast.rs index 9cf583a..053f07f 100644 --- a/src/parse/ast.rs +++ b/src/parse/ast.rs @@ -160,7 +160,7 @@ impl Word { fn build_ast(pair: pest::iterators::Pair) -> Self { assert!(matches!(pair.as_rule(), Rule::word)); Self { - parts: pair.into_inner().map(WordPart::build_ast).collect(), + parts: pair.into_inner().flat_map(WordPart::build_ast).collect(), } } @@ -183,8 +183,11 @@ enum WordPart { impl WordPart { #[allow(clippy::needless_pass_by_value)] - fn build_ast(pair: pest::iterators::Pair) -> Self { - match pair.as_rule() { + fn build_ast( + pair: pest::iterators::Pair, + ) -> impl Iterator + '_ { + assert!(matches!(pair.as_rule(), Rule::word_part)); + pair.into_inner().map(|pair| match pair.as_rule() { Rule::var => { let s = pair.as_str(); let inner = s.strip_prefix('$').unwrap(); @@ -205,7 +208,7 @@ impl WordPart { Self::SingleQuoted(strip_basic_escape(pair.as_str())) } _ => unreachable!(), - } + }) } fn eval(self, env: &Env) -> String { @@ -282,7 +285,7 @@ enum WordOrRedirect { impl WordOrRedirect { fn build_ast(pair: pest::iterators::Pair) -> Self { - assert!(matches!(pair.as_rule(), Rule::word)); + assert!(matches!(pair.as_rule(), Rule::word_or_redirect)); let mut inner = pair.into_inner().peekable(); let prefix = if matches!( inner.peek().map(pest::iterators::Pair::as_rule), @@ -292,9 +295,7 @@ impl WordOrRedirect { } else { None }; - let word = Word { - parts: inner.map(WordPart::build_ast).collect(), - }; + let word = Word::build_ast(inner.next().unwrap()); if let Some(prefix) = prefix { Self::Redirect(Redirect::parse(&prefix, word)) } else { -- cgit v1.2.3-54-g00ecf