From a3d602d6e84a91b7656b80e3a7e45caa532b841e Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Wed, 31 Mar 2021 13:28:28 -0600 Subject: avoid redundancy --- src/makefile/conditional.rs | 48 ++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 25 deletions(-) (limited to 'src/makefile/conditional.rs') diff --git a/src/makefile/conditional.rs b/src/makefile/conditional.rs index a3c3e0d..6795051 100644 --- a/src/makefile/conditional.rs +++ b/src/makefile/conditional.rs @@ -1,6 +1,6 @@ use super::token::TokenString; -pub enum ConditionalLine { +pub enum Line { /// spelled "ifeq" IfEqual(TokenString, TokenString), /// spelled "ifneq" @@ -12,12 +12,12 @@ pub enum ConditionalLine { /// spelled "else" Else, /// spelled "else condition" - ElseIf(Box), + ElseIf(Box), /// spelled "endif" EndIf, } -pub enum ConditionalState { +pub enum State { /// we saw a conditional, the condition was true, we're executing now /// and if we hit an else we will start SkippingUntilEndIf Executing, @@ -31,7 +31,7 @@ pub enum ConditionalState { SkippingUntilEndIf, } -impl ConditionalState { +impl State { pub const fn skipping(&self) -> bool { match self { Self::Executing => false, @@ -40,14 +40,14 @@ impl ConditionalState { } } -pub enum ConditionalStateAction { - Push(ConditionalState), - Replace(ConditionalState), +pub enum StateAction { + Push(State), + Replace(State), Pop, } -impl ConditionalStateAction { - pub fn apply_to(self, stack: &mut Vec) { +impl StateAction { + pub fn apply_to(self, stack: &mut Vec) { match self { Self::Push(state) => stack.push(state), Self::Replace(state) => match stack.last_mut() { @@ -77,7 +77,7 @@ fn decode_condition_args(line_body: &str) -> Option<(TokenString, TokenString)> Some((arg1, arg2)) } -impl ConditionalLine { +impl Line { pub fn from( line: &str, expand_macro: impl Fn(&TokenString) -> anyhow::Result, @@ -112,46 +112,44 @@ impl ConditionalLine { pub fn action( &self, - current_state: Option<&ConditionalState>, + current_state: Option<&State>, is_macro_defined: impl Fn(&str) -> bool, expand_macro: impl Fn(&TokenString) -> anyhow::Result, - ) -> anyhow::Result { - use ConditionalState as State; - use ConditionalStateAction as Action; + ) -> anyhow::Result { Ok(match self { Self::IfEqual(arg1, arg2) => { let arg1 = expand_macro(arg1)?; let arg2 = expand_macro(arg2)?; if arg1 == arg2 { - Action::Push(State::Executing) + StateAction::Push(State::Executing) } else { - Action::Push(State::SkippingUntilElseOrEndIf) + StateAction::Push(State::SkippingUntilElseOrEndIf) } } Self::IfNotEqual(arg1, arg2) => { let arg1 = expand_macro(arg1)?; let arg2 = expand_macro(arg2)?; if arg1 == arg2 { - Action::Push(State::SkippingUntilElseOrEndIf) + StateAction::Push(State::SkippingUntilElseOrEndIf) } else { - Action::Push(State::Executing) + StateAction::Push(State::Executing) } } Self::IfDefined(name) => { if is_macro_defined(name) { - Action::Push(State::Executing) + StateAction::Push(State::Executing) } else { - Action::Push(State::SkippingUntilElseOrEndIf) + StateAction::Push(State::SkippingUntilElseOrEndIf) } } Self::IfNotDefined(name) => { if is_macro_defined(name) { - Action::Push(State::SkippingUntilElseOrEndIf) + StateAction::Push(State::SkippingUntilElseOrEndIf) } else { - Action::Push(State::Executing) + StateAction::Push(State::Executing) } } - Self::Else => Action::Replace(match current_state { + Self::Else => StateAction::Replace(match current_state { Some(State::Executing) | Some(State::SkippingUntilEndIf) => { State::SkippingUntilEndIf } @@ -160,14 +158,14 @@ impl ConditionalLine { }), Self::ElseIf(inner_condition) => match current_state { Some(State::Executing) | Some(State::SkippingUntilEndIf) => { - Action::Replace(State::SkippingUntilEndIf) + StateAction::Replace(State::SkippingUntilEndIf) } Some(State::SkippingUntilElseOrEndIf) => { inner_condition.action(current_state, is_macro_defined, expand_macro)? } None => panic!("got an ElseIf but not in a conditional"), }, - Self::EndIf => Action::Pop, + Self::EndIf => StateAction::Pop, }) } } -- cgit v1.2.3