Skip to content

Commit

Permalink
refactor(parser): Rename ParserError to ContextError to align with wi…
Browse files Browse the repository at this point in the history
…nnow
  • Loading branch information
epage committed Jul 10, 2023
1 parent f19b78f commit e77ba0e
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 89 deletions.
6 changes: 3 additions & 3 deletions crates/toml_edit/src/parser/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::parser::prelude::*;
// ;; Array

// array = array-open array-values array-close
pub(crate) fn array<'i>(check: RecursionCheck) -> impl Parser<Input<'i>, Array, ParserError<'i>> {
pub(crate) fn array<'i>(check: RecursionCheck) -> impl Parser<Input<'i>, Array, ContextError<'i>> {
move |input| {
delimited(
ARRAY_OPEN,
Expand All @@ -39,7 +39,7 @@ const ARRAY_SEP: u8 = b',';
// array-value / ws-comment-newline ]
pub(crate) fn array_values<'i>(
check: RecursionCheck,
) -> impl Parser<Input<'i>, Array, ParserError<'i>> {
) -> impl Parser<Input<'i>, Array, ContextError<'i>> {
move |input| {
let check = check.recursing(input)?;
(
Expand Down Expand Up @@ -67,7 +67,7 @@ pub(crate) fn array_values<'i>(

pub(crate) fn array_value<'i>(
check: RecursionCheck,
) -> impl Parser<Input<'i>, Value, ParserError<'i>> {
) -> impl Parser<Input<'i>, Value, ContextError<'i>> {
move |input| {
(
ws_comment_newline.span(),
Expand Down
26 changes: 13 additions & 13 deletions crates/toml_edit/src/parser/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use winnow::token::take_while;
// local-date = full-date
// local-time = partial-time
// full-time = partial-time time-offset
pub(crate) fn date_time(input: Input<'_>) -> IResult<Input<'_>, Datetime, ParserError<'_>> {
pub(crate) fn date_time(input: Input<'_>) -> IResult<Input<'_>, Datetime, ContextError<'_>> {
alt((
(full_date, opt((time_delim, partial_time, opt(time_offset))))
.map(|(date, opt)| {
Expand Down Expand Up @@ -48,14 +48,14 @@ pub(crate) fn date_time(input: Input<'_>) -> IResult<Input<'_>, Datetime, Parser
}

// full-date = date-fullyear "-" date-month "-" date-mday
pub(crate) fn full_date(input: Input<'_>) -> IResult<Input<'_>, Date, ParserError<'_>> {
pub(crate) fn full_date(input: Input<'_>) -> IResult<Input<'_>, Date, ContextError<'_>> {
(date_fullyear, b'-', cut_err((date_month, b'-', date_mday)))
.map(|(year, _, (month, _, day))| Date { year, month, day })
.parse_next(input)
}

// partial-time = time-hour ":" time-minute ":" time-second [time-secfrac]
pub(crate) fn partial_time(input: Input<'_>) -> IResult<Input<'_>, Time, ParserError<'_>> {
pub(crate) fn partial_time(input: Input<'_>) -> IResult<Input<'_>, Time, ContextError<'_>> {
(
time_hour,
b':',
Expand All @@ -72,7 +72,7 @@ pub(crate) fn partial_time(input: Input<'_>) -> IResult<Input<'_>, Time, ParserE

// time-offset = "Z" / time-numoffset
// time-numoffset = ( "+" / "-" ) time-hour ":" time-minute
pub(crate) fn time_offset(input: Input<'_>) -> IResult<Input<'_>, Offset, ParserError<'_>> {
pub(crate) fn time_offset(input: Input<'_>) -> IResult<Input<'_>, Offset, ContextError<'_>> {
alt((
one_of((b'Z', b'z')).value(Offset::Z),
(
Expand All @@ -95,14 +95,14 @@ pub(crate) fn time_offset(input: Input<'_>) -> IResult<Input<'_>, Offset, Parser
}

// date-fullyear = 4DIGIT
pub(crate) fn date_fullyear(input: Input<'_>) -> IResult<Input<'_>, u16, ParserError<'_>> {
pub(crate) fn date_fullyear(input: Input<'_>) -> IResult<Input<'_>, u16, ContextError<'_>> {
unsigned_digits::<4, 4>
.map(|s: &str| s.parse::<u16>().expect("4DIGIT should match u8"))
.parse_next(input)
}

// date-month = 2DIGIT ; 01-12
pub(crate) fn date_month(input: Input<'_>) -> IResult<Input<'_>, u8, ParserError<'_>> {
pub(crate) fn date_month(input: Input<'_>) -> IResult<Input<'_>, u8, ContextError<'_>> {
unsigned_digits::<2, 2>
.try_map(|s: &str| {
let d = s.parse::<u8>().expect("2DIGIT should match u8");
Expand All @@ -116,7 +116,7 @@ pub(crate) fn date_month(input: Input<'_>) -> IResult<Input<'_>, u8, ParserError
}

// date-mday = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on month/year
pub(crate) fn date_mday(input: Input<'_>) -> IResult<Input<'_>, u8, ParserError<'_>> {
pub(crate) fn date_mday(input: Input<'_>) -> IResult<Input<'_>, u8, ContextError<'_>> {
unsigned_digits::<2, 2>
.try_map(|s: &str| {
let d = s.parse::<u8>().expect("2DIGIT should match u8");
Expand All @@ -130,14 +130,14 @@ pub(crate) fn date_mday(input: Input<'_>) -> IResult<Input<'_>, u8, ParserError<
}

// time-delim = "T" / %x20 ; T, t, or space
pub(crate) fn time_delim(input: Input<'_>) -> IResult<Input<'_>, u8, ParserError<'_>> {
pub(crate) fn time_delim(input: Input<'_>) -> IResult<Input<'_>, u8, ContextError<'_>> {
one_of(TIME_DELIM).parse_next(input)
}

const TIME_DELIM: (u8, u8, u8) = (b'T', b't', b' ');

// time-hour = 2DIGIT ; 00-23
pub(crate) fn time_hour(input: Input<'_>) -> IResult<Input<'_>, u8, ParserError<'_>> {
pub(crate) fn time_hour(input: Input<'_>) -> IResult<Input<'_>, u8, ContextError<'_>> {
unsigned_digits::<2, 2>
.try_map(|s: &str| {
let d = s.parse::<u8>().expect("2DIGIT should match u8");
Expand All @@ -151,7 +151,7 @@ pub(crate) fn time_hour(input: Input<'_>) -> IResult<Input<'_>, u8, ParserError<
}

// time-minute = 2DIGIT ; 00-59
pub(crate) fn time_minute(input: Input<'_>) -> IResult<Input<'_>, u8, ParserError<'_>> {
pub(crate) fn time_minute(input: Input<'_>) -> IResult<Input<'_>, u8, ContextError<'_>> {
unsigned_digits::<2, 2>
.try_map(|s: &str| {
let d = s.parse::<u8>().expect("2DIGIT should match u8");
Expand All @@ -165,7 +165,7 @@ pub(crate) fn time_minute(input: Input<'_>) -> IResult<Input<'_>, u8, ParserErro
}

// time-second = 2DIGIT ; 00-58, 00-59, 00-60 based on leap second rules
pub(crate) fn time_second(input: Input<'_>) -> IResult<Input<'_>, u8, ParserError<'_>> {
pub(crate) fn time_second(input: Input<'_>) -> IResult<Input<'_>, u8, ContextError<'_>> {
unsigned_digits::<2, 2>
.try_map(|s: &str| {
let d = s.parse::<u8>().expect("2DIGIT should match u8");
Expand All @@ -179,7 +179,7 @@ pub(crate) fn time_second(input: Input<'_>) -> IResult<Input<'_>, u8, ParserErro
}

// time-secfrac = "." 1*DIGIT
pub(crate) fn time_secfrac(input: Input<'_>) -> IResult<Input<'_>, u32, ParserError<'_>> {
pub(crate) fn time_secfrac(input: Input<'_>) -> IResult<Input<'_>, u32, ContextError<'_>> {
static SCALE: [u32; 10] = [
0,
100_000_000,
Expand Down Expand Up @@ -216,7 +216,7 @@ pub(crate) fn time_secfrac(input: Input<'_>) -> IResult<Input<'_>, u32, ParserEr

pub(crate) fn unsigned_digits<const MIN: usize, const MAX: usize>(
input: Input<'_>,
) -> IResult<Input<'_>, &str, ParserError<'_>> {
) -> IResult<Input<'_>, &str, ContextError<'_>> {
take_while(MIN..=MAX, DIGIT)
.map(|b: &[u8]| unsafe { from_utf8_unchecked(b, "`is_ascii_digit` filters out on-ASCII") })
.parse_next(input)
Expand Down
14 changes: 7 additions & 7 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 keyval ws [ comment ] ) /
// ( ws table ws [ comment ] ) /
// ws )
pub(crate) fn document(input: Input<'_>) -> IResult<Input<'_>, Document, ParserError<'_>> {
pub(crate) fn document(input: Input<'_>) -> IResult<Input<'_>, Document, ContextError<'_>> {
let state = RefCell::new(ParseState::default());
let state_ref = &state;

Expand All @@ -57,7 +57,7 @@ pub(crate) fn document(input: Input<'_>) -> IResult<Input<'_>, Document, ParserE
.into_document()
.map(|document| (i, document))
.map_err(|err| {
winnow::error::ErrMode::Backtrack(ParserError::from_external_error(
winnow::error::ErrMode::Backtrack(ContextError::from_external_error(
i,
winnow::error::ErrorKind::Verify,
err,
Expand All @@ -67,7 +67,7 @@ pub(crate) fn document(input: Input<'_>) -> IResult<Input<'_>, Document, ParserE

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

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

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

pub(crate) fn keyval<'s, 'i>(
state: &'s RefCell<ParseState>,
) -> impl Parser<Input<'i>, (), ParserError<'i>> + 's {
) -> impl Parser<Input<'i>, (), ContextError<'i>> + 's {
move |i| {
parse_keyval
.try_map(|(p, kv)| state.borrow_mut().on_keyval(p, kv))
Expand All @@ -112,7 +112,7 @@ pub(crate) fn keyval<'s, 'i>(
// keyval = key keyval-sep val
pub(crate) fn parse_keyval(
input: Input<'_>,
) -> IResult<Input<'_>, (Vec<Key>, TableKeyValue), ParserError<'_>> {
) -> IResult<Input<'_>, (Vec<Key>, TableKeyValue), ContextError<'_>> {
(
key,
cut_err((
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 @@ -16,7 +16,7 @@ pub struct TomlError {
}

impl TomlError {
pub(crate) fn new(error: ParserError<'_>, original: Input<'_>) -> Self {
pub(crate) fn new(error: ContextError<'_>, original: Input<'_>) -> Self {
use winnow::stream::Offset;
use winnow::stream::Stream;

Expand Down Expand Up @@ -147,13 +147,13 @@ impl StdError for TomlError {
}

#[derive(Debug)]
pub(crate) struct ParserError<'b> {
pub(crate) struct ContextError<'b> {
input: Input<'b>,
context: Vec<StrContext>,
cause: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
}

impl<'b> winnow::error::ParserError<Input<'b>> for ParserError<'b> {
impl<'b> winnow::error::ParserError<Input<'b>> for ContextError<'b> {
fn from_error_kind(input: Input<'b>, _kind: winnow::error::ErrorKind) -> Self {
Self {
input,
Expand All @@ -171,7 +171,7 @@ impl<'b> winnow::error::ParserError<Input<'b>> for ParserError<'b> {
}
}

impl<'b> winnow::error::ParserError<&'b str> for ParserError<'b> {
impl<'b> winnow::error::ParserError<&'b str> for ContextError<'b> {
fn from_error_kind(input: &'b str, _kind: winnow::error::ErrorKind) -> Self {
Self {
input: Input::new(BStr::new(input)),
Expand All @@ -189,15 +189,15 @@ impl<'b> winnow::error::ParserError<&'b str> for ParserError<'b> {
}
}

impl<'b> winnow::error::AddContext<Input<'b>, StrContext> for ParserError<'b> {
impl<'b> winnow::error::AddContext<Input<'b>, StrContext> for ContextError<'b> {
fn add_context(mut self, _input: Input<'b>, ctx: StrContext) -> Self {
self.context.push(ctx);
self
}
}

impl<'b, E: std::error::Error + Send + Sync + 'static>
winnow::error::FromExternalError<Input<'b>, E> for ParserError<'b>
winnow::error::FromExternalError<Input<'b>, E> for ContextError<'b>
{
fn from_external_error(input: Input<'b>, _kind: winnow::error::ErrorKind, e: E) -> Self {
Self {
Expand All @@ -209,7 +209,7 @@ impl<'b, E: std::error::Error + Send + Sync + 'static>
}

impl<'b, E: std::error::Error + Send + Sync + 'static> winnow::error::FromExternalError<&'b str, E>
for ParserError<'b>
for ContextError<'b>
{
fn from_external_error(input: &'b str, _kind: winnow::error::ErrorKind, e: E) -> Self {
Self {
Expand All @@ -221,7 +221,7 @@ impl<'b, E: std::error::Error + Send + Sync + 'static> winnow::error::FromExtern
}

// For tests
impl<'b> std::cmp::PartialEq for ParserError<'b> {
impl<'b> std::cmp::PartialEq for ContextError<'b> {
fn eq(&self, other: &Self) -> bool {
self.input == other.input
&& self.context == other.context
Expand All @@ -230,7 +230,7 @@ impl<'b> std::cmp::PartialEq for ParserError<'b> {
}
}

impl<'a> std::fmt::Display for ParserError<'a> {
impl<'a> std::fmt::Display for ContextError<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let expression = self.context.iter().find_map(|c| match c {
StrContext::Label(c) => Some(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 @@ -19,7 +19,7 @@ use indexmap::map::Entry;
// inline-table = inline-table-open inline-table-keyvals inline-table-close
pub(crate) fn inline_table<'i>(
check: RecursionCheck,
) -> impl Parser<Input<'i>, InlineTable, ParserError<'i>> {
) -> impl Parser<Input<'i>, InlineTable, ContextError<'i>> {
move |input| {
delimited(
INLINE_TABLE_OPEN,
Expand Down Expand Up @@ -98,7 +98,7 @@ pub(crate) const KEYVAL_SEP: u8 = b'=';

fn inline_table_keyvals<'i>(
check: RecursionCheck,
) -> impl Parser<Input<'i>, (Vec<(Vec<Key>, TableKeyValue)>, RawString), ParserError<'i>> {
) -> impl Parser<Input<'i>, (Vec<(Vec<Key>, TableKeyValue)>, RawString), ContextError<'i>> {
move |input| {
let check = check.recursing(input)?;
(
Expand All @@ -111,7 +111,7 @@ fn inline_table_keyvals<'i>(

fn keyval<'i>(
check: RecursionCheck,
) -> impl Parser<Input<'i>, (Vec<Key>, TableKeyValue), ParserError<'i>> {
) -> impl Parser<Input<'i>, (Vec<Key>, TableKeyValue), ContextError<'i>> {
move |input| {
(
key,
Expand Down
6 changes: 3 additions & 3 deletions crates/toml_edit/src/parser/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::RawString;

// key = simple-key / dotted-key
// dotted-key = simple-key 1*( dot-sep simple-key )
pub(crate) fn key(input: Input<'_>) -> IResult<Input<'_>, Vec<Key>, ParserError<'_>> {
pub(crate) fn key(input: Input<'_>) -> IResult<Input<'_>, Vec<Key>, ContextError<'_>> {
separated1(
(ws.span(), simple_key, ws.span()).map(|(pre, (raw, key), suffix)| {
Key::new(key)
Expand All @@ -41,7 +41,7 @@ pub(crate) fn key(input: Input<'_>) -> IResult<Input<'_>, Vec<Key>, ParserError<
// quoted-key = basic-string / literal-string
pub(crate) fn simple_key(
input: Input<'_>,
) -> IResult<Input<'_>, (RawString, InternalString), ParserError<'_>> {
) -> IResult<Input<'_>, (RawString, InternalString), ContextError<'_>> {
dispatch! {peek(any);
crate::parser::strings::QUOTATION_MARK => basic_string
.map(|s: std::borrow::Cow<'_, str>| s.as_ref().into()),
Expand All @@ -57,7 +57,7 @@ pub(crate) fn simple_key(
}

// unquoted-key = 1*( ALPHA / DIGIT / %x2D / %x5F ) ; A-Z / a-z / 0-9 / - / _
fn unquoted_key(input: Input<'_>) -> IResult<Input<'_>, &str, ParserError<'_>> {
fn unquoted_key(input: Input<'_>) -> IResult<Input<'_>, &str, ContextError<'_>> {
take_while(1.., UNQUOTED_CHAR)
.map(|b| unsafe { from_utf8_unchecked(b, "`is_unquoted_char` filters out on-ASCII") })
.parse_next(input)
Expand Down
6 changes: 3 additions & 3 deletions crates/toml_edit/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ 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::ContextError;
pub(crate) use super::errors::StrContext;
pub(crate) use super::errors::StrContextValue;
pub(crate) use winnow::combinator::dispatch;
Expand Down Expand Up @@ -115,7 +115,7 @@ pub(crate) mod prelude {
pub(crate) fn recursing(
mut self,
input: Input<'_>,
) -> Result<Self, winnow::error::ErrMode<ParserError<'_>>> {
) -> Result<Self, winnow::error::ErrMode<ContextError<'_>>> {
self.current += 1;
if self.current < 128 {
Ok(self)
Expand Down Expand Up @@ -144,7 +144,7 @@ pub(crate) mod prelude {
pub(crate) fn recursing(
self,
_input: Input<'_>,
) -> Result<Self, winnow::error::ErrMode<ParserError<'_>>> {
) -> Result<Self, winnow::error::ErrMode<ContextError<'_>>> {
Ok(self)
}
}
Expand Down
Loading

0 comments on commit e77ba0e

Please sign in to comment.