viperjs 0.3.0

An embeddable JavaScript engine in safe Rust, with zero runtime dependencies.
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
//! What a token *is*: the kinds and the punctuator table.
//!
//! Data and its spellings, with no scanning logic — which is why the test here can afford to
//! write every punctuator out a second time by hand. Two independent spellings of the same list
//! catch a drifting table; one spelling proves nothing.

use super::{ReservedWord, TemplatePart};
use crate::span::Span;

/// One lexical token: what it is, where it is, and whether a line break preceded it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Token {
    /// Which token this is.
    pub kind: TokenKind,
    /// The bytes the token itself covers — never the trivia around it.
    pub span: Span,
    /// Whether at least one line terminator was crossed since the previous token.
    ///
    /// Recorded here rather than recomputed later because automatic semicolon insertion
    /// (ECMA-262 §12.10) is defined in terms of it, and by the time the parser asks, the trivia
    /// is gone. A block comment containing a line terminator sets this too — §12.4 says such a
    /// comment *is* a line terminator for the syntactic grammar, and that is exactly the rule
    /// that decides whether `a = b /*\n*/ ++c` is one statement or two.
    ///
    /// True for the first token of a source that begins with a line terminator. Nothing
    /// consults it there, and the alternative is a special case that earns nothing.
    pub newline_before: bool,
}

