yara-x-parser 1.12.0

A parsing library for YARA rules.
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
/*! Implements the YARA tokenizer.

Tokenization is the first step in the compilation process. The tokenizer takes
YARA source code and produces a sequence of tokens that is later processed by
the parser. Each token is represented by a variant of the [`Token`] type.
*/

use std::str::from_utf8;

use logos::Logos;

use crate::Span;

pub(crate) use tokens::Token;
pub(crate) use tokens::TokenId;

mod tokens;

#[cfg(test)]
mod tests;

/// Takes YARA source code and produces a sequence of tokens.
///
/// The tokenizer has three modes of operation: normal mode, hex pattern mode,
/// and hex jump.
///
/// In normal mode the tokenizer recognizes most of the tokens in YARA's syntax,
/// like keywords (e.g: `rule`, `condition`, `for`, etc.), identifiers, string
/// literals, etc. In hex pattern mode, the tokenizer only recognizes the tokens
/// that can appear in a hex pattern, and in hex jump only the tokens that can
/// appear inside a hex jump.
///
/// This distinction is crucial because certain tokens, like `a0`, have
/// different meanings depending on the mode. Outside a hex pattern, `a0` is an
/// identifier; inside, it's a byte literal. Another example is `10` which is
/// an integer literal in normal mode, a hex byte in hex pattern mode, and
/// also an integer literal in hex jump mode.
///
/// The tokenizer itself is unable to know whether a token is inside a hex
/// pattern, or inside a hex jump, only the parser can know that. Therefore,
/// it is the parser's responsibility to switch the tokenizer to hex pattern
/// mode after parsing the opening brace (`{`) of a hex pattern. This is done
/// by invoking [`Tokenizer::enter_hex_pattern_mode`]. The tokenizer will
/// automatically revert to normal mode when it encounters the closing brace
/// (`}`). Similarly, the parser must call [`Tokenizer::enter_hex_jump_mode`]
/// after parsing the opening bracket (`[`) of a jump in a hex pattern, and
/// the tokenizer will go back to hex pattern mode when the closing bracket
/// (`]`) is found.
///
/// The input to the tokenizer is a byte slice, it doesn't require the source
/// code to be valid UTF-8. However, most of the tokens produced are guaranteed
/// to be valid UTF-8, except literal strings, regular expressions and
/// comments. Also, when the tokenizer finds some invalid UTF-8 characters,
/// outside a literal string, a regular expression, or a comment, it issues
/// the special token [`Token::INVALID_UTF8`] containing the invalid bytes,
/// and continues tokenizing the remaining content.
pub struct Tokenizer<'src> {
    source: &'src [u8],
    mode: Mode<'src>,
    /// Absolute offset within the source code where the lexer started, all
    /// spans reported by the lexer will be relative to the point were the
    /// lexer started, so we must offset those spans by this amount.
    /// For instance, if the source code is "abcde", and the lexer starts
    /// at the "c", from the lexer standpoint the span for the "c" is 0..1,
    /// but `lexer_starting_pos` will be 2, so the real span for "c" is 2..3.
    lexer_starting_pos: usize,
}

impl<'src> Tokenizer<'src> {
    /// Creates a new [`Tokenizer`].
    pub fn new(source: &'src [u8]) -> Self {
        // Can't handle source files greater than the maximum span size.
        assert!(source.len() < Span::MAX);
        Self {
            source,
            lexer_starting_pos: 0,
            mode: Mode::Normal(Logos::lexer(source)),
        }
    }

