use std::fmt;
use crate::render::visible;
pub const DEFAULT_MAX_DEPTH: u32 = 5_000;
pub const DEFAULT_MAX_STEPS: u64 = 4_000_000_000;
pub const DEFAULT_MEMORY_BYTES_PER_INPUT_BYTE: u64 = 4 * 1024;
pub const DEFAULT_MIN_MEMORY_BYTES: u64 = 64 * 1024 * 1024;
pub const DEFAULT_MAX_MEMORY_BYTES: u64 = 4 * 1024 * 1024 * 1024;
pub(crate) const END_OF_INPUT: &str = "end of input";
pub(crate) const INDENTATION: &str = "indentation";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Limits {
pub max_depth: u32,
pub max_steps: u64,
pub max_memory_bytes: Option<u64>,
}
impl Limits {
pub fn memory_bytes(&self, input_bytes: usize) -> u64 {
let relative = (input_bytes as u64).saturating_mul(DEFAULT_MEMORY_BYTES_PER_INPUT_BYTE);
let default = relative.clamp(DEFAULT_MIN_MEMORY_BYTES, DEFAULT_MAX_MEMORY_BYTES);
self.max_memory_bytes.unwrap_or(default)
}
}
impl Default for Limits {
fn default() -> Limits {
Limits {
max_depth: DEFAULT_MAX_DEPTH,
max_steps: DEFAULT_MAX_STEPS,
max_memory_bytes: None,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ParseError {
pub position: usize,
pub kind: ParseErrorKind,
pub recoveries: Vec<Recovery>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Recovery {
pub label: String,
pub start: usize,
pub end: usize,
pub position: usize,
pub expected: Vec<String>,
}
impl Recovery {
pub fn message(&self) -> String {
named(&self.label, &self.expected)
}
}
#[derive(Clone, Debug)]
pub(crate) struct Found {
pub(crate) recovery: Recovery,
pub(crate) label: u32,
pub(crate) floor: Option<usize>,
}
fn named(name: &str, expected: &[String]) -> String {
match expected {
[] => name.to_owned(),
_ => format!("{name}: expected {}", expected.join(", ")),
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum ParseErrorKind {
NoMatch {
expected: Vec<String>,
},
Label {
label: String,
named: bool,
expected: Vec<String>,
},
ErrorAlternative {
message: String,
end: usize,
},
Limit(Limit),
UnknownStart(String),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Limit {
Depth,
Steps,
InputSize,
Memory,
}
impl ParseError {
pub fn end(&self) -> usize {
match &self.kind {
ParseErrorKind::ErrorAlternative { end, .. } => *end,
_ => self.position,
}
}
pub fn message(&self) -> String {
match &self.kind {
ParseErrorKind::NoMatch { expected } if expected.is_empty() => "no match".to_owned(),
ParseErrorKind::NoMatch { expected } => format!("expected {}", expected.join(", ")),
ParseErrorKind::Label {
label,
named: true,
expected,
} => named(label, expected),
ParseErrorKind::Label { label, .. } => label.clone(),
ParseErrorKind::ErrorAlternative { message, .. } => message.clone(),
ParseErrorKind::Limit(Limit::Depth) => {
"rule calls nest deeper than the depth limit".to_owned()
}
ParseErrorKind::Limit(Limit::Steps) => {
"the parse takes more steps than the step limit".to_owned()
}
ParseErrorKind::Limit(Limit::InputSize) => {
"the input is too large for 32-bit offsets".to_owned()
}
ParseErrorKind::Limit(Limit::Memory) => {
"the parse needs more memory than the memory limit allows".to_owned()
}
ParseErrorKind::UnknownStart(name) => format!("no rule {name} to start from"),
}
}
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let message = visible(&self.message());
match self.end() {
end if end > self.position => {
write!(f, "error at {}..{end}: {message}", self.position)
}
_ => write!(f, "error at {}: {message}", self.position),
}
}
}
impl std::error::Error for ParseError {}