token-lang 1.0.0

Token type definitions - the shared seam between lexer and parser.
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
//! The [`Token`] type: a syntactic kind paired with the span it covers.

use core::fmt;

use intern_lang::Symbol;
use span_lang::{Span, Spanned};

use crate::TokenKind;

/// A single lexical token: a [`kind`](Token::kind) paired with the [`Span`] of
/// source it covers.
///
/// `Token<K>` is the shared seam between a lexer and a parser. The lexer produces
/// a stream of `Token<K>`; the parser consumes it. Neither needs to know the
/// other's internals — they agree only on this pair. The *kind* `K` is the
/// language's own classification (an `enum` of keywords, punctuation, literals,
/// and so on); token-lang stays language-agnostic by leaving `K` to the language
/// and owning only the pairing with a span.
///
/// A token is a *classified span*: `K` says **what** was lexed, the [`Span`] says
/// **where**. The type is `Copy` whenever `K` is, so a token whose kind is a plain
/// enum (a discriminant plus an eight-byte span) is cheap to pass by value.
///
/// `Token<K>` mirrors [`Spanned<K>`](span_lang::Spanned) but carries token
/// semantics: it orders by span first, so a slice of tokens sorts into source
/// order; its `Display` reads as `kind @ start..end`; and when `K: TokenKind` it
/// forwards the classification queries — [`is_trivia`](Token::is_trivia),
/// [`is_eof`](Token::is_eof), and [`symbol`](Token::symbol) — to the kind. Convert
/// between the two with the [`From`] impls when a layer wants the plain pair.
///
/// # Examples
///
/// ```
/// use token_lang::{Span, Token, TokenKind};
///
/// #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
/// enum Kind {
///     Ident,
///     Plus,
///     Eof,
/// }
/// impl TokenKind for Kind {
///     fn is_eof(&self) -> bool {
///         matches!(self, Kind::Eof)
///     }
/// }
///
/// // `a + b` lexed into tokens, then the end marker.
/// let a = Token::new(Kind::Ident, Span::new(0, 1));
/// let plus = Token::new(Kind::Plus, Span::new(2, 3));
/// let eof = Token::new(Kind::Eof, Span::new(5, 5));
///
/// assert_eq!(*a.kind(), Kind::Ident);
/// assert_eq!(plus.span(), Span::new(2, 3));
/// assert!(eof.is_eof());
///
/// // Tokens are `Copy` and sort into source order.
/// let mut stream = [eof, a, plus];
/// stream.sort();
/// assert_eq!(stream, [a, plus, eof]);
/// ```
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Token<K> {
    /// The half-open source range this token covers.
    ///
    /// Declared before [`kind`](Token::kind) so the derived ordering compares the
    /// span first, sorting a slice of tokens into source order.
    pub span: Span,
    /// The language-specific kind of this token.
    pub kind: K,
}

impl<K> Token<K> {
    /// Pairs a kind with the span it was lexed from.
    ///
    /// `const`, so a token can initialise a `const` or `static` table — useful for
    /// the fixed marker tokens (an end-of-input, a synthetic delimiter) a lexer
    /// hands out.
    ///
    /// # Examples
    ///
    /// ```
    /// use token_lang::{Span, Token};
    ///
    /// let tok = Token::new("ident", Span::new(0, 5));
    /// assert_eq!(*tok.kind(), "ident");
    /// assert_eq!(tok.span(), Span::new(0, 5));
    /// ```
    #[inline]
    #[must_use]
    pub const fn new(kind: K, span: Span) -> Self {
        Self { span, kind }
    }

    /// Borrows this token's kind.
    ///
    /// # Examples
    ///
    /// ```
    /// use token_lang::{Span, Token};
    ///
    /// let tok = Token::new(42u8, Span::new(0, 1));
    /// assert_eq!(*tok.kind(), 42);
    /// ```
    #[inline]
    #[must_use]
    pub const fn kind(&self) -> &K {
        &self.kind
    }

    /// Returns this token's span.
    ///
    /// `Span` is `Copy`, so this hands back the range by value rather than
    /// borrowing it.
    ///
    /// # Examples
    ///
    /// ```
    /// use token_lang::{Span, Token};
    ///
    /// let tok = Token::new((), Span::new(3, 7));
    /// assert_eq!(tok.span(), Span::new(3, 7));
    /// ```
    #[inline]
    #[must_use]
    pub const fn span(&self) -> Span {
        self.span
    }

    /// Consumes the token, returning just its kind and dropping the span.
    ///
    /// # Examples
    ///
    /// ```
    /// use token_lang::{Span, Token};
    ///
    /// let tok = Token::new(String::from("if"), Span::new(0, 2));
    /// assert_eq!(tok.into_kind(), "if");
    /// ```
    #[inline]
    #[must_use]
    pub fn into_kind(self) -> K {
        self.kind
    }

