diff options
Diffstat (limited to 'src/makefile/conditional.rs')
-rw-r--r-- | src/makefile/conditional.rs | 55 |
1 files changed, 32 insertions, 23 deletions
diff --git a/src/makefile/conditional.rs b/src/makefile/conditional.rs index f9d2d60..bf373bd 100644 --- a/src/makefile/conditional.rs +++ b/src/makefile/conditional.rs @@ -78,41 +78,50 @@ fn decode_condition_args(line_body: &str) -> Option<(TokenString, TokenString)> } impl ConditionalLine { - pub(crate) fn from(line: &str, expand_macro: impl Fn(&TokenString) -> String) -> Option<Self> { - if let Some(line) = line.strip_prefix("ifeq ") { - let (arg1, arg2) = decode_condition_args(line)?; - Some(Self::IfEqual(arg1, arg2)) + pub(crate) fn from( + line: &str, + expand_macro: impl Fn(&TokenString) -> anyhow::Result<String>, + ) -> anyhow::Result<Option<Self>> { + Ok(Some(if let Some(line) = line.strip_prefix("ifeq ") { + match decode_condition_args(line) { + Some((arg1, arg2)) => Self::IfEqual(arg1, arg2), + None => return Ok(None), + } } else if let Some(line) = line.strip_prefix("ifneq ") { - let (arg1, arg2) = decode_condition_args(line)?; - Some(Self::IfNotEqual(arg1, arg2)) + match decode_condition_args(line) { + Some((arg1, arg2)) => Self::IfNotEqual(arg1, arg2), + None => return Ok(None), + } } else if let Some(line) = line.strip_prefix("ifdef ") { - Some(Self::IfDefined(expand_macro(&line.parse().ok()?))) + Self::IfDefined(expand_macro(&line.parse()?)?) } else if let Some(line) = line.strip_prefix("ifndef ") { - Some(Self::IfNotDefined(expand_macro(&line.parse().ok()?))) + Self::IfNotDefined(expand_macro(&line.parse()?)?) } else if line == "else" { - Some(Self::Else) + Self::Else } else if let Some(line) = line.strip_prefix("else ") { - let sub_condition = Self::from(line, expand_macro)?; - Some(Self::ElseIf(Box::new(sub_condition))) + match Self::from(line, expand_macro)? { + Some(sub_condition) => Self::ElseIf(Box::new(sub_condition)), + None => return Ok(None), + } } else if line == "endif" { - Some(Self::EndIf) + Self::EndIf } else { - None - } + return Ok(None); + })) } pub(crate) fn action( &self, current_state: Option<&ConditionalState>, is_macro_defined: impl Fn(&str) -> bool, - expand_macro: impl Fn(&TokenString) -> String, - ) -> ConditionalStateAction { + expand_macro: impl Fn(&TokenString) -> anyhow::Result<String>, + ) -> anyhow::Result<ConditionalStateAction> { use ConditionalState as State; use ConditionalStateAction as Action; - match self { + Ok(match self { Self::IfEqual(arg1, arg2) => { - let arg1 = expand_macro(arg1); - let arg2 = expand_macro(arg2); + let arg1 = expand_macro(arg1)?; + let arg2 = expand_macro(arg2)?; if arg1 == arg2 { Action::Push(State::Executing) } else { @@ -120,8 +129,8 @@ impl ConditionalLine { } } Self::IfNotEqual(arg1, arg2) => { - let arg1 = expand_macro(arg1); - let arg2 = expand_macro(arg2); + let arg1 = expand_macro(arg1)?; + let arg2 = expand_macro(arg2)?; if arg1 == arg2 { Action::Push(State::SkippingUntilElseOrEndIf) } else { @@ -154,11 +163,11 @@ impl ConditionalLine { Action::Replace(State::SkippingUntilEndIf) } Some(State::SkippingUntilElseOrEndIf) => { - inner_condition.action(current_state, is_macro_defined, expand_macro) + inner_condition.action(current_state, is_macro_defined, expand_macro)? } None => panic!("got an ElseIf but not in a conditional"), }, Self::EndIf => Action::Pop, - } + }) } } |