/// Every token form this slice can produce: the punctuators of ECMA-262 §12.8, plus end of
/// input.
///
/// End of input is a token, not `None`. A parser that has to handle "no more tokens" separately
/// from "wrong token" grows a second error path for every construct; giving EOF a kind and an
/// empty span at the end of the source collapses the two.
///
/// Every variant except [`TokenKind::Eof`] must also appear in the `PUNCTUATORS` table — the
/// tests cross-check the two.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TokenKind {
    /// End of input. Empty span at the end of the source; repeats forever once reached.
    Eof,

    /// An `IdentifierName` (§12.7) that is not one of the unconditionally reserved words.
    ///
    /// The span covers the spelling as written, escapes included;
    /// [`super::identifier_value`] turns it into the sequence of code points the spec calls
    /// its `IdentifierCodePoints`.
    Identifier {
        /// Whether a `\u` escape contributed a code point to the spelling.
        ///
        /// The parser needs this and cannot recover it later without re-lexing. §12.7.2 Note 1:
        /// a keyword matches a literal sequence of source characters, so `els\u{65}` is an
        /// `IdentifierName` and **not** the keyword `else` — while the early errors of §13.1.1
        /// separately forbid using it as a binding. Both rules need to know an escape was used.
        contains_escape: bool,
    },

    /// `# IdentifierName` (§12.7) — a private class member name.
    ///
    /// The span includes the `#`, as does the value: the spec's `StringValue` of a
    /// `PrivateIdentifier` is the number sign concatenated with the name's.
    PrivateIdentifier {
        /// Whether a `\u` escape contributed a code point to the name.
        contains_escape: bool,
    },

    /// One of the 38 spellings §12.7.2 reserves unconditionally, written without escapes.
    Keyword(ReservedWord),

    /// A `NumericLiteral` (§12.9.3) denoting a Number. [`crate::lexer::numeric_value`] reads it.
    Number {
        /// Whether this is one of Annex B.1.1's two legacy forms — a `LegacyOctalIntegerLiteral`
        /// like `0123`, or a `NonOctalDecimalIntegerLiteral` like `08`.
        ///
        /// §12.9.3.1 makes both a Syntax Error in strict code and legal outside it. The lexer
        /// cannot know which it is reading, so it records the fact and leaves the verdict to the
        /// parser — the same division of labour as `contains_escape` on an identifier.
        legacy: bool,
    },

    /// A `StringLiteral` (§12.9.4). [`crate::lexer::string_value`] reads its code units.
    String {
        /// Whether it used one of Annex B's legacy escapes — `LegacyOctalEscapeSequence` like
        /// `\7`, or `NonOctalDecimalEscapeSequence`, which is `\8` and `\9`.
        ///
        /// §12.9.4.1 makes both a Syntax Error in strict code. Its Note 2 explains why the lexer
        /// cannot settle it: a literal may *precede* the directive that makes its own code
        /// strict, as in `function invalid() { "\7"; "use strict"; }`.
        legacy_escape: bool,
    },

    /// One component of a template literal (§12.9.6).
    ///
    /// A template is not one token: `` `a${x}b` `` is a `Head`, an expression, and a `Tail`.
    /// [`crate::lexer::template_value`] gives a component its `TV` and `TRV`.
    Template {
        /// Which of the four components this is, and so whether a substitution follows.
        part: TemplatePart,
        /// Whether the component contains a `NotEscapeSequence`, making its `TV` undefined.
        ///
        /// Not an error: §12.9.6 admits ill-formed escapes as `TemplateCharacter`s so that a
        /// *tagged* template can carry raw text a cooked string could not represent. Only an
        /// untagged one is a Syntax Error (§13.2.8.1), and only the parser knows which it has.
        cooked_undefined: bool,
    },

    /// A `RegularExpressionLiteral` (§12.9.5), read only under [`crate::lexer::Goal::RegExp`].
    ///
    /// The token is its extent and nothing more: §12.9.5 says the body and flags "are
    /// subsequently parsed again using the more stringent ECMAScript Regular Expression
    /// grammar", which is M4's work. [`crate::lexer::regexp_parts`] carves the span into the
    /// two halves that grammar will want.
    RegExp,

    /// A `NumericLiteral` carrying the `n` of `BigIntLiteralSuffix` (§12.9.3).
    ///
    /// Recognised now although BigInt values arrive at M7, because the alternative is worse than
    /// waiting: without the suffix, `123n` lexes as `123` followed by the name `n`, which is
    /// nonsense that parses.
    BigInt,

    /// `{`
    LBrace,
    /// `}` — the spec's `RightBracePunctuator`, produced under every goal except the two that
    /// let a `}` resume a template instead (see [`crate::lexer::Goal`]).
    RBrace,
    /// `(`
    LParen,
    /// `)`
    RParen,
    /// `[`
    LBracket,
    /// `]`
    RBracket,

    /// `.`
    Dot,
    /// `...`
    DotDotDot,
    /// `;`
    Semicolon,
    /// `,`
    Comma,
    /// `:`
    Colon,
    /// `=>`
    Arrow,
    /// `?.` — only when the next code point is not a decimal digit (§12.8).
    QuestionDot,
    /// `?`
    Question,

    /// `<`
    Lt,
    /// `>`
    Gt,
    /// `<=`
    LtEq,
    /// `>=`
    GtEq,
    /// `==`
    EqEq,
    /// `!=`
    BangEq,
    /// `===`
    EqEqEq,
    /// `!==`
    BangEqEq,

    /// `+`
    Plus,
    /// `-`
    Minus,
    /// `*`
    Star,
    /// `/` — the spec's `DivPunctuator`, produced only under [`crate::lexer::Goal::Div`].
    /// Under `Goal::RegExp` the same character opens a [`TokenKind::RegExp`] instead.
    Slash,
    /// `%`
    Percent,
    /// `**`
    StarStar,
    /// `++`
    PlusPlus,
    /// `--`
    MinusMinus,

    /// `<<`
    LtLt,
    /// `>>`
    GtGt,
    /// `>>>`
    GtGtGt,

    /// `&`
    Amp,
    /// `|`
    Pipe,
    /// `^`
    Caret,
    /// `!`
    Bang,
    /// `~`
    Tilde,
    /// `&&`
    AmpAmp,
    /// `||`
    PipePipe,
    /// `??`
    QuestionQuestion,

    /// `=`
    Eq,
    /// `+=`
    PlusEq,
    /// `-=`
    MinusEq,
    /// `*=`
    StarEq,
    /// `/=` — like [`TokenKind::Slash`], only under [`crate::lexer::Goal::Div`]; under
    /// `Goal::RegExp` this is a literal whose body begins with `=`.
    SlashEq,
    /// `%=`
    PercentEq,
    /// `**=`
    StarStarEq,
    /// `<<=`
    LtLtEq,
    /// `>>=`
    GtGtEq,
    /// `>>>=`
    GtGtGtEq,
    /// `&=`
    AmpEq,
    /// `|=`
    PipeEq,
    /// `^=`
    CaretEq,
    /// `&&=`
    AmpAmpEq,
    /// `||=`
    PipePipeEq,
    /// `??=`
    QuestionQuestionEq,
}

