quiltlang 0.6.0

Quilt, a polyglot metaprogramming language: source code with arrow-bracket syntax for embedding and manipulating code fragments of other languages at code-generation time.
Documentation
use crate::{
    lang::{Arity, Comments, InnerKind},
    qterm::QTerm,
    treesitter::{DynTSLanguage, FragmentWrapper, TSLanguage, TSProvider},
};
use miette::{miette, Result};
use tree_sitter::Parser;

/**************************************************************/

pub struct RustProvider(tree_sitter::Parser);

impl Default for RustProvider {
    fn default() -> Self {
        let mut parser = Parser::new();
        parser
            .set_language(&crate::grammars::rust::LANGUAGE.into())
            .expect("Error loading Rust parser");
        Self(parser)
    }
}

impl TSProvider for RustProvider {
    fn parser(&mut self) -> &mut tree_sitter::Parser {
        &mut self.0
    }

    fn hole_str(&self) -> &'static str {
        "{}"
    }

    fn unwrap(&self, qterm: QTerm, ikind: Option<InnerKind>) -> Result<(QTerm, InnerKind)> {
        // dbg!(&qterm);
        #[allow(clippy::single_match)]
        Ok(match &qterm {
            QTerm::Tuple { tag, terms, .. } => match &**tag {
                "source_file" => {
                    if terms.len() == 1 {
                        let q0 = &terms[0];
                        match &**q0 {
                            QTerm::Tuple { tag, .. } => {
                                // The hole's position (when known) settles
                                // ambiguous bodies like a bare `{ }`; otherwise
                                // guess from the tag.
                                let kind = match ikind {
                                    Some(k) if k != InnerKind::File => k,
                                    _ if **tag == *"{}" => InnerKind::Stmt,
                                    _ => self.typ(tag),
                                };
                                (qterm.squash(), kind)
                            }
                            _ => return Err(unsupported_shape(&qterm)),
                        }
                    } else {
                        (qterm, InnerKind::File)
                    }
                }
                "{}" => (qterm, InnerKind::Expr),
                _ => return Err(unsupported_shape(&qterm)),
            },
            _ => return Err(unsupported_shape(&qterm)),
        })
    }

    /// Derived from the grammar's `REPEAT` rules by `bin/gen-arity`, not
    /// hand-curated — see `quilt/src/langs/arity.rs` (#202).
    fn arity(&self, tag: &str) -> Arity {
        Arity::from_table(crate::langs::arity::RUST, tag)
    }

    /// `,` for the kinds `bin/gen-arity` finds comma-separated (#293).
    fn separator(&self, tag: &str) -> Option<&'static str> {
        crate::lang::comma_separator(crate::langs::arity::RUST_COMMA_SEPARATED, tag)
    }

    fn typ(&self, tag: &str) -> InnerKind {
        match tag {
            "source_file" => InnerKind::File,
            // `let_declaration` ends with "declaration" but is a statement, not
            // an item — match it before the item rule below.
            "let_declaration" => InnerKind::Stmt,
            _ if tag.ends_with("item") || tag.ends_with("declaration") => InnerKind::Item,
            _ if tag.ends_with("statement") => InnerKind::Stmt,
            _ => InnerKind::Expr,
        }
    }

    fn hole_kind(&self, node: tree_sitter::Node) -> InnerKind {
        // A `block` is a value (expression) by tag, but in body/branch
        // position it is a block body. Read the parent to tell the two apart:
        // `fn f() {…}` / `loop {…}` use the `body` field, `if c {…}` uses
        // `consequence`, and the `else {…}` block hangs off an `else_clause`.
        // (A `block` in `value` position — `let x = {…}` — stays `Expr`.)
        // A hole among the items of an `impl`, `mod` or `trait` body stands
        // for items. Its tag is the grammar's `quilt_hole`, which says nothing
        // about position on its own (#302).
        if node.kind() == "quilt_hole"
            && node
                .parent()
                .is_some_and(|p| p.kind() == "declaration_list")
        {
            return InnerKind::Item;
        }
        if node.kind() == "block" {
            if let Some(parent) = node.parent() {
                let is_body = parent.kind() == "else_clause"
                    || ["body", "consequence"]
                        .iter()
                        .any(|field| parent.child_by_field_name(field) == Some(node));
                if is_body {
                    return InnerKind::Block;
                }
            }
        }
        self.typ(node.kind())
    }

    fn hashbang(&self) -> Option<&'static str> {
        Some("#!/usr/bin/env rust-script")
    }

    /// A match arm, an enum variant, a struct field, a function parameter or
    /// a field pattern, quoted on its own (`rs↖A(u64)↗`), parses inside the
    /// construct it belongs to. Order settles the one overlap: `a: u64` is a
    /// struct field before it is a parameter, and both write back the same.
    fn fragment_wrappers(&self) -> &'static [FragmentWrapper] {
        &[
            FragmentWrapper {
                before: "match q {",
                after: "}",
                container: "match_block",
                fragments: &["match_arm"],
                leading: &[],
            },
            FragmentWrapper {
                before: "enum Q {",
                after: "}",
                container: "enum_variant_list",
                fragments: &["enum_variant"],
                leading: &["attribute_item"],
            },
            FragmentWrapper {
                before: "struct Q {",
                after: "}",
                container: "field_declaration_list",
                fragments: &["field_declaration"],
                leading: &["attribute_item"],
            },
            FragmentWrapper {
                before: "fn q(",
                after: ") {}",
                container: "parameters",
                fragments: &["parameter", "self_parameter"],
                leading: &["attribute_item"],
            },
            FragmentWrapper {
                before: "let Q {",
                after: "} = q;",
                container: "struct_pattern",
                fragments: &["field_pattern", "remaining_field_pattern"],
                leading: &[],
            },
        ]
    }
}

