ecma-syntax-cat 0.1.0

ECMAScript abstract syntax tree as comp-cat-rs-idiomatic Rust types. ESTree-shaped, ES2024-complete, no panics, no Rc, no interior mutability. Foundation crate for boa-cat and related downstream tooling.
Documentation
//! Construction-time error type.
//!
//! The AST itself is data; errors arise only when validating constructor
//! input (e.g. rejecting an empty identifier).  Downstream crates (lexer,
//! parser, evaluator) layer their own errors on top.

/// All errors that arise when constructing AST nodes from raw input.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
    /// An identifier was empty or contained an illegal character.
    InvalidIdentifier {
        /// The string that was attempted.
        attempted: String,
        /// Short description of why it is invalid.
        reason: &'static str,
    },
    /// A regex literal pattern was empty.
    EmptyRegexPattern,
    /// A regex literal carried a flag character not recognised by ECMA-262.
    InvalidRegexFlag {
        /// The offending flag character.
        flag: char,
    },
    /// A template literal had a different number of quasis than expressions
    /// allow (must satisfy `quasis.len() == expressions.len() + 1`).
    UnbalancedTemplate {
        /// Number of literal chunks supplied.
        quasis: usize,
        /// Number of interpolated expressions supplied.
        expressions: usize,
    },
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidIdentifier { attempted, reason } => {
                write!(f, "invalid identifier {attempted:?}: {reason}")
            }
            Self::EmptyRegexPattern => f.write_str("regex literal pattern is empty"),
            Self::InvalidRegexFlag { flag } => {
                write!(f, "invalid regex flag {flag:?}")
            }
            Self::UnbalancedTemplate {
                quasis,
                expressions,
            } => write!(
                f,
                "unbalanced template literal: {quasis} quasis and {expressions} expressions (must satisfy quasis == expressions + 1)"
            ),
        }
    }
}

impl std::error::Error for Error {}