Skip to content

Commit

Permalink
refactor(parser): Rename ParserValue to StrContextValue to align with…
Browse files Browse the repository at this point in the history
… winnow
  • Loading branch information
epage committed Jul 10, 2023
1 parent fcc9642 commit f19b78f
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 54 deletions.
2 changes: 1 addition & 1 deletion crates/toml_edit/src/parser/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) fn array<'i>(check: RecursionCheck) -> impl Parser<Input<'i>, Array,
cut_err(array_values(check)),
cut_err(ARRAY_CLOSE)
.context(StrContext::Label("array"))
.context(StrContext::Expected(ParserValue::CharLiteral(']'))),
.context(StrContext::Expected(StrContextValue::CharLiteral(']'))),
)
.parse_next(input)
}
Expand Down
8 changes: 4 additions & 4 deletions crates/toml_edit/src/parser/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ pub(crate) fn parse_keyval(
key,
cut_err((
one_of(KEYVAL_SEP)
.context(StrContext::Expected(ParserValue::CharLiteral('.')))
.context(StrContext::Expected(ParserValue::CharLiteral('='))),
.context(StrContext::Expected(StrContextValue::CharLiteral('.')))
.context(StrContext::Expected(StrContextValue::CharLiteral('='))),
(
ws.span(),
value(RecursionCheck::default()),
line_trailing
.context(StrContext::Expected(ParserValue::CharLiteral('\n')))
.context(StrContext::Expected(ParserValue::CharLiteral('#'))),
.context(StrContext::Expected(StrContextValue::CharLiteral('\n')))
.context(StrContext::Expected(StrContextValue::CharLiteral('#'))),
),
)),
)
Expand Down
18 changes: 9 additions & 9 deletions crates/toml_edit/src/parser/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,27 +281,27 @@ impl<'a> std::fmt::Display for ParserError<'a> {
#[derive(Copy, Clone, Debug, PartialEq)]
pub(crate) enum StrContext {
Label(&'static str),
Expected(ParserValue),
Expected(StrContextValue),
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub(crate) enum ParserValue {
pub(crate) enum StrContextValue {
CharLiteral(char),
StringLiteral(&'static str),
Description(&'static str),
}

impl std::fmt::Display for ParserValue {
impl std::fmt::Display for StrContextValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ParserValue::CharLiteral('\n') => "newline".fmt(f),
ParserValue::CharLiteral('`') => "'`'".fmt(f),
ParserValue::CharLiteral(c) if c.is_ascii_control() => {
StrContextValue::CharLiteral('\n') => "newline".fmt(f),
StrContextValue::CharLiteral('`') => "'`'".fmt(f),
StrContextValue::CharLiteral(c) if c.is_ascii_control() => {
write!(f, "`{}`", c.escape_debug())
}
ParserValue::CharLiteral(c) => write!(f, "`{}`", c),
ParserValue::StringLiteral(c) => write!(f, "`{}`", c),
ParserValue::Description(c) => write!(f, "{}", c),
StrContextValue::CharLiteral(c) => write!(f, "`{}`", c),
StrContextValue::StringLiteral(c) => write!(f, "`{}`", c),
StrContextValue::Description(c) => write!(f, "{}", c),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/toml_edit/src/parser/inline_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(crate) fn inline_table<'i>(
cut_err(inline_table_keyvals(check).try_map(|(kv, p)| table_from_pairs(kv, p))),
cut_err(INLINE_TABLE_CLOSE)
.context(StrContext::Label("inline table"))
.context(StrContext::Expected(ParserValue::CharLiteral('}'))),
.context(StrContext::Expected(StrContextValue::CharLiteral('}'))),
)
.parse_next(input)
}
Expand Down Expand Up @@ -117,8 +117,8 @@ fn keyval<'i>(
key,
cut_err((
one_of(KEYVAL_SEP)
.context(StrContext::Expected(ParserValue::CharLiteral('.')))
.context(StrContext::Expected(ParserValue::CharLiteral('='))),
.context(StrContext::Expected(StrContextValue::CharLiteral('.')))
.context(StrContext::Expected(StrContextValue::CharLiteral('='))),
(ws.span(), value(check), ws.span()),
)),
)
Expand Down
2 changes: 1 addition & 1 deletion crates/toml_edit/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ pub(crate) fn parse_value(raw: &str) -> Result<crate::Value, TomlError> {

pub(crate) mod prelude {
pub(crate) use super::errors::ParserError;
pub(crate) use super::errors::ParserValue;
pub(crate) use super::errors::StrContext;
pub(crate) use super::errors::StrContextValue;
pub(crate) use winnow::combinator::dispatch;
pub(crate) use winnow::IResult;
pub(crate) use winnow::Parser;
Expand Down
16 changes: 9 additions & 7 deletions crates/toml_edit/src/parser/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ pub(crate) fn dec_int(input: Input<'_>) -> IResult<Input<'_>, &str, ParserError<
digit.value(()),
(
one_of(b'_'),
cut_err(digit)
.context(StrContext::Expected(ParserValue::Description("digit"))),
cut_err(digit).context(StrContext::Expected(
StrContextValue::Description("digit"),
)),
)
.value(()),
)),
Expand Down Expand Up @@ -95,7 +96,7 @@ pub(crate) fn hex_int(input: Input<'_>) -> IResult<Input<'_>, &str, ParserError<
(
one_of(b'_'),
cut_err(hexdig)
.context(StrContext::Expected(ParserValue::Description("digit"))),
.context(StrContext::Expected(StrContextValue::Description("digit"))),
)
.value(()),
)),
Expand Down Expand Up @@ -124,7 +125,7 @@ pub(crate) fn oct_int(input: Input<'_>) -> IResult<Input<'_>, &str, ParserError<
(
one_of(b'_'),
cut_err(one_of(DIGIT0_7))
.context(StrContext::Expected(ParserValue::Description("digit"))),
.context(StrContext::Expected(StrContextValue::Description("digit"))),
)
.value(()),
)),
Expand Down Expand Up @@ -154,7 +155,7 @@ pub(crate) fn bin_int(input: Input<'_>) -> IResult<Input<'_>, &str, ParserError<
(
one_of(b'_'),
cut_err(one_of(DIGIT0_1))
.context(StrContext::Expected(ParserValue::Description("digit"))),
.context(StrContext::Expected(StrContextValue::Description("digit"))),
)
.value(()),
)),
Expand Down Expand Up @@ -205,7 +206,7 @@ pub(crate) fn frac(input: Input<'_>) -> IResult<Input<'_>, &str, ParserError<'_>
(
b'.',
cut_err(zero_prefixable_int)
.context(StrContext::Expected(ParserValue::Description("digit"))),
.context(StrContext::Expected(StrContextValue::Description("digit"))),
)
.recognize()
.map(|b: &[u8]| unsafe {
Expand All @@ -227,7 +228,8 @@ pub(crate) fn zero_prefixable_int(input: Input<'_>) -> IResult<Input<'_>, &str,
digit.value(()),
(
one_of(b'_'),
cut_err(digit).context(StrContext::Expected(ParserValue::Description("digit"))),
cut_err(digit)
.context(StrContext::Expected(StrContextValue::Description("digit"))),
)
.value(()),
)),
Expand Down
18 changes: 9 additions & 9 deletions crates/toml_edit/src/parser/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ fn escape_seq_char(input: Input<'_>) -> IResult<Input<'_>, char, ParserError<'_>
_ => {
cut_err(fail::<_, char, _>)
.context(StrContext::Label("escape sequence"))
.context(StrContext::Expected(ParserValue::CharLiteral('b')))
.context(StrContext::Expected(ParserValue::CharLiteral('f')))
.context(StrContext::Expected(ParserValue::CharLiteral('n')))
.context(StrContext::Expected(ParserValue::CharLiteral('r')))
.context(StrContext::Expected(ParserValue::CharLiteral('t')))
.context(StrContext::Expected(ParserValue::CharLiteral('u')))
.context(StrContext::Expected(ParserValue::CharLiteral('U')))
.context(StrContext::Expected(ParserValue::CharLiteral('\\')))
.context(StrContext::Expected(ParserValue::CharLiteral('"')))
.context(StrContext::Expected(StrContextValue::CharLiteral('b')))
.context(StrContext::Expected(StrContextValue::CharLiteral('f')))
.context(StrContext::Expected(StrContextValue::CharLiteral('n')))
.context(StrContext::Expected(StrContextValue::CharLiteral('r')))
.context(StrContext::Expected(StrContextValue::CharLiteral('t')))
.context(StrContext::Expected(StrContextValue::CharLiteral('u')))
.context(StrContext::Expected(StrContextValue::CharLiteral('U')))
.context(StrContext::Expected(StrContextValue::CharLiteral('\\')))
.context(StrContext::Expected(StrContextValue::CharLiteral('"')))
}
}
.parse_next(input)
Expand Down
16 changes: 8 additions & 8 deletions crates/toml_edit/src/parser/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ pub(crate) fn std_table<'s, 'i>(
STD_TABLE_OPEN,
cut_err(key),
cut_err(STD_TABLE_CLOSE)
.context(StrContext::Expected(ParserValue::CharLiteral('.')))
.context(StrContext::Expected(ParserValue::StringLiteral("]"))),
.context(StrContext::Expected(StrContextValue::CharLiteral('.')))
.context(StrContext::Expected(StrContextValue::StringLiteral("]"))),
)
.with_span(),
cut_err(line_trailing)
.context(StrContext::Expected(ParserValue::CharLiteral('\n')))
.context(StrContext::Expected(ParserValue::CharLiteral('#'))),
.context(StrContext::Expected(StrContextValue::CharLiteral('\n')))
.context(StrContext::Expected(StrContextValue::CharLiteral('#'))),
)
.try_map(|((h, span), t)| state.borrow_mut().deref_mut().on_std_header(h, t, span))
.parse_next(i)
Expand All @@ -59,13 +59,13 @@ pub(crate) fn array_table<'s, 'i>(
ARRAY_TABLE_OPEN,
cut_err(key),
cut_err(ARRAY_TABLE_CLOSE)
.context(StrContext::Expected(ParserValue::CharLiteral('.')))
.context(StrContext::Expected(ParserValue::StringLiteral("]]"))),
.context(StrContext::Expected(StrContextValue::CharLiteral('.')))
.context(StrContext::Expected(StrContextValue::StringLiteral("]]"))),
)
.with_span(),
cut_err(line_trailing)
.context(StrContext::Expected(ParserValue::CharLiteral('\n')))
.context(StrContext::Expected(ParserValue::CharLiteral('#'))),
.context(StrContext::Expected(StrContextValue::CharLiteral('\n')))
.context(StrContext::Expected(StrContextValue::CharLiteral('#'))),
)
.try_map(|((h, span), t)| state.borrow_mut().deref_mut().on_array_header(h, t, span))
.parse_next(i)
Expand Down
24 changes: 12 additions & 12 deletions crates/toml_edit/src/parser/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,43 +44,43 @@ pub(crate) fn value<'i>(
b'_' => {
integer
.map(v::Value::from)
.context(StrContext::Expected(ParserValue::Description("leading digit")))
.context(StrContext::Expected(StrContextValue::Description("leading digit")))
},
// Report as if they were numbers because its most likely a typo
b'.' => {
float
.map(v::Value::from)
.context(StrContext::Expected(ParserValue::Description("leading digit")))
.context(StrContext::Expected(StrContextValue::Description("leading digit")))
},
b't' => {
crate::parser::numbers::true_.map(v::Value::from)
.context(StrContext::Label("string"))
.context(StrContext::Expected(ParserValue::CharLiteral('"')))
.context(StrContext::Expected(ParserValue::CharLiteral('\'')))
.context(StrContext::Expected(StrContextValue::CharLiteral('"')))
.context(StrContext::Expected(StrContextValue::CharLiteral('\'')))
},
b'f' => {
crate::parser::numbers::false_.map(v::Value::from)
.context(StrContext::Label("string"))
.context(StrContext::Expected(ParserValue::CharLiteral('"')))
.context(StrContext::Expected(ParserValue::CharLiteral('\'')))
.context(StrContext::Expected(StrContextValue::CharLiteral('"')))
.context(StrContext::Expected(StrContextValue::CharLiteral('\'')))
},
b'i' => {
crate::parser::numbers::inf.map(v::Value::from)
.context(StrContext::Label("string"))
.context(StrContext::Expected(ParserValue::CharLiteral('"')))
.context(StrContext::Expected(ParserValue::CharLiteral('\'')))
.context(StrContext::Expected(StrContextValue::CharLiteral('"')))
.context(StrContext::Expected(StrContextValue::CharLiteral('\'')))
},
b'n' => {
crate::parser::numbers::nan.map(v::Value::from)
.context(StrContext::Label("string"))
.context(StrContext::Expected(ParserValue::CharLiteral('"')))
.context(StrContext::Expected(ParserValue::CharLiteral('\'')))
.context(StrContext::Expected(StrContextValue::CharLiteral('"')))
.context(StrContext::Expected(StrContextValue::CharLiteral('\'')))
},
_ => {
fail
.context(StrContext::Label("string"))
.context(StrContext::Expected(ParserValue::CharLiteral('"')))
.context(StrContext::Expected(ParserValue::CharLiteral('\'')))
.context(StrContext::Expected(StrContextValue::CharLiteral('"')))
.context(StrContext::Expected(StrContextValue::CharLiteral('\'')))
},
}
.with_span()
Expand Down

0 comments on commit f19b78f

Please sign in to comment.