aboutsummaryrefslogtreecommitdiff
path: root/src/makefile/token.rs
diff options
context:
space:
mode:
authorzseri <zseri.devel@ytrizja.de>2021-03-27 07:42:28 +0100
committerzseri <zseri.devel@ytrizja.de>2021-03-27 07:42:28 +0100
commit27c119a252c01a621368624a9ab3e8efd9f18faf (patch)
tree89b303193d2d3ddf9aff2306de7e8d02449d7901 /src/makefile/token.rs
parent64790d036be21d9fc76bcec2102d2e1865ccd84f (diff)
downloadmakers-27c119a252c01a621368624a9ab3e8efd9f18faf.tar.gz
makers-27c119a252c01a621368624a9ab3e8efd9f18faf.zip
cargo fmt
Diffstat (limited to 'src/makefile/token.rs')
-rw-r--r--src/makefile/token.rs164
1 files changed, 105 insertions, 59 deletions
diff --git a/src/makefile/token.rs b/src/makefile/token.rs
index 8bac34f..720055f 100644
--- a/src/makefile/token.rs
+++ b/src/makefile/token.rs
@@ -2,13 +2,13 @@ use std::fmt;
use std::str::FromStr;
use nom::{
- Finish, IResult,
branch::alt,
bytes::complete::{tag, take_till1, take_while1},
character::complete::anychar,
combinator::{all_consuming, map, opt, verify},
multi::many1,
sequence::{delimited, pair, preceded, separated_pair},
+ Finish, IResult,
};
#[derive(PartialEq, Eq, Clone, Debug)]
@@ -19,7 +19,7 @@ impl TokenString {
Self(vec![Token::Text(text.into())])
}
- pub fn tokens(&self) -> impl Iterator<Item=&Token> {
+ pub fn tokens(&self) -> impl Iterator<Item = &Token> {
self.0.iter()
}
@@ -50,7 +50,7 @@ impl TokenString {
pub fn ends_with(&self, pattern: &str) -> bool {
match self.0.last() {
Some(Token::Text(t)) => t.ends_with(pattern),
- _ => false
+ _ => false,
}
}
@@ -95,25 +95,34 @@ 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),
+ 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| {
- c == '.' || c == '_' || c.is_alphanumeric()
- })(input)
+ take_while1(|c: char| c == '.' || c == '_' || c.is_alphanumeric())(input)
}
fn macro_expansion_body<'a>(end: char) -> impl FnMut(&'a str) -> IResult<&'a str, Token> {
- let subst = preceded(tag(":"), separated_pair(tokens_but_not('='), tag("="), tokens_but_not(end)));
- map(
- pair(macro_name, opt(subst)),
- |(name, replacement)| Token::MacroExpansion { name: name.into(), replacement },
- )
+ let subst = preceded(
+ tag(":"),
+ separated_pair(tokens_but_not('='), tag("="), tokens_but_not(end)),
+ );
+ map(pair(macro_name, opt(subst)), |(name, replacement)| {
+ Token::MacroExpansion {
+ name: name.into(),
+ replacement,
+ }
+ })
}
fn parens_macro_expansion(input: &str) -> IResult<&str, Token> {
@@ -139,7 +148,11 @@ fn tiny_macro_expansion(input: &str) -> IResult<&str, Token> {
}
fn macro_expansion(input: &str) -> IResult<&str, Token> {
- alt((tiny_macro_expansion, parens_macro_expansion, braces_macro_expansion))(input)
+ alt((
+ tiny_macro_expansion,
+ parens_macro_expansion,
+ braces_macro_expansion,
+ ))(input)
}
fn text(input: &str) -> IResult<&str, Token> {
@@ -147,7 +160,9 @@ fn text(input: &str) -> IResult<&str, Token> {
}
fn text_but_not<'a>(end: char) -> impl FnMut(&'a str) -> IResult<&'a str, Token> {
- map(take_till1(move |c| c == '$' || c == end), |x: &str| Token::Text(x.into()))
+ map(take_till1(move |c| c == '$' || c == end), |x: &str| {
+ Token::Text(x.into())
+ })
}
fn single_token(input: &str) -> IResult<&str, Token> {
@@ -167,7 +182,10 @@ fn tokens(input: &str) -> IResult<&str, TokenString> {
}
fn tokens_but_not<'a>(end: char) -> impl FnMut(&'a str) -> IResult<&'a str, TokenString> {
- alt((map(many1(single_token_but_not(end)), TokenString), empty_tokens))
+ alt((
+ map(many1(single_token_but_not(end)), TokenString),
+ empty_tokens,
+ ))
}
fn full_text_tokens(input: &str) -> IResult<&str, TokenString> {
@@ -185,15 +203,13 @@ impl FromStr for TokenString {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
- full_text_tokens(s).finish()
- .map(|(_, x)| x)
- .map_err(|_| ())
+ full_text_tokens(s).finish().map(|(_, x)| x).map_err(|_| ())
}
}
#[cfg(test)]
mod test {
- use super::{Token, TokenString, tokenize};
+ use super::{tokenize, Token, TokenString};
impl From<Vec<Token>> for TokenString {
fn from(x: Vec<Token>) -> Self {
@@ -206,13 +222,21 @@ mod test {
}
fn token_macro_expansion(name: impl Into<String>) -> Token {
- Token::MacroExpansion { name: name.into(), replacement: None }
+ Token::MacroExpansion {
+ name: name.into(),
+ replacement: None,
+ }
}
- fn token_macro_expansion_replacement(name: impl Into<String>,
- subst1: impl Into<TokenString>,
- subst2: impl Into<TokenString>) -> Token {
- Token::MacroExpansion { name: name.into(), replacement: Some((subst1.into(), subst2.into())) }
+ fn token_macro_expansion_replacement(
+ name: impl Into<String>,
+ subst1: impl Into<TokenString>,
+ subst2: impl Into<TokenString>,
+ ) -> Token {
+ Token::MacroExpansion {
+ name: name.into(),
+ replacement: Some((subst1.into(), subst2.into())),
+ }
}
#[test]
@@ -226,60 +250,82 @@ mod test {
fn no_replacement() {
let text = "This is a $Q sentence! There are $(BORING) macros in it at ${YEET}!";
let tokens = tokenize(text);
- assert_eq!(tokens, TokenString(vec![
- token_text("This is a "),
- token_macro_expansion("Q"),
- token_text(" sentence! There are "),
- token_macro_expansion("BORING"),
- token_text(" macros in it at "),
- token_macro_expansion("YEET"),
- token_text("!"),
- ]));
+ assert_eq!(
+ tokens,
+ TokenString(vec![
+ token_text("This is a "),
+ token_macro_expansion("Q"),
+ token_text(" sentence! There are "),
+ token_macro_expansion("BORING"),
+ token_text(" macros in it at "),
+ token_macro_expansion("YEET"),
+ token_text("!"),
+ ])
+ );
}
#[test]
fn escaped() {
let text = "This costs $$2 to run, which isn't ideal";
let tokens = tokenize(text);
- assert_eq!(tokens, TokenString(vec![
- token_text("This costs "),
- token_text("$"),
- token_text("2 to run, which isn't ideal"),
- ]));
+ assert_eq!(
+ tokens,
+ TokenString(vec![
+ token_text("This costs "),
+ token_text("$"),
+ token_text("2 to run, which isn't ideal"),
+ ])
+ );
}
#[test]
fn replacement() {
let text = "Can I get a $(DATA:.c=.oof) in this ${SWAG:.yolo=}";
let tokens = tokenize(text);
- assert_eq!(tokens, TokenString(vec![
- token_text("Can I get a "),
- token_macro_expansion_replacement("DATA", vec![token_text(".c")], vec![token_text(".oof")]),
- token_text(" in this "),
- token_macro_expansion_replacement("SWAG", vec![token_text(".yolo")], vec![token_text("")]),
- ]));
+ assert_eq!(
+ tokens,
+ TokenString(vec![
+ token_text("Can I get a "),
+ token_macro_expansion_replacement(
+ "DATA",
+ vec![token_text(".c")],
+ vec![token_text(".oof")]
+ ),
+ token_text(" in this "),
+ token_macro_expansion_replacement(
+ "SWAG",
+ vec![token_text(".yolo")],
+ vec![token_text("")]
+ ),
+ ])
+ );
}
#[test]
fn hell() {
let text = "$(OOF:${ouch:hi=hey} there=$(owie:$(my)=${bones})), bro.";
let tokens = tokenize(text);
- assert_eq!(tokens, TokenString(vec![
- token_macro_expansion_replacement(
- "OOF",
- vec![
- token_macro_expansion_replacement("ouch", vec![token_text("hi")], vec![token_text("hey")]),
- token_text(" there"),
- ],
- vec![
- token_macro_expansion_replacement(
+ assert_eq!(
+ tokens,
+ TokenString(vec![
+ token_macro_expansion_replacement(
+ "OOF",
+ vec![
+ token_macro_expansion_replacement(
+ "ouch",
+ vec![token_text("hi")],
+ vec![token_text("hey")]
+ ),
+ token_text(" there"),
+ ],
+ vec![token_macro_expansion_replacement(
"owie",
vec![token_macro_expansion("my")],
vec![token_macro_expansion("bones")],
- ),
- ],
- ),
- token_text(", bro."),
- ]));
+ ),],
+ ),
+ token_text(", bro."),
+ ])
+ );
}
}