impl TokenKind {
    /// The source text every token of this kind has, or `None` when the text varies.
    ///
    /// Punctuators and keywords have exactly one spelling; [`TokenKind::Eof`] has the empty one.
    /// Identifiers do not, so they answer `None` rather than a lie — a caller that wants their
    /// text asks the span, and a caller that wants their *value* asks
    /// [`super::identifier_value`].
    ///
    /// Written as a match rather than a lookup in `PUNCTUATORS` on purpose: two independent
    /// spellings of the same fact let the tests catch a table row that drifted, which a
    /// self-consistent lookup never could.
    pub fn as_str(&self) -> Option<&'static str> {
        Some(match self {
            Self::Eof => "",

            Self::Identifier { .. }
            | Self::PrivateIdentifier { .. }
            | Self::Number { .. }
            | Self::BigInt
            | Self::String { .. }
            | Self::RegExp
            | Self::Template { .. } => return None,
            Self::Keyword(word) => word.as_str(),

            Self::LBrace => "{",
            Self::RBrace => "}",
            Self::LParen => "(",
            Self::RParen => ")",
            Self::LBracket => "[",
            Self::RBracket => "]",

            Self::Dot => ".",
            Self::DotDotDot => "...",
            Self::Semicolon => ";",
            Self::Comma => ",",
            Self::Colon => ":",
            Self::Arrow => "=>",
            Self::QuestionDot => "?.",
            Self::Question => "?",

            Self::Lt => "<",
            Self::Gt => ">",
            Self::LtEq => "<=",
            Self::GtEq => ">=",
            Self::EqEq => "==",
            Self::BangEq => "!=",
            Self::EqEqEq => "===",
            Self::BangEqEq => "!==",

            Self::Plus => "+",
            Self::Minus => "-",
            Self::Star => "*",
            Self::Slash => "/",
            Self::Percent => "%",
            Self::StarStar => "**",
            Self::PlusPlus => "++",
            Self::MinusMinus => "--",

            Self::LtLt => "<<",
            Self::GtGt => ">>",
            Self::GtGtGt => ">>>",

            Self::Amp => "&",
            Self::Pipe => "|",
            Self::Caret => "^",
            Self::Bang => "!",
            Self::Tilde => "~",
            Self::AmpAmp => "&&",
            Self::PipePipe => "||",
            Self::QuestionQuestion => "??",

            Self::Eq => "=",
            Self::PlusEq => "+=",
            Self::MinusEq => "-=",
            Self::StarEq => "*=",
            Self::SlashEq => "/=",
            Self::PercentEq => "%=",
            Self::StarStarEq => "**=",
            Self::LtLtEq => "<<=",
            Self::GtGtEq => ">>=",
            Self::GtGtGtEq => ">>>=",
            Self::AmpEq => "&=",
            Self::PipeEq => "|=",
            Self::CaretEq => "^=",
            Self::AmpAmpEq => "&&=",
            Self::PipePipeEq => "||=",
            Self::QuestionQuestionEq => "??=",
        })
    }
}

