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
//! Literal token types for language syntax trees.
//!
//! This module provides generic literal types that represent various kinds of
//! literal values found in programming languages: numbers, strings, booleans, etc.
//! Each literal type carries its source representation along with span information.
//!
//! # Design Philosophy
//!
//! All literal types follow the same pattern as [`Ident`](super::Ident):
//!
//! - **Generic string type `S`**: Support `&str`, `String`, or interned strings
//! - **Language marker `Lang`**: Type-safe language distinction
//! - **Span tracking**: All literals carry source location for diagnostics
//! - **Error recovery**: Implement [`ErrorNode`] for placeholder creation
//!
//! # Available Literal Types
//!
//! ## Generic Literal
//!
//! - [`Lit`]: Generic literal (any literal type)
//!
//! ## Numeric Literals
//!
//! - [`LitDecimal`]: Base-10 integer (e.g., `42`, `1_000`)
//! - [`LitHex`]: Hexadecimal integer (e.g., `0xFF`, `0x1A2B`)
//! - [`LitOctal`]: Octal integer (e.g., `0o77`, `0o644`)
//! - [`LitBinary`]: Binary integer (e.g., `0b1010`, `0b1111_0000`)
//! - [`LitFloat`](crate::types::lit::LitFloat): Floating-point (e.g., `3.14`, `1.0e-5`)
//! - [`LitHexFloat`]: Hexadecimal float (e.g., `0x1.8p3`)
//!
//! ## String Literals
//!
//! - [`LitString`]: Single-line string (e.g., `"hello"`)
//! - [`LitMultilineString`]: Multi-line string (e.g., `"""..."""`)
//! - [`LitRawString`]: Raw string without escape processing (e.g., `r"C:\path"`)
//!
//! ## Character/Byte Literals
//!
//! - [`LitChar`]: Character literal (e.g., `'a'`, `'\n'`)
//! - [`LitByte`]: Byte literal (e.g., `b'a'`, `b'\x7F'`)
//! - [`LitByteString`]: Byte string (e.g., `b"bytes"`)
//!
//! ## Boolean and Null
//!
//! - [`LitBool`]: Boolean literal (`true`/`false`)
//! - [`LitNull`]: Null/nil literal
//!
//! # Common Usage Patterns
//!
//! ## Zero-Copy Parsing
//!
//! ```rust,ignore
//! use tokit::types::{Lit, LitDecimal, LitString};
//! use tokit::utils::SimpleSpan;
//!
//! // Parse literals without allocating
//! type YulLit<'a> = Lit<&'a str, YulLang>;
//! type YulDecimal<'a> = LitDecimal<&'a str, YulLang>;
//! type YulString<'a> = LitString<&'a str, YulLang>;
//!
//! let generic = YulLit::new(Span::new(0, 2), "42");
//! let num = YulDecimal::new(Span::new(0, 2), "42");
//! let str = YulString::new(Span::new(5, 12), "\"hello\"");
//! ```
//!
//! ## Owned Literals
//!
//! ```rust,ignore
//! // Store literals in AST nodes
//! type OwnedDecimal = LitDecimal<String, MyLang>;
//!
//! let lit = OwnedDecimal::new(span, source.to_string());
//! ```
//!
//! # Error Recovery
//!
//! All literal types implement [`ErrorNode`] when `S: ErrorNode`:
//!
//! ```rust,ignore
//! use tokit::types::LitDecimal;
//! use tokit::error::ErrorNode;
//!
//! // Create placeholder for malformed literal
//! let bad_lit = LitDecimal::<String, YulLang>::error(span);
//!
//! // Create placeholder for missing literal
//! let missing_lit = LitDecimal::<String, YulLang>::missing(span);
//! ```

use core::marker::PhantomData;

use crate::{
  error::ErrorNode,
  utils::{AsSpan, IntoComponents},
};

