diff options
Diffstat (limited to 'src/makefile/token.rs')
-rw-r--r-- | src/makefile/token.rs | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/src/makefile/token.rs b/src/makefile/token.rs index 60cffe0..8bd0179 100644 --- a/src/makefile/token.rs +++ b/src/makefile/token.rs @@ -5,13 +5,15 @@ use eyre::WrapErr; use nom::{ branch::alt, bytes::complete::{tag, take_till1, take_while1}, - character::complete::{anychar, space1}, + character::complete::anychar, combinator::{all_consuming, map, opt, verify}, error::{context, convert_error, ContextError, ParseError, VerboseError}, - multi::{many1, separated_list1}, + multi::many1, sequence::{delimited, pair, preceded, separated_pair}, Finish, IResult, }; +#[cfg(feature = "full")] +use nom::{character::complete::space1, multi::separated_list1}; trait Err<'a>: 'a + ParseError<&'a str> + ContextError<&'a str> {} impl<'a, T: 'a + ParseError<&'a str> + ContextError<&'a str>> Err<'a> for T {} @@ -25,6 +27,7 @@ impl TokenString { Self(vec![Token::Text(text.into())]) } + #[cfg(feature = "full")] pub fn r#macro(name: impl Into<String>) -> Self { Self(vec![Token::MacroExpansion { name: name.into(), @@ -60,6 +63,7 @@ impl TokenString { None } + #[cfg(feature = "full")] pub fn starts_with(&self, pattern: &str) -> bool { match self.0.first() { Some(Token::Text(t)) => t.starts_with(pattern), @@ -74,6 +78,7 @@ impl TokenString { } } + #[cfg(feature = "full")] pub fn strip_prefix(&mut self, suffix: &str) { if let Some(Token::Text(t)) = self.0.first_mut() { if let Some(x) = t.strip_prefix(suffix) { @@ -100,6 +105,7 @@ impl TokenString { } } + #[cfg(feature = "full")] pub fn trim_end(&mut self) { if let Some(Token::Text(t)) = self.0.last_mut() { *t = t.trim_end().into(); @@ -123,6 +129,7 @@ pub enum Token { name: String, replacement: Option<(TokenString, TokenString)>, }, + #[cfg(feature = "full")] FunctionCall { name: String, args: Vec<TokenString>, @@ -141,6 +148,7 @@ impl fmt::Display for Token { name, replacement: Some((r1, r2)), } => write!(f, "$({}:{}={})", name, r1, r2), + #[cfg(feature = "full")] Self::FunctionCall { name, args } => write!( f, "$({} {})", @@ -183,6 +191,7 @@ fn macro_expansion_body<'a, E: Err<'a>>( ) } +#[cfg(feature = "full")] fn function_call_body<'a, E: Err<'a>>( end: char, ) -> impl FnMut(&'a str) -> IResult<&'a str, Token, E> { @@ -202,20 +211,22 @@ fn function_call_body<'a, E: Err<'a>>( ) } +#[cfg(feature = "full")] +fn macro_body<'a, E: Err<'a>>(end: char) -> impl FnMut(&'a str) -> IResult<&'a str, Token, E> { + alt((function_call_body(end), macro_expansion_body(end))) +} + +#[cfg(not(feature = "full"))] +fn macro_body<'a, E: Err<'a>>(end: char) -> impl FnMut(&'a str) -> IResult<&'a str, Token, E> { + macro_expansion_body(end) +} + fn parens_macro_expansion<'a, E: Err<'a>>(input: &'a str) -> IResult<&'a str, Token, E> { - delimited( - tag("$("), - alt((function_call_body(')'), macro_expansion_body(')'))), - tag(")"), - )(input) + delimited(tag("$("), macro_body(')'), tag(")"))(input) } fn braces_macro_expansion<'a, E: Err<'a>>(input: &'a str) -> IResult<&'a str, Token, E> { - delimited( - tag("${"), - alt((function_call_body('}'), macro_expansion_body('}'))), - tag("}"), - )(input) + delimited(tag("${"), macro_body('}'), tag("}"))(input) } fn tiny_macro_expansion<'a, E: Err<'a>>(input: &'a str) -> IResult<&'a str, Token, E> { @@ -328,6 +339,7 @@ mod test { } } + #[cfg(feature = "full")] fn token_function_call(name: impl Into<String>, args: Vec<impl Into<TokenString>>) -> Token { Token::FunctionCall { name: name.into(), @@ -430,6 +442,7 @@ mod test { Ok(()) } + #[cfg(feature = "full")] #[test] fn function_hell() -> R { let text = "$(foo bar, $(baz))"; |