tokit 0.0.0

Blazing fast parser combinators: parse-while-lexing (zero-copy), deterministic LALR-style parsing, no backtracking. Flexible emitters for fail-fast runtime or greedy compiler diagnostics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
use core::{convert::Infallible, fmt, hash::Hash, ops::AddAssign};

use mayber::Maybe;

use crate::utils::{SimpleSpan, Spanned};

pub(crate) use input::Input;

#[cfg(feature = "logos")]
pub use self::logos::LogosLexer;
pub use cache::*;
pub use checkpoint::Checkpoint;
pub use cursor::Cursor;
pub use input::InputContext;
pub use input_ref::InputRef;
pub use source::Source;
#[cfg(feature = "logos")]
pub use token::Logos;
pub use token::{
  DelimiterToken, IdentifierToken, KeywordToken, Lexed, LitToken, OperatorToken, PunctuatorToken,
  Token,
};

pub use peek::Peeked;

/// The token related structures and traits
pub mod token;

/// The source related structures and traits
pub mod source;

mod cache;
mod checkpoint;
mod cursor;
mod input;
mod input_ref;

#[cfg(feature = "logos")]
mod logos;

mod peek;

/// A trait to convert a type into a lexer.
pub trait IntoLexer<'inp, T: ?Sized> {
  /// The lexer type.
  type Lexer;

  /// Converts `self` into a lexer.
  fn into_lexer(self) -> Self::Lexer
  where
    Self: 'inp,
    T: Token<'inp>;
}

impl<'inp, T, L> IntoLexer<'inp, T> for L
where
  T: Token<'inp>,
  L: Lexer<'inp, Token = T>,
{
  type Lexer = L;

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn into_lexer(self) -> Self::Lexer {
    self
  }
}

/// A trait for lexers
pub trait Lexer<'inp>: 'inp {
  /// The state of the lexer.
  type State: State;
  /// The source type of the lexer.
  type Source: source::Source<Self::Offset> + ?Sized;
  /// The token type produced by the lexer.
  type Token: Token<'inp>;

  /// The span type of the lexer.
  type Span: fmt::Debug + Span<Offset = Self::Offset> + Ord + Clone + Hash;
  /// The offset type of the source.
  type Offset: Default + fmt::Debug + Ord + Clone + Hash;

