Skip to content

Commit

Permalink
refactor(parser): Switch to ModalParser
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 30, 2025
1 parent 5dfa5b0 commit 9415da2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
10 changes: 5 additions & 5 deletions crates/toml_edit/src/parser/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::RawString;
// ws )
pub(crate) fn document<'s, 'i>(
state_ref: &'s RefCell<ParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + 's {
) -> impl ModalParser<Input<'i>, (), ContextError> + 's {
move |i: &mut Input<'i>| {
(
// Remove BOM if present
Expand All @@ -54,7 +54,7 @@ pub(crate) fn document<'s, 'i>(

pub(crate) fn parse_comment<'s, 'i>(
state: &'s RefCell<ParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + 's {
) -> impl ModalParser<Input<'i>, (), ContextError> + 's {
move |i: &mut Input<'i>| {
(comment, line_ending)
.span()
Expand All @@ -67,7 +67,7 @@ pub(crate) fn parse_comment<'s, 'i>(

pub(crate) fn parse_ws<'s, 'i>(
state: &'s RefCell<ParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + 's {
) -> impl ModalParser<Input<'i>, (), ContextError> + 's {
move |i: &mut Input<'i>| {
ws.span()
.map(|span| state.borrow_mut().on_ws(span))
Expand All @@ -77,7 +77,7 @@ pub(crate) fn parse_ws<'s, 'i>(

pub(crate) fn parse_newline<'s, 'i>(
state: &'s RefCell<ParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + 's {
) -> impl ModalParser<Input<'i>, (), ContextError> + 's {
move |i: &mut Input<'i>| {
newline
.span()
Expand All @@ -88,7 +88,7 @@ pub(crate) fn parse_newline<'s, 'i>(

pub(crate) fn keyval<'s, 'i>(
state: &'s RefCell<ParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + 's {
) -> impl ModalParser<Input<'i>, (), ContextError> + 's {
move |i: &mut Input<'i>| {
parse_keyval
.try_map(|(p, kv)| state.borrow_mut().on_keyval(p, kv))
Expand Down
7 changes: 4 additions & 3 deletions crates/toml_edit/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ pub(crate) mod prelude {
pub(crate) use winnow::error::FromExternalError;
pub(crate) use winnow::error::StrContext;
pub(crate) use winnow::error::StrContextValue;
pub(crate) use winnow::ModalParser;
pub(crate) use winnow::ModalResult;
pub(crate) use winnow::Parser;
pub(crate) use winnow::Parser as _;

pub(crate) type Input<'b> =
winnow::Stateful<winnow::LocatingSlice<&'b winnow::BStr>, RecursionCheck>;
Expand Down Expand Up @@ -135,8 +136,8 @@ pub(crate) mod prelude {
}

pub(crate) fn check_recursion<'b, O>(
mut parser: impl Parser<Input<'b>, O, ContextError>,
) -> impl Parser<Input<'b>, O, ContextError> {
mut parser: impl ModalParser<Input<'b>, O, ContextError>,
) -> impl ModalParser<Input<'b>, O, ContextError> {
move |input: &mut Input<'b>| {
input.state.enter().map_err(|err| {
#[allow(deprecated)]
Expand Down
8 changes: 4 additions & 4 deletions crates/toml_edit/src/parser/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ fn mlb_content<'i>(input: &mut Input<'i>) -> ModalResult<Cow<'i, str>> {

// mlb-quotes = 1*2quotation-mark
fn mlb_quotes<'i>(
mut term: impl Parser<Input<'i>, (), ContextError>,
) -> impl Parser<Input<'i>, &'i str, ContextError> {
mut term: impl ModalParser<Input<'i>, (), ContextError>,
) -> impl ModalParser<Input<'i>, &'i str, ContextError> {
move |input: &mut Input<'i>| {
let start = input.checkpoint();
let res = terminated(b"\"\"", peek(term.by_ref()))
Expand Down Expand Up @@ -341,8 +341,8 @@ const MLL_CHAR: (

// mll-quotes = 1*2apostrophe
fn mll_quotes<'i>(
mut term: impl Parser<Input<'i>, (), ContextError>,
) -> impl Parser<Input<'i>, &'i str, ContextError> {
mut term: impl ModalParser<Input<'i>, (), ContextError>,
) -> impl ModalParser<Input<'i>, &'i str, ContextError> {
move |input: &mut Input<'i>| {
let start = input.checkpoint();
let res = terminated(b"''", peek(term.by_ref()))
Expand Down
6 changes: 3 additions & 3 deletions crates/toml_edit/src/parser/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const ARRAY_TABLE_CLOSE: &[u8] = b"]]";
// std-table = std-table-open key *( table-key-sep key) std-table-close
pub(crate) fn std_table<'s, 'i>(
state: &'s RefCell<ParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + 's {
) -> impl ModalParser<Input<'i>, (), ContextError> + 's {
move |i: &mut Input<'i>| {
(
delimited(
Expand All @@ -52,7 +52,7 @@ pub(crate) fn std_table<'s, 'i>(
// array-table = array-table-open key *( table-key-sep key) array-table-close
pub(crate) fn array_table<'s, 'i>(
state: &'s RefCell<ParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + 's {
) -> impl ModalParser<Input<'i>, (), ContextError> + 's {
move |i: &mut Input<'i>| {
(
delimited(
Expand All @@ -77,7 +77,7 @@ pub(crate) fn array_table<'s, 'i>(
// table = std-table / array-table
pub(crate) fn table<'s, 'i>(
state: &'s RefCell<ParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + 's {
) -> impl ModalParser<Input<'i>, (), ContextError> + 's {
move |i: &mut Input<'i>| {
dispatch!(peek::<_, &[u8],_,_>(take(2usize));
b"[[" => array_table(state),
Expand Down

0 comments on commit 9415da2

Please sign in to comment.