/// Specialized input for parsing lexed tokens /// /// Helpful impls /// - Any `PartialEq` type (e.g. a `TokenKind` or `&str`) can be used with /// [`literal`][crate::token::literal] /// - A `PartialEq` for `&str` allows for using `&str` as a parser for tokens /// - [`ContainsToken`][crate::stream::ContainsToken] for `T` to for parsing with token sets /// - [`Location`] for `T` to extract spans from tokens /// /// See also [Lexing and Parsing][crate::_topic::lexing]. #[derive(Copy, Clone, PartialEq, Eq)] pubstruct TokenSlice<'t, T> {
initial: &'t [T],
input: &'t [T],
}
impl<'t, T> TokenSlice<'t, T> where
T: crate::lib::std::fmt::Debug + Clone,
{ /// Make a stream to parse tokens #[inline] pubfn new(input: &'t [T]) -> Self { Self {
initial: input,
input,
}
}
/// Reset the stream to the start /// /// This is useful for formats that encode a graph with addresses relative to the start of the /// input. #[doc(alias = "fseek")] #[inline] pubfn reset_to_start(&mutself) { let start = self.initial.checkpoint(); self.input.reset(&start);
}
/// Iterate over consumed tokens starting with the last emitted /// /// This is intended to help build up appropriate context when reporting errors. #[inline] pubfn previous_tokens(&self) -> impl Iterator<Item = &'t T> { let offset = self.input.offset_from(&self.initial); self.initial[0..offset].iter().rev()
}
}
/// Track locations by implementing [`Location`] on the Token. impl<T> TokenSlice<'_, T> where
T: Location,
{ #[inline(always)] fn previous_token_end(&self) -> Option<usize> { let index = self.input.offset_from(&self.initial);
index
.checked_sub(1)
.map(|i| self.initial[i].previous_token_end())
}
/// Report whether the [`Stream`] can save off errors for recovery #[inline(always)] fn is_recovery_supported() -> bool { false
}
}
impl<'t, T> StreamIsPartial for TokenSlice<'t, T> where
T: crate::lib::std::fmt::Debug + Clone,
{ type PartialState = <&'t [T] as StreamIsPartial>::PartialState;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.