Skip to main content

xgrammar/compiler/
error.rs

1//! Errors from [`GrammarCompiler`](super::GrammarCompiler).
2
3use crate::{
4    converter::{LarkError, RegexError, SchemaError, StructuralTagError},
5    functor::FunctorError,
6    parser::EbnfError,
7};
8
9/// An error from converting or compiling a grammar.
10#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
11pub enum CompileError {
12    /// The EBNF grammar is invalid.
13    #[error(transparent)]
14    Ebnf(#[from] EbnfError),
15    /// The JSON schema is invalid.
16    #[error(transparent)]
17    Schema(#[from] SchemaError),
18    /// The regex is invalid.
19    #[error(transparent)]
20    Regex(#[from] RegexError),
21    /// The structural tag is invalid.
22    #[error(transparent)]
23    StructuralTag(#[from] StructuralTagError),
24    /// A grammar pass rejected the grammar.
25    #[error(transparent)]
26    Functor(#[from] FunctorError),
27    /// The Lark grammar is invalid.
28    #[error(transparent)]
29    Lark(#[from] LarkError),
30    /// A `Token(...)`/`ExcludeToken(...)`/`TokenTagDispatch` id is outside the vocabulary.
31    #[error("Token id {token_id} in the grammar is out of the vocabulary range [0, {vocab_size})")]
32    TokenIdOutOfRange {
33        /// The offending token id.
34        token_id: i32,
35        /// The vocabulary size.
36        vocab_size: i32,
37    },
38}