  /// Lexes the input source and returns a tokenizer.
  fn new(src: &'inp Self::Source) -> Self
  where
    Self::State: Default;

  /// Lexes the input source with the given initial state and returns a tokenizer.
  fn with_state(src: &'inp Self::Source, state: Self::State) -> Self;

  /// Checks the current state of the lexer for errors.
  ///
  /// If the state is valid, returns `Ok(())`, otherwise returns an error.
  fn check(&self) -> Result<(), <Self::Token as Token<'inp>>::Error>;

  /// Returns a reference to the current state of the lexer.
  fn state(&self) -> &Self::State;

  /// Returns a mutable reference to the current state of the lexer.
  fn state_mut(&mut self) -> &mut Self::State;

  /// Consumes the lexer and returns the current state.
  fn into_state(self) -> Self::State;

  /// Returns a reference to the source being lexed.
  fn source(&self) -> &'inp Self::Source;

  /// Get the range for the current token in `Source`.
  fn span(&self) -> Self::Span;

  /// Returns the slice of the current token in the source.
  fn slice(&self) -> <Self::Source as Source<Self::Offset>>::Slice<'inp>;

  /// Lexes the next token from the input source.
  fn lex(&mut self) -> Option<Result<Self::Token, <Self::Token as Token<'inp>>::Error>>;

  /// Bumps the end of currently lexed token by `n` offsets.
  ///
  /// # Panics
  ///
  /// Panics if adding `n` to current offset would place the `Lexer` beyond the last byte,
  /// or in the middle of an UTF-8 code point (does not apply when lexing raw `&[u8]`).
  fn bump(&mut self, n: &Self::Offset);
}

/// A trait for types that can be lexed from the input.
///
/// This trait provides a standardized way to lex (tokenize) an entire input
/// into a structured type. It's useful for types that represent complete
/// lexical structures that can be built from an input source.
///
/// # Type Parameters
///
/// - `I`: The input type to lex from (e.g., `&str`, `&[u8]`)
/// - `Error`: The error type returned when lexing fails
///
/// # Example
///
/// ```rust,ignore
/// use tokit::Lexable;
///
/// struct Document {
///     tokens: Vec<Token>,
/// }
///
/// impl Lexable<&str, LexError> for Document {
///     fn lex(input: &str) -> Result<Self, LexError> {
///         // Lex the entire input into a Document
///         let tokens = tokenize(input)?;
///         Ok(Document { tokens })
///     }
/// }
/// ```
pub trait Lexable<I, Error> {
  /// Lexes `Self` from the given input.
  ///
  /// This method consumes the input and attempts to construct `Self` by
  /// lexing the entire input. It returns an error if the input cannot be
  /// successfully lexed.
  ///
  /// # Errors
  ///
  /// Returns an error if the input is malformed or cannot be lexed according
  /// to the rules of the implementing type.
  fn lex(input: I) -> Result<Self, Error>
  where
    Self: Sized;
}

/// The state trait for lexers
pub trait State: core::fmt::Debug + Clone {
  /// The error type of the state.
  type Error: Clone;

  /// Checks the state for errors.
  fn check(&self) -> Result<(), Self::Error>;
}

impl State for () {
  type Error = ();

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn check(&self) -> Result<(), Self::Error> {
    Ok(())
  }
}

impl State for Infallible {
  type Error = Infallible;

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn check(&self) -> Result<(), Self::Error> {
    Ok(())
  }
}

/// A cached token with its associated state for a specific lexer.
pub type CachedTokenOf<
  'a,
  L,
  T = Lexed<'a, <L as Lexer<'a>>::Token>,
  Span = <L as Lexer<'a>>::Span,
> = CachedToken<T, <L as Lexer<'a>>::State, Span>;
/// A cached token with its associated state for a specific lexer.
pub type CachedTokenRefOf<
  'r,
  'a,
  L,
  T = Lexed<'a, <L as Lexer<'a>>::Token>,
  Span = <L as Lexer<'a>>::Span,
> = CachedToken<&'r T, &'r <L as Lexer<'a>>::State, &'r Span>;
/// A maybe reference to a cached token with its associated state for a specific lexer.
pub type MaybeRefCachedTokenOf<
  'r,
  'a,
  L,
  T = Lexed<'a, <L as Lexer<'a>>::Token>,
  Span = <L as Lexer<'a>>::Span,
> = Maybe<CachedTokenRefOf<'r, 'a, L, T, Span>, CachedTokenOf<'a, L, T, Span>>;

/// A cached token with its associated state.
pub struct CachedToken<T, State, Span> {
  token: Spanned<T, Span>,
  state: State,
}

impl<T, State, Span> Clone for CachedToken<T, State, Span>
where
  State: Clone,
  Span: Clone,
  T: Clone,
{
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn clone(&self) -> Self {
    Self {
      token: self.token.clone(),
      state: self.state.clone(),
    }
  }
}

// impl<'a, L: Lexer<'a>> TryFrom<> for CachedToken<T, State, Span> {
//   #[cfg_attr(not(tarpaulin), inline(always))]
//   pub(super) fn try_into_token(
//     self,
//   ) -> Result<CachedToken<T, State, Span>, <T as Token<>>::Error> {
//     match self.token.data {
//       Lexed::Token(token) => Ok(CachedToken::new(Spanned::new(self.token.span, token), self.state)),
//       Lexed::Error(e) => Err(e),
//     }
//   }
// }

impl<T, State, Span> CachedToken<T, State, Span> {
  /// Creates a new cached token.
  #[cfg_attr(not(tarpaulin), inline(always))]
  const fn new(token: Spanned<T, Span>, state: State) -> Self {
    Self { token, state }
  }

  /// Returns a reference to the token.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn token(&self) -> Spanned<&T, &Span> {
    self.token.as_ref()
  }

  /// Consumes the cached token and returns the lexed token.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn into_token(self) -> Spanned<T, Span> {
    self.token
  }

  /// Returns a reference to the cached token.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn as_ref(&self) -> CachedToken<&T, &State, &Span> {
    CachedToken {
      token: self.token.as_ref(),
      state: &self.state,
    }
  }

  /// Maps the token to a new type using the provided function.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn map_token<U, F>(self, f: F) -> CachedToken<U, State, Span>
  where
    F: FnOnce(T) -> U,
  {
    CachedToken {
      token: self.token.map_data(f),
      state: self.state,
    }
  }

  /// Returns a reference to the state.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn state(&self) -> &State {
    &self.state
  }

  /// Consumes the cached token and returns the extras.
  #[cfg_attr(not(tarpaulin), inline(always))]
  #[allow(clippy::type_complexity)]
  pub fn into_components(self) -> (Spanned<T, Span>, State) {
    (self.token, self.state)
  }
}

/// A trait representing a span in the source code.
pub trait Span {
  /// The offset type of the span.
  type Offset: Ord + Clone + Hash;

  /// Creates a new span from the given start and end offsets.
  fn new(start: Self::Offset, end: Self::Offset) -> Self;

  /// Consumes the span and returns it.
  fn into_range(self) -> core::ops::Range<Self::Offset>
  where
    Self: Sized;