/// Diagnostic for a Rust fragment whose tree-sitter parse shape the provider
/// doesn't know how to unwrap. Replaces the `unimplemented!` panics that used
/// to crash the expander on unusual-but-valid Rust (issue #11); the parse's
/// s-expression is included so the gap can be reported and reproduced.
fn unsupported_shape(qterm: &QTerm) -> miette::Report {
    miette!(
        "Quilt can't place this Rust fragment — unsupported parse shape: {}.\n\
         This is a gap in Quilt's Rust support; please report it along with the \
         fragment that triggered it.",
        qterm.sexp()
    )
}

pub type RustLanguage = TSLanguage<RustProvider>;
pub type DynRustLanguage = DynTSLanguage<RustProvider>;

impl Comments for RustLanguage {
    const LINE: Option<&'static str> = Some("//");
    /// Rust is the one language whose header is not its plain line comment.
    /// `//!` is an inner doc comment, so the `DO NOT EDIT` banner documents the
    /// generated module rather than whatever item happens to follow it.
    const HEADER: Option<&'static str> = Some("//!");
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::qterm::tb;

    /// A parse shape the provider can't place (here, a root tag that is neither
    /// `source_file` nor the `{}` hole) now surfaces a diagnostic instead of
    /// panicking via `unimplemented!` (issue #11).
    #[test]
    fn unsupported_shape_returns_err_not_panic() {
        let provider = RustProvider::default();
        let err = provider
            .unwrap(tb("not_a_real_node_kind").build(), None)
            .expect_err("an unrecognised parse shape should be an error");
        assert!(
            err.to_string().contains("unsupported parse shape"),
            "diagnostic should name the unsupported shape, got: {err}"
        );
    }

    /// A well-formed single-node `source_file` still unwraps successfully.
    #[test]
    fn source_file_single_node_unwraps_ok() {
        let provider = RustProvider::default();
        let qterm = tb("source_file").c(&tb("expression_statement").b()).build();
        assert!(provider.unwrap(qterm, None).is_ok());
    }
}