summaryrefslogtreecommitdiffstats
path: root/src/parse
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2022-01-10 01:54:21 -0500
committerJesse Luehrs <doy@tozt.net>2022-01-10 01:54:21 -0500
commitfeb02a8048a31a46ab57fa268d3f22b40af73c61 (patch)
treef252b92ca9c6ab80d4acc21d296b9f1fac80d009 /src/parse
parentfd823c090b2a1cd95c237206f9887215c0f9230a (diff)
downloadnbsh-feb02a8048a31a46ab57fa268d3f22b40af73c61.tar.gz
nbsh-feb02a8048a31a46ab57fa268d3f22b40af73c61.zip
refactor the grammar a bit
Diffstat (limited to 'src/parse')
-rw-r--r--src/parse/ast.rs17
1 files changed, 9 insertions, 8 deletions
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<Rule>) -> 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<Rule>) -> Self {
- match pair.as_rule() {
+ fn build_ast(
+ pair: pest::iterators::Pair<Rule>,
+ ) -> impl Iterator<Item = Self> + '_ {
+ 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<Rule>) -> 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 {