Skip to main content

xgrammar/parser/
ebnf_error.rs

1//! Unified error for turning an EBNF string into a grammar (lexing or parsing).
2
3use super::{lexer_error::LexerError, parse_error::ParserError};
4
5/// A failure while converting EBNF text to a grammar.
6#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
7pub enum EbnfError {
8    /// The lexer rejected the input.
9    #[error(transparent)]
10    Lexer(#[from] LexerError),
11    /// The parser rejected the token stream.
12    #[error(transparent)]
13    Parser(#[from] ParserError),
14    /// A normalization pass failed (mirrors C++ `XGRAMMAR_LOG(FATAL)` becoming a Python error).
15    #[error("{0}")]
16    Normalization(String),
17}
18
19impl EbnfError {
20    /// The human-readable message of the underlying error.
21    #[must_use]
22    pub fn message(&self) -> &str {
23        match self {
24            EbnfError::Lexer(e) => &e.message,
25            EbnfError::Parser(e) => &e.message,
26            EbnfError::Normalization(message) => message,
27        }
28    }
29}