| 1 |
use crate::stream::Checkpoint; |
| 2 |
use crate::stream::Compare; |
| 3 |
use crate::stream::CompareResult; |
| 4 |
use crate::stream::Location; |
| 5 |
use crate::stream::Needed; |
| 6 |
use crate::stream::Offset; |
| 7 |
#[cfg(feature = "unstable-recover")] |
| 8 |
#[cfg(feature = "std")] |
| 9 |
use crate::stream::Recover; |
| 10 |
use crate::stream::SliceLen; |
| 11 |
use crate::stream::Stream; |
| 12 |
use crate::stream::StreamIsPartial; |
| 13 |
use crate::stream::UpdateSlice; |
| 14 |
use core::iter::Enumerate; |
| 15 |
use core::slice::Iter; |
| 16 |
|
| 17 |
/// Specialized input for parsing lexed tokens |
| 18 |
/// |
| 19 |
/// Helpful impls |
| 20 |
/// - Any `PartialEq` type (e.g. a `TokenKind` or `&str`) can be used with |
| 21 |
/// [`literal`][crate::token::literal] |
| 22 |
/// - A `PartialEq` for `&str` allows for using `&str` as a parser for tokens |
| 23 |
/// - [`ContainsToken`][crate::stream::ContainsToken] for `T` to for parsing with token sets |
| 24 |
/// - [`Location`] for `T` to extract spans from tokens |
| 25 |
/// |
| 26 |
/// See also [Lexing and Parsing][crate::_topic::lexing]. |
| 27 |
#[derive(Copy, Clone, PartialEq, Eq)] |
| 28 |
pub struct TokenSlice<'t, T> { |
| 29 |
initial: &'t [T], |
| 30 |
input: &'t [T], |
| 31 |
} |
| 32 |
|
| 33 |
impl<'t, T> TokenSlice<'t, T> |
| 34 |
where |
| 35 |
T: core::fmt::Debug + Clone, |
| 36 |
{ |
| 37 |
/// Make a stream to parse tokens |
| 38 |
#[inline] |
| 39 |
pub fn new(input: &'t [T]) -> Self { |
| 40 |
Self { |
| 41 |
initial: input, |
| 42 |
input, |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/// Reset the stream to the start |
| 47 |
/// |
| 48 |
/// This is useful for formats that encode a graph with addresses relative to the start of the |
| 49 |
/// input. |
| 50 |
#[doc(alias = "fseek")] |
| 51 |
#[inline] |
| 52 |
pub fn reset_to_start(&mut self) { |
| 53 |
let start = self.initial.checkpoint(); |
| 54 |
self.input.reset(&start); |
| 55 |
} |
| 56 |
|
| 57 |
/// Iterate over consumed tokens starting with the last emitted |
| 58 |
/// |
| 59 |
/// This is intended to help build up appropriate context when reporting errors. |
| 60 |
#[inline] |
| 61 |
pub fn previous_tokens(&self) -> impl Iterator<Item = &'t T> { |
| 62 |
let offset = self.input.offset_from(&self.initial); |
| 63 |
self.initial[0..offset].iter().rev() |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/// Track locations by implementing [`Location`] on the Token. |
| 68 |
impl<T> TokenSlice<'_, T> |
| 69 |
where |
| 70 |
T: Location, |
| 71 |
{ |
| 72 |
#[inline(always)] |
| 73 |
fn previous_token_end(&self) -> Option<usize> { |
| 74 |
let index = self.input.offset_from(&self.initial); |
| 75 |
index |
| 76 |
.checked_sub(1) |
| 77 |
.map(|i| self.initial[i].previous_token_end()) |
| 78 |
} |
| 79 |
|
| 80 |
#[inline(always)] |
| 81 |
fn current_token_start(&self) -> Option<usize> { |
| 82 |
self.input.first().map(|t| t.current_token_start()) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
impl<T> Default for TokenSlice<'_, T> |
| 87 |
where |
| 88 |
T: core::fmt::Debug + Clone, |
| 89 |
{ |
| 90 |
fn default() -> Self { |
| 91 |
Self::new(&[]) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
impl<T> core::ops::Deref for TokenSlice<'_, T> { |
| 96 |
type Target = [T]; |
| 97 |
|
| 98 |
fn deref(&self) -> &Self::Target { |
| 99 |
self.input |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
impl<T: core::fmt::Debug> core::fmt::Debug for TokenSlice<'_, T> { |
| 104 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 105 |
self.input.fmt(f) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
impl<T> SliceLen for TokenSlice<'_, T> { |
| 110 |
#[inline(always)] |
| 111 |
fn slice_len(&self) -> usize { |
| 112 |
self.input.slice_len() |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
impl<'t, T> Stream for TokenSlice<'t, T> |
| 117 |
where |
| 118 |
T: core::fmt::Debug + Clone, |
| 119 |
{ |
| 120 |
type Token = &'t T; |
| 121 |
type Slice = &'t [T]; |
| 122 |
|
| 123 |
type IterOffsets = Enumerate<Iter<'t, T>>; |
| 124 |
|
| 125 |
type Checkpoint = Checkpoint<&'t [T], Self>; |
| 126 |
|
| 127 |
#[inline(always)] |
| 128 |
fn iter_offsets(&self) -> Self::IterOffsets { |
| 129 |
self.input.iter().enumerate() |
| 130 |
} |
| 131 |
#[inline(always)] |
| 132 |
fn eof_offset(&self) -> usize { |
| 133 |
self.input.eof_offset() |
| 134 |
} |
| 135 |
|
| 136 |
#[inline(always)] |
| 137 |
fn next_token(&mut self) -> Option<Self::Token> { |
| 138 |
let (token, next) = self.input.split_first()?; |
| 139 |
self.input = next; |
| 140 |
Some(token) |
| 141 |
} |
| 142 |
|
| 143 |
#[inline(always)] |
| 144 |
fn peek_token(&self) -> Option<Self::Token> { |
| 145 |
self.input.first() |
| 146 |
} |
| 147 |
|
| 148 |
#[inline(always)] |
| 149 |
fn offset_for<P>(&self, predicate: P) -> Option<usize> |
| 150 |
where |
| 151 |
P: Fn(Self::Token) -> bool, |
| 152 |
{ |
| 153 |
self.input.iter().position(predicate) |
| 154 |
} |
| 155 |
#[inline(always)] |
| 156 |
fn offset_at(&self, tokens: usize) -> Result<usize, Needed> { |
| 157 |
self.input.offset_at(tokens) |
| 158 |
} |
| 159 |
#[inline(always)] |
| 160 |
fn next_slice(&mut self, offset: usize) -> Self::Slice { |
| 161 |
self.input.next_slice(offset) |
| 162 |
} |
| 163 |
#[inline(always)] |
| 164 |
unsafe fn next_slice_unchecked(&mut self, offset: usize) -> Self::Slice { |
| 165 |
// SAFETY: Passing up invariants |
| 166 |
unsafe { self.input.next_slice_unchecked(offset) } |
| 167 |
} |
| 168 |
#[inline(always)] |
| 169 |
fn peek_slice(&self, offset: usize) -> Self::Slice { |
| 170 |
self.input.peek_slice(offset) |
| 171 |
} |
| 172 |
#[inline(always)] |
| 173 |
unsafe fn peek_slice_unchecked(&self, offset: usize) -> Self::Slice { |
| 174 |
// SAFETY: Passing up invariants |
| 175 |
unsafe { self.input.peek_slice_unchecked(offset) } |
| 176 |
} |
| 177 |
|
| 178 |
#[inline(always)] |
| 179 |
fn checkpoint(&self) -> Self::Checkpoint { |
| 180 |
Checkpoint::<_, Self>::new(self.input) |
| 181 |
} |
| 182 |
#[inline(always)] |
| 183 |
fn reset(&mut self, checkpoint: &Self::Checkpoint) { |
| 184 |
self.input = checkpoint.inner; |
| 185 |
} |
| 186 |
|
| 187 |
fn trace(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 188 |
self.input.trace(f) |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
impl<T> Location for TokenSlice<'_, T> |
| 193 |
where |
| 194 |
T: Location, |
| 195 |
{ |
| 196 |
#[inline(always)] |
| 197 |
fn previous_token_end(&self) -> usize { |
| 198 |
self.previous_token_end() |
| 199 |
.or_else(|| self.current_token_start()) |
| 200 |
.unwrap_or(0) |
| 201 |
} |
| 202 |
#[inline(always)] |
| 203 |
fn current_token_start(&self) -> usize { |
| 204 |
self.current_token_start() |
| 205 |
.or_else(|| self.previous_token_end()) |
| 206 |
.unwrap_or(0) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
#[cfg(feature = "unstable-recover")] |
| 211 |
#[cfg(feature = "std")] |
| 212 |
impl<T, E> Recover<E> for TokenSlice<'_, T> |
| 213 |
where |
| 214 |
T: core::fmt::Debug + Clone, |
| 215 |
{ |
| 216 |
#[inline(always)] |
| 217 |
fn record_err( |
| 218 |
&mut self, |
| 219 |
_token_start: &Self::Checkpoint, |
| 220 |
_err_start: &Self::Checkpoint, |
| 221 |
err: E, |
| 222 |
) -> Result<(), E> { |
| 223 |
Err(err) |
| 224 |
} |
| 225 |
|
| 226 |
/// Report whether the [`Stream`] can save off errors for recovery |
| 227 |
#[inline(always)] |
| 228 |
fn is_recovery_supported() -> bool { |
| 229 |
false |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
impl<'t, T> StreamIsPartial for TokenSlice<'t, T> |
| 234 |
where |
| 235 |
T: core::fmt::Debug + Clone, |
| 236 |
{ |
| 237 |
type PartialState = <&'t [T] as StreamIsPartial>::PartialState; |
| 238 |
|
| 239 |
#[inline] |
| 240 |
fn complete(&mut self) -> Self::PartialState { |
| 241 |
#![allow(clippy::semicolon_if_nothing_returned)] |
| 242 |
self.input.complete() |
| 243 |
} |
| 244 |
|
| 245 |
#[inline] |
| 246 |
fn restore_partial(&mut self, state: Self::PartialState) { |
| 247 |
self.input.restore_partial(state); |
| 248 |
} |
| 249 |
|
| 250 |
#[inline(always)] |
| 251 |
fn is_partial_supported() -> bool { |
| 252 |
<&[T] as StreamIsPartial>::is_partial_supported() |
| 253 |
} |
| 254 |
|
| 255 |
#[inline(always)] |
| 256 |
fn is_partial(&self) -> bool { |
| 257 |
self.input.is_partial() |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
impl<T> Offset for TokenSlice<'_, T> |
| 262 |
where |
| 263 |
T: core::fmt::Debug + Clone, |
| 264 |
{ |
| 265 |
#[inline(always)] |
| 266 |
fn offset_from(&self, other: &Self) -> usize { |
| 267 |
self.offset_from(&other.checkpoint()) |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
impl<T> Offset<<TokenSlice<'_, T> as Stream>::Checkpoint> for TokenSlice<'_, T> |
| 272 |
where |
| 273 |
T: core::fmt::Debug + Clone, |
| 274 |
{ |
| 275 |
#[inline(always)] |
| 276 |
fn offset_from(&self, other: &<TokenSlice<'_, T> as Stream>::Checkpoint) -> usize { |
| 277 |
self.checkpoint().offset_from(other) |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
impl<T, O> Compare<O> for TokenSlice<'_, T> |
| 282 |
where |
| 283 |
T: PartialEq<O> + Eq, |
| 284 |
{ |
| 285 |
#[inline] |
| 286 |
fn compare(&self, t: O) -> CompareResult { |
| 287 |
if let Some(token) = self.first() { |
| 288 |
if *token == t { |
| 289 |
CompareResult::Ok(1) |
| 290 |
} else { |
| 291 |
CompareResult::Error |
| 292 |
} |
| 293 |
} else { |
| 294 |
CompareResult::Incomplete |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
impl<T> UpdateSlice for TokenSlice<'_, T> |
| 300 |
where |
| 301 |
T: core::fmt::Debug + Clone, |
| 302 |
{ |
| 303 |
#[inline(always)] |
| 304 |
fn update_slice(mut self, inner: Self::Slice) -> Self { |
| 305 |
self.input = <&[T] as UpdateSlice>::update_slice(self.input, inner); |
| 306 |
self |
| 307 |
} |
| 308 |
} |