/// A macro to generate literal type structures.
///
/// This reduces boilerplate by generating identical structure and implementations
/// for all literal types.
macro_rules! define_literal {
  (
    $(#[$meta:meta])*
    $name:ident,
    $doc:expr,
    $example_str:expr,
    $example_desc:expr
  ) => {
    paste::paste! {
      $(#[$meta])*
      #[doc = $doc]
      ///
      /// # Type Parameters
      ///
      /// - `S`: The source string type (`&str`, `String`, interned string, etc.)
      /// - `Lang`: Language marker type for type safety
      ///
      /// # Examples
      ///
      /// ## Creating Literals
      ///
      /// ```rust
      #[doc = "use tokit::types::" $name ";"]
      /// use tokit::utils::SimpleSpan;
      /// # struct MyLang;
      ///
      #[doc = "let lit = " $name "::<&str, MyLang>::new("]
      #[doc = "    Span::new(0, 4),"]
      #[doc = "    " $example_str ]
      /// );
      ///
      #[doc = "assert_eq!(lit.source_ref(), &" $example_str ");"]
      /// ```
      ///
      /// ## With Error Recovery
      ///
      /// ```rust,ignore
      #[doc = "use tokit::types::" $name ";"]
      /// use tokit::error::ErrorNode;
      ///
      #[doc = "// " $example_desc]
      #[doc = "let bad_lit = " $name "::<String, YulLang>::error(span);"]
      /// ```
      #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
      pub struct $name<D, S = $crate::__private::utils::SimpleSpan, Lang = ()> {
        span: S,
        data: D,
        _lang: PhantomData<Lang>,
      }
    }

    impl<D, S, Lang> AsSpan<S> for $name<D, S, Lang> {
      #[cfg_attr(not(tarpaulin), inline(always))]
      fn as_span(&self) -> &S {
        self.span_ref()
      }
    }

    impl<D, S, Lang> IntoComponents for $name<D, S, Lang> {
      type Components = (S, D);

      #[cfg_attr(not(tarpaulin), inline(always))]
      fn into_components(self) -> Self::Components {
        (self.span, self.data)
      }
    }

    impl<D, S, Lang> $name<D, S, Lang> {
      /// Creates a new literal with the given span and source string.
      ///
      /// # Parameters
      ///
      /// - `span`: The source location of this literal
      /// - `data`: The literal's data
      #[cfg_attr(not(tarpaulin), inline(always))]
      pub const fn new(span: S, data: D) -> Self {
        Self {
          span,
          data,
          _lang: PhantomData,
        }
      }

      /// Returns the span (source location) of this literal.
      #[cfg_attr(not(tarpaulin), inline(always))]
      pub const fn span(&self) -> S where S: ::core::marker::Copy {
        self.span
      }

      /// Returns an immutable reference to the span.
      #[cfg_attr(not(tarpaulin), inline(always))]
      pub const fn span_ref(&self) -> &S {
        &self.span
      }

      /// Returns a mutable reference to the span.
      #[cfg_attr(not(tarpaulin), inline(always))]
      pub const fn span_mut(&mut self) -> &mut S {
        &mut self.span
      }

      /// Returns a mutable reference to the source string.
      #[cfg_attr(not(tarpaulin), inline(always))]
      pub const fn data_mut(&mut self) -> &mut D {
        &mut self.data
      }

      /// Returns an immutable reference to the source string.
      ///
      /// This is the most common way to access the literal's text.
      #[cfg_attr(not(tarpaulin), inline(always))]
      pub const fn data_ref(&self) -> &D {
        &self.data
      }

      /// Returns a copy of the source string by value.
      ///
      /// Only available when `S` implements [`Copy`].
      #[cfg_attr(not(tarpaulin), inline(always))]
      pub const fn data(&self) -> D
      where
        D: Copy,
      {
        self.data
      }
    }

    impl<D, S, Lang> ErrorNode<S> for $name<D, S, Lang>
    where
      D: ErrorNode<S>,
      S: Clone,
    {
      /// Creates a placeholder literal for **malformed content**.
      #[cfg_attr(not(tarpaulin), inline(always))]
      fn error(span: S) -> Self {
        Self::new(span.clone(), D::error(span))
      }

      /// Creates a placeholder literal for **missing required content**.
      #[cfg_attr(not(tarpaulin), inline(always))]
      fn missing(span: S) -> Self {
        Self::new(span.clone(), D::missing(span))
      }
    }
  };
}

// Generic literal
define_literal!(
  /// A generic literal.
  ///
  /// Represents any kind of literal value without distinguishing between specific
  /// types (numeric, string, boolean, etc.). Useful when the exact literal type
  /// doesn't matter for your use case.
  Lit,
  "A generic literal (any literal type).",
  "\"value\"",
  "Malformed literal"
);

// Numeric literals
define_literal!(
  /// A decimal (base-10) integer literal.
  ///
  /// Represents numeric literals in standard decimal notation, such as `42`, `1000`,
  /// or `123_456`. The source string may include underscores for readability but
  /// represents a single integer value.
  LitDecimal,
  "A decimal integer literal (e.g., `42`, `1_000`).",
  "\"42\"",
  "Malformed decimal literal like \"12abc\""
);

define_literal!(
  /// A hexadecimal (base-16) integer literal.
  ///
  /// Represents integer literals in hexadecimal notation, typically prefixed with
  /// `0x` or `0X`, such as `0xFF`, `0x1A2B`, or `0xDEAD_BEEF`.
  LitHex,
  "A hexadecimal integer literal (e.g., `0xFF`, `0x1A2B`).",
  "\"0xFF\"",
  "Malformed hex literal like \"0xGG\""
);

define_literal!(
  /// An octal (base-8) integer literal.
  ///
  /// Represents integer literals in octal notation, typically prefixed with `0o`,
  /// such as `0o77`, `0o644`, or `0o755`.
  LitOctal,
  "An octal integer literal (e.g., `0o77`, `0o644`).",
  "\"0o77\"",
  "Malformed octal literal like \"0o89\""
);

define_literal!(
  /// A binary (base-2) integer literal.
  ///
  /// Represents integer literals in binary notation, typically prefixed with `0b`,
  /// such as `0b1010`, `0b11110000`, or `0b1111_0000`.
  LitBinary,
  "A binary integer literal (e.g., `0b1010`, `0b1111_0000`).",
  "\"0b1010\"",
  "Malformed binary literal like \"0b123\""
);

define_literal!(
  /// A floating-point literal.
  ///
  /// Represents floating-point literals in standard decimal notation with optional
  /// fractional and exponent parts, such as `3.14`, `1.0`, `2.5e-3`, or `6.022e23`.
  LitFloat,
  "A floating-point literal (e.g., `3.14`, `1.0e-5`).",
  "\"3.14\"",
  "Malformed float literal like \"3.14.15\""
);

define_literal!(
  /// A hexadecimal floating-point literal.
  ///
  /// Represents floating-point literals in hexadecimal notation with binary exponent,
  /// such as `0x1.8p3` (which equals 12.0 in decimal). Used in languages like C and Rust
  /// for precise floating-point representation.
  LitHexFloat,
  "A hexadecimal floating-point literal (e.g., `0x1.8p3`).",
  "\"0x1.8p3\"",
  "Malformed hex float like \"0x1.Gp3\""
);

// String literals
define_literal!(
  /// A single-line string literal.
  ///
  /// Represents string literals enclosed in quotes, typically on a single line,
  /// such as `"hello"`, `"world\n"`, or `"escaped \"quotes\""`. May contain
  /// escape sequences.
  LitString,
  "A single-line string literal (e.g., `\"hello\"`, `\"world\\n\"`).",
  "\"\\\"hello\\\"\"",
  "Malformed string like unterminated \"hello"
);

define_literal!(
  /// A multi-line string literal.
  ///
  /// Represents string literals that span multiple lines, often with special delimiters
  /// like triple quotes (`"""..."""` or `'''...'''`). Common in languages like Python,
  /// Kotlin, and Swift.
  LitMultilineString,
  "A multi-line string literal (e.g., `\"\"\"...\"\"\"`).",
  "\"\\\"\\\"\\\"multi\\nline\\\"\\\"\\\"\"",
  "Malformed multiline string"
);

define_literal!(
  /// A raw string literal.
  ///
  /// Represents string literals where escape sequences are not processed, often
  /// prefixed with `r` (e.g., Rust's `r"C:\path"`, Python's `r"\n stays literal"`).
  /// Useful for regular expressions and file paths.
  LitRawString,
  "A raw string literal without escape processing (e.g., `r\"C:\\path\"`).",
  "\"r\\\"C:\\\\path\\\"\"",
  "Malformed raw string"
);

// Character and byte literals
define_literal!(
  /// A character literal.
  ///
  /// Represents a single character enclosed in single quotes, such as `'a'`, `'\\n'`,
  /// or `'\\u{1F600}'`. May contain escape sequences for special characters.
  LitChar,
  "A character literal (e.g., `'a'`, `'\\n'`, `'\\u{1F600}'`).",
  "\"'a'\"",
  "Malformed char like unclosed 'a"
);

define_literal!(
  /// A byte literal.
  ///
  /// Represents a single byte value enclosed in single quotes with a `b` prefix,
  /// such as `b'a'`, `b'\\x7F'`, or `b'\\n'`. Used in languages like Rust for
  /// ASCII/byte manipulation.
  LitByte,
  "A byte literal (e.g., `b'a'`, `b'\\x7F'`).",
  "\"b'a'\"",
  "Malformed byte literal"
);

define_literal!(
  /// A byte string literal.
  ///
  /// Represents a sequence of bytes enclosed in quotes with a `b` prefix, such as
  /// `b"bytes"`, `b"\\x48\\x65\\x6C\\x6C\\x6F"`. Used for binary data or ASCII strings.
  LitByteString,
  "A byte string literal (e.g., `b\"bytes\"`, `b\"\\x48\\x65\\x6C\\x6C\\x6F\"`).",
  "\"b\\\"bytes\\\"\"",
  "Malformed byte string"
);

// Boolean and null
define_literal!(
  /// A boolean literal.
  ///
  /// Represents boolean values `true` or `false`. The source string contains the
  /// actual keyword as it appears in source code.
  LitBool,
  "A boolean literal (`true` or `false`).",
  "\"true\"",
  "Malformed boolean like \"tru\" or \"fals\""
);

define_literal!(
  /// A null/nil literal.
  ///
  /// Represents the null, nil, or None value in various programming languages.
  /// The source string contains the keyword as it appears (e.g., `null`, `nil`,
  /// `None`, `nullptr`).
  LitNull,
  "A null/nil literal (e.g., `null`, `nil`, `None`).",
  "\"null\"",
  "Malformed null literal"
);