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 ++++++++++++++++++++++----------------------- src/makefile/functions.rs | 4 ++-- src/makefile/macro.rs | 30 ++++++++++++++-------------- src/makefile/mod.rs | 4 ++-- src/makefile/token.rs | 1 + 5 files changed, 43 insertions(+), 44 deletions(-) (limited to 'src/makefile') 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, }) } } diff --git a/src/makefile/functions.rs b/src/makefile/functions.rs index c73d7e7..005f906 100644 --- a/src/makefile/functions.rs +++ b/src/makefile/functions.rs @@ -1,5 +1,5 @@ use super::pattern::r#match; -use super::r#macro::{MacroSet, MacroSource}; +use super::r#macro::{Set as MacroSet, Source as MacroSource}; use super::token::TokenString; pub fn expand_call(name: &str, args: &[TokenString], macros: &MacroSet) -> anyhow::Result { @@ -242,7 +242,7 @@ mod call { mod test { use super::*; - use crate::makefile::r#macro::{MacroSet, MacroSource}; + use crate::makefile::r#macro::{Set as MacroSet, Source as MacroSource}; type R = anyhow::Result<()>; diff --git a/src/makefile/macro.rs b/src/makefile/macro.rs index 0b7a27d..f18060f 100644 --- a/src/makefile/macro.rs +++ b/src/makefile/macro.rs @@ -9,7 +9,7 @@ use super::functions; use super::token::{Token, TokenString}; #[derive(Debug, Clone)] -pub enum MacroSource { +pub enum Source { File, CommandLineOrMakeflags, Environment, @@ -21,13 +21,13 @@ pub trait LookupInternal: for<'a> Fn(&'a str) -> anyhow::Result {} impl Fn(&'a str) -> anyhow::Result> LookupInternal for F {} #[derive(Clone)] -pub struct MacroSet<'parent, 'lookup> { - parent: Option<&'parent MacroSet<'parent, 'lookup>>, - data: HashMap, +pub struct Set<'parent, 'lookup> { + parent: Option<&'parent Set<'parent, 'lookup>>, + data: HashMap, lookup_internal: Option<&'lookup dyn LookupInternal>, } -impl<'parent, 'lookup> MacroSet<'parent, 'lookup> { +impl<'parent, 'lookup> Set<'parent, 'lookup> { pub fn new() -> Self { Self { parent: None, @@ -38,7 +38,7 @@ impl<'parent, 'lookup> MacroSet<'parent, 'lookup> { pub fn add_builtins(&mut self) { for (k, v) in builtins() { - self.data.insert(k.into(), (MacroSource::Builtin, v)); + self.data.insert(k.into(), (Source::Builtin, v)); } } @@ -46,7 +46,7 @@ impl<'parent, 'lookup> MacroSet<'parent, 'lookup> { for (k, v) in env::vars() { if k != "MAKEFLAGS" && k != "SHELL" { self.data - .insert(k, (MacroSource::Environment, TokenString::text(v))); + .insert(k, (Source::Environment, TokenString::text(v))); } } } @@ -61,13 +61,13 @@ impl<'parent, 'lookup> MacroSet<'parent, 'lookup> { } } - pub fn get(&self, name: &str) -> Option<&(MacroSource, TokenString)> { + pub fn get(&self, name: &str) -> Option<&(Source, TokenString)> { self.data .get(name) .or_else(|| self.parent.and_then(|parent| parent.get(name))) } - pub fn set(&mut self, name: String, source: MacroSource, text: TokenString) { + pub fn set(&mut self, name: String, source: Source, text: TokenString) { self.data.insert(name, (source, text)); } @@ -76,7 +76,7 @@ impl<'parent, 'lookup> MacroSet<'parent, 'lookup> { } // `remove` is fine, but I think for "remove-and-return" `pop` is better - pub fn pop(&mut self, name: &str) -> Option<(MacroSource, TokenString)> { + pub fn pop(&mut self, name: &str) -> Option<(Source, TokenString)> { self.data.remove(name) } @@ -121,16 +121,16 @@ impl<'parent, 'lookup> MacroSet<'parent, 'lookup> { Ok(result) } - pub fn with_lookup<'l, 's: 'l>(&'s self, lookup: &'l dyn LookupInternal) -> MacroSet<'s, 'l> { - MacroSet { + pub fn with_lookup<'l, 's: 'l>(&'s self, lookup: &'l dyn LookupInternal) -> Set<'s, 'l> { + Set { parent: Some(self), data: HashMap::new(), lookup_internal: Some(lookup), } } - pub fn with_overlay<'s>(&'s self) -> MacroSet<'s, 'lookup> { - MacroSet { + pub fn with_overlay<'s>(&'s self) -> Set<'s, 'lookup> { + Set { parent: Some(self), data: HashMap::new(), lookup_internal: None, @@ -138,7 +138,7 @@ impl<'parent, 'lookup> MacroSet<'parent, 'lookup> { } } -impl fmt::Display for MacroSet<'_, '_> { +impl fmt::Display for Set<'_, '_> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let pieces = self .data diff --git a/src/makefile/mod.rs b/src/makefile/mod.rs index d9c9b46..94a104a 100644 --- a/src/makefile/mod.rs +++ b/src/makefile/mod.rs @@ -22,9 +22,9 @@ mod target; mod token; use command_line::CommandLine; -use conditional::{ConditionalLine, ConditionalState}; +use conditional::{Line as ConditionalLine, State as ConditionalState}; use inference_rules::InferenceRule; -use r#macro::{MacroSet, MacroSource}; +use r#macro::{Set as MacroSet, Source as MacroSource}; use target::Target; use token::{tokenize, Token, TokenString}; diff --git a/src/makefile/token.rs b/src/makefile/token.rs index dee290e..85d2e96 100644 --- a/src/makefile/token.rs +++ b/src/makefile/token.rs @@ -12,6 +12,7 @@ use nom::{ Finish, IResult, }; +#[allow(clippy::module_name_repetitions)] #[derive(PartialEq, Eq, Clone, Debug)] pub struct TokenString(Vec); -- cgit v1.2.3