litnik 0.2.1

An embedded Lit and FileCheck runner for compiler test suites.
Documentation
use std::collections::{HashMap, HashSet};
use std::ops::Deref;
use std::path::Path;

use crate::boolean_expression;
use crate::substitution::{CommandDirective, ScriptDirective, SubstitutionDirective};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ParserKind {
    Command,
    BooleanExpr,
    Integer,
    Tag,
    Define,
    Redefine,
}

#[derive(Debug, Clone)]
struct ParserState {
    kind: ParserKind,
    values: Vec<String>,
}

impl ParserState {
    fn new(kind: ParserKind) -> Self {
        Self {
            kind,
            values: Vec::new(),
        }
    }

    fn append<I>(&mut self, items: I)
    where
        I: IntoIterator<Item = String>,
    {
        self.values.extend(items);
    }
}

#[derive(Debug, Clone)]
pub(crate) struct ParsedIntegrated {
    pub(crate) directives: Vec<ScriptDirective>,
    pub(crate) xfails: Vec<String>,
    pub(crate) requires: Vec<String>,
    pub(crate) unsupported: Vec<String>,
    pub(crate) allow_retries: Option<usize>,
}

#[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 {}

pub(crate) fn parse_integrated_test_script(path: &Path) -> Result<ParsedIntegrated, Error> {
    let data = std::fs::read(path).map_err(|error| Error::new(error.to_string()))?;

    let keywords = [
        "RUN:",
        "XFAIL:",
        "REQUIRES:",
        "UNSUPPORTED:",
        "ALLOW_RETRIES:",
        "END.",
        "DEFINE:",
        "REDEFINE:",
    ];

    let mut parsers: HashMap<&str, ParserState> = HashMap::from([
        ("RUN:", ParserState::new(ParserKind::Command)),
        ("XFAIL:", ParserState::new(ParserKind::BooleanExpr)),
        ("REQUIRES:", ParserState::new(ParserKind::BooleanExpr)),
        ("UNSUPPORTED:", ParserState::new(ParserKind::BooleanExpr)),
        ("ALLOW_RETRIES:", ParserState::new(ParserKind::Integer)),
        ("END.", ParserState::new(ParserKind::Tag)),
        ("DEFINE:", ParserState::new(ParserKind::Define)),
        ("REDEFINE:", ParserState::new(ParserKind::Redefine)),
    ]);

    let mut directives = Vec::new();
    let mut end_reached = false;

    for (line_no, raw_line) in data.split(|byte| *byte == b'\n').enumerate() {
        let line_end = raw_line
            .iter()
            .rposition(|byte| *byte != b'\r')
            .map_or(0, |position| position + 1);
        let line = &raw_line[..line_end];
        let line_number = line_no + 1;

        if let Some((offset, keyword)) = find_next_keyword(line, &keywords) {
            let line_tail =
                std::str::from_utf8(&line[offset + keyword.len()..]).map_err(|error| {
                    Error::new(format!(
                        "{} directive on line {} is not valid UTF-8: {error}",
                        keyword, line_number
                    ))
                })?;

            let parser = parsers
                .get_mut(keyword)
                .ok_or_else(|| Error::new(format!("unknown keyword '{keyword}' in parsed line")))?;

            match parser.kind {
                ParserKind::Command => {
                    let body = crate::substitution::substitute_line_numbers(line_number, line_tail);
                    if !directives.is_empty() {
                        if let Some(ScriptDirective::Command(prev)) = directives.last_mut() {
                            if prev.add_continuation(line_number, keyword, &body) {
                                continue;
                            }
                        }
                    }
                    directives.push(ScriptDirective::Command(CommandDirective::new(
                        line_number,
                        keyword,
                        &body,
                    )));
                }
                ParserKind::Define | ParserKind::Redefine => {
                    let body = crate::substitution::substitute_line_numbers(line_number, line_tail);
                    if !directives.is_empty() {
                        if let Some(ScriptDirective::Substitution(prev)) = directives.last_mut() {
                            if prev.add_continuation(line_number, keyword, &body).map_err(
                                |error| {
                                    Error::new(format!(
                                        "{error}\nin {keyword} directive on test line {line_number}"
                                    ))
                                },
                            )? {
                                continue;
                            }
                        }
                    }
                    let new_subst = matches!(parser.kind, ParserKind::Define);
                    let directive =
                        SubstitutionDirective::new(line_number, keyword, new_subst, &body)
                            .map_err(|error| {
                                Error::new(format!(
                                    "{error}\nin {keyword} directive on test line {line_number}"
                                ))
                            })?;
                    directives.push(ScriptDirective::Substitution(directive));
                }
                ParserKind::BooleanExpr => {
                    let mut values = line_tail
                        .split(',')
                        .map(|value| value.trim().to_string())
                        .filter(|value| !value.is_empty())
                        .collect::<Vec<_>>();

                    if parser
                        .values
                        .last()
                        .is_some_and(|prev| prev.ends_with('\\'))
                    {
                        let mut merged = parser.values.pop().unwrap_or_default();
                        merged.pop();
                        let first = values.first_mut().ok_or_else(|| {
                            Error::new(format!(
                                "Test has unterminated '{keyword}' lines (with '\\')"
                            ))
                        })?;
                        let mut merged_line = merged;
                        merged_line.push_str(first);
                        *first = merged_line;
                    }

                    for value in &values {
                        if value == "*" || value.ends_with('\\') {
                            continue;
                        }
                        boolean_expression::evaluate(value, &HashSet::new()).map_err(|err| {
                            Error::new(format!(
                                "invalid boolean expression in {} directive on line {}: {err}",
                                keyword, line_number
                            ))
                        })?;
                    }

                    parser.append(values);
                }
                ParserKind::Integer => {
                    if !parser.values.is_empty() {
                        return Err(Error::new(format!(
                            "Test has more than one ALLOW_RETRIES line (at line {})",
                            line_number
                        )));
                    }
                    let value = line_tail.trim();
                    value.parse::<usize>().map_err(|_| {
                        Error::new(format!(
                            "invalid ALLOW_RETRIES value in line {}: '{}'",
                            line_number, value
                        ))
                    })?;
                    parser.append(std::iter::once(value.to_string()));
                }
                ParserKind::Tag => {
                    if line_tail.trim().is_empty() {
                        parser.append(std::iter::once("END".to_string()));
                        end_reached = true;
                    }
                }
            }
        }

        if end_reached {
            break;
        }
    }

    for directive in &directives {
        match directive {
            ScriptDirective::Command(cmd) if cmd.needs_continuation() => {
                return Err(Error::new(format!(
                    "Test has unterminated '{}' at line {}",
                    cmd.keyword, cmd.start_line
                )));
            }
            ScriptDirective::Substitution(subst) if subst.needs_continuation() => {
                return Err(Error::new(format!(
                    "Test has unterminated '{}' at line {}",
                    subst.keyword, subst.start_line
                )));
            }
            _ => {}
        }
    }

    for keyword in ["XFAIL:", "REQUIRES:", "UNSUPPORTED:"] {
        match parsers.get(keyword).and_then(|parser| parser.values.last()) {
            Some(last) if last.ends_with('\\') => {
                return Err(Error::new(format!(
                    "Test has unterminated '{keyword}' lines (with '\\')"
                )));
            }
            _ => {}
        }
    }

    if !directives
        .iter()
        .any(|directive| matches!(directive, ScriptDirective::Command(_)))
    {
        return Err(Error::new("Test has no 'RUN:' line"));
    }

    let xfails = parsers
        .get("XFAIL:")
        .map(|p| p.values.clone())
        .unwrap_or_default();
    let requires = parsers
        .get("REQUIRES:")
        .map(|p| p.values.clone())
        .unwrap_or_default();
    let unsupported = parsers
        .get("UNSUPPORTED:")
        .map(|p| p.values.clone())
        .unwrap_or_default();

    let allow_retries = parsers
        .get("ALLOW_RETRIES:")
        .and_then(|parser| parser.values.first())
        .and_then(|raw| raw.parse::<usize>().ok());

    Ok(ParsedIntegrated {
        directives,
        xfails,
        requires,
        unsupported,
        allow_retries,
    })
}

