Skip to content

Commit

Permalink
Revert "feat(longest): require Location"
Browse files Browse the repository at this point in the history
This reverts commit 5b7b6d2.
  • Loading branch information
urso committed Jan 3, 2024
1 parent 5b7b6d2 commit 07d0447
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
51 changes: 30 additions & 21 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#[cfg(feature = "alloc")]
use crate::lib::std::borrow::ToOwned;
use crate::lib::std::fmt;
use crate::stream::Location;
use core::num::NonZeroUsize;

use crate::stream::AsBStr;
Expand Down Expand Up @@ -755,16 +754,20 @@ impl crate::lib::std::fmt::Display for StrContextValue {

/// Collect context of the longest matching parser while backtracking on errors
#[derive(Clone, Debug)]
pub struct LongestMatch<E>
pub struct LongestMatch<I, E>
where
I: Stream,
<I as Stream>::Checkpoint: Ord,
E: MergeContext,
{
pos: usize,
checkpoint: I::Checkpoint,
inner: E,
}

impl<E> LongestMatch<E>
impl<I, E> LongestMatch<I, E>
where
I: Stream,
<I as Stream>::Checkpoint: Ord,
E: MergeContext,
{
/// Extract the error for the longest matching parser
Expand All @@ -774,27 +777,27 @@ where
}
}

impl<I, E> ParserError<I> for LongestMatch<E>
impl<I, E> ParserError<I> for LongestMatch<I, E>
where
I: Stream + Location,
I: Stream,
<I as Stream>::Checkpoint: Ord,
E: ParserError<I> + MergeContext,
{
#[inline]
fn from_error_kind(input: &I, kind: ErrorKind) -> Self {
Self {
pos: input.location(),
checkpoint: input.checkpoint(),
inner: E::from_error_kind(input, kind),
}
}

#[inline]
fn append(mut self, input: &I, kind: ErrorKind) -> Self {
let pos = input.location();
match pos.cmp(&self.pos) {
let checkpoint = input.checkpoint();
match checkpoint.cmp(&self.checkpoint) {
core::cmp::Ordering::Less => self,
core::cmp::Ordering::Greater => {
self.pos = pos;
self.checkpoint = checkpoint;
self.clear_context();
self.inner = self.inner.append(input, kind);
self
Expand All @@ -812,22 +815,22 @@ where
}
}

impl<I, E, C> AddContext<I, C> for LongestMatch<E>
impl<I, E, C> AddContext<I, C> for LongestMatch<I, E>
where
I: Stream + Location,
I: Stream,
<I as Stream>::Checkpoint: Ord,
E: AddContext<I, C> + MergeContext,
{
#[inline]
fn add_context(mut self, input: &I, ctx: C) -> Self {
let pos = input.location();
match pos.cmp(&self.pos) {
let checkpoint = input.checkpoint();
match checkpoint.cmp(&self.checkpoint) {
core::cmp::Ordering::Less => {}
core::cmp::Ordering::Equal => {
self.inner = self.inner.add_context(input, ctx);
}
core::cmp::Ordering::Greater => {
self.pos = pos;
self.checkpoint = checkpoint;
self.inner.clear_context();
self.inner = self.inner.add_context(input, ctx);
}
Expand All @@ -836,10 +839,14 @@ where
}
}

impl<E: MergeContext> MergeContext for LongestMatch<E> {
impl<I, E: MergeContext> MergeContext for LongestMatch<I, E>
where
I: Stream,
<I as Stream>::Checkpoint: Ord,
{
#[inline]
fn merge_context(mut self, other: Self) -> Self {
match other.pos.cmp(&self.pos) {
match other.checkpoint.cmp(&self.checkpoint) {
core::cmp::Ordering::Less => self,
core::cmp::Ordering::Greater => other,
core::cmp::Ordering::Equal => {
Expand All @@ -850,23 +857,25 @@ impl<E: MergeContext> MergeContext for LongestMatch<E> {
}
}

impl<I, E, EX> FromExternalError<I, EX> for LongestMatch<E>
impl<I, E, EX> FromExternalError<I, EX> for LongestMatch<I, E>
where
I: Stream + Location,
I: Stream,
<I as Stream>::Checkpoint: Ord,
E: FromExternalError<I, EX> + MergeContext,
{
#[inline]
fn from_external_error(input: &I, kind: ErrorKind, e: EX) -> Self {
Self {
pos: input.location(),
checkpoint: input.checkpoint(),
inner: E::from_external_error(input, kind, e),
}
}
}

impl<E> crate::lib::std::fmt::Display for LongestMatch<E>
impl<I, E> crate::lib::std::fmt::Display for LongestMatch<I, E>
where
I: Stream,
<I as Stream>::Checkpoint: Ord,
E: crate::lib::std::fmt::Display + MergeContext,
{
fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result {
Expand Down
3 changes: 2 additions & 1 deletion src/error/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod longest_match {
};

type Input<'a> = Located<&'a str>;
type Error<'a> = LongestMatch<ContextError<&'static str>>;
type Error<'a> = LongestMatch<Input<'a>, ContextError<&'static str>>;
type PResult<'a, O> = crate::error::PResult<O, Error<'a>>;

fn tag<'a>(t: &'static str) -> impl Parser<Input<'a>, &'a str, Error<'a>> {
Expand Down Expand Up @@ -55,6 +55,7 @@ mod longest_match {
}

#[test]
#[ignore]
fn multi_longest_match_eof() {
fn parser<'a>(input: &mut Input<'a>) -> PResult<'a, &'a str> {
alt((syms("abcd"), syms("abc"), syms("def"), syms("defg"))).parse_next(input)
Expand Down

0 comments on commit 07d0447

Please sign in to comment.