From 45fa49be5dc2630187e78c8adec498b751d9481e Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Fri, 26 Mar 2021 19:43:40 -0600 Subject: overhaul target handling --- src/makefile/token.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/makefile/token.rs') diff --git a/src/makefile/token.rs b/src/makefile/token.rs index 3f69b50..4943010 100644 --- a/src/makefile/token.rs +++ b/src/makefile/token.rs @@ -1,3 +1,4 @@ +use std::fmt; use std::str::FromStr; use nom::{ @@ -14,6 +15,10 @@ use nom::{ pub struct TokenString(Vec); impl TokenString { + pub fn text(text: impl Into) -> Self { + Self(vec![Token::Text(text.into())]) + } + pub fn tokens(&self) -> impl Iterator { self.0.iter() } @@ -41,6 +46,40 @@ impl TokenString { } None } + + pub fn ends_with(&self, pattern: &str) -> bool { + match self.0.last() { + Some(Token::Text(t)) => t.ends_with(pattern), + _ => false + } + } + + pub fn strip_suffix(&mut self, suffix: &str) { + if let Some(Token::Text(t)) = self.0.last_mut() { + if let Some(x) = t.strip_suffix(suffix) { + *t = x.into() + } + } + } + + pub fn extend(&mut self, other: TokenString) { + self.0.extend(other.0); + } + + pub fn trim_start(&mut self) { + if let Some(Token::Text(t)) = self.0.last_mut() { + *t = t.trim_start().into(); + } + } +} + +impl fmt::Display for TokenString { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + for t in &self.0 { + write!(f, "{}", t)?; + } + Ok(()) + } } #[derive(PartialEq, Eq, Clone, Debug)] @@ -52,6 +91,16 @@ pub enum Token { }, } +impl fmt::Display for Token { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Token::Text(t) => write!(f, "{}", t), + Token::MacroExpansion { name, replacement: None } => write!(f, "$({})", name), + Token::MacroExpansion { name, replacement: Some((r1, r2)) } => write!(f, "$({}:{}={})", name, r1, r2), + } + } +} + fn macro_name(input: &str) -> IResult<&str, &str> { // POSIX says "periods, underscores, digits, and alphabetics from the portable character set" take_while1(|c: char| { -- cgit v1.2.3