Skip to content

Commit

Permalink
perf(parse): Reduce throwaway work
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jul 29, 2024
1 parent 8be588b commit 4e9b4e8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
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 @@ -328,7 +328,7 @@ fn ml_literal_body<'i>(input: &mut Input<'i>) -> PResult<&'i str> {

// mll-content = mll-char / newline
fn mll_content(input: &mut Input<'_>) -> PResult<u8> {
alt((one_of(MLL_CHAR), newline)).parse_next(input)
alt((one_of(MLL_CHAR), newline.value(b'\n'))).parse_next(input)
}

// mll-char = %x09 / %x20-26 / %x28-7E / non-ascii
Expand Down
37 changes: 14 additions & 23 deletions crates/toml_edit/src/parser/trivia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,61 +50,52 @@ pub(crate) const NON_EOL: (u8, RangeInclusive<u8>, RangeInclusive<u8>) =
pub(crate) const COMMENT_START_SYMBOL: u8 = b'#';

// comment = comment-start-symbol *non-eol
pub(crate) fn comment<'i>(input: &mut Input<'i>) -> PResult<&'i [u8]> {
pub(crate) fn comment<'i>(input: &mut Input<'i>) -> PResult<()> {
(COMMENT_START_SYMBOL, take_while(0.., NON_EOL))
.recognize()
.void()
.parse_next(input)
}

// newline = ( %x0A / ; LF
// %x0D.0A ) ; CRLF
pub(crate) fn newline(input: &mut Input<'_>) -> PResult<u8> {
alt((
one_of(LF).value(b'\n'),
(one_of(CR), one_of(LF)).value(b'\n'),
))
.parse_next(input)
pub(crate) fn newline(input: &mut Input<'_>) -> PResult<()> {
alt((one_of(LF).void(), (one_of(CR), one_of(LF)).void())).parse_next(input)
}
pub(crate) const LF: u8 = b'\n';
pub(crate) const CR: u8 = b'\r';

// ws-newline = *( wschar / newline )
pub(crate) fn ws_newline<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
pub(crate) fn ws_newline<'i>(input: &mut Input<'i>) -> PResult<()> {
repeat(
0..,
alt((newline.value(&b"\n"[..]), take_while(1.., WSCHAR))),
)
.map(|()| ())
.recognize()
.map(|b| unsafe { from_utf8_unchecked(b, "`is_wschar` and `newline` filters out on-ASCII") })
.parse_next(input)
}

// ws-newlines = newline *( wschar / newline )
pub(crate) fn ws_newlines<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
(newline, ws_newline)
.recognize()
.map(|b| unsafe {
from_utf8_unchecked(b, "`is_wschar` and `newline` filters out on-ASCII")
})
.parse_next(input)
pub(crate) fn ws_newlines<'i>(input: &mut Input<'i>) -> PResult<()> {
(newline, ws_newline).void().parse_next(input)
}

// note: this rule is not present in the original grammar
// ws-comment-newline = *( ws-newline-nonempty / comment )
pub(crate) fn ws_comment_newline<'i>(input: &mut Input<'i>) -> PResult<&'i [u8]> {
pub(crate) fn ws_comment_newline<'i>(input: &mut Input<'i>) -> PResult<()> {
(
repeat(0.., (take_while(0.., WSCHAR), opt(comment), newline)).map(|()| ()),
take_while(0.., WSCHAR),
)
.recognize()
.void()
.parse_next(input)
}

// note: this rule is not present in the original grammar
// line-ending = newline / eof
pub(crate) fn line_ending<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
alt((newline.value("\n"), eof.value(""))).parse_next(input)
pub(crate) fn line_ending<'i>(input: &mut Input<'i>) -> PResult<()> {
alt((newline.value("\n"), eof.value("")))
.void()
.parse_next(input)
}

// note: this rule is not present in the original grammar
Expand Down Expand Up @@ -143,7 +134,7 @@ mod test {
];
for input in inputs {
dbg!(input);
let parsed = ws_comment_newline.parse(new_input(input));
let parsed = ws_comment_newline.recognize().parse(new_input(input));
assert!(parsed.is_ok(), "{:?}", parsed);
let parsed = parsed.unwrap();
assert_eq!(parsed, input.as_bytes());
Expand Down

0 comments on commit 4e9b4e8

Please sign in to comment.