#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ParserLimits {
pub(crate) source_bytes: usize,
pub(crate) tokens: usize,
pub(crate) ast_nodes: usize,
pub(crate) pratt_frames: usize,
pub(crate) ast_height: usize,
}
impl Default for ParserLimits {
fn default() -> Self {
Self {
source_bytes: 65536,
tokens: 16384,
ast_nodes: 8192,
pratt_frames: 72,
ast_height: 256,
}
}
}
impl ParserLimits {
#[must_use]
pub const fn with_source_bytes(mut self, source_bytes: usize) -> Self {
self.source_bytes = source_bytes;
self
}
#[must_use]
pub const fn with_tokens(mut self, tokens: usize) -> Self {
self.tokens = tokens;
self
}
#[must_use]
pub const fn with_ast_nodes(mut self, ast_nodes: usize) -> Self {
self.ast_nodes = ast_nodes;
self
}
#[must_use]
pub const fn with_pratt_frames(mut self, pratt_frames: usize) -> Self {
self.pratt_frames = pratt_frames;
self
}
#[must_use]
pub const fn with_ast_height(mut self, ast_height: usize) -> Self {
self.ast_height = ast_height;
self
}
pub const fn source_bytes(self) -> usize {
self.source_bytes
}
pub const fn tokens(self) -> usize {
self.tokens
}
pub const fn ast_nodes(self) -> usize {
self.ast_nodes
}
pub const fn pratt_frames(self) -> usize {
self.pratt_frames
}
pub const fn ast_height(self) -> usize {
self.ast_height
}
pub(crate) fn check_source(self, source: &str) -> Result<(), crate::TokenizerError> {
if source.len() > self.source_bytes {
return Err(crate::TokenizerError {
message: format!(
"Formula source byte limit exceeded (max {})",
self.source_bytes
),
pos: 0,
});
}
Ok(())
}
}