-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: use a custom parser input * test: add SO tests * fixes
- Loading branch information
Showing
12 changed files
with
215 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#![allow(missing_docs, missing_copy_implementations, missing_debug_implementations)] | ||
|
||
// Recursion implementation modified from `toml`: https://github.com/toml-rs/toml/blob/a02cbf46cab4a8683e641efdba648a31498f7342/crates/toml_edit/src/parser/mod.rs#L99 | ||
|
||
use core::fmt; | ||
use winnow::{ | ||
error::{ContextError, FromExternalError}, | ||
Parser, | ||
}; | ||
|
||
#[allow(dead_code)] | ||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
enum CustomError { | ||
RecursionLimitExceeded, | ||
} | ||
|
||
impl fmt::Display for CustomError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
Self::RecursionLimitExceeded => f.write_str("recursion limit exceeded"), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl std::error::Error for CustomError {} | ||
|
||
pub type Input<'a> = winnow::Stateful<&'a str, RecursionCheck>; | ||
|
||
pub fn new_input(input: &str) -> Input<'_> { | ||
winnow::Stateful { input, state: Default::default() } | ||
} | ||
|
||
pub fn check_recursion<'a, O>( | ||
mut parser: impl Parser<Input<'a>, O, ContextError>, | ||
) -> impl Parser<Input<'a>, O, ContextError> { | ||
move |input: &mut Input<'a>| { | ||
input.state.enter().map_err(|err| { | ||
winnow::error::ErrMode::from_external_error(input, winnow::error::ErrorKind::Eof, err) | ||
.cut() | ||
})?; | ||
let result = parser.parse_next(input); | ||
input.state.exit(); | ||
result | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, Default, PartialEq, Eq)] | ||
pub struct RecursionCheck { | ||
current: usize, | ||
} | ||
|
||
const LIMIT: usize = 80; | ||
|
||
impl RecursionCheck { | ||
#[cfg(any())] | ||
fn check_depth(_depth: usize) -> Result<(), CustomError> { | ||
if LIMIT <= _depth { | ||
return Err(CustomError::RecursionLimitExceeded); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn enter(&mut self) -> Result<(), CustomError> { | ||
self.current += 1; | ||
if LIMIT <= self.current { | ||
return Err(CustomError::RecursionLimitExceeded); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn exit(&mut self) { | ||
self.current -= 1; | ||
} | ||
} |
Oops, something went wrong.