    /// Returns the source code passed to the tokenizer.
    #[inline]
    pub fn source(&self) -> &'src [u8] {
        self.source
    }

    /// Returns the next token.
    pub fn next_token(&mut self) -> Option<Token> {
        loop {
            match &mut self.mode {
                Mode::Normal(lexer) => match lexer.next()? {
                    Ok(token) => {
                        return Some(convert_normal_token(
                            token,
                            Span::from(lexer.span())
                                .offset(self.lexer_starting_pos as isize),
                        ));
                    }
                    Err(()) => return Some(self.unexpected_token()),
                },
                Mode::HexPattern(lexer) => match lexer.next()? {
                    Ok(token) => {
                        return Some(convert_hex_pattern_token(
                            token,
                            Span::from(lexer.span())
                                .offset(self.lexer_starting_pos as isize),
                        ))
                    }
                    Err(()) => {
                        // Found a token that was not expected in hex pattern
                        // mode, switch back to normal mode and try again. The
                        // start position for the new lexer is where the token
                        // was found.
                        self.lexer_starting_pos += match &self.mode {
                            Mode::HexPattern(lexer) => lexer.span().start,
                            _ => unreachable!(),
                        };
                        self.mode = Mode::Normal(Logos::lexer(
                            &self.source[self.lexer_starting_pos..],
                        ));
                    }
                },
                Mode::HexJump(lexer) => match lexer.next()? {
                    Ok(token) => {
                        return Some(convert_hex_jump_token(
                            token,
                            Span::from(lexer.span())
                                .offset(self.lexer_starting_pos as isize),
                        ))
                    }
                    Err(()) => {
                        // Found a token that was not expected in hex jump
                        // mode, switch back to hex pattern mode and try again.
                        // The start position for the new lexer is where the
                        // token was found.
                        self.lexer_starting_pos += match &self.mode {
                            Mode::HexJump(lexer) => lexer.span().start,
                            _ => unreachable!(),
                        };
                        self.mode = Mode::HexPattern(Logos::lexer(
                            &self.source[self.lexer_starting_pos..],
                        ));
                    }
                },
            }
        }
    }

    /// Switches the tokenizer to hex pattern operation mode.
    ///
    /// The parser must invoke this function after processing the opening
    /// brace (`{`) of a hex pattern. The tokenizer will automatically revert
    /// back to normal mode when encounters the closing brace (`}`).
    ///
    /// See [`Tokenizer`] for more details about operation modes.
    ///
    /// # Panics
    ///
    /// If the tokenizer is not currently in normal mode.
    pub fn enter_hex_pattern_mode(&mut self) {
        self.lexer_starting_pos += match &self.mode {
            Mode::Normal(lexer) => lexer.span().end,
            mode => {
                panic!(r"enter_hex_pattern_mode called from mode: {mode:?}")
            }
        };
        self.mode = Mode::HexPattern(Logos::lexer(
            &self.source[self.lexer_starting_pos..],
        ));
    }

    /// Switches the tokenizer to hex jump operation mode.
    ///
    /// The parser must invoke this function after processing the opening
    /// bracket (`[`) of a hex jump. The tokenizer will automatically revert
    /// back to hex pattern mode when encounters the closing bracket (`]`).
    ///
    /// See [`Tokenizer`] for more details about operation modes.
    ///
    /// # Panics
    ///
    /// If the tokenizer is not currently in hex pattern mode.
    pub fn enter_hex_jump_mode(&mut self) {
        self.lexer_starting_pos += match &self.mode {
            Mode::HexPattern(lexer) => lexer.span().end,
            mode => {
                panic!(r"enter_hex_jump_mode called from mode: {mode:?}")
            }
        };
        self.mode = Mode::HexJump(Logos::lexer(
            &self.source[self.lexer_starting_pos..],
        ));
    }
}