  /// Returns the start offset of the span.
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn start(&self) -> Self::Offset {
    self.start_ref().clone()
  }

  /// Returns the start offset of the span.
  fn start_ref(&self) -> &Self::Offset;

  /// Returns the mutable reference to the start offset of the span.
  fn start_mut(&mut self) -> &mut Self::Offset;

  /// Returns the end offset of the span.
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn end(&self) -> Self::Offset {
    self.end_ref().clone()
  }

  /// Returns the end offset of the span.
  fn end_ref(&self) -> &Self::Offset;

  /// Returns the mutable reference to the end offset of the span.
  fn end_mut(&mut self) -> &mut Self::Offset;

  /// Bumps the span by `n` offsets.
  fn bump(&mut self, n: &Self::Offset);
}

impl Span for core::ops::Range<usize> {
  type Offset = usize;

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn new(start: Self::Offset, end: Self::Offset) -> Self {
    start..end
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn start_ref(&self) -> &Self::Offset {
    &self.start
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn end_ref(&self) -> &Self::Offset {
    &self.end
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn start_mut(&mut self) -> &mut Self::Offset {
    &mut self.start
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn end_mut(&mut self) -> &mut Self::Offset {
    &mut self.end
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn bump(&mut self, n: &Self::Offset) {
    self.end += *n;
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn into_range(self) -> core::ops::Range<Self::Offset> {
    self.start..self.end
  }
}

impl<O> Span for SimpleSpan<O>
where
  O: Ord + Clone + Hash + for<'a> AddAssign<&'a O>,
{
  type Offset = O;

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn new(start: Self::Offset, end: Self::Offset) -> Self {
    SimpleSpan::new(start, end)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn start_ref(&self) -> &Self::Offset {
    self.start_ref()
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn start_mut(&mut self) -> &mut Self::Offset {
    self.start_mut()
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn end_ref(&self) -> &Self::Offset {
    self.end_ref()
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn end_mut(&mut self) -> &mut Self::Offset {
    self.end_mut()
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn bump(&mut self, n: &Self::Offset) {
    self.bump(n);
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn into_range(self) -> core::ops::Range<Self::Offset> {
    self.start..self.end
  }
}

/// A black hole cache that discards all tokens.
///
/// `BlackHole` implements the [`Cache`] trait but doesn't actually store any tokens.
/// All tokens pushed to it are immediately discarded. This is useful when you want to
/// process tokens in a streaming fashion without maintaining a lookahead buffer.
#[derive(Debug, Clone, Copy, Default)]
pub struct BlackHole;

impl<O> From<O> for BlackHole
where
  (): From<O>,
{
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn from(_: O) -> Self {
    BlackHole
  }
}

#[cfg(test)]
pub(crate) struct DummyLexer;

#[cfg(test)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, derive_more::Display)]
#[display("DummyToken")]
pub(crate) struct DummyToken;

#[cfg(test)]
const _: () = {
  impl Token<'_> for DummyToken {
    type Kind = Self;
    type Error = ();

    #[cfg_attr(not(tarpaulin), inline(always))]
    fn kind(&self) -> Self::Kind {
      *self
    }

    #[cfg_attr(not(tarpaulin), inline(always))]
    fn is_trivia(&self) -> bool {
      true
    }
  }

  impl PunctuatorToken<'_> for DummyToken {}

  impl LitToken<'_> for DummyToken {}

  impl OperatorToken<'_> for DummyToken {}

  impl<'inp> Lexer<'inp> for DummyLexer {
    type State = ();

    type Source = str;

    type Token = DummyToken;

    type Span = SimpleSpan;

    type Offset = usize;

    fn new(_: &'inp Self::Source) -> Self
    where
      Self::State: Default,
    {
      todo!()
    }

    fn with_state(_: &'inp Self::Source, _: Self::State) -> Self {
      todo!()
    }

    fn check(&self) -> Result<(), <Self::Token as Token<'inp>>::Error> {
      todo!()
    }

    fn state(&self) -> &Self::State {
      todo!()
    }

    fn state_mut(&mut self) -> &mut Self::State {
      todo!()
    }

    fn into_state(self) -> Self::State {
      todo!()
    }

    fn source(&self) -> &'inp Self::Source {
      todo!()
    }

    fn span(&self) -> Self::Span {
      todo!()
    }

    fn slice(&self) -> <Self::Source as Source<Self::Offset>>::Slice<'inp> {
      todo!()
    }

    fn lex(&mut self) -> Option<Result<Self::Token, <Self::Token as Token<'inp>>::Error>> {
      todo!()
    }

    fn bump(&mut self, _: &Self::Offset) {
      todo!()
    }
  }
};