spacetimedb_sql_parser/parser/recursion.rs
1//! A utility for guarding against stack overflows in the SQL parser.
2//!
3//! Different parts of the parser may have different recursion limits, based in the size of the structures they parse.
4
5use crate::parser::errors::{RecursionError, SqlParseError};
6
7/// A conservative limit for recursion depth on `parse_expr`.
8pub const MAX_RECURSION_EXPR: usize = 1_600;
9/// A conservative limit for recursion depth on `type_expr`.
10pub const MAX_RECURSION_TYP_EXPR: usize = 2_500;
11
12/// A utility for guarding against stack overflows in the SQL parser.
13///
14/// **Usage:**
15/// ```
16/// use spacetimedb_sql_parser::parser::recursion;
17/// let mut depth = 0;
18/// assert!(recursion::guard(depth, 10, "test").is_ok());
19/// ```
20pub fn guard(depth: usize, limit: usize, source: &'static str) -> Result<(), SqlParseError> {
21 if depth > limit {
22 Err(RecursionError { source_: source }.into())
23 } else {
24 Ok(())
25 }
26}