impl Tokenizer<'_> {
    fn unexpected_token(&mut self) -> Token {
        let lexer = match &mut self.mode {
            Mode::Normal(lexer) => lexer,
            // This function is called only in Normal mode.
            _ => unreachable!(),
        };
        let start = lexer.span().start;
        let end = lexer.source().len();
        let unexpected = lexer.source().get(start..end).unwrap();

        // Make sure that `unexpected` contains a valid UTF-8 string, or take
        // the first few bytes that are valid and ignore the rest. It's safe to
        // call .unwrap() because there must be at least one UTF-8 chunk, either
        // valid or invalid.
        let chunk = unexpected.utf8_chunks().next().unwrap();

        if chunk.valid().is_empty() {
            return Token::INVALID_UTF8(
                Span(start as u32..(start + 1) as u32)
                    .offset(self.lexer_starting_pos as isize),
            );
        }

        // `unexpected` is the valid UTF-8 prefix.
        let unexpected = chunk.valid();

        // Truncate `unexpected` at the first whitespace if any.
        let unexpected = unexpected.split(char::is_whitespace).next().unwrap();

        // If `unexpected` is larger than the current token, bump the lexer to the
        // end of `unexpected`.
        lexer.bump(unexpected.len().saturating_sub(lexer.span().len()));

        Token::UNKNOWN(
            Span::from(lexer.span()).offset(self.lexer_starting_pos as isize),
        )
    }
}

/// Describes the current mode of operation for a tokenizer.
///
/// [`Tokenizer`] uses the [`logos`] crate under the hood for doing the actual
/// work. It uses three different logos lexers, one for each of the three modes
/// of operation of the lexer: normal, hex pattern and hex jump.
#[derive(Debug)]
enum Mode<'src> {
    Normal(logos::Lexer<'src, NormalToken<'src>>),
    HexPattern(logos::Lexer<'src, HexPatternToken>),
    HexJump(logos::Lexer<'src, HexJumpToken<'src>>),
}

