From bd5267165026e6ad3d4fc26d78fcf7dd155d23a7 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Thu, 4 May 2023 12:01:52 -0500 Subject: [PATCH] parser doesnt include brackets into source substitution array --- src/parser/parse.rs | 32 ++++++++++++++++++++++++++++---- src/tests/test_parser.rs | 8 ++++---- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/parser/parse.rs b/src/parser/parse.rs index b314c40..5af268d 100644 --- a/src/parser/parse.rs +++ b/src/parser/parse.rs @@ -62,12 +62,19 @@ fn parse_array_defs(pairs: Pairs) -> HashMap<&'_ str, Vec<&'_ str>> { fn parse_source(pairs: Pairs) -> Vec { let mut vec: Vec = Vec::new(); + macro_rules! push_src ( + (var($s:expr)) => { vec.push(Source::Sub(Substitution::Variable($s))) }; + (arr($s:expr)) => { vec.push(Source::Sub(Substitution::Array($s))) }; + (pat($s:expr)) => { vec.push(Source::Sub(Substitution::Pattern($s))) }; + ($s:expr) => { vec.push(Source::Str($s)) }; + ); + for pair in pairs { match pair.as_rule() { - Rule::var_sub => vec.push(Source::Sub(Substitution::Variable(pair.as_str()))), - Rule::arr_sub => vec.push(Source::Sub(Substitution::Array(pair.as_str()))), - Rule::pat_sub => vec.push(Source::Sub(Substitution::Pattern(pair.as_str()))), - Rule::char_seq => vec.push(Source::Str(pair.as_str())), + Rule::var_sub => push_src!(var(parse_substitution(pair))), + Rule::arr_sub => push_src!(arr(parse_substitution(pair))), + Rule::pat_sub => push_src!(pat(parse_substitution(pair))), + Rule::char_seq => push_src!(pair.as_str()), // anything that isn't a substitution is a char_seq inside source _ => unreachable!(), } @@ -76,6 +83,23 @@ fn parse_source(pairs: Pairs) -> Vec { vec } +fn parse_substitution(pair: Pair) -> &'_ str { + match pair.as_rule() { + Rule::var_sub | Rule::arr_sub | Rule::pat_sub => { + let str = pair.as_str(); + // return the value as the inner string for substitution + // all substitutions have the format of + // *{ ... } + // we return everything except the first + // two characters: (sigil and preceding brace) + // and the trailing brace + &str[2..str.len() - 1] + } + // this function only gets called to parse substituiton patterns + _ => unreachable!(), + } +} + fn parse_assign(pair: Pair) -> (&'_ str, &'_ str) { let mut key = ""; let mut val = ""; diff --git a/src/tests/test_parser.rs b/src/tests/test_parser.rs index c03cb3c..c477446 100644 --- a/src/tests/test_parser.rs +++ b/src/tests/test_parser.rs @@ -46,12 +46,12 @@ fn build_pattern_file() -> Result<()> { let mut pattern_src = parse_file(PATTERN)?.source.into_iter(); pattern_src.next(); - assert_eq!(pattern_src.next().unwrap(), source!(var("${var}"))); + assert_eq!(pattern_src.next().unwrap(), source!(var("var"))); pattern_src.next(); - assert_eq!(pattern_src.next().unwrap(), source!(pat("&{pat}"))); - assert_eq!(pattern_src.next().unwrap(), source!(arr("@{array}"))); + assert_eq!(pattern_src.next().unwrap(), source!(pat("pat"))); + assert_eq!(pattern_src.next().unwrap(), source!(arr("array"))); pattern_src.next(); - assert_eq!(pattern_src.next().unwrap(), source!(var("${blank}"))); + assert_eq!(pattern_src.next().unwrap(), source!(var("blank"))); Ok(()) } -- 2.44.2