aboutsummaryrefslogtreecommitdiff
path: root/src/makefile/token.rs
diff options
context:
space:
mode:
authorMelody Horn <melody@boringcactus.com>2021-03-26 19:43:40 -0600
committerMelody Horn <melody@boringcactus.com>2021-03-26 19:43:40 -0600
commit45fa49be5dc2630187e78c8adec498b751d9481e (patch)
treed6af3f08f4349caa867795ef52a4a356a1d8ce76 /src/makefile/token.rs
parentabd55e36d781645566a7815c7712ded6b5cc1923 (diff)
downloadmakers-45fa49be5dc2630187e78c8adec498b751d9481e.tar.gz
makers-45fa49be5dc2630187e78c8adec498b751d9481e.zip
overhaul target handling
Diffstat (limited to 'src/makefile/token.rs')
-rw-r--r--src/makefile/token.rs49
1 files changed, 49 insertions, 0 deletions
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<Token>);
impl TokenString {
+ pub fn text(text: impl Into<String>) -> Self {
+ Self(vec![Token::Text(text.into())])
+ }
+
pub fn tokens(&self) -> impl Iterator<Item=&Token> {
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| {