/// Tokens recognized in normal mode.
#[allow(clippy::upper_case_acronyms)]
#[derive(logos::Logos, Debug, PartialEq)]
#[logos(source = [u8])]
enum NormalToken<'src> {
    // Keywords
    #[token("all")]
    All,
    #[token("and")]
    And,
    #[token("any")]
    Any,
    #[token("ascii")]
    Ascii,
    #[token("at")]
    At,
    #[token("base64")]
    Base64,
    #[token("base64wide")]
    Base64Wide,
    #[token("condition")]
    Condition,
    #[token("contains")]
    Contains,
    #[token("defined")]
    Defined,
    #[token("endswith")]
    EndsWith,
    #[token("entrypoint")]
    Entrypoint,
    #[token("false")]
    False,
    #[token("filesize")]
    Filesize,
    #[token("for")]
    For,
    #[token("fullword")]
    Fullword,
    #[token("global")]
    Global,
    #[token("icontains")]
    IContains,
    #[token("iendswith")]
    IEndsWith,
    #[token("iequals")]
    IEquals,
    #[token("import")]
    Import,
    #[token("in")]
    In,
    #[token("include")]
    Include,
    #[token("istartswith")]
    IStarsWith,
    #[token("matches")]
    Matches,
    #[token("meta")]
    Meta,
    #[token("nocase")]
    Nocase,
    #[token("none")]
    None,
    #[token("not")]
    Not,
    #[token("of")]
    Of,
    #[token("or")]
    Or,
    #[token("private")]
    Private,
    #[token("rule")]
    Rule,
    #[token("startswith")]
    StartsWith,
    #[token("strings")]
    Strings,
    #[token("them")]
    Them,
    #[token("true")]
    True,
    #[token("wide")]
    Wide,
    #[token("xor")]
    Xor,
    #[token("with")]
    With,

    // Bitwise
    #[token("<<")]
    Shl,
    #[token(">>")]
    Shr,

    // Comparison
    #[token("==")]
    Eq,
    #[token("!=")]
    Ne,
    #[token("<=")]
    Le,
    #[token(">=")]
    Ge,
    #[token("<")]
    Lt,
    #[token(">")]
    Gt,

    // Punctuation
    #[token("&")]
    Ampersand,
    #[token("*")]
    Asterisk,
    #[token("\\")]
    Backslash,
    #[token(":")]
    Colon,
    #[token(",")]
    Comma,
    #[token(".")]
    Dot,
    #[token("=")]
    Equal,
    #[token("+")]
    Plus,
    #[token("-")]
    Minus,
    #[token("%")]
    Percent,
    #[token("|")]
    Pipe,
    #[token("^")]
    Caret,
    #[token("~")]
    Tilde,

    #[token("{")]
    LBrace,
    #[token("}")]
    RBrace,
    #[token("(")]
    LParen,
    #[token(")")]
    RParen,
    #[token("[")]
    LBracket,
    #[token("]")]
    RBracket,

    // Pattern identifiers (i.e: $, $a, $b, $foo, $bar).
    #[regex(
        r#"(?x)                         # allow comments in the regexp
            \$                          # first character is $
            ([[:alpha:]]|\d|_)*         # any number of letters, digits, or _
        "#,
        |token| token.slice())
    ]
    PatternIdent(&'src [u8]),

    // Pattern count (i.e: #a, #b, #foo, #bar).
    #[regex(
        r#"(?x)                         # allow comments in the regexp
            \#                          # first character is #
            ([[:alpha:]]|\d|_)*         # any number of letters, digits, or _
        "#,
        |token| token.slice())
    ]
    PatternCount(&'src [u8]),

    // Pattern offset (i.e: @a, @b, @foo, @bar).
    #[regex(
        r#"(?x)                         # allow comments in the regexp
            @                           # first character is @
            ([[:alpha:]]|\d|_)*         # any number of letters, digits, or _
        "#,
        |token| token.slice())
    ]
    PatternOffset(&'src [u8]),

    // Pattern offset (i.e: @a, @b, @foo, @bar).
    #[regex(
        r#"(?x)                         # allow comments in the regexp
            !                           # first character is !
            ([[:alpha:]]|\d|_)*         # any number of letters, digits, or _
        "#,
        |token| token.slice())
    ]
    PatternLength(&'src [u8]),

    // Identifiers must start with underscore or letter, followed by any
    // number of underscores, letters, or digits.
    #[regex(
        r#"(?x)                         # allow comments in the regexp
            ([[:alpha:]]|_)             # first character is letter or _
            ([[:alpha:]]|\d|_)*         # any number of letters, digits, or _
        "#,
        |token| token.slice())
    ]
    Ident(&'src [u8]),

    // Float literals. Underscores are allowed to separate digits.
    #[regex(
        r#"(?x)                         # allow comments in the regexp
            ([0-9]+_*)+                 # one or more digits or underscores
            \.                          # a dot
            ([0-9_]+_*)+                # one more digits or underscores
        "#,
        |token| token.slice())
    ]
    FloatLit(&'src [u8]),

    // Integer literals. Underscores are allowed to separate digits.
    #[regex(
        r#"(?x)
           (
             0x([a-fA-F0-9]+_*)+ |       # hexadecimal number
             0o([0-7]+_*)+       |       # octal number
             ([0-9]+_*)+(KB|MB)?         # decimal number followed by optional underscore and optional KB or MB
           )
        "#,
        |token| token.slice())
    ]
    IntegerLit(&'src [u8]),

    // String literals start and ends with double quotes, in-between the quotes
    // they contain either an escape sequence, or anything that is not a quote
    // newline or backslash, including non UTF-8 characters.
    #[regex(
        r#"(?x)                         # allow comments in the regexp
        "                               # starts with double quotes
        (                               # any number of
          \\.                           #   escape sequence
          |                             #   or ..
          [^"\n\\]                      #   anything except quotes, newlines and backslashes
        )*
        "                               # ends with double quotes
        "#)
    ]
    StringLit,

    // Multi-line string literals start and ends with 3 double quotes, in-between the
    // quotes they contain either an escape sequence, or anything that is not a
    // quote or backslash, including non UTF-8 characters.
    #[regex(
        r#"(?x)                         # allow comments in the regexp
        """                             # starts with 3 double quotes
        (                               # any number of
          \\.                           #   escape sequence
          |                             #   or ..
          [^"\\]                        #   anything except quotes, newlines and backslashes
        )*
        """                             # ends with 3 double quotes
        "#)
    ]
    MultiLineStringLit,

    // Regular expression.
    #[regex(
        r#"(?x)                         # allow comments in the regexp
        /                               # starts with /
        (\\.|[^*/\\\n])                 # followed by escape sequence or anything that is
                                        # not *, /, \, or newline. This prevents collision
                                        # with comments.
        (                               # zero or more..
          \\.                           #   escape sequence
          |                             #   or ..
          [^\\/\n]                      #   anything except \, / and newlines
        )*
        /                               # ends with /
        [[:alpha:]]{0,2}                # up to 2 optional modifiers like "s" and "i"
        "#)
    ]
    Regexp,

    // Block comment.
    #[regex(
        r#"(?x)                        # allow comments in the regexp
        /\*                            # starts with /*
        [^*]*                          # zero or more characters except *
        \*+                            # one or more *
        (                              # zero or more..
            [^/*]                      #   anything except / and *
            [^*]*                      #   zero or more characters except *
            \*+                        #   one or more *
        )*
        /                              # ends with /
        "#
    )]
    BlockComment,

    // Single-line comment
    #[regex(r#"//[^\n]*"#)]
    Comment,

    // Space, tab, and many other Unicode characters that are considered spaces.
    // https://www.compart.com/en/unicode/U+00A0
    // https://www.compart.com/en/unicode/bidiclass/WS
    #[regex("[ \t\u{a0}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{202f}\u{205f}]+")]
    Whitespace,

    #[token("\n")]
    LF,

    #[token("\r")]
    CR,

    #[token("\r\n")]
    CRLF,
}

/// Tokens recognized in hex pattern mode.
#[allow(clippy::upper_case_acronyms)]
#[derive(logos::Logos, Debug, PartialEq)]
#[logos(source = [u8])]
enum HexPatternToken {
    // A hex byte is an optional tilde ~, followed by two hex digits or
    // question marks. The following are valid tokens:
    //
    // 10, A0, ef, 3?, ?3, ??, ~AB, ~A?, ~??
    //
    // Some tokens like ~?? are not actually valid, but the tokenizer accepts
    // them, and they are rejected later on during the compilation process.
    // This way we can provide meaningful error messages.
    #[regex("~?[?0-9a-fA-F]{2}")]
    Byte,

    #[token("|")]
    Pipe,

    #[token("(")]
    LParen,

    #[token(")")]
    RParen,

    #[token("[")]
    LBracket,

    #[token("]")]
    RBracket,

    // Space, tab, and many other Unicode characters that are considered spaces.
    // https://www.compart.com/en/unicode/U+00A0
    // https://www.compart.com/en/unicode/bidiclass/WS
    #[regex("[ \t\u{a0}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{202f}\u{205f}]+")]
    Whitespace,

    #[token("\n")]
    LF,

    #[token("\r")]
    CR,

    #[token("\r\n")]
    CRLF,

    // Block comment.
    #[regex(
        r#"(?x)                        # allow comments in the regexp
        /\*                            # starts with /*
        [^*]*                          # zero or more characters except *
        \*+                            # one or more *
        (                              # zero or more..
            [^/*]                      #   anything except / and *
            [^*]*                      #   zero or more characters except *
            \*+                        #   one or more *
        )*
        /                              # ends with /
        "#
    )]
    BlockComment,

    // Single-line comment
    #[regex(r#"//[^\n]*"#)]
    Comment,
}

/// Tokens recognized in hex jump mode.
#[allow(clippy::upper_case_acronyms)]
#[derive(logos::Logos, Debug, PartialEq)]
#[logos(source = [u8])]
enum HexJumpToken<'src> {
    #[token("-")]
    Hyphen,

    // Integer literals.
    #[regex(
        r#"(?x)
           (
             0x[a-fA-F0-9]+ |           # hexadecimal number
             0o[0-7]+       |           # octal number
             [0-9]+                     # decimal number
           )
        "#,
        |token| token.slice())
    ]
    IntegerLit(&'src [u8]),

    // Space, tab, and many other Unicode characters that are considered spaces.
    // https://www.compart.com/en/unicode/U+00A0
    // https://www.compart.com/en/unicode/bidiclass/WS
    #[regex("[ \t\u{a0}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{202f}\u{205f}]+")]
    Whitespace,

    #[token("\n")]
    LF,

    #[token("\r")]
    CR,

    #[token("\r\n")]
    CRLF,
}

fn convert_normal_token(token: NormalToken, span: Span) -> Token {
    match token {
        // Keywords.
        NormalToken::All => Token::ALL_KW(span),
        NormalToken::And => Token::AND_KW(span),
        NormalToken::Any => Token::ANY_KW(span),
        NormalToken::Ascii => Token::ASCII_KW(span),
        NormalToken::At => Token::AT_KW(span),
        NormalToken::Base64 => Token::BASE64_KW(span),
        NormalToken::Base64Wide => Token::BASE64WIDE_KW(span),
        NormalToken::Condition => Token::CONDITION_KW(span),
        NormalToken::Contains => Token::CONTAINS_KW(span),
        NormalToken::Defined => Token::DEFINED_KW(span),
        NormalToken::EndsWith => Token::ENDSWITH_KW(span),
        NormalToken::Entrypoint => Token::ENTRYPOINT_KW(span),
        NormalToken::False => Token::FALSE_KW(span),
        NormalToken::Filesize => Token::FILESIZE_KW(span),
        NormalToken::For => Token::FOR_KW(span),
        NormalToken::Fullword => Token::FULLWORD_KW(span),
        NormalToken::Global => Token::GLOBAL_KW(span),
        NormalToken::IContains => Token::ICONTAINS_KW(span),
        NormalToken::IEndsWith => Token::IENDSWITH_KW(span),
        NormalToken::IEquals => Token::IEQUALS_KW(span),
        NormalToken::Import => Token::IMPORT_KW(span),
        NormalToken::In => Token::IN_KW(span),
        NormalToken::Include => Token::INCLUDE_KW(span),
        NormalToken::IStarsWith => Token::ISTARTSWITH_KW(span),
        NormalToken::Matches => Token::MATCHES_KW(span),
        NormalToken::Meta => Token::META_KW(span),
        NormalToken::Nocase => Token::NOCASE_KW(span),
        NormalToken::None => Token::NONE_KW(span),
        NormalToken::Not => Token::NOT_KW(span),
        NormalToken::Of => Token::OF_KW(span),
        NormalToken::Or => Token::OR_KW(span),
        NormalToken::Private => Token::PRIVATE_KW(span),
        NormalToken::Rule => Token::RULE_KW(span),
        NormalToken::StartsWith => Token::STARTSWITH_KW(span),
        NormalToken::Strings => Token::STRINGS_KW(span),
        NormalToken::Them => Token::THEM_KW(span),
        NormalToken::True => Token::TRUE_KW(span),
        NormalToken::Wide => Token::WIDE_KW(span),
        NormalToken::Xor => Token::XOR_KW(span),
        NormalToken::With => Token::WITH_KW(span),

        // Bitwise.
        NormalToken::Shl => Token::SHL(span),
        NormalToken::Shr => Token::SHR(span),

        // Comparison.
        NormalToken::Eq => Token::EQ(span),
        NormalToken::Ne => Token::NE(span),
        NormalToken::Lt => Token::LT(span),
        NormalToken::Gt => Token::GT(span),
        NormalToken::Le => Token::LE(span),
        NormalToken::Ge => Token::GE(span),

        // Punctuation.
        NormalToken::Ampersand => Token::AMPERSAND(span),
        NormalToken::Asterisk => Token::ASTERISK(span),
        NormalToken::Backslash => Token::BACKSLASH(span),
        NormalToken::Caret => Token::CARET(span),
        NormalToken::Comma => Token::COMMA(span),
        NormalToken::Colon => Token::COLON(span),
        NormalToken::Dot => Token::DOT(span),
        NormalToken::Equal => Token::EQUAL(span),
        NormalToken::Minus => Token::HYPHEN(span),
        NormalToken::Percent => Token::PERCENT(span),
        NormalToken::Pipe => Token::PIPE(span),
        NormalToken::Plus => Token::PLUS(span),
        NormalToken::Tilde => Token::TILDE(span),

        NormalToken::LBrace => Token::L_BRACE(span),
        NormalToken::RBrace => Token::R_BRACE(span),
        NormalToken::LParen => Token::L_PAREN(span),
        NormalToken::RParen => Token::R_PAREN(span),
        NormalToken::LBracket => Token::L_BRACKET(span),
        NormalToken::RBracket => Token::R_BRACKET(span),

        NormalToken::StringLit | NormalToken::MultiLineStringLit => {
            Token::STRING_LIT(span)
        }

        NormalToken::Regexp => Token::REGEXP(span),

        NormalToken::BlockComment | NormalToken::Comment => {
            Token::COMMENT(span)
        }

        NormalToken::Whitespace => Token::WHITESPACE(span),

        NormalToken::LF | NormalToken::CR | NormalToken::CRLF => {
            Token::NEWLINE(span)
        }

        NormalToken::Ident(ident) => match from_utf8(ident) {
            Ok(_) => Token::IDENT(span),
            Err(_) => unreachable!(),
        },
        NormalToken::PatternIdent(ident) => match from_utf8(ident) {
            Ok(_) => Token::PATTERN_IDENT(span),
            Err(_) => unreachable!(),
        },
        NormalToken::PatternCount(ident) => match from_utf8(ident) {
            Ok(_) => Token::PATTERN_COUNT(span),
            Err(_) => unreachable!(),
        },
        NormalToken::PatternOffset(ident) => match from_utf8(ident) {
            Ok(_) => Token::PATTERN_OFFSET(span),
            Err(_) => unreachable!(),
        },
        NormalToken::PatternLength(ident) => match from_utf8(ident) {
            Ok(_) => Token::PATTERN_LENGTH(span),
            Err(_) => unreachable!(),
        },
        NormalToken::FloatLit(lit) => match from_utf8(lit) {
            Ok(_) => Token::FLOAT_LIT(span),
            Err(_) => unreachable!(),
        },
        NormalToken::IntegerLit(lit) => match from_utf8(lit) {
            Ok(_) => Token::INTEGER_LIT(span),
            Err(_) => unreachable!(),
        },
    }
}

fn convert_hex_pattern_token(token: HexPatternToken, span: Span) -> Token {
    match token {
        HexPatternToken::Byte => Token::HEX_BYTE(span),
        HexPatternToken::Pipe => Token::PIPE(span),
        HexPatternToken::LParen => Token::L_PAREN(span),
        HexPatternToken::RParen => Token::R_PAREN(span),
        HexPatternToken::LBracket => Token::L_BRACKET(span),
        HexPatternToken::RBracket => Token::R_BRACKET(span),
        HexPatternToken::Whitespace => Token::WHITESPACE(span),
        HexPatternToken::LF | HexPatternToken::CR | HexPatternToken::CRLF => {
            Token::NEWLINE(span)
        }
        HexPatternToken::BlockComment | HexPatternToken::Comment => {
            Token::COMMENT(span)
        }
    }
}

fn convert_hex_jump_token(token: HexJumpToken, span: Span) -> Token {
    match token {
        HexJumpToken::Hyphen => Token::HYPHEN(span),
        HexJumpToken::Whitespace => Token::WHITESPACE(span),
        HexJumpToken::LF | HexJumpToken::CR | HexJumpToken::CRLF => {
            Token::NEWLINE(span)
        }
        HexJumpToken::IntegerLit(lit) => match from_utf8(lit) {
            Ok(_) => Token::INTEGER_LIT(span),
            Err(_) => unreachable!(),
        },
    }
}