    /// Transforms the kind with `f`, keeping the span unchanged.
    ///
    /// This is how a layer lifts a token from one kind to another without losing
    /// where it came from — for example, mapping a raw lexer kind onto a coarser
    /// parser kind, or wrapping the kind in a richer type.
    ///
    /// # Examples
    ///
    /// ```
    /// use token_lang::{Span, Token};
    ///
    /// let raw = Token::new("123", Span::new(4, 7));
    /// let parsed = raw.map(|s| s.parse::<u32>().unwrap());
    /// assert_eq!(*parsed.kind(), 123);
    /// assert_eq!(parsed.span(), Span::new(4, 7));
    /// ```
    #[inline]
    #[must_use]
    pub fn map<U>(self, f: impl FnOnce(K) -> U) -> Token<U> {
        Token {
            span: self.span,
            kind: f(self.kind),
        }
    }

    /// Borrows the kind, yielding a `Token<&K>` with the same span.
    ///
    /// Mirrors [`Option::as_ref`]: it lets you inspect or [`map`](Token::map) the
    /// kind without consuming the original token.
    ///
    /// # Examples
    ///
    /// ```
    /// use token_lang::{Span, Token};
    ///
    /// let owned = Token::new(String::from("name"), Span::new(0, 4));
    /// let len = owned.as_ref().map(|s| s.len());
    /// assert_eq!(*len.kind(), 4);
    /// // `owned` is still usable.
    /// assert_eq!(owned.kind(), "name");
    /// ```
    #[inline]
    #[must_use]
    pub fn as_ref(&self) -> Token<&K> {
        Token {
            span: self.span,
            kind: &self.kind,
        }
    }
}

impl<K: TokenKind> Token<K> {
    /// Whether this token's kind is trivia. Forwards to
    /// [`TokenKind::is_trivia`].
    ///
    /// # Examples
    ///
    /// ```
    /// use token_lang::{Span, Token, TokenKind};
    ///
    /// #[derive(Clone, Copy)]
    /// enum Kind {
    ///     Word,
    ///     Space,
    /// }
    /// impl TokenKind for Kind {
    ///     fn is_trivia(&self) -> bool {
    ///         matches!(self, Kind::Space)
    ///     }
    /// }
    ///
    /// assert!(Token::new(Kind::Space, Span::new(0, 1)).is_trivia());
    /// assert!(!Token::new(Kind::Word, Span::new(1, 5)).is_trivia());
    /// ```
    #[inline]
    #[must_use]
    pub fn is_trivia(&self) -> bool {
        self.kind.is_trivia()
    }

    /// Whether this token's kind is the end-of-input marker. Forwards to
    /// [`TokenKind::is_eof`].
    ///
    /// # Examples
    ///
    /// ```
    /// use token_lang::{Span, Token, TokenKind};
    ///
    /// #[derive(Clone, Copy)]
    /// enum Kind {
    ///     Word,
    ///     Eof,
    /// }
    /// impl TokenKind for Kind {
    ///     fn is_eof(&self) -> bool {
    ///         matches!(self, Kind::Eof)
    ///     }
    /// }
    ///
    /// assert!(Token::new(Kind::Eof, Span::empty(9)).is_eof());
    /// assert!(!Token::new(Kind::Word, Span::new(0, 4)).is_eof());
    /// ```
    #[inline]
    #[must_use]
    pub fn is_eof(&self) -> bool {
        self.kind.is_eof()
    }

    /// The interned lexeme this token carries, if any. Forwards to
    /// [`TokenKind::symbol`].
    ///
    /// # Examples
    ///
    /// ```
    /// use intern_lang::Interner;
    /// use token_lang::{Span, Symbol, Token, TokenKind};
    ///
    /// #[derive(Clone, Copy)]
    /// enum Kind {
    ///     Ident(Symbol),
    ///     Plus,
    /// }
    /// impl TokenKind for Kind {
    ///     fn symbol(&self) -> Option<Symbol> {
    ///         match self {
    ///             Kind::Ident(s) => Some(*s),
    ///             Kind::Plus => None,
    ///         }
    ///     }
    /// }
    ///
    /// let mut interner = Interner::new();
    /// let tok = Token::new(Kind::Ident(interner.intern("x")), Span::new(0, 1));
    /// assert_eq!(tok.symbol().and_then(|s| interner.resolve(s)), Some("x"));
    /// assert_eq!(Token::new(Kind::Plus, Span::new(1, 2)).symbol(), None);
    /// ```
    #[inline]
    #[must_use]
    pub fn symbol(&self) -> Option<Symbol> {
        self.kind.symbol()
    }
}