/// Every punctuator, **longest first**.
///
/// "A token is always as long as possible" (§12.4 states the rule while explaining comments; it
/// governs the whole lexical grammar), so `>>>=` must be tried before `>>>`, `>>` and `>`. The
/// ordering is the entire correctness argument for the match loop, so a test asserts it rather
/// than trusting the next person to insert a row in the right place.
pub(super) const PUNCTUATORS: &[(&str, TokenKind)] = &[
    // 4 bytes.
    (">>>=", TokenKind::GtGtGtEq),
    // 3 bytes.
    ("...", TokenKind::DotDotDot),
    ("===", TokenKind::EqEqEq),
    ("!==", TokenKind::BangEqEq),
    ("**=", TokenKind::StarStarEq),
    ("<<=", TokenKind::LtLtEq),
    (">>=", TokenKind::GtGtEq),
    (">>>", TokenKind::GtGtGt),
    ("&&=", TokenKind::AmpAmpEq),
    ("||=", TokenKind::PipePipeEq),
    ("??=", TokenKind::QuestionQuestionEq),
    // 2 bytes.
    ("=>", TokenKind::Arrow),
    ("==", TokenKind::EqEq),
    ("!=", TokenKind::BangEq),
    ("<=", TokenKind::LtEq),
    (">=", TokenKind::GtEq),
    ("+=", TokenKind::PlusEq),
    ("-=", TokenKind::MinusEq),
    ("*=", TokenKind::StarEq),
    ("/=", TokenKind::SlashEq),
    ("%=", TokenKind::PercentEq),
    ("&=", TokenKind::AmpEq),
    ("|=", TokenKind::PipeEq),
    ("^=", TokenKind::CaretEq),
    ("**", TokenKind::StarStar),
    ("++", TokenKind::PlusPlus),
    ("--", TokenKind::MinusMinus),
    ("<<", TokenKind::LtLt),
    (">>", TokenKind::GtGt),
    ("&&", TokenKind::AmpAmp),
    ("||", TokenKind::PipePipe),
    ("??", TokenKind::QuestionQuestion),
    ("?.", TokenKind::QuestionDot),
    // 1 byte.
    ("{", TokenKind::LBrace),
    ("}", TokenKind::RBrace),
    ("(", TokenKind::LParen),
    (")", TokenKind::RParen),
    ("[", TokenKind::LBracket),
    ("]", TokenKind::RBracket),
    (".", TokenKind::Dot),
    (";", TokenKind::Semicolon),
    (",", TokenKind::Comma),
    (":", TokenKind::Colon),
    ("?", TokenKind::Question),
    ("<", TokenKind::Lt),
    (">", TokenKind::Gt),
    ("+", TokenKind::Plus),
    ("-", TokenKind::Minus),
    ("*", TokenKind::Star),
    ("/", TokenKind::Slash),
    ("%", TokenKind::Percent),
    ("&", TokenKind::Amp),
    ("|", TokenKind::Pipe),
    ("^", TokenKind::Caret),
    ("!", TokenKind::Bang),
    ("~", TokenKind::Tilde),
    ("=", TokenKind::Eq),
];

#[cfg(test)]
mod tests {
    use super::*;
    use crate::lexer::test_support::*;
    #[test]
    fn every_punctuator_lexes_as_itself_and_the_table_is_ordered_longest_first() {
        // Longest-first ordering is the whole correctness argument for the match loop, and it
        // is a property of the table's *order* — nothing else in the file would notice a row
        // inserted in the wrong place.
        for pair in PUNCTUATORS.windows(2) {
            let [(before, _), (after, _)] = pair else {
                continue;
            };
            assert!(
                before.len() >= after.len(),
                "{before:?} must not precede the longer {after:?}"
            );
        }
        // The table and `as_str` are written independently; each row must agree with its kind,
        // and each kind must appear exactly once.
        let mut seen = std::collections::HashSet::new();
        for &(text, kind) in PUNCTUATORS {
            assert_eq!(
                kind.as_str(),
                Some(text),
                "table row {text:?} disagrees with as_str"
            );
            assert!(seen.insert(kind), "{text:?} appears twice in the table");
            // …and every one of them actually lexes, in isolation, to exactly itself. `?.` is
            // the one exception to "text in, kind out" and has its own test.
            if kind != TokenKind::QuestionDot {
                assert_eq!(kinds(text), [kind, TokenKind::Eof], "lexing {text:?}");
            }
        }
        assert_eq!(seen.len(), 57, "ECMA-262 §12.8 has 57 punctuators");
        // Eof's fixed text is the empty one, which is what makes the span/kind cross-check in
        // `retile` work uniformly for it. Identifiers have no fixed text at all — the
        // difference between `Some("")` and `None` is "always empty" versus "ask the source".
        assert_eq!(TokenKind::Eof.as_str(), Some(""));
        assert_eq!(
            TokenKind::Identifier {
                contains_escape: false
            }
            .as_str(),
            None
        );
        assert_eq!(
            TokenKind::PrivateIdentifier {
                contains_escape: false
            }
            .as_str(),
            None
        );
    }
}