fn find_next_keyword<'a>(line: &[u8], keywords: &'a [&str]) -> Option<(usize, &'a str)> {
    let mut best: Option<(usize, &str)> = None;

    for keyword in keywords {
        if let Some(pos) = line
            .windows(keyword.len())
            .position(|window| window == keyword.as_bytes())
        {
            match best {
                None => best = Some((pos, keyword)),
                Some((best_pos, _)) => {
                    if pos < best_pos {
                        best = Some((pos, keyword));
                    }
                }
            }
        }
    }

    best
}

#[cfg(test)]
mod tests {
    use tempfile::tempdir;

    use super::parse_integrated_test_script;

    #[test]
    fn parse_run_and_requires() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("a.mlir");
        std::fs::write(
            &path,
            "// RUN: echo foo \\\n// RUN: && echo bar\n// REQUIRES: linux\n",
        )
        .unwrap();

        let parsed = parse_integrated_test_script(&path).unwrap();
        assert_eq!(parsed.requires, vec!["linux".to_string()]);
        assert_eq!(parsed.directives.len(), 1);
    }

    #[test]
    fn parse_define_and_redefine() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("b.mlir");
        std::fs::write(
            &path,
            "// DEFINE: %{X}=1\n// REDEFINE: %{X}=2\n// RUN: echo %{X}\n",
        )
        .unwrap();

        let parsed = parse_integrated_test_script(&path).unwrap();
        assert!(parsed.directives.len() >= 3);
    }

    #[test]
    fn parse_allow_retries_rejects_duplicates() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("c.mlir");
        std::fs::write(
            &path,
            "// RUN: true\n// ALLOW_RETRIES: 2\n// ALLOW_RETRIES: 1\n",
        )
        .unwrap();

        let err = parse_integrated_test_script(&path).unwrap_err();
        assert!(err.contains("more than one ALLOW_RETRIES"));
    }

    #[test]
    fn parse_unterminated_define_is_rejected() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("d.mlir");
        std::fs::write(&path, "// DEFINE: %{X}=1\\\n").unwrap();
        let err = parse_integrated_test_script(&path).unwrap_err();
        assert!(
            err.contains("unterminated") || err.contains("line 1"),
            "{err}"
        );
    }
}