From b8ab245fa35143ec6c1884d7aa685f25a5213940 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 10 Jan 2022 03:33:43 -0500 Subject: parse alternations --- src/parse/ast.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src/parse/ast.rs') diff --git a/src/parse/ast.rs b/src/parse/ast.rs index 053f07f..2f4ae86 100644 --- a/src/parse/ast.rs +++ b/src/parse/ast.rs @@ -158,7 +158,10 @@ pub struct Word { impl Word { fn build_ast(pair: pest::iterators::Pair) -> Self { - assert!(matches!(pair.as_rule(), Rule::word)); + assert!(matches!( + pair.as_rule(), + Rule::word | Rule::alternation_word + )); Self { parts: pair.into_inner().flat_map(WordPart::build_ast).collect(), } @@ -175,6 +178,7 @@ impl Word { #[derive(Debug, Clone, PartialEq, Eq)] enum WordPart { + Alternation(Vec), Var(String), Bareword(String), DoubleQuoted(String), @@ -186,7 +190,10 @@ impl WordPart { fn build_ast( pair: pest::iterators::Pair, ) -> impl Iterator + '_ { - assert!(matches!(pair.as_rule(), Rule::word_part)); + assert!(matches!( + pair.as_rule(), + Rule::word_part | Rule::alternation_word_part + )); pair.into_inner().map(|pair| match pair.as_rule() { Rule::var => { let s = pair.as_str(); @@ -200,19 +207,25 @@ impl WordPart { .to_string(), ) } - Rule::bareword => Self::Bareword(strip_escape(pair.as_str())), + Rule::bareword | Rule::alternation_bareword => { + Self::Bareword(strip_escape(pair.as_str())) + } Rule::double_string => { Self::DoubleQuoted(strip_escape(pair.as_str())) } Rule::single_string => { Self::SingleQuoted(strip_basic_escape(pair.as_str())) } + Rule::alternation => Self::Alternation( + pair.into_inner().map(Word::build_ast).collect(), + ), _ => unreachable!(), }) } fn eval(self, env: &Env) -> String { match self { + Self::Alternation(_) => todo!(), Self::Var(name) => env.var(&name), Self::Bareword(s) | Self::DoubleQuoted(s) -- cgit v1.2.3-54-g00ecf