pub use crate::stream::{BoxedExactSizeStream, BoxedStream, Stream};
use core::cell::RefCell;
use super::*;
#[cfg(feature = "memoization")]
use hashbrown::HashMap;
#[cfg(feature = "std")]
use std::io::{BufReader, Read, Seek};
pub trait Input<'a>: Sealed + 'a {
#[doc(hidden)]
type Offset: Copy + Hash + Ord + Into<usize>;
type Token;
type Span: Span;
#[doc(hidden)]
fn start(&self) -> Self::Offset;
#[doc(hidden)]
type TokenMaybe: Borrow<Self::Token> + Into<MaybeRef<'a, Self::Token>>;
#[doc(hidden)]
unsafe fn next_maybe(&self, offset: Self::Offset) -> (Self::Offset, Option<Self::TokenMaybe>);
#[doc(hidden)]
unsafe fn span(&self, range: Range<Self::Offset>) -> Self::Span;
#[doc(hidden)]
fn prev(offs: Self::Offset) -> Self::Offset;
fn spanned<T, S>(self, eoi: S) -> SpannedInput<T, S, Self>
where
Self: Input<'a, Token = (T, S)> + Sized,
T: 'a,
S: Span + Clone + 'a,
{
SpannedInput {
input: self,
eoi,
phantom: PhantomData,
}
}
fn with_context<S: Span>(self, context: S::Context) -> WithContext<S, Self>
where
Self: Sized,
{
WithContext {
input: self,
context,
phantom: PhantomData,
}
}
fn map_span<S: Span, F>(self, map_fn: F) -> MappedSpan<S, Self, F>
where
Self: Input<'a> + Sized,
F: Fn(Self::Span) -> S,
{
MappedSpan {
input: self,
map_fn,
phantom: PhantomData,
}
}
}
pub trait ExactSizeInput<'a>: Input<'a> {
#[doc(hidden)]
unsafe fn span_from(&self, range: RangeFrom<Self::Offset>) -> Self::Span;
}
pub trait SliceInput<'a>: ExactSizeInput<'a> {
type Slice;
#[doc(hidden)]
fn full_slice(&self) -> Self::Slice;
#[doc(hidden)]
fn slice(&self, range: Range<Self::Offset>) -> Self::Slice;
#[doc(hidden)]
fn slice_from(&self, from: RangeFrom<Self::Offset>) -> Self::Slice;
}
pub trait StrInput<'a, C: Char>:
ValueInput<'a, Offset = usize, Token = C> + SliceInput<'a, Slice = &'a C::Str>
{
}
pub trait ValueInput<'a>: Input<'a> {
#[doc(hidden)]
unsafe fn next(&self, offset: Self::Offset) -> (Self::Offset, Option<Self::Token>);
}
pub trait BorrowInput<'a>: Input<'a> {
#[doc(hidden)]
unsafe fn next_ref(&self, offset: Self::Offset) -> (Self::Offset, Option<&'a Self::Token>);
}
impl<'a> Sealed for &'a str {}
impl<'a> Input<'a> for &'a str {
type Offset = usize;
type Token = char;
type Span = SimpleSpan<usize>;
#[inline]
fn start(&self) -> Self::Offset {
0
}
type TokenMaybe = char;
#[inline(always)]
unsafe fn next_maybe(&self, offset: Self::Offset) -> (Self::Offset, Option<Self::TokenMaybe>) {
self.next(offset)
}
#[inline(always)]
unsafe fn span(&self, range: Range<Self::Offset>) -> Self::Span {
range.into()
}
#[inline(always)]
fn prev(offs: Self::Offset) -> Self::Offset {
offs.saturating_sub(1)
}
}
impl<'a> ExactSizeInput<'a> for &'a str {
#[inline(always)]
unsafe fn span_from(&self, range: RangeFrom<Self::Offset>) -> Self::Span {
(range.start..self.len()).into()
}
}
impl<'a> ValueInput<'a> for &'a str {
#[inline(always)]
unsafe fn next(&self, offset: Self::Offset) -> (Self::Offset, Option<Self::Token>) {
if offset < self.len() {
let c = unsafe {
self.get_unchecked(offset..)
.chars()
.next()
.unwrap_unchecked()
};
(offset + c.len_utf8(), Some(c))
} else {
(offset, None)
}
}
}
impl<'a> StrInput<'a, char> for &'a str {}
impl<'a> SliceInput<'a> for &'a str {
type Slice = &'a str;
#[inline(always)]
fn full_slice(&self) -> Self::Slice {
*self
}
#[inline(always)]
fn slice(&self, range: Range<Self::Offset>) -> Self::Slice {
&self[range]
}
#[inline(always)]
fn slice_from(&self, from: RangeFrom<Self::Offset>) -> Self::Slice {
&self[from]
}
}
impl<'a, T> Sealed for &'a [T] {}
impl<'a, T> Input<'a> for &'a [T] {
type Offset = usize;
type Token = T;
type Span = SimpleSpan<usize>;
#[inline(always)]
fn start(&self) -> Self::Offset {
0
}
type TokenMaybe = &'a T;
#[inline(always)]
unsafe fn next_maybe(&self, offset: Self::Offset) -> (Self::Offset, Option<Self::TokenMaybe>) {
self.next_ref(offset)
}
#[inline(always)]
unsafe fn span(&self, range: Range<Self::Offset>) -> Self::Span {
range.into()
}
#[inline(always)]
fn prev(offs: Self::Offset) -> Self::Offset {
offs.saturating_sub(1)
}
}
impl<'a, T> ExactSizeInput<'a> for &'a [T] {
#[inline(always)]
unsafe fn span_from(&self, range: RangeFrom<Self::Offset>) -> Self::Span {
(range.start..self.len()).into()
}
}
impl<'a> StrInput<'a, u8> for &'a [u8] {}
impl<'a, T> SliceInput<'a> for &'a [T] {
type Slice = &'a [T];
#[inline(always)]
fn full_slice(&self) -> Self::Slice {
*self
}
#[inline(always)]
fn slice(&self, range: Range<Self::Offset>) -> Self::Slice {
&self[range]
}
#[inline(always)]
fn slice_from(&self, from: RangeFrom<Self::Offset>) -> Self::Slice {
&self[from]
}
}
impl<'a, T: Clone> ValueInput<'a> for &'a [T] {
#[inline(always)]
unsafe fn next(&self, offset: Self::Offset) -> (Self::Offset, Option<Self::Token>) {
if let Some(tok) = self.get(offset) {
(offset + 1, Some(tok.clone()))
} else {
(offset, None)
}
}
}
impl<'a, T> BorrowInput<'a> for &'a [T] {
#[inline(always)]
unsafe fn next_ref(&self, offset: Self::Offset) -> (Self::Offset, Option<&'a Self::Token>) {
if let Some(tok) = self.get(offset) {
(offset + 1, Some(tok))
} else {
(offset, None)
}
}
}
impl<'a, T: 'a, const N: usize> Sealed for &'a [T; N] {}
impl<'a, T: 'a, const N: usize> Input<'a> for &'a [T; N] {
type Offset = usize;
type Token = T;
type Span = SimpleSpan<usize>;
#[inline(always)]
fn start(&self) -> Self::Offset {
0
}
type TokenMaybe = &'a T;
#[inline(always)]
unsafe fn next_maybe(&self, offset: Self::Offset) -> (Self::Offset, Option<Self::TokenMaybe>) {
self.next_ref(offset)
}
#[inline(always)]
unsafe fn span(&self, range: Range<Self::Offset>) -> Self::Span {
range.into()
}
#[inline(always)]
fn prev(offs: Self::Offset) -> Self::Offset {
offs.saturating_sub(1)
}
}
impl<'a, T: 'a, const N: usize> ExactSizeInput<'a> for &'a [T; N] {
#[inline(always)]
unsafe fn span_from(&self, range: RangeFrom<Self::Offset>) -> Self::Span {
(range.start..N).into()
}
}
impl<'a, const N: usize> StrInput<'a, u8> for &'a [u8; N] {}
impl<'a, T: 'a, const N: usize> SliceInput<'a> for &'a [T; N] {
type Slice = &'a [T];
#[inline(always)]
fn full_slice(&self) -> Self::Slice {
*self
}
#[inline(always)]
fn slice(&self, range: Range<Self::Offset>) -> Self::Slice {
&self[range]
}
#[inline(always)]
fn slice_from(&self, from: RangeFrom<Self::Offset>) -> Self::Slice {
&self[from]
}
}
impl<'a, T: Clone + 'a, const N: usize> ValueInput<'a> for &'a [T; N] {
#[inline(always)]
unsafe fn next(&self, offset: Self::Offset) -> (Self::Offset, Option<Self::Token>) {
if let Some(tok) = self.get(offset) {
(offset + 1, Some(tok.clone()))
} else {
(offset, None)
}
}
}
impl<'a, T: 'a, const N: usize> BorrowInput<'a> for &'a [T; N] {
#[inline(always)]
unsafe fn next_ref(&self, offset: Self::Offset) -> (Self::Offset, Option<&'a Self::Token>) {
if let Some(tok) = self.get(offset) {
(offset + 1, Some(tok))
} else {
(offset, None)
}
}
}
#[derive(Copy, Clone)]
pub struct SpannedInput<T, S, I> {
input: I,
eoi: S,
phantom: PhantomData<T>,
}
#[doc(hidden)]
pub struct SpannedTokenMaybe<'a, I: Input<'a>, T, S>(I::TokenMaybe, PhantomData<(T, S)>);
impl<'a, I: Input<'a, Token = (T, S)>, T, S> Borrow<T> for SpannedTokenMaybe<'a, I, T, S> {
#[inline(always)]
fn borrow(&self) -> &T {
&self.0.borrow().0
}
}
impl<'a, I: Input<'a, Token = (T, S)>, T, S: 'a> From<SpannedTokenMaybe<'a, I, T, S>>
for MaybeRef<'a, T>
{
#[inline(always)]
fn from(st: SpannedTokenMaybe<'a, I, T, S>) -> MaybeRef<'a, T> {
match st.0.into() {
MaybeRef::Ref((tok, _)) => MaybeRef::Ref(tok),
MaybeRef::Val((tok, _)) => MaybeRef::Val(tok),
}
}
}
impl<'a, T, S, I: Input<'a>> Sealed for SpannedInput<T, S, I> {}
impl<'a, T, S, I> Input<'a> for SpannedInput<T, S, I>
where
I: Input<'a, Token = (T, S)>,
T: 'a,
S: Span + Clone + 'a,
{
type Offset = I::Offset;
type Token = T;
type Span = S;
#[inline(always)]
fn start(&self) -> Self::Offset {
self.input.start()
}
type TokenMaybe = SpannedTokenMaybe<'a, I, T, S>;
#[inline(always)]
unsafe fn next_maybe(&self, offset: Self::Offset) -> (Self::Offset, Option<Self::TokenMaybe>) {
let (offset, tok) = self.input.next_maybe(offset);
(offset, tok.map(|tok| SpannedTokenMaybe(tok, PhantomData)))
}
#[inline(always)]
unsafe fn span(&self, range: Range<Self::Offset>) -> Self::Span {
let start = self
.input
.next_maybe(range.start)
.1
.map_or(self.eoi.start(), |tok| tok.borrow().1.start());
let end = self
.input
.next_maybe(I::prev(range.end))
.1
.map_or(self.eoi.start(), |tok| tok.borrow().1.end());
S::new(self.eoi.context(), start..end)
}
#[inline(always)]
fn prev(offs: Self::Offset) -> Self::Offset {
I::prev(offs)
}
}
impl<'a, T, S, I> ExactSizeInput<'a> for SpannedInput<T, S, I>
where
I: ExactSizeInput<'a, Token = (T, S)>,
T: 'a,
S: Span + Clone + 'a,
{
#[inline(always)]
unsafe fn span_from(&self, range: RangeFrom<Self::Offset>) -> Self::Span {
let start = self
.input
.next_maybe(range.start)
.1
.map_or(self.eoi.start(), |tok| tok.borrow().1.start());
S::new(self.eoi.context(), start..self.eoi.start())
}
}
impl<'a, T, S, I> ValueInput<'a> for SpannedInput<T, S, I>
where
I: ValueInput<'a, Token = (T, S)>,
T: 'a,
S: Span + Clone + 'a,
{
#[inline(always)]
unsafe fn next(&self, offset: Self::Offset) -> (Self::Offset, Option<Self::Token>) {
let (offs, tok) = self.input.next(offset);
(offs, tok.map(|(tok, _)| tok))
}
}
impl<'a, T, S, I> BorrowInput<'a> for SpannedInput<T, S, I>
where
I: Input<'a> + BorrowInput<'a, Token = (T, S)>,
T: 'a,
S: Span + Clone + 'a,
{
#[inline(always)]
unsafe fn next_ref(&self, offset: Self::Offset) -> (Self::Offset, Option<&'a Self::Token>) {
let (offs, tok) = self.input.next_ref(offset);
(offs, tok.map(|(tok, _)| tok))
}
}
impl<'a, T, S, I> SliceInput<'a> for SpannedInput<T, S, I>
where
I: Input<'a> + SliceInput<'a, Token = (T, S)>,
T: 'a,
S: Span + Clone + 'a,
{
type Slice = I::Slice;
#[inline(always)]
fn full_slice(&self) -> Self::Slice {
<I as SliceInput>::full_slice(&self.input)
}
#[inline(always)]
fn slice(&self, range: Range<Self::Offset>) -> Self::Slice {
<I as SliceInput>::slice(&self.input, range)
}
#[inline(always)]
fn slice_from(&self, from: RangeFrom<Self::Offset>) -> Self::Slice {
<I as SliceInput>::slice_from(&self.input, from)
}
}
#[derive(Copy, Clone)]
pub struct WithContext<S: Span, I> {
input: I,
context: S::Context,
phantom: PhantomData<S>,
}
impl<S: Span, I> Sealed for WithContext<S, I> {}
impl<'a, S, I: Input<'a>> Input<'a> for WithContext<S, I>
where
S: Span + Clone + 'a,
S::Context: Clone + 'a,
S::Offset: From<<I::Span as Span>::Offset>,
{
type Offset = I::Offset;
type Token = I::Token;
type Span = S;
#[inline(always)]
fn start(&self) -> Self::Offset {
self.input.start()
}
type TokenMaybe = I::TokenMaybe;
#[inline(always)]
unsafe fn next_maybe(&self, offset: Self::Offset) -> (Self::Offset, Option<Self::TokenMaybe>) {
self.input.next_maybe(offset)
}
#[inline(always)]
unsafe fn span(&self, range: Range<Self::Offset>) -> Self::Span {
let inner_span = self.input.span(range);
Span::new(
self.context.clone(),
inner_span.start().into()..inner_span.end().into(),
)
}
#[inline(always)]
fn prev(offs: Self::Offset) -> Self::Offset {
I::prev(offs)
}
}
impl<'a, S, I: Input<'a>> ExactSizeInput<'a> for WithContext<S, I>
where
I: ExactSizeInput<'a>,
S: Span + Clone + 'a,
S::Context: Clone + 'a,
S::Offset: From<<I::Span as Span>::Offset>,
{
#[inline(always)]
unsafe fn span_from(&self, range: RangeFrom<Self::Offset>) -> Self::Span {
let inner_span = self.input.span_from(range);
Span::new(
self.context.clone(),
inner_span.start().into()..inner_span.end().into(),
)
}
}
impl<'a, S, I: ValueInput<'a>> ValueInput<'a> for WithContext<S, I>
where
S: Span + Clone + 'a,
S::Context: Clone + 'a,
S::Offset: From<<I::Span as Span>::Offset>,
{
#[inline(always)]
unsafe fn next(&self, offset: Self::Offset) -> (Self::Offset, Option<Self::Token>) {
self.input.next(offset)
}
}
impl<'a, S, I: BorrowInput<'a>> BorrowInput<'a> for WithContext<S, I>
where
S: Span + Clone + 'a,
S::Context: Clone + 'a,
S::Offset: From<<I::Span as Span>::Offset>,
{
#[inline(always)]
unsafe fn next_ref(&self, offset: Self::Offset) -> (Self::Offset, Option<&'a Self::Token>) {
self.input.next_ref(offset)
}
}
impl<'a, S, I: SliceInput<'a>> SliceInput<'a> for WithContext<S, I>
where
S: Span + Clone + 'a,
S::Context: Clone + 'a,
S::Offset: From<<I::Span as Span>::Offset>,
{
type Slice = I::Slice;
#[inline(always)]
fn full_slice(&self) -> Self::Slice {
<I as SliceInput>::full_slice(&self.input)
}
#[inline(always)]
fn slice(&self, range: Range<Self::Offset>) -> Self::Slice {
<I as SliceInput>::slice(&self.input, range)
}
#[inline(always)]
fn slice_from(&self, from: RangeFrom<Self::Offset>) -> Self::Slice {
<I as SliceInput>::slice_from(&self.input, from)
}
}
impl<'a, C, S, I> StrInput<'a, C> for WithContext<S, I>
where
I: StrInput<'a, C>,
S: Span + Clone + 'a,
S::Context: Clone + 'a,
S::Offset: From<<I::Span as Span>::Offset>,
C: Char,
{
}
#[derive(Copy, Clone)]
pub struct MappedSpan<S: Span, I, F> {
input: I,
map_fn: F,
phantom: PhantomData<S>,
}
impl<'a, S: Span, I: Input<'a>, F> Sealed for MappedSpan<S, I, F> {}
impl<'a, S, I: Input<'a>, F: 'a> Input<'a> for MappedSpan<S, I, F>
where
S: Span + Clone + 'a,
S::Context: Clone + 'a,
S::Offset: From<<I::Span as Span>::Offset>,
F: Fn(I::Span) -> S,
{
type Offset = I::Offset;
type Token = I::Token;
type Span = S;
#[inline(always)]
fn start(&self) -> Self::Offset {
self.input.start()
}
type TokenMaybe = I::TokenMaybe;
#[inline(always)]
unsafe fn next_maybe(&self, offset: Self::Offset) -> (Self::Offset, Option<Self::TokenMaybe>) {
self.input.next_maybe(offset)
}
#[inline(always)]
unsafe fn span(&self, range: Range<Self::Offset>) -> Self::Span {
let inner_span = self.input.span(range);
(self.map_fn)(inner_span)
}
#[inline(always)]
fn prev(offs: Self::Offset) -> Self::Offset {
I::prev(offs)
}
}
impl<'a, S, I: Input<'a>, F: 'a> ExactSizeInput<'a> for MappedSpan<S, I, F>
where
I: ExactSizeInput<'a>,
S: Span + Clone + 'a,
S::Context: Clone + 'a,
S::Offset: From<<I::Span as Span>::Offset>,
F: Fn(I::Span) -> S,
{
#[inline(always)]
unsafe fn span_from(&self, range: RangeFrom<Self::Offset>) -> Self::Span {
let inner_span = self.input.span_from(range);
(self.map_fn)(inner_span)
}
}
impl<'a, S, I: ValueInput<'a>, F: 'a> ValueInput<'a> for MappedSpan<S, I, F>
where
S: Span + Clone + 'a,
S::Context: Clone + 'a,
S::Offset: From<<I::Span as Span>::Offset>,
F: Fn(I::Span) -> S,
{
#[inline(always)]
unsafe fn next(&self, offset: Self::Offset) -> (Self::Offset, Option<Self::Token>) {
self.input.next(offset)
}
}
impl<'a, S, I: BorrowInput<'a>, F: 'a> BorrowInput<'a> for MappedSpan<S, I, F>
where
S: Span + Clone + 'a,
S::Context: Clone + 'a,
S::Offset: From<<I::Span as Span>::Offset>,
F: Fn(I::Span) -> S,
{
#[inline(always)]
unsafe fn next_ref(&self, offset: Self::Offset) -> (Self::Offset, Option<&'a Self::Token>) {
self.input.next_ref(offset)
}
}
impl<'a, S, I: SliceInput<'a>, F: 'a> SliceInput<'a> for MappedSpan<S, I, F>
where
S: Span + Clone + 'a,
S::Context: Clone + 'a,
S::Offset: From<<I::Span as Span>::Offset>,
F: Fn(I::Span) -> S,
{
type Slice = I::Slice;
#[inline(always)]
fn full_slice(&self) -> Self::Slice {
<I as SliceInput>::full_slice(&self.input)
}
#[inline(always)]
fn slice(&self, range: Range<Self::Offset>) -> Self::Slice {
<I as SliceInput>::slice(&self.input, range)
}
#[inline(always)]
fn slice_from(&self, from: RangeFrom<Self::Offset>) -> Self::Slice {
<I as SliceInput>::slice_from(&self.input, from)
}
}
impl<'a, C, S, I, F: 'a> StrInput<'a, C> for MappedSpan<S, I, F>
where
I: StrInput<'a, C>,
S: Span + Clone + 'a,
S::Context: Clone + 'a,
S::Offset: From<<I::Span as Span>::Offset>,
F: Fn(I::Span) -> S,
C: Char,
{
}
#[cfg(feature = "std")]
struct IoInner<R> {
reader: BufReader<R>,
last_offset: usize,
}
#[cfg(feature = "std")]
pub struct IoInput<R>(RefCell<IoInner<R>>);
#[cfg(feature = "std")]
impl<R: Read + Seek> IoInput<R> {
pub fn new(reader: R) -> IoInput<R> {
IoInput(RefCell::new(IoInner {
reader: BufReader::new(reader),
last_offset: 0,
}))
}
}
#[cfg(feature = "std")]
impl<R: Read + Seek> Sealed for IoInput<R> {}
#[cfg(feature = "std")]
impl<'a, R: Read + Seek + 'a> Input<'a> for IoInput<R> {
type Offset = usize;
type Token = u8;
type Span = SimpleSpan;
fn start(&self) -> Self::Offset {
0
}
type TokenMaybe = u8;
unsafe fn next_maybe(&self, offset: Self::Offset) -> (Self::Offset, Option<Self::TokenMaybe>) {
Self::next(self, offset)
}
unsafe fn span(&self, range: Range<Self::Offset>) -> Self::Span {
SimpleSpan::from(range)
}
fn prev(offs: Self::Offset) -> Self::Offset {
offs.saturating_sub(1)
}
}
#[cfg(feature = "std")]
impl<'a, R: Read + Seek + 'a> ValueInput<'a> for IoInput<R> {
unsafe fn next(&self, offset: Self::Offset) -> (Self::Offset, Option<Self::Token>) {
let mut inner = self.0.borrow_mut();
if offset != inner.last_offset {
let seek = offset as i64 - inner.last_offset as i64;
inner.reader.seek_relative(seek).unwrap();
inner.last_offset = offset;
}
let mut out = 0;
let r = inner.reader.read_exact(std::slice::from_mut(&mut out));
match r {
Ok(()) => {
inner.last_offset += 1;
(offset + 1, Some(out))
}
Err(_) => (offset, None),
}
}
}
pub struct Marker<'a, 'parse, I: Input<'a>> {
pub(crate) offset: I::Offset,
pub(crate) err_count: usize,
phantom: PhantomData<fn(&'parse ()) -> &'parse ()>, }
impl<'a, 'parse, I: Input<'a>> Marker<'a, 'parse, I> {
pub fn offset(self) -> Offset<'a, 'parse, I> {
Offset {
offset: self.offset,
phantom: PhantomData,
}
}
}
impl<'a, 'parse, I: Input<'a>> Copy for Marker<'a, 'parse, I> {}
impl<'a, 'parse, I: Input<'a>> Clone for Marker<'a, 'parse, I> {
#[inline(always)]
fn clone(&self) -> Self {
*self
}
}
pub struct Offset<'a, 'parse, I: Input<'a>> {
pub(crate) offset: I::Offset,
phantom: PhantomData<fn(&'parse ()) -> &'parse ()>, }
impl<'a, 'parse, I: Input<'a>> Copy for Offset<'a, 'parse, I> {}
impl<'a, 'parse, I: Input<'a>> Clone for Offset<'a, 'parse, I> {
#[inline(always)]
fn clone(&self) -> Self {
*self
}
}
impl<'a, 'parse, I: Input<'a>> Eq for Offset<'a, 'parse, I> {}
impl<'a, 'parse, I: Input<'a>> PartialEq for Offset<'a, 'parse, I> {
fn eq(&self, other: &Self) -> bool {
self.offset == other.offset
}
}
impl<'a, 'parse, I: Input<'a>> PartialOrd for Offset<'a, 'parse, I> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<'a, 'parse, I: Input<'a>> Ord for Offset<'a, 'parse, I> {
fn cmp(&self, other: &Self) -> Ordering {
self.offset.cmp(&other.offset)
}
}
pub(crate) struct Errors<T, E> {
pub(crate) alt: Option<Located<T, E>>,
pub(crate) secondary: Vec<Located<T, E>>,
}
impl<T, E> Errors<T, E> {
#[inline]
pub(crate) fn secondary_errors_since(&mut self, err_count: usize) -> &mut [Located<T, E>] {
self.secondary.get_mut(err_count..).unwrap_or(&mut [])
}
}
impl<T, E> Default for Errors<T, E> {
fn default() -> Self {
Self {
alt: None,
secondary: Vec::new(),
}
}
}
pub(crate) struct InputOwn<'a, 's, I: Input<'a>, E: ParserExtra<'a, I>> {
pub(crate) input: I,
pub(crate) errors: Errors<I::Offset, E::Error>,
pub(crate) state: MaybeMut<'s, E::State>,
pub(crate) ctx: E::Context,
#[cfg(feature = "memoization")]
pub(crate) memos: HashMap<(I::Offset, usize), Option<Located<I::Offset, E::Error>>>,
}
impl<'a, 's, I, E> InputOwn<'a, 's, I, E>
where
I: Input<'a>,
E: ParserExtra<'a, I>,
{
#[cfg_attr(not(test), allow(dead_code))]
pub(crate) fn new(input: I) -> InputOwn<'a, 's, I, E>
where
E::State: Default,
E::Context: Default,
{
InputOwn {
input,
errors: Errors::default(),
state: MaybeMut::Val(E::State::default()),
ctx: E::Context::default(),
#[cfg(feature = "memoization")]
memos: HashMap::default(),
}
}
pub(crate) fn new_state(input: I, state: &'s mut E::State) -> InputOwn<'a, 's, I, E>
where
E::Context: Default,
{
InputOwn {
input,
errors: Errors::default(),
state: MaybeMut::Ref(state),
ctx: E::Context::default(),
#[cfg(feature = "memoization")]
memos: HashMap::default(),
}
}
pub(crate) fn as_ref_start<'parse>(&'parse mut self) -> InputRef<'a, 'parse, I, E> {
InputRef {
offset: self.input.start(),
input: &self.input,
errors: &mut self.errors,
state: &mut self.state,
ctx: &self.ctx,
#[cfg(feature = "memoization")]
memos: &mut self.memos,
}
}
#[cfg(test)]
pub(crate) fn as_ref_at<'parse>(
&'parse mut self,
offset: I::Offset,
) -> InputRef<'a, 'parse, I, E> {
InputRef {
offset,
input: &self.input,
errors: &mut self.errors,
state: &mut self.state,
ctx: &self.ctx,
#[cfg(feature = "memoization")]
memos: &mut self.memos,
}
}
pub(crate) fn into_errs(self) -> Vec<E::Error> {
self.errors
.secondary
.into_iter()
.map(|err| err.err)
.collect()
}
}
pub struct InputRef<'a, 'parse, I: Input<'a>, E: ParserExtra<'a, I>> {
pub(crate) offset: I::Offset,
pub(crate) input: &'parse I,
pub(crate) errors: &'parse mut Errors<I::Offset, E::Error>,
pub(crate) state: &'parse mut E::State,
pub(crate) ctx: &'parse E::Context,
#[cfg(feature = "memoization")]
pub(crate) memos: &'parse mut HashMap<(I::Offset, usize), Option<Located<I::Offset, E::Error>>>,
}
impl<'a, 'parse, I: Input<'a>, E: ParserExtra<'a, I>> InputRef<'a, 'parse, I, E> {
#[inline]
pub(crate) fn with_ctx<'sub_parse, EM, O>(
&'sub_parse mut self,
new_ctx: &'sub_parse EM::Context,
f: impl FnOnce(&mut InputRef<'a, 'sub_parse, I, EM>) -> O,
) -> O
where
'parse: 'sub_parse,
EM: ParserExtra<'a, I, Error = E::Error, State = E::State>,
{
let mut new_inp = InputRef {
input: self.input,
offset: self.offset,
state: self.state,
ctx: new_ctx,
errors: self.errors,
#[cfg(feature = "memoization")]
memos: self.memos,
};
let res = f(&mut new_inp);
self.offset = new_inp.offset;
res
}
#[inline]
pub(crate) fn with_state<'sub_parse, S, O>(
&'sub_parse mut self,
new_state: &'sub_parse mut S,
f: impl FnOnce(&mut InputRef<'a, 'sub_parse, I, extra::Full<E::Error, S, E::Context>>) -> O,
) -> O
where
'parse: 'sub_parse,
S: 'a,
{
let mut new_inp = InputRef {
input: self.input,
offset: self.offset,
state: new_state,
ctx: self.ctx,
errors: self.errors,
#[cfg(feature = "memoization")]
memos: self.memos,
};
let res = f(&mut new_inp);
self.offset = new_inp.offset;
res
}
#[inline]
pub(crate) fn with_input<'sub_parse, O>(
&'sub_parse mut self,
new_input: &'sub_parse I,
f: impl FnOnce(&mut InputRef<'a, 'sub_parse, I, E>) -> O,
#[cfg(feature = "memoization")] memos: &'sub_parse mut HashMap<
(I::Offset, usize),
Option<Located<I::Offset, E::Error>>,
>,
) -> O
where
'parse: 'sub_parse,
{
let mut new_inp = InputRef {
offset: new_input.start(),
input: new_input,
state: self.state,
ctx: self.ctx,
errors: self.errors,
#[cfg(feature = "memoization")]
memos,
};
f(&mut new_inp)
}
#[inline(always)]
pub fn offset(&self) -> Offset<'a, 'parse, I> {
Offset {
offset: self.offset,
phantom: PhantomData,
}
}
#[inline(always)]
pub fn save(&self) -> Marker<'a, 'parse, I> {
Marker {
offset: self.offset,
err_count: self.errors.secondary.len(),
phantom: PhantomData,
}
}
#[inline(always)]
pub fn rewind(&mut self, marker: Marker<'a, 'parse, I>) {
self.errors.secondary.truncate(marker.err_count);
self.offset = marker.offset;
}
#[inline(always)]
pub fn state(&mut self) -> &mut E::State {
self.state
}
#[inline(always)]
pub fn ctx(&self) -> &E::Context {
self.ctx
}
#[inline]
pub(crate) fn skip_while<F: FnMut(&I::Token) -> bool>(&mut self, mut f: F)
where
I: ValueInput<'a>,
{
loop {
let (offset, token) = unsafe { self.input.next(self.offset) };
if token.filter(&mut f).is_none() {
break;
} else {
self.offset = offset;
}
}
}
#[inline(always)]
pub(crate) fn next_inner(&mut self) -> (I::Offset, Option<I::Token>)
where
I: ValueInput<'a>,
{
let (offset, token) = unsafe { self.input.next(self.offset) };
self.offset = offset;
(self.offset, token)
}
#[inline(always)]
pub(crate) fn next_maybe_inner(&mut self) -> (I::Offset, Option<I::TokenMaybe>) {
let (offset, token) = unsafe { self.input.next_maybe(self.offset) };
let r = (self.offset, token);
self.offset = offset;
r
}
#[inline(always)]
pub(crate) fn next_ref_inner(&mut self) -> (I::Offset, Option<&'a I::Token>)
where
I: BorrowInput<'a>,
{
let (offset, token) = unsafe { self.input.next_ref(self.offset) };
self.offset = offset;
(self.offset, token)
}
pub fn parse<O, P: Parser<'a, I, O, E>>(&mut self, parser: P) -> Result<O, E::Error> {
match parser.go::<Emit>(self) {
Ok(out) => Ok(out),
Err(()) => Err(self.errors.alt.take().expect("error but no alt?").err),
}
}
pub fn check<O, P: Parser<'a, I, O, E>>(&mut self, parser: P) -> Result<(), E::Error> {
match parser.go::<Check>(self) {
Ok(()) => Ok(()),
Err(()) => Err(self.errors.alt.take().expect("error but no alt?").err),
}
}
#[inline(always)]
pub fn next_maybe(&mut self) -> Option<MaybeRef<'a, I::Token>> {
self.next_maybe_inner().1.map(Into::into)
}
#[inline(always)]
pub fn next(&mut self) -> Option<I::Token>
where
I: ValueInput<'a>,
{
self.next_inner().1
}
#[inline(always)]
pub fn next_ref(&mut self) -> Option<&'a I::Token>
where
I: BorrowInput<'a>,
{
self.next_ref_inner().1
}
#[inline(always)]
pub fn peek_maybe(&self) -> Option<MaybeRef<'a, I::Token>> {
unsafe { self.input.next_maybe(self.offset).1.map(Into::into) }
}
#[inline(always)]
pub fn peek(&self) -> Option<I::Token>
where
I: ValueInput<'a>,
{
unsafe { self.input.next(self.offset).1 }
}
#[inline(always)]
pub fn peek_ref(&self) -> Option<&'a I::Token>
where
I: BorrowInput<'a>,
{
unsafe { self.input.next_ref(self.offset).1 }
}
#[inline(always)]
pub fn skip(&mut self)
where
I: ValueInput<'a>,
{
let _ = self.next_inner();
}
#[cfg_attr(not(feature = "regex"), allow(dead_code))]
#[inline]
pub(crate) fn full_slice(&self) -> I::Slice
where
I: SliceInput<'a>,
{
self.input.full_slice()
}
#[inline]
pub fn slice(&self, range: Range<Offset<'a, 'parse, I>>) -> I::Slice
where
I: SliceInput<'a>,
{
self.slice_inner(range.start.offset..range.end.offset)
}
#[inline]
pub fn slice_from(&self, range: RangeFrom<Offset<'a, 'parse, I>>) -> I::Slice
where
I: SliceInput<'a>,
{
self.slice_from_inner(range.start.offset..)
}
#[inline]
pub fn slice_since(&self, range: RangeFrom<Offset<'a, 'parse, I>>) -> I::Slice
where
I: SliceInput<'a>,
{
self.slice_inner(range.start.offset..self.offset)
}
#[inline(always)]
pub(crate) fn slice_inner(&self, range: Range<I::Offset>) -> I::Slice
where
I: SliceInput<'a>,
{
self.input.slice(range)
}
#[allow(dead_code)]
#[inline(always)]
pub(crate) fn slice_from_inner(&self, range: RangeFrom<I::Offset>) -> I::Slice
where
I: SliceInput<'a>,
{
self.input.slice_from(range)
}
#[cfg_attr(not(feature = "lexical-numbers"), allow(dead_code))]
#[inline(always)]
pub(crate) fn slice_trailing_inner(&self) -> I::Slice
where
I: SliceInput<'a>,
{
self.input.slice_from(self.offset..)
}
#[inline(always)]
pub fn span(&self, range: Range<Offset<'a, 'parse, I>>) -> I::Span {
unsafe { self.input.span(range.start.offset..range.end.offset) }
}
#[inline(always)]
pub fn span_from(&self, range: RangeFrom<Offset<'a, 'parse, I>>) -> I::Span
where
I: ExactSizeInput<'a>,
{
unsafe { self.input.span_from(range.start.offset..) }
}
#[inline(always)]
pub fn span_since(&self, before: Offset<'a, 'parse, I>) -> I::Span {
unsafe { self.input.span(before.offset..self.offset) }
}
#[inline(always)]
#[cfg(any(feature = "regex", feature = "lexical-numbers"))]
pub(crate) fn skip_bytes(&mut self, skip: usize)
where
I: SliceInput<'a, Offset = usize>,
{
self.offset += skip;
}
#[inline]
pub(crate) fn emit(&mut self, pos: I::Offset, error: E::Error) {
self.errors.secondary.push(Located::at(pos, error));
}
#[inline]
pub(crate) fn add_alt<Exp: IntoIterator<Item = Option<MaybeRef<'a, I::Token>>>>(
&mut self,
at: I::Offset,
expected: Exp,
found: Option<MaybeRef<'a, I::Token>>,
span: I::Span,
) {
self.errors.alt = Some(match self.errors.alt.take() {
Some(alt) => match alt.pos.into().cmp(&at.into()) {
Ordering::Equal => {
Located::at(alt.pos, alt.err.merge_expected_found(expected, found, span))
}
Ordering::Greater => alt,
Ordering::Less => {
Located::at(at, alt.err.replace_expected_found(expected, found, span))
}
},
None => Located::at(at, Error::expected_found(expected, found, span)),
});
}
#[inline]
pub(crate) fn add_alt_err(&mut self, at: I::Offset, err: E::Error) {
self.errors.alt = Some(match self.errors.alt.take() {
Some(alt) => match alt.pos.into().cmp(&at.into()) {
Ordering::Equal => Located::at(alt.pos, alt.err.merge(err)),
Ordering::Greater => alt,
Ordering::Less => Located::at(at, err),
},
None => Located::at(at, err),
});
}
}
pub struct Emitter<E> {
emitted: Vec<E>,
}
impl<E> Emitter<E> {
#[inline]
pub(crate) fn new() -> Emitter<E> {
Emitter {
emitted: Vec::new(),
}
}
#[inline]
pub(crate) fn errors(self) -> Vec<E> {
self.emitted
}
#[inline]
pub fn emit(&mut self, err: E) {
self.emitted.push(err)
}
}
pub struct MapExtra<'a, 'b, I: Input<'a>, E: ParserExtra<'a, I>> {
before: I::Offset,
after: I::Offset,
inp: &'b I,
state: &'b mut E::State,
ctx: &'b E::Context,
}
impl<'a, 'b, I: Input<'a>, E: ParserExtra<'a, I>> MapExtra<'a, 'b, I, E> {
#[inline(always)]
pub(crate) fn new<'parse>(
before: Offset<'a, 'parse, I>,
inp: &'b mut InputRef<'a, 'parse, I, E>,
) -> Self {
Self {
before: before.offset,
after: inp.offset,
ctx: inp.ctx,
state: inp.state,
inp: inp.input,
}
}
#[inline(always)]
pub fn span(&self) -> I::Span {
unsafe { self.inp.span(self.before..self.after) }
}
#[inline(always)]
pub fn slice(&self) -> I::Slice
where
I: SliceInput<'a>,
{
self.inp.slice(self.before..self.after)
}
#[inline(always)]
pub fn state(&mut self) -> &mut E::State {
self.state
}
#[inline(always)]
pub fn ctx(&self) -> &E::Context {
self.ctx
}
}