Skip to content

Commit

Permalink
Merge pull request #767 from epage/perf
Browse files Browse the repository at this point in the history
refactor(parse): Clean up the parser
  • Loading branch information
epage committed Jul 29, 2024
2 parents 27b30fd + de2e737 commit e922b5c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 46 deletions.
51 changes: 18 additions & 33 deletions crates/toml_edit/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,56 +92,41 @@ pub(crate) mod prelude {
winnow::Located::new(winnow::BStr::new(s))
}

#[cfg(not(feature = "unbounded"))]
#[derive(Copy, Clone, Debug, Default)]
pub(crate) struct RecursionCheck {
#[cfg(not(feature = "unbounded"))]
current: usize,
}

#[cfg(not(feature = "unbounded"))]
const LIMIT: usize = 80;

#[cfg(not(feature = "unbounded"))]
impl RecursionCheck {
pub(crate) fn check_depth(depth: usize) -> Result<(), super::error::CustomError> {
if depth < LIMIT {
Ok(())
} else {
Err(super::error::CustomError::RecursionLimitExceeded)
}
}

pub(crate) fn recursing(
mut self,
input: &mut Input<'_>,
) -> Result<Self, winnow::error::ErrMode<ContextError>> {
self.current += 1;
if self.current < LIMIT {
Ok(self)
} else {
Err(winnow::error::ErrMode::from_external_error(
input,
winnow::error::ErrorKind::Eof,
super::error::CustomError::RecursionLimitExceeded,
))
pub(crate) fn check_depth(_depth: usize) -> Result<(), super::error::CustomError> {
#[cfg(not(feature = "unbounded"))]
if LIMIT <= _depth {
return Err(super::error::CustomError::RecursionLimitExceeded);
}
}
}

#[cfg(feature = "unbounded")]
#[derive(Copy, Clone, Debug, Default)]
pub(crate) struct RecursionCheck {}

#[cfg(feature = "unbounded")]
impl RecursionCheck {
pub(crate) fn check_depth(_depth: usize) -> Result<(), super::error::CustomError> {
Ok(())
}

#[allow(unused_mut)]
pub(crate) fn recursing(
self,
mut self,
_input: &mut Input<'_>,
) -> Result<Self, winnow::error::ErrMode<ContextError>> {
#[cfg(not(feature = "unbounded"))]
{
self.current += 1;
if LIMIT <= self.current {
return Err(winnow::error::ErrMode::from_external_error(
_input,
winnow::error::ErrorKind::Eof,
super::error::CustomError::RecursionLimitExceeded,
));
}
}
Ok(self)
}
}
Expand Down
16 changes: 8 additions & 8 deletions crates/toml_edit/src/parser/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub(crate) fn dec_int<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
digit.void(),
)),
)
.recognize()
.take()
.map(|b: &[u8]| unsafe {
from_utf8_unchecked(b, "`digit` and `_` filter out non-ASCII")
})
Expand Down Expand Up @@ -112,7 +112,7 @@ pub(crate) fn hex_int<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
)
.map(|()| ()),
))
.recognize(),
.take(),
)
.map(|b| unsafe { from_utf8_unchecked(b, "`hexdig` and `_` filter out non-ASCII") })
.context(StrContext::Label("hexadecimal integer")),
Expand Down Expand Up @@ -145,7 +145,7 @@ pub(crate) fn oct_int<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
)
.map(|()| ()),
))
.recognize(),
.take(),
)
.map(|b| unsafe { from_utf8_unchecked(b, "`DIGIT0_7` and `_` filter out non-ASCII") })
.context(StrContext::Label("octal integer")),
Expand Down Expand Up @@ -179,7 +179,7 @@ pub(crate) fn bin_int<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
)
.map(|()| ()),
))
.recognize(),
.take(),
)
.map(|b| unsafe { from_utf8_unchecked(b, "`DIGIT0_1` and `_` filter out non-ASCII") })
.context(StrContext::Label("binary integer")),
Expand Down Expand Up @@ -214,7 +214,7 @@ pub(crate) fn float_<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
dec_int,
alt((exp.void(), (frac.void(), opt(exp.void())).void())),
)
.recognize()
.take()
.map(|b: &[u8]| unsafe {
from_utf8_unchecked(
b,
Expand All @@ -232,7 +232,7 @@ pub(crate) fn frac<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
cut_err(zero_prefixable_int)
.context(StrContext::Expected(StrContextValue::Description("digit"))),
)
.recognize()
.take()
.map(|b: &[u8]| unsafe {
from_utf8_unchecked(
b,
Expand Down Expand Up @@ -260,7 +260,7 @@ pub(crate) fn zero_prefixable_int<'i>(input: &mut Input<'i>) -> PResult<&'i str>
)
.map(|()| ()),
)
.recognize()
.take()
.map(|b: &[u8]| unsafe { from_utf8_unchecked(b, "`digit` and `_` filter out non-ASCII") })
.parse_next(input)
}
Expand All @@ -273,7 +273,7 @@ pub(crate) fn exp<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
opt(one_of([b'+', b'-'])),
cut_err(zero_prefixable_int),
)
.recognize()
.take()
.map(|b: &[u8]| unsafe {
from_utf8_unchecked(
b,
Expand Down
2 changes: 1 addition & 1 deletion crates/toml_edit/src/parser/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ fn ml_literal_body<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
.map(|()| ()),
opt(mll_quotes(ML_LITERAL_STRING_DELIM.void())),
)
.recognize()
.take()
.try_map(std::str::from_utf8)
.parse_next(input)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/toml_edit/src/parser/trivia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ mod test {
];
for input in inputs {
dbg!(input);
let parsed = ws_comment_newline.recognize().parse(new_input(input));
let parsed = ws_comment_newline.take().parse(new_input(input));
assert!(parsed.is_ok(), "{:?}", parsed);
let parsed = parsed.unwrap();
assert_eq!(parsed, input.as_bytes());
Expand Down
6 changes: 3 additions & 3 deletions crates/toml_edit/src/parser/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ pub(crate) fn value<'i>(check: RecursionCheck) -> impl Parser<Input<'i>, Value,
},
}
.with_span()
.try_map(|(value, span)| apply_raw(value, span))
.map(|(value, span)| apply_raw(value, span))
.parse_next(input)
}
}

fn apply_raw(mut val: Value, span: std::ops::Range<usize>) -> Result<Value, std::str::Utf8Error> {
fn apply_raw(mut val: Value, span: std::ops::Range<usize>) -> Value {
match val {
Value::String(ref mut f) => {
let raw = RawString::with_span(span);
Expand Down Expand Up @@ -116,7 +116,7 @@ fn apply_raw(mut val: Value, span: std::ops::Range<usize>) -> Result<Value, std:
}
};
val.decorate("", "");
Ok(val)
val
}

#[cfg(test)]
Expand Down

0 comments on commit e922b5c

Please sign in to comment.