use std::collections::HashSet;
use std::ops::Deref;
use std::path::{Component, Path, PathBuf};
use regex::{NoExpand, Regex};
use crate::boolean_expression;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum ScriptDirective {
Command(CommandDirective),
Substitution(SubstitutionDirective),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct CommandDirective {
pub(crate) start_line: usize,
end_line: usize,
pub(crate) keyword: String,
pub(crate) command: String,
}
impl CommandDirective {
pub(crate) fn new(start_line: usize, keyword: &str, line: &str) -> Self {
Self {
start_line,
end_line: start_line,
keyword: keyword.to_string(),
command: line.trim_end().to_string(),
}
}
pub(crate) fn add_continuation(
&mut self,
line_number: usize,
keyword: &str,
line: &str,
) -> bool {
if self.keyword != keyword || !self.needs_continuation() {
return false;
}
let previous = self.command.trim_end();
let line = line.trim_end();
self.command = format!("{}{}", &previous[..previous.len() - 1], line);
self.end_line = line_number;
true
}
pub(crate) fn needs_continuation(&self) -> bool {
self.command.trim_end().ends_with('\\')
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct SubstitutionDirective {
pub(crate) start_line: usize,
end_line: usize,
pub(crate) keyword: String,
new_subst: bool,
body: String,
name: Option<String>,
value: Option<String>,
}
impl SubstitutionDirective {
pub(crate) fn new(
start_line: usize,
keyword: &str,
new_subst: bool,
line: &str,
) -> Result<Self, Error> {
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(crate) fn add_continuation(
&mut self,
line_number: usize,
keyword: &str,
line: &str,
) -> Result<bool, Error> {
if self.keyword != keyword || !self.needs_continuation() {
return Ok(false);
}
if line.trim().is_empty() {
return Err(Error::new("Substitution's continuation is empty"));
}
let previous = self.body.trim_end();
self.body = format!(
"{} {}",
previous[..previous.len() - 1].trim_end(),
line.trim_start()
);
self.end_line = line_number;
self.parse_body()?;
Ok(true)
}
pub(crate) fn needs_continuation(&self) -> bool {
self.body.trim_end().ends_with('\\')
}
fn parse_body(&mut self) -> Result<(), Error> {
if self.needs_continuation() {
return Ok(());
}
let mut parts = self.body.splitn(2, '=');
let raw_name = parts
.next()
.ok_or_else(|| {
Error::new(format!(
"Substitution definition incomplete at line {}",
self.start_line
))
})?
.trim();
let raw_value = parts
.next()
.ok_or_else(|| {
Error::new(format!(
"Substitution's definition does not contain '=' at line {}",
self.start_line
))
})?
.trim();
if raw_name.is_empty() {
return Err(Error::new(format!(
"Substitution name cannot be empty at line {}",
self.start_line
)));
}
if !Regex::new(r"^%\{[_a-zA-Z][-_:0-9a-zA-Z]*\}$")
.expect("valid regex for substitution names")
.is_match(raw_name)
{
return Err(Error::new(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(crate) fn adjust_substitutions(
&self,
substitutions: &mut Vec<(String, String)>,
) -> Result<(), Error> {
if self.needs_continuation() {
return Err(Error::new(format!(
"Substitution is unterminated at line {}",
self.start_line
)));
}
let name = self
.name
.as_ref()
.ok_or_else(|| Error::new("substitution name is missing"))?;
let value = self
.value
.as_ref()
.ok_or_else(|| Error::new("substitution value is missing"))?;
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(Error::new(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(Error::new(format!(
"No substitution for '{name}' is defined before '{}' directive at {}",
self.keyword, self.start_line
)));
}
if existing.len() > 1 {
return Err(Error::new(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(Error::new(format!(
"Existing substitution whose pattern contains '{name}' does not match exactly '{}': {}",
name, substitutions[existing_idx].0
)));
}
substitutions[existing_idx] = (name.clone(), value.clone());
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Error(String);
impl Error {
fn new(message: impl Into<String>) -> Self {
Self(message.into())
}
}
impl Deref for Error {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for Error {}
fn substitute_if_else(text: &str, conditions: &HashSet<String>) -> Result<String, Error> {
if !text.contains("%if ") {
return Ok(text.to_string());
}
fn try_parse_if_cond(text: &str) -> Result<Option<(String, String)>, Error> {
if !text.starts_with("%if ") {
return Ok(None);
}
let rest = &text[4..];
let open = rest
.find("%{")
.ok_or_else(|| Error::new("'%{' is missing for %if substitution"))?;
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), Error> {
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(Error::new("'%{' is missing for %else substitution"));
}
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), Error> {
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(Error::new("'%}' is missing for %if substitution"));
}
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(Error::new("'%}' is missing for %else substitution"));
}
(branch_else, after_else)
} else {
(String::new(), ln)
};
let taken = if boolean_expression::evaluate(&cond, conditions)
.map_err(|error| Error::new(error.to_string()))?
{
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(Error::new(
"'%}' is extra at end of conditional substitution",
));
}
Ok(result)
}
const PERCENT_MARKER: &str = "#_LIT_LITERAL_PERCENT_#";
fn escape_percents(text: &str) -> String {
text.replace("%%", PERCENT_MARKER)
}
fn compile_substitution_pattern(pattern: &str) -> Result<Regex, regex::Error> {
let mut translated = String::with_capacity(pattern.len());
let bytes = pattern.as_bytes();
let mut index = 0;
let mut in_character_class = false;
let mut character_class_has_content = false;
let mut character_class_at_start = false;
while index < bytes.len() {
if bytes[index] == b'\\' {
translated.push('\\');
index += 1;
if index < bytes.len() {
let character = pattern[index..]
.chars()
.next()
.expect("index is at a character boundary");
translated.push(character);
index += character.len_utf8();
}
if in_character_class {
character_class_has_content = true;
character_class_at_start = false;
}
continue;
}
if bytes[index] == b'[' && !in_character_class {
in_character_class = true;
character_class_has_content = false;
character_class_at_start = true;
translated.push('[');
index += 1;
continue;
}
if in_character_class && character_class_at_start && bytes[index] == b'^' {
character_class_at_start = false;
translated.push('^');
index += 1;
continue;
}
if bytes[index] == b']' && in_character_class && character_class_has_content {
in_character_class = false;
translated.push(']');
index += 1;
continue;
}
if in_character_class {
character_class_has_content = true;
character_class_at_start = false;
}
if bytes[index] == b'{' && !in_character_class {
let mut end = index + 1;
let lower_start = end;
while end < bytes.len() && bytes[end].is_ascii_digit() {
end += 1;
}
let has_lower = end > lower_start;
let mut has_upper = false;
let mut has_comma = false;
if end < bytes.len() && bytes[end] == b',' {
has_comma = true;
end += 1;
let upper_start = end;
while end < bytes.len() && bytes[end].is_ascii_digit() {
end += 1;
}
has_upper = end > upper_start;
}
if end < bytes.len() && bytes[end] == b'}' && (has_lower || has_upper) {
if has_comma && !has_lower {
translated.push_str("{0,");
translated.push_str(&pattern[lower_start + 1..end]);
translated.push('}');
} else {
translated.push_str(&pattern[index..=end]);
}
index = end + 1;
continue;
}
translated.push_str(r"\{");
index += 1;
continue;
}
if bytes[index] == b'}' && !in_character_class {
translated.push_str(r"\}");
index += 1;
continue;
}
let character = pattern[index..]
.chars()
.next()
.expect("index is at a character boundary");
translated.push(character);
index += character.len_utf8();
}
Regex::new(&translated)
}
fn apply_substitutions_once(
text: &str,
substitutions: &[(String, String)],
conditions: &HashSet<String>,
) -> Result<String, Error> {
let mut processed = escape_percents(text);
processed = substitute_if_else(&processed, conditions)?;
for (pattern, replacement) in substitutions {
processed = escape_percents(&processed);
let regex = compile_substitution_pattern(pattern).map_err(|source| {
Error::new(format!(
"cannot compile substitution pattern '{pattern}': {source}"
))
})?;
processed = regex
.replace_all(&processed, NoExpand(replacement))
.into_owned();
}
Ok(processed.trim().to_string())
}
pub(crate) fn apply_substitutions(
directives: &[ScriptDirective],
substitutions: &mut Vec<(String, String)>,
conditions: &HashSet<String>,
recursion_limit: Option<usize>,
) -> Result<Vec<String>, Error> {
let mut output = Vec::new();
for directive in directives {
match directive {
ScriptDirective::Substitution(subst) => {
subst.adjust_substitutions(substitutions)?;
}
ScriptDirective::Command(command) => {
let line = substitute_line_numbers(command.start_line, &command.command);
let mut current = apply_substitutions_once(&line, substitutions, conditions)?;
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, conditions)?;
steps += 1;
}
if current != prev {
return Err(Error::new(format!(
"Recursive substitution of '{}' did not complete in provided recursion limit ({limit})",
command.command
)));
}
}
output.push(current.replace(PERCENT_MARKER, "%"));
}
}
}
Ok(output)
}
pub(crate) fn substitute_line_numbers(line: usize, text: &str) -> String {
let mut result = text.replace("%(line)", &line.to_string());
let re = Regex::new(r"%\(line *([+-]) *(\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::<i128>().ok())
.unwrap_or(0);
let current = line as i128;
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) -> String {
#[cfg(windows)]
{
let normalized = path.replace('\\', "/");
if normalized.as_bytes().get(1) == Some(&b':') {
return format!("{}{}", &normalized[..1], &normalized[2..]);
}
normalized
}
#[cfg(not(windows))]
{
path.strip_prefix('/').unwrap_or(path).to_string()
}
}
fn path_anchor(path: &str, normalize_slashes: bool) -> String {
let mut components = Path::new(path).components();
let mut anchor = match components.next() {
Some(Component::Prefix(prefix)) => prefix.as_os_str().to_string_lossy().to_string(),
Some(Component::RootDir) => std::path::MAIN_SEPARATOR.to_string(),
_ => return String::new(),
};
if matches!(components.next(), Some(Component::RootDir)) {
anchor.push(std::path::MAIN_SEPARATOR);
}
if normalize_slashes {
anchor = anchor.replace('\\', "/");
}
anchor
}
pub(crate) 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 source_base_name = Path::new(&source_path)
.file_name()
.unwrap_or_default()
.to_string_lossy()
.to_string();
let source_stem = Path::new(&source_base_name)
.file_stem()
.unwrap_or_default()
.to_string_lossy()
.to_string();
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(),
if cfg!(windows) { ";" } else { ":" }.to_string(),
));
out.push(("%basename_t".to_string(), tmp_base_name.clone()));
out.push(("%{s:basename}".to_string(), source_base_name));
out.push(("%{s:stem}".to_string(), source_stem));
out.push(("%{t:stem}".to_string(), tmp_base_name.clone()));
let source_root = path_anchor(&sourcedir, normalize_slashes);
let tmp_root = path_anchor(&tmp_base.to_string_lossy(), normalize_slashes);
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(("%/et".to_string(), tmp_name.replace('\\', r"\\\\\\\\")));
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)));
}
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");
}
}