impl<K: fmt::Display> fmt::Display for Token<K> {
    /// Formats as `kind @ start..end`.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} @ {}", self.kind, self.span)
    }
}

impl<K> From<(K, Span)> for Token<K> {
    /// Builds a token from a `(kind, span)` pair, matching the
    /// [`new`](Token::new) argument order.
    #[inline]
    fn from((kind, span): (K, Span)) -> Self {
        Self::new(kind, span)
    }
}

impl<K> From<Token<K>> for Spanned<K> {
    /// A token is a spanned kind; this drops the token semantics for the plain
    /// [`Spanned`] pair, preserving both fields.
    #[inline]
    fn from(token: Token<K>) -> Self {
        Spanned::new(token.span, token.kind)
    }
}

impl<K> From<Spanned<K>> for Token<K> {
    /// The inverse of the token-to-[`Spanned`] conversion: reinterprets a spanned
    /// value as a token of that kind, preserving both fields.
    #[inline]
    fn from(spanned: Spanned<K>) -> Self {
        Self::new(spanned.value, spanned.span)
    }
}

#[cfg(test)]
mod tests {
    // Reconstructing a `Symbol` from a known id is `Option`-returning; unwrapping
    // it in a test where the id is a literal is the clearest form.
    #![allow(clippy::unwrap_used)]

    extern crate alloc;
    use alloc::string::{String, ToString};

    use super::*;

    #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
    enum Kind {
        Ident(Symbol),
        Plus,
        Space,
        Eof,
    }

    impl TokenKind for Kind {
        fn is_trivia(&self) -> bool {
            matches!(self, Kind::Space)
        }
        fn is_eof(&self) -> bool {
            matches!(self, Kind::Eof)
        }
        fn symbol(&self) -> Option<Symbol> {
            match self {
                Kind::Ident(s) => Some(*s),
                _ => None,
            }
        }
    }

    #[test]
    fn test_new_stores_kind_and_span() {
        let tok = Token::new(Kind::Plus, Span::new(2, 3));
        assert_eq!(*tok.kind(), Kind::Plus);
        assert_eq!(tok.span(), Span::new(2, 3));
    }

    #[test]
    fn test_map_keeps_span() {
        let tok = Token::new("123", Span::new(4, 7));
        let mapped = tok.map(|s| s.len());
        assert_eq!(*mapped.kind(), 3);
        assert_eq!(mapped.span(), Span::new(4, 7));
    }

    #[test]
    fn test_as_ref_does_not_consume() {
        let owned = Token::new(String::from("name"), Span::new(0, 4));
        let len = owned.as_ref().map(String::len);
        assert_eq!(*len.kind(), 4);
        assert_eq!(owned.kind(), "name");
    }

    #[test]
    fn test_orders_by_span_first() {
        // Spans differ; the smaller span sorts first regardless of kind value.
        let a = Token::new(Kind::Eof, Span::new(0, 1));
        let b = Token::new(Kind::Plus, Span::new(1, 2));
        assert!(a < b);
    }

    #[test]
    fn test_delegators_forward_to_kind() {
        let sym = Symbol::from_u32(3).unwrap();
        assert!(Token::new(Kind::Space, Span::empty(0)).is_trivia());
        assert!(Token::new(Kind::Eof, Span::empty(0)).is_eof());
        assert_eq!(
            Token::new(Kind::Ident(sym), Span::new(0, 1)).symbol(),
            Some(sym)
        );
        assert_eq!(Token::new(Kind::Plus, Span::new(0, 1)).symbol(), None);
    }

    #[test]
    fn test_display_reads_as_kind_at_span() {
        let tok = Token::new("if", Span::new(0, 2));
        assert_eq!(tok.to_string(), "if @ 0..2");
    }

    #[test]
    fn test_roundtrips_through_spanned() {
        let tok = Token::new(Kind::Plus, Span::new(2, 3));
        let spanned: Spanned<Kind> = tok.into();
        assert_eq!(spanned.span, Span::new(2, 3));
        assert_eq!(spanned.value, Kind::Plus);
        assert_eq!(Token::from(spanned), tok);
    }

    #[test]
    fn test_from_tuple_matches_new() {
        let from_tuple: Token<Kind> = (Kind::Plus, Span::new(0, 1)).into();
        assert_eq!(from_tuple, Token::new(Kind::Plus, Span::new(0, 1)));
    }

    #[test]
    fn test_token_is_copy_when_kind_is() {
        let tok = Token::new(Kind::Plus, Span::new(0, 1));
        let copy = tok;
        // Both usable: `tok` was copied, not moved.
        assert_eq!(tok, copy);
    }
}