use std::collections::HashSet;
use std::path::{Path, PathBuf};
use regex::Regex;
use crate::boolean_expression;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScriptDirective {
Command(CommandDirective),
Substitution(SubstitutionDirective),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandDirective {
pub start_line: usize,
pub end_line: usize,
pub keyword: String,
pub command: String,
}
impl CommandDirective {
pub fn new(start_line: usize, _start_of_line: usize, keyword: &str, line: &str) -> Self {
Self {
start_line,
end_line: start_line,
keyword: keyword.to_string(),
command: line.to_string(),
}
}
pub fn add_continuation(&mut self, line_number: usize, keyword: &str, line: &str) -> bool {
if self.keyword != keyword || !self.needs_continuation() {
return false;
}
let line = line.trim_end();
self.command = format!("{}{}", self.command.trim_end_matches('\\'), line);
self.end_line = line_number;
true
}
pub fn needs_continuation(&self) -> bool {
self.command.trim_end().ends_with('\\')
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SubstitutionDirective {
pub start_line: usize,
pub end_line: usize,
pub keyword: String,
pub new_subst: bool,
body: String,
name: Option<String>,
value: Option<String>,
}
impl SubstitutionDirective {
pub fn new(
start_line: usize,
keyword: &str,
new_subst: bool,
line: &str,
) -> Result<Self, String> {
let mut this = Self {
start_line,
end_line: start_line,
keyword: keyword.to_string(),
new_subst,
body: line.to_string(),
name: None,
value: None,
};
this.parse_body()?;
Ok(this)
}
pub fn add_continuation(&mut self, line_number: usize, keyword: &str, line: &str) -> bool {
if self.keyword != keyword || !self.needs_continuation() {
return false;
}
if line.trim().is_empty() {
return false;
}
self.body = format!(
"{} {}",
self.body.trim_end_matches('\\').trim_end(),
line.trim_start()
);
self.end_line = line_number;
true
}
pub fn needs_continuation(&self) -> bool {
self.body.trim_end().ends_with('\\')
}
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
pub fn value(&self) -> Option<&str> {
self.value.as_deref()
}
fn parse_body(&mut self) -> Result<(), String> {
if self.needs_continuation() {
return Ok(());
}
let mut parts = self.body.splitn(2, '=');
let raw_name = parts
.next()
.ok_or_else(|| {
format!(
"Substitution definition incomplete at line {}",
self.start_line
)
})?
.trim();
let raw_value = parts
.next()
.ok_or_else(|| {
format!(
"Substitution's definition does not contain '=' at line {}",
self.start_line
)
})?
.trim();
if raw_name.is_empty() {
return Err(format!(
"Substitution name cannot be empty at line {}",
self.start_line
));
}
if !is_valid_substitution_name(raw_name) {
return Err(format!(
"Substitution name '{raw_name}' is malformed as it must start with '%{{', end with '}}', and contain only letters, digits, '-' '_' ':'",
));
}
self.name = Some(raw_name.to_string());
self.value = Some(raw_value.to_string());
Ok(())
}
pub fn adjust_substitutions(
&self,
substitutions: &mut Vec<(String, String)>,
) -> Result<(), String> {
if self.needs_continuation() {
return Err(format!(
"Substitution is unterminated at line {}",
self.start_line
));
}
let name = self
.name
.as_ref()
.ok_or_else(|| "substitution name is missing".to_string())?;
let value = self
.value
.as_ref()
.ok_or_else(|| "substitution value is missing".to_string())?;
let existing: Vec<usize> = substitutions
.iter()
.enumerate()
.filter_map(|(idx, (pattern, _))| {
if pattern.contains(name) {
Some(idx)
} else {
None
}
})
.collect();
if self.new_subst {
if !existing.is_empty() {
return Err(format!(
"Substitution whose pattern contains '{name}' is already defined before '{}' directive at {}",
self.keyword, self.start_line
));
}
substitutions.insert(0, (name.clone(), value.clone()));
return Ok(());
}
if existing.is_empty() {
return Err(format!(
"No substitution for '{name}' is defined before '{}' directive at {}",
self.keyword, self.start_line
));
}
if existing.len() > 1 {
return Err(format!(
"Multiple substitutions whose patterns contain '{name}' are defined before '{}' directive at {}",
self.keyword, self.start_line
));
}
let existing_idx = existing[0];
if substitutions[existing_idx].0 != *name {
return Err(format!(
"Existing substitution whose pattern contains '{name}' does not match exactly '{}': {}",
name, substitutions[existing_idx].0
));
}
substitutions[existing_idx] = (name.clone(), value.clone());
Ok(())
}
pub fn location(&self) -> String {
if self.start_line == self.end_line {
format!("line {}", self.start_line)
} else {
format!("from line {} to {}", self.start_line, self.end_line)
}
}
}
fn is_valid_substitution_name(text: &str) -> bool {
Regex::new(r"^%\{[_a-zA-Z][-_:0-9a-zA-Z]*\}$")
.expect("valid regex for substitution names")
.is_match(text)
}
fn substitute_if_else(text: &str, conditions: &HashSet<String>) -> Result<String, String> {
if !text.contains("%if ") {
return Ok(text.to_string());
}
fn try_parse_if_cond(text: &str) -> Result<Option<(String, String)>, String> {
if !text.starts_with("%if ") {
return Ok(None);
}
let rest = &text[4..];
let open = rest
.find("%{")
.ok_or_else(|| "'%{' is missing for %if substitution".to_string())?;
let condition = rest[..open].trim().to_string();
let tail = rest[open + 2..].to_string();
Ok(Some((condition, tail)))
}
fn try_parse_else(text: &str) -> Result<(bool, String), String> {
let trimmed = text.trim_start();
if !trimmed.starts_with("%else") {
return Ok((false, text.to_string()));
}
let mut after = trimmed[5..].trim_start();
if !after.starts_with("%{") {
return Err("'%{' is missing for %else substitution".to_string());
}
after = &after[2..];
Ok((true, after.to_string()))
}
fn try_parse_end(text: &str) -> (bool, String) {
if let Some(stripped) = text.strip_prefix("%}") {
(true, stripped.to_string())
} else {
(false, text.to_string())
}
}
fn parse_text(text: &str, nested: bool) -> (String, String) {
let len = text.len();
let next_if = text.find("%if ").unwrap_or(len);
let end_token = if nested { text.find("%}") } else { None };
let stop = match end_token {
Some(end_if_like) => next_if.min(end_if_like),
None => next_if,
};
if stop >= text.len() {
return (text.to_string(), String::new());
}
(text[..stop].to_string(), text[stop..].to_string())
}
fn parse_recursive(
text: &str,
nested: bool,
conditions: &HashSet<String>,
) -> Result<(String, String), String> {
let mut result = String::new();
let mut rest = text.to_string();
while !rest.is_empty() {
if nested {
let (ended, _) = try_parse_end(&rest);
if ended {
return Ok((result, rest));
}
}
if let Some((cond, ln)) = try_parse_if_cond(&rest)? {
let (if_true, mut ln) = parse_recursive(&ln, true, conditions)?;
let (ended, tail) = try_parse_end(&ln);
if !ended {
return Err("'%}' is missing for %if substitution".to_string());
}
ln = tail;
let (has_else, ln_else) = try_parse_else(&ln)?;
let (if_false, remaining) = if has_else {
let (branch_else, tail2) = parse_recursive(&ln_else, true, conditions)?;
let (ended_else, after_else) = try_parse_end(&tail2);
if !ended_else {
return Err("'%}' is missing for %else substitution".to_string());
}
(branch_else, after_else)
} else {
(String::new(), ln)
};
let taken = if boolean_expression::evaluate(&cond, conditions).unwrap_or(false) {
if_true
} else {
if_false
};
result.push_str(&taken);
rest = remaining;
continue;
}
let (text, tail) = parse_text(&rest, nested);
result.push_str(&text);
rest = tail;
}
Ok((result, String::new()))
}
let (result, rest) = parse_recursive(text, false, conditions)?;
if !rest.is_empty() {
return Err("'%}' is extra at end of conditional substitution".to_string());
}
Ok(result)
}
fn apply_substitutions_once(
text: &str,
substitutions: &[(String, String)],
hide_percents: bool,
) -> Result<String, String> {
let mut hidden = text.to_string();
if hide_percents {
hidden = hidden.replace("%%", "#_LIT_LITERAL_PERCENT_#");
}
let processed = substitutions
.iter()
.try_fold(hidden, |acc, (pattern, replacement)| {
let pattern = regex::escape(pattern);
let re = Regex::new(&pattern).map_err(|error| {
format!("cannot compile substitution pattern '{pattern}': {error}")
})?;
Ok::<String, String>(
re.replace_all(&acc, replacement.replace('\\', r"\\"))
.to_string(),
)
})?;
if hide_percents {
Ok(processed.replace("#_LIT_LITERAL_PERCENT_#", "%"))
} else {
Ok(processed)
}
}
pub fn apply_substitutions(
directives: &[ScriptDirective],
substitutions: &mut Vec<(String, String)>,
conditions: &HashSet<String>,
recursion_limit: Option<usize>,
) -> Result<Vec<String>, String> {
let mut output = Vec::new();
for directive in directives {
match directive {
ScriptDirective::Substitution(subst) => {
subst.adjust_substitutions(substitutions)?;
}
ScriptDirective::Command(command) => {
let mut line = substitute_line_numbers(command.start_line, &command.command);
line = substitute_if_else(&line, conditions)?;
let mut current = apply_substitutions_once(&line, substitutions, true)?;
if let Some(limit) = recursion_limit {
let mut prev = line;
let mut steps = 0usize;
while current != prev && steps < limit {
prev = current.clone();
current = apply_substitutions_once(&prev, substitutions, true)?;
steps += 1;
}
if current != prev {
return Err(format!(
"Recursive substitution of '{}' did not complete in provided recursion limit ({limit})",
command.command
));
}
}
output.push(current.trim().to_string());
}
}
}
Ok(output)
}
pub fn substitute_line_numbers(line: usize, text: &str) -> String {
let mut result = text.replace("%(line)", &line.to_string());
let re = Regex::new(r"%\(line\s*([+-])\s*(\d+)\)").expect("line arithmetic regex is valid");
loop {
let replaced = re
.replace_all(&result, |captures: ®ex::Captures| {
let sign = captures.get(1).map(|v| v.as_str()).unwrap_or("+");
let value = captures
.get(2)
.and_then(|v| v.as_str().parse::<isize>().ok())
.unwrap_or(0);
let current = line as isize;
if sign == "+" {
(current + value).to_string()
} else {
(current - value).to_string()
}
})
.to_string();
if replaced == result {
break;
}
result = replaced;
}
result
}
fn colon_normalize_path(path: &str, normalize_slashes: bool) -> String {
if !normalize_slashes {
return path.to_string();
}
if path.len() >= 2 && path.as_bytes().get(1) == Some(&b':') {
path.to_lowercase().replace('\\', "/")
} else if path.starts_with('/') {
path.trim_start_matches('/').to_string()
} else {
path.to_string()
}
}
pub fn get_default_substitutions(
source_path: &Path,
tmp_dir: &Path,
tmp_base: &Path,
normalize_slashes: bool,
extra: &[(String, String)],
) -> Vec<(String, String)> {
let mut source = source_path.to_path_buf();
let mut sourcedir = source
.parent()
.map(Path::to_path_buf)
.unwrap_or_else(|| PathBuf::from("."));
let mut tmp_dir = tmp_dir.to_path_buf();
let mut tmp_base = tmp_base.to_path_buf();
if normalize_slashes {
source = PathBuf::from(source.to_string_lossy().to_string().replace('\\', "/"));
sourcedir = PathBuf::from(sourcedir.to_string_lossy().replace('\\', "/"));
tmp_dir = PathBuf::from(tmp_dir.to_string_lossy().replace('\\', "/"));
tmp_base = PathBuf::from(tmp_base.to_string_lossy().replace('\\', "/"));
}
let source_path = source.to_string_lossy().to_string();
let sourcedir = sourcedir.to_string_lossy().to_string();
let tmp_dir = tmp_dir.to_string_lossy().to_string();
let tmp_base_name = tmp_base
.file_name()
.map(|v| v.to_string_lossy().to_string())
.unwrap_or_default();
let tmp_name = format!("{}.tmp", tmp_base.to_string_lossy());
let mut out = Vec::new();
out.extend_from_slice(extra);
out.push((
"%{pathsep}".to_string(),
std::path::MAIN_SEPARATOR.to_string(),
));
out.push(("%basename_t".to_string(), tmp_base_name.clone()));
out.push((
"%{s:basename}".to_string(),
Path::new(&source_path)
.file_name()
.unwrap_or_default()
.to_string_lossy()
.to_string(),
));
out.push(("%{t:stem}".to_string(), tmp_base_name.clone()));
let source_root = source_path
.chars()
.next()
.and_then(|_| Path::new(&source_path).components().next())
.map(|c| c.as_os_str().to_string_lossy().to_string())
.unwrap_or_default();
let tmp_root = tmp_dir
.chars()
.next()
.and_then(|_| Path::new(&tmp_dir).components().next())
.map(|c| c.as_os_str().to_string_lossy().to_string())
.unwrap_or_default();
out.push(("%{fs-src-root}".to_string(), source_root));
out.push(("%{fs-tmp-root}".to_string(), tmp_root));
out.push((
"%{fs-sep}".to_string(),
if normalize_slashes {
"/".to_string()
} else {
std::path::MAIN_SEPARATOR.to_string()
},
));
out.push(("%t".to_string(), tmp_name.clone()));
out.push(("%T".to_string(), tmp_dir.clone()));
out.push(("%/T".to_string(), tmp_dir.replace('\\', "/")));
out.push(("%/et".to_string(), tmp_name.replace('\\', "/")));
for letter in ["s", "S", "p", "t", "T"] {
let value = match letter {
"s" => source_path.clone(),
"S" => sourcedir.clone(),
"p" => sourcedir.clone(),
"t" => tmp_name.clone(),
"T" => tmp_dir.clone(),
_ => continue,
};
out.push((format!("%{letter}"), value.clone()));
out.push((format!("%/{letter}"), value.replace('\\', "/")));
let real = Path::new(&value)
.canonicalize()
.unwrap_or_else(|_| PathBuf::from(&value))
.to_string_lossy()
.to_string();
out.push((format!("%{{{letter}:real}}"), real.clone()));
out.push((format!("%{{/{letter}:real}}"), real.replace('\\', "/")));
out.push((
format!("%{{/{letter}:regex_replacement}}"),
value
.replace('\\', "/")
.replace('@', r"\@")
.replace('&', r"\&"),
));
out.push((
format!("%:{letter}"),
colon_normalize_path(&value, normalize_slashes),
));
}
out
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use std::path::Path;
use super::{
apply_substitutions, get_default_substitutions, substitute_line_numbers, CommandDirective,
ScriptDirective, SubstitutionDirective,
};
#[test]
fn define_and_redefine_directives() {
let mut substitutions = vec![("%{X}".to_string(), "x".to_string())];
let directives = vec![
ScriptDirective::Substitution(
SubstitutionDirective::new(1, "DEFINE:", true, "%{name}=value").unwrap(),
),
ScriptDirective::Command(CommandDirective {
start_line: 2,
end_line: 2,
keyword: "RUN:".into(),
command: "echo %{name}".into(),
}),
];
let res = apply_substitutions(&directives, &mut substitutions, &HashSet::new(), None)
.expect("substitutions should be applied");
assert_eq!(res, vec!["echo value"]);
}
#[test]
fn conditional_substitution_selects_true_and_false_branches() {
let mut substitutions = vec![("%{tool}".to_string(), "sh".to_string())];
let mut conditions = HashSet::new();
conditions.insert("linux".to_string());
conditions.insert("linux".to_string());
let directives = vec![ScriptDirective::Command(CommandDirective {
start_line: 1,
end_line: 1,
keyword: "RUN:".into(),
command: "%if linux %{echo linux%} %else %{echo not-linux%}".into(),
})];
let result = apply_substitutions(&directives, &mut substitutions, &conditions, None)
.expect("conditional substitution should resolve");
assert_eq!(result, vec!["echo linux"]);
let conditions = HashSet::new();
let directives = vec![ScriptDirective::Command(CommandDirective {
start_line: 1,
end_line: 1,
keyword: "RUN:".into(),
command: "%if windows %{echo windows%} %else %{echo fallback%}".into(),
})];
let result = apply_substitutions(&directives, &mut substitutions, &conditions, None)
.expect("conditional substitution should resolve");
assert_eq!(result, vec!["echo fallback"]);
}
#[test]
fn recursive_substitution_honors_limit() {
let directives = vec![
ScriptDirective::Substitution(
SubstitutionDirective::new(1, "DEFINE:", true, "%{A}=%{B}").unwrap(),
),
ScriptDirective::Substitution(
SubstitutionDirective::new(2, "DEFINE:", true, "%{B}=value").unwrap(),
),
ScriptDirective::Command(CommandDirective {
start_line: 3,
end_line: 3,
keyword: "RUN:".into(),
command: "echo %{A}".into(),
}),
];
let mut substitutions = Vec::new();
let limited =
apply_substitutions(&directives, &mut substitutions, &HashSet::new(), Some(0));
let error =
limited.expect_err("recursive expansion limit 0 should reject additional expansion");
assert!(error.contains("did not complete"));
let mut substitutions = Vec::new();
let full = apply_substitutions(&directives, &mut substitutions, &HashSet::new(), Some(2))
.expect("recursive substitution should fully expand");
assert_eq!(full, vec!["echo value"]);
}
#[test]
fn recursive_substitution_can_error_when_over_limit() {
let mut substitutions = Vec::new();
let directives = vec![
ScriptDirective::Substitution(
SubstitutionDirective::new(1, "DEFINE:", true, "%{A}=%{B}").unwrap(),
),
ScriptDirective::Substitution(
SubstitutionDirective::new(2, "DEFINE:", true, "%{B}=%{A}").unwrap(),
),
ScriptDirective::Command(CommandDirective {
start_line: 3,
end_line: 3,
keyword: "RUN:".into(),
command: "echo %{A}".into(),
}),
];
let err = apply_substitutions(&directives, &mut substitutions, &HashSet::new(), Some(0))
.expect_err("infinite recursion should be rejected");
assert!(err.contains("did not complete"));
}
#[test]
fn line_number_substitution() {
assert_eq!(
substitute_line_numbers(10, "echo line-%(line)"),
"echo line-10"
);
assert_eq!(
substitute_line_numbers(10, "echo line-%(line + 3)"),
"echo line-13"
);
assert_eq!(
substitute_line_numbers(10, "echo line-%(line - 3)"),
"echo line-7"
);
}
#[test]
fn default_substitutions_have_tmp_and_tmp_dir() {
let s = get_default_substitutions(
Path::new("/tmp/source/foo.mlir"),
Path::new("/tmp/source/Output"),
Path::new("/tmp/source/Output/foo.mlir"),
false,
&[],
);
let names: HashSet<_> = s.iter().map(|(k, _)| k.as_str()).collect();
assert!(names.contains("%t"));
assert!(names.contains("%T"));
assert!(names.contains("%/T"));
assert!(names.contains("%/et"));
let et = s
.into_iter()
.find(|(key, _)| key == "%/et")
.map(|(_, value)| value)
.expect("must contain %/et");
assert_eq!(et, "/tmp/source/Output/foo.mlir.tmp");
}
}