From 5a72fb28fb69a476437141a4eb898d6d001dd281 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Wed, 14 Apr 2021 20:17:42 -0600 Subject: implement potentially-mixed-quoted conditional args --- src/makefile/conditional.rs | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) (limited to 'src/makefile/conditional.rs') diff --git a/src/makefile/conditional.rs b/src/makefile/conditional.rs index 95cab9e..98400e6 100644 --- a/src/makefile/conditional.rs +++ b/src/makefile/conditional.rs @@ -2,7 +2,7 @@ use eyre::{bail, Result}; use super::token::TokenString; -#[derive(Debug)] +#[derive(Debug, Eq, PartialEq)] pub enum Line { /// spelled "ifeq" IfEqual(TokenString, TokenString), @@ -73,10 +73,23 @@ fn decode_condition_args(line_body: &str) -> Option<(TokenString, TokenString)> let mut tokens = tokens; tokens.strip_prefix("("); tokens.strip_suffix(")"); - tokens.split_once(',')? + tokens.split_once(",")? } else { - // TODO see if i really need to implement potentially-mixed-quoted args - return None; + let quotes = &["\"", "'"]; + let mut split_pair = None; + for (left_quote, right_quote) in quotes + .iter() + .flat_map(|left| quotes.iter().map(move |right| (left, right))) + { + if tokens.starts_with(left_quote) && tokens.ends_with(right_quote) { + let mut tokens = tokens; + tokens.strip_prefix(left_quote); + tokens.strip_suffix(right_quote); + split_pair = Some(tokens.split_once(&format!("{} {}", left_quote, right_quote))?); + break; + } + } + split_pair? }; arg1.trim_end(); arg2.trim_start(); @@ -182,3 +195,23 @@ impl Line { }) } } + +#[cfg(test)] +mod test { + use super::*; + + type R = Result<()>; + + #[test] + fn quotes() -> R { + let line = r#"ifeq 'x' "3""#; + assert_eq!( + Line::from(line, |_| panic!())?, + Some(Line::IfEqual( + TokenString::text("x"), + TokenString::text("3") + )) + ); + Ok(()) + } +} -- cgit v1.2.3