From 5947cb97b51b5bbd78ec4213bff8541e3167e737 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Tue, 17 Dec 2024 20:27:44 -0700 Subject: allow commas within balanced parentheses in function args i am miserable --- src/makefile/token.rs | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/makefile/token.rs b/src/makefile/token.rs index 7b88823..8507a3a 100644 --- a/src/makefile/token.rs +++ b/src/makefile/token.rs @@ -255,6 +255,13 @@ impl Delimiter { Self::Braces => "}", } } + + const fn end_char(&self) -> char { + match self { + Delimiter::Parens => ')', + Delimiter::Braces => '}', + } + } } fn macro_function_name<'a, E: Err<'a>>( @@ -365,13 +372,12 @@ fn text_but_not<'a, E: Err<'a>>( } fn nested_delimiters<'a, E: Err<'a>>( - ends: Vec, context: Delimiter, ) -> impl FnMut(&'a str) -> IResult<&'a str, TokenString, E> { map( tuple(( tag(context.start()), - move |x| tokens_but_not(ends.clone(), context)(x), + move |x| tokens_but_not(vec![context.end_char()], context)(x), tag(context.end()), )), |(left, center, right)| { @@ -392,7 +398,7 @@ fn single_token_but_not<'a, E: Err<'a>>( alt(( text_but_not(tbn_ends), macro_expansion, - nested_delimiters(ends, context), + nested_delimiters(context), )) } @@ -638,4 +644,28 @@ mod test { ); Ok(()) } + + #[cfg(feature = "full")] + #[test] + fn quoted_function_call_comma() -> R { + let text = "$(egg $$(bug a, b/c))"; + let tokens = tokenize(text)?; + + assert_eq!( + tokens, + TokenString::just(Token::FunctionCall { + name: TokenString::text("egg"), + args: vec![TokenString::text("$(bug a, b/c)")], + }) + ); + Ok(()) + } + + #[cfg(feature = "full")] + #[test] + fn unbalanced_parentheses_rejected() -> R { + let text = "$(egg ()"; + assert!(tokenize(text).is_err()); + Ok(()) + } } -- cgit v1.2.3