Skip to main content

oxillama_runtime/sampling/grammar/
error.rs

1//! Error types for GBNF grammar-constrained sampling.
2
3use thiserror::Error;
4
5/// Errors that can occur during grammar parsing or state machine execution.
6#[derive(Debug, Error, Clone)]
7pub enum GrammarError {
8    /// Syntax error during GBNF grammar parsing.
9    #[error("grammar parse error at position {pos}: {msg}")]
10    ParseError {
11        /// Byte offset in the input where the error occurred.
12        pos: usize,
13        /// Human-readable description.
14        msg: String,
15    },
16
17    /// Grammar state machine reached a dead state — no valid next tokens exist.
18    #[error("grammar reached a stuck state — no valid next tokens")]
19    Stuck,
20
21    /// A rule reference in the grammar points to a rule that was never defined.
22    #[error("unknown rule reference: '{rule}'")]
23    UnknownRule {
24        /// The missing rule name.
25        rule: String,
26    },
27
28    /// Recursion depth limit exceeded during grammar simulation.
29    #[error(
30        "grammar recursion depth limit exceeded (possible infinite recursion in rule '{rule}')"
31    )]
32    RecursionLimit {
33        /// Rule that was being evaluated.
34        rule: String,
35    },
36}
37
38/// Convenience alias.
39pub type GrammarResult<T> = Result<T, GrammarError>;