/// Allow collecting the span of a parsed token within a slice /// /// Converting byte offsets to line or column numbers is left up to the user, as computing column /// numbers requires domain knowledge (are columns byte-based, codepoint-based, or grapheme-based?) /// and O(n) iteration over the input to determine codepoint and line boundaries. /// /// [The `line-span` crate](https://docs.rs/line-span/latest/line_span/) can help with converting /// byte offsets to line numbers. /// /// See [`Parser::span`][crate::Parser::span] and [`Parser::with_span`][crate::Parser::with_span] for more details #[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord)] #[doc(alias = "LocatingSliceSpan")] #[doc(alias = "Located")] pubstruct LocatingSlice<I> {
initial: I,
input: I,
}
impl<I> LocatingSlice<I> where
I: Clone + Offset,
{ /// Wrap another Stream with span tracking pubfn new(input: I) -> Self { let initial = input.clone(); Self { initial, input }
}
#[inline] fn previous_token_end(&self) -> usize { // Assumptions: // - Index offsets is sufficient // - Tokens are continuous self.input.offset_from(&self.initial)
} #[inline] fn current_token_start(&self) -> usize { // Assumptions: // - Index offsets is sufficient self.input.offset_from(&self.initial)
}
}
impl<I> LocatingSlice<I> where
I: Clone + Stream + Offset,
{ /// 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);
}
}
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.