vimlrs 0.2.1

Faithful Rust port of the Vimscript (VimL) interpreter, from the Neovim C eval engine
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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
//! ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
//! EXTENSION — NO `vendor/` COUNTERPART. (PORT.md synthesis-layer carve-out, the
//! vimlrs analogue of zshrs's crate-root `fusevm_bridge.rs`/`compile_zsh.rs`.)
//!
//! Neovim's `eval.c` has no separate lexer — `eval1`…`eval7` scan characters
//! off the source string inline while evaluating. The bytecode frontend needs a
//! real token stream, so this is net-new code, NOT a port. It is bound by the
//! "no fake C names" rule only in the negative sense: nothing here may claim to
//! be a port or carry a `// c:` citation it doesn't have. The token set is still
//! dictated by what `eval.c` recognizes (operator spellings, literal forms,
//! sigil-prefixed names).
//! ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

/// A Vimscript evaluation error carrying a Vim-style message (`E…:` code +
/// text), raised by the synthesis lexer/parser/compiler before execution. (At
/// run time the ported eval engine signals via `emsg`/`did_emsg`.)
#[derive(Debug, Clone, thiserror::Error)]
#[error("{0}")]
pub struct VimlError(pub String);

impl VimlError {
    /// Construct from any message string.
    pub fn msg(m: impl Into<String>) -> Self {
        VimlError(m.into())
    }
}

/// A lexical token plus its byte offset in the source (for diagnostics).
#[derive(Debug, Clone, PartialEq)]
pub struct Token {
    /// The token kind/value.
    pub kind: Tok,
    /// Byte offset of the token start in the source line.
    pub span: usize,
    /// Byte offset just past the token end (for adjacency checks like `d.key`
    /// member access vs `a . b` concatenation).
    pub end: usize,
}

/// Token kinds recognized in a Vimscript expression.
#[derive(Debug, Clone, PartialEq)]
pub enum Tok {
    /// Integer literal, already parsed.
    Number(i64),
    /// Float literal.
    Float(f64),
    /// Blob literal `0z00112233` — the decoded bytes.
    Blob(Vec<u8>),
    /// String literal, already unescaped.
    Str(String),
    /// Interpolated string `$'…{expr}…'` / `$"…{expr}…"` — the ordered list of
    /// literal chunks and raw `{expr}` sources, lowered by the parser to a
    /// concat of the chunks with each expression echo-stringified.
    InterpStr(Vec<InterpPart>),
    /// Bare identifier or scoped name (`x`, `g:foo`, `v:true`).
    Ident(String),
    /// Option reference `&name`.
    Option(String),
    /// Environment variable `$NAME`.
    Env(String),
    /// Register `@x`.
    Register(char),

    /// `?`
    Question,
    /// `??`
    QuestionQuestion,
    /// `:`
    Colon,
    /// `||`
    OrOr,
    /// `&&`
    AndAnd,
    /// A comparison operator, with its case-sensitivity flag.
    Cmp(CmpOp, CaseFlag),
    /// `+`
    Plus,
    /// `-`
    Minus,
    /// `.`
    Dot,
    /// `..`
    DotDot,
    /// `*`
    Star,
    /// `/`
    Slash,
    /// `%`
    Percent,
    /// `!`
    Bang,
    /// `->`
    Arrow,
    /// `=>` (vim9 lambda arrow)
    FatArrow,
    /// `(`
    LParen,
    /// `)`
    RParen,
    /// `[`
    LBracket,
    /// `]`
    RBracket,
    /// `{`
    LBrace,
    /// `#{` — opens a literal-key Dict (`#{a: 1}`, bare-word keys).
    HashBrace,
    /// `}`
    RBrace,
    /// `,`
    Comma,
    /// `=`
    Assign,
    /// End of input.
    Eof,
}

/// One piece of an interpolated string literal (`$'…'` / `$"…"`): either a
/// resolved literal chunk (escapes, `''`, `{{`/`}}` already applied) or the raw
/// source text of a `{expr}` region to be sub-parsed and echo-stringified.
#[derive(Debug, Clone, PartialEq)]
pub enum InterpPart {
    /// Literal text with all escapes already resolved.
    Lit(String),
    /// Raw expression source that appeared between `{` and `}`.
    Expr(String),
}

/// The relational families recognized in `eval4` (`eval.c`). These map onto the
/// ported `exprtype_T` in the bridge.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CmpOp {
    /// `==`
    Equal,
    /// `!=`
    NotEqual,
    /// `=~`
    Match,
    /// `!~`
    NoMatch,
    /// `>`
    Greater,
    /// `>=`
    GreaterEqual,
    /// `<`
    Less,
    /// `<=`
    LessEqual,
    /// `is`
    Is,
    /// `isnot`
    IsNot,
}

/// Case-sensitivity suffix on a comparison (`==#` match-case, `==?` ignore-case,
/// bare `==` follows `'ignorecase'`). Mirrors the `ic` derivation in `eval4`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CaseFlag {
    /// No suffix — follows `'ignorecase'` (default match-case here).
    Default,
    /// `#` — always match case.
    MatchCase,
    /// `?` — always ignore case.
    IgnoreCase,
}

/// Lex a Vimscript expression string into a token stream (ending in [`Tok::Eof`]).
pub fn lex(src: &str) -> Result<Vec<Token>, VimlError> {
    Lexer::new(src).run()
}

struct Lexer<'a> {
    src: &'a [u8],
    s: &'a str,
    pos: usize,
}

impl<'a> Lexer<'a> {
    fn new(s: &'a str) -> Self {
        Lexer {
            src: s.as_bytes(),
            s,
            pos: 0,
        }
    }

    fn peek(&self) -> u8 {
        self.src.get(self.pos).copied().unwrap_or(0)
    }

    fn peek2(&self) -> u8 {
        self.src.get(self.pos + 1).copied().unwrap_or(0)
    }

    fn run(mut self) -> Result<Vec<Token>, VimlError> {
        let mut out = Vec::new();
        loop {
            self.skip_ws();
            let span = self.pos;
            if self.pos >= self.src.len() {
                out.push(Token {
                    kind: Tok::Eof,
                    span,
                    end: span,
                });
                return Ok(out);
            }
            let kind = self.next_token()?;
            out.push(Token {
                kind,
                span,
                end: self.pos,
            });
        }
    }

    fn skip_ws(&mut self) {
        while matches!(self.peek(), b' ' | b'\t' | b'\r' | b'\n') {
            self.pos += 1;
        }
    }

    fn next_token(&mut self) -> Result<Tok, VimlError> {
        let c = self.peek();
        match c {
            b'0'..=b'9' => Ok(self.lex_number()),
            b'\'' => self.lex_single_string(),
            b'"' => self.lex_double_string(),
            b'a'..=b'z' | b'A'..=b'Z' | b'_' => Ok(self.lex_ident()),
            // `<SID>name` / `<SNR>123_name` are script-local function names, not a
            // `<` comparison: Vim accepts them wherever a function name is expected
            // (`:call <SID>Foo()`, `<SID>Foo(...)`, `function('<SID>Foo')`). The
            // `<SID>`/`<SNR>` marker is case-insensitive (userfunc.c STRNICMP), so
            // scan the whole marker plus the following name as one identifier and
            // let the parser treat it as a `Tok::Ident` (call or funcref name).
            b'<' if self.at_scriptid_name() => Ok(self.lex_scriptid_name()),
            // `#{` opens a literal-key Dict.
            b'#' if self.peek2() == b'{' => {
                self.pos += 2;
                Ok(Tok::HashBrace)
            }
            // `&&` is the logical-AND operator; only a lone `&` is an option sigil.
            b'&' if self.peek2() == b'&' => self.lex_operator(),
            b'&' => Ok(self.lex_sigil_name(Tok::Option as fn(String) -> Tok)),
            // `$'…'` / `$"…"` is an interpolated string (vim9 and legacy); a `$`
            // followed by a name is an environment-variable reference.
            b'$' if matches!(self.peek2(), b'\'' | b'"') => self.lex_interp_string(),
            b'$' => Ok(self.lex_sigil_name(Tok::Env as fn(String) -> Tok)),
            b'@' => {
                self.pos += 1;
                let r = self.peek() as char;
                if self.peek() != 0 {
                    self.pos += 1;
                }
                Ok(Tok::Register(r))
            }
            _ => self.lex_operator(),
        }
    }

    fn lex_number(&mut self) -> Tok {
        let start = self.pos;
        if self.peek() == b'0' {
            match self.peek2() {
                b'x' | b'X' => return self.lex_radix(16),
                b'b' | b'B' => return self.lex_radix(2),
                b'o' | b'O' => return self.lex_radix(8),
                b'z' | b'Z' => return self.lex_blob(),
                _ => {}
            }
        }
        while self.peek().is_ascii_digit() {
            self.pos += 1;
        }
        let mut is_float = false;
        if self.peek() == b'.' && self.peek2().is_ascii_digit() {
            is_float = true;
            self.pos += 1;
            while self.peek().is_ascii_digit() {
                self.pos += 1;
            }
        }
        // An exponent is only part of the literal after a `.{digits}` fraction:
        // Vim/Neovim's float grammar is `[0-9]+\.[0-9]+([eE][+-]?[0-9]+)?`, so a
        // dotless `1e100` is the Number `1` followed by the name `e100` (an error
        // at parse time), never a float.
        if is_float && matches!(self.peek(), b'e' | b'E') {
            let save = self.pos;
            self.pos += 1;
            if matches!(self.peek(), b'+' | b'-') {
                self.pos += 1;
            }
            if self.peek().is_ascii_digit() {
                while self.peek().is_ascii_digit() {
                    self.pos += 1;
                }
            } else {
                self.pos = save;
            }
        }
        let text = &self.s[start..self.pos];
        if is_float {
            return Tok::Float(text.parse::<f64>().unwrap_or(0.0));
        }
        // Vim octal literal: a leading `0` followed only by octal digits (`010`
        // == 8). A `8`/`9` anywhere (`08`, `0129`) keeps it decimal, matching
        // vim_str2nr's STR2NR_OCT detection in eval_number() (Src/eval.c).
        if text.len() > 1
            && text.starts_with('0')
            && text.bytes().all(|b| (b'0'..=b'7').contains(&b))
        {
            return Tok::Number(i64::from_str_radix(text, 8).unwrap_or(0));
        }
        Tok::Number(text.parse::<i64>().unwrap_or(0))
    }

    /// Lex a Blob literal `0z` followed by an even number of hex digits (Vim
    /// also allows a `.` separating byte groups, e.g. `0z00.11`). Port of the
    /// `0z` branch of `eval_number()` (`Src/eval.c`).
    fn lex_blob(&mut self) -> Tok {
        self.pos += 2; // skip "0z"
        let mut bytes = Vec::new();
        loop {
            let hi = self.peek();
            if hi == b'.' {
                self.pos += 1;
                continue;
            }
            if !(hi as char).is_ascii_hexdigit() {
                break;
            }
            let lo = self.peek2();
            if !(lo as char).is_ascii_hexdigit() {
                // odd trailing nibble — stop (Vim requires pairs)
                break;
            }
            let s = &self.s[self.pos..self.pos + 2];
            bytes.push(u8::from_str_radix(s, 16).unwrap_or(0));
            self.pos += 2;
        }
        Tok::Blob(bytes)
    }

    fn lex_radix(&mut self, radix: u32) -> Tok {
        let start = self.pos;
        self.pos += 2;
        let digits_start = self.pos;
        while (self.peek() as char).is_digit(radix) {
            self.pos += 1;
        }
        let digits = &self.s[digits_start..self.pos];
        match i64::from_str_radix(digits, radix) {
            Ok(n) => Tok::Number(n),
            Err(_) => {
                self.pos = start + 1;
                Tok::Number(0)
            }
        }
    }

    fn lex_single_string(&mut self) -> Result<Tok, VimlError> {
        self.pos += 1;
        let mut out = String::new();
        loop {
            match self.peek() {
                0 => return Err(VimlError::msg("E115: Missing quote")),
                b'\'' => {
                    if self.peek2() == b'\'' {
                        out.push('\'');
                        self.pos += 2;
                    } else {
                        self.pos += 1;
                        return Ok(Tok::Str(out));
                    }
                }
                _ => out.push(self.next_char()),
            }
        }
    }

    fn lex_double_string(&mut self) -> Result<Tok, VimlError> {
        self.pos += 1;
        let mut out = String::new();
        loop {
            match self.peek() {
                0 => return Err(VimlError::msg("E114: Missing quote")),
                b'"' => {
                    self.pos += 1;
                    return Ok(Tok::Str(out));
                }
                b'\\' => self.push_double_escape(&mut out)?,
                _ => out.push(self.next_char()),
            }
        }
    }

    /// Decode one backslash escape in a double-quoted string body (the leading
    /// `\` is at the current position) and push the resulting char(s) to `out`.
    /// Shared by [`Self::lex_double_string`] and the double-quote interpolated
    /// string body, so both honour the identical escape set.
    fn push_double_escape(&mut self, out: &mut String) -> Result<(), VimlError> {
        self.pos += 1; // consume the backslash
        let e = self.peek();
        self.pos += 1;
        match e {
            b'n' => out.push('\n'),
            b't' => out.push('\t'),
            b'r' => out.push('\r'),
            b'e' => out.push('\x1b'),
            b'b' => out.push('\x08'),
            b'\\' => out.push('\\'),
            b'"' => out.push('"'),
            b'0'..=b'7' => {
                let mut n = (e - b'0') as u32;
                for _ in 0..2 {
                    let d = self.peek();
                    if (b'0'..=b'7').contains(&d) {
                        n = n * 8 + (d - b'0') as u32;
                        self.pos += 1;
                    } else {
                        break;
                    }
                }
                if let Some(ch) = char::from_u32(n) {
                    out.push(ch);
                }
            }
            b'x' | b'X' => {
                let mut n = 0u32;
                for _ in 0..2 {
                    let d = self.peek();
                    if (d as char).is_ascii_hexdigit() {
                        n = n * 16 + (d as char).to_digit(16).unwrap();
                        self.pos += 1;
                    } else {
                        break;
                    }
                }
                if let Some(ch) = char::from_u32(n) {
                    out.push(ch);
                }
            }
            0 => return Err(VimlError::msg("E114: Missing quote")),
            other => out.push(other as char),
        }
        Ok(())
    }

    /// Lex an interpolated string `$'…'` (literal body, `''`→`'`, no escapes) or
    /// `$"…"` (double-quote body, backslash escapes apply). In both, `{expr}`
    /// marks an embedded expression, `{{`/`}}` (and, for `$"…"`, `\{`/`\}`) are
    /// literal braces, and a lone unmatched `}` is E1278. The `$` and opening
    /// quote are at the current position. Faithful to Vim 9.2's interpolated
    /// string (`:help interpolated-string`), which works in both vim9 and legacy.
    fn lex_interp_string(&mut self) -> Result<Tok, VimlError> {
        let double = self.peek2() == b'"';
        self.pos += 2; // skip `$` and the opening quote
        let quote = if double { b'"' } else { b'\'' };
        let mut parts: Vec<InterpPart> = Vec::new();
        let mut lit = String::new();
        loop {
            let c = self.peek();
            if c == 0 {
                return Err(VimlError::msg(if double {
                    "E114: Missing quote"
                } else {
                    "E115: Missing quote"
                }));
            }
            if double && c == b'\\' {
                // `\{`/`\}` fall through to the escape's default arm (literal
                // brace), so an escaped brace never opens an expression.
                self.push_double_escape(&mut lit)?;
                continue;
            }
            if c == quote {
                // A doubled `''` inside a `$'…'` body is a literal quote.
                if !double && self.peek2() == b'\'' {
                    lit.push('\'');
                    self.pos += 2;
                    continue;
                }
                self.pos += 1; // closing quote
                break;
            }
            match c {
                b'{' => {
                    if self.peek2() == b'{' {
                        lit.push('{');
                        self.pos += 2;
                    } else {
                        self.pos += 1; // consume `{`
                        if !lit.is_empty() {
                            parts.push(InterpPart::Lit(std::mem::take(&mut lit)));
                        }
                        parts.push(InterpPart::Expr(self.scan_interp_expr()?));
                    }
                }
                b'}' => {
                    if self.peek2() == b'}' {
                        lit.push('}');
                        self.pos += 2;
                    } else {
                        return Err(VimlError::msg("E1278: Stray '}' without a matching '{'"));
                    }
                }
                _ => lit.push(self.next_char()),
            }
        }
        if !lit.is_empty() {
            parts.push(InterpPart::Lit(lit));
        }
        Ok(Tok::InterpStr(parts))
    }

    /// Scan the source of one `{expr}` region: the opening `{` is already
    /// consumed; return the raw text up to (and consuming) the matching `}`.
    /// Nested `{…}` (dict literals) are depth-counted, and string literals
    /// `'…'`/`"…"` are skipped whole so a brace or quote inside them neither
    /// closes the expression nor the enclosing string. Unbalanced → E1279.
    fn scan_interp_expr(&mut self) -> Result<String, VimlError> {
        let start = self.pos;
        let mut depth = 1u32;
        loop {
            match self.peek() {
                0 => return Err(VimlError::msg("E1279: Missing '}'")),
                b'\'' => self.skip_sq_in_expr(),
                b'"' => self.skip_dq_in_expr(),
                b'{' => {
                    depth += 1;
                    self.pos += 1;
                }
                b'}' => {
                    depth -= 1;
                    if depth == 0 {
                        let expr = self.s[start..self.pos].to_string();
                        self.pos += 1; // consume the matching `}`
                        return Ok(expr);
                    }
                    self.pos += 1;
                }
                _ => {
                    self.next_char();
                }
            }
        }
    }

    /// Skip a single-quoted string body inside an interpolation expression (the
    /// opening `'` is at the current position); `''` is an embedded quote.
    fn skip_sq_in_expr(&mut self) {
        self.pos += 1; // opening `'`
        loop {
            match self.peek() {
                0 => return,
                b'\'' => {
                    if self.peek2() == b'\'' {
                        self.pos += 2;
                    } else {
                        self.pos += 1;
                        return;
                    }
                }
                _ => {
                    self.next_char();
                }
            }
        }
    }

    /// Skip a double-quoted string body inside an interpolation expression (the
    /// opening `"` is at the current position); a `\` escapes the next byte.
    fn skip_dq_in_expr(&mut self) {
        self.pos += 1; // opening `"`
        loop {
            match self.peek() {
                0 => return,
                b'\\' => {
                    self.pos += 1;
                    if self.peek() != 0 {
                        self.next_char();
                    }
                }
                b'"' => {
                    self.pos += 1;
                    return;
                }
                _ => {
                    self.next_char();
                }
            }
        }
    }

    /// Whether the bytes at the cursor begin a script-local function name marker
    /// (`<SID>` or `<SNR>`, case-insensitive). Called only when `peek()` is `<`.
    fn at_scriptid_name(&self) -> bool {
        self.src
            .get(self.pos..self.pos + 5)
            .is_some_and(|m| m.eq_ignore_ascii_case(b"<SID>") || m.eq_ignore_ascii_case(b"<SNR>"))
    }

    /// Scan a `<SID>`/`<SNR>` script-local function name into a single
    /// `Tok::Ident` carrying the literal marker plus the following name (for
    /// `<SNR>` this includes the `123_` script-id prefix, which is ordinary
    /// name-tail bytes). The marker is preserved verbatim; the registry resolves
    /// `<SID>Foo` against the `func <SID>Foo()` definition stored under the same
    /// literal name.
    fn lex_scriptid_name(&mut self) -> Tok {
        let start = self.pos;
        self.pos += 5; // past `<SID>` / `<SNR>`
        while matches!(self.peek(), b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_') {
            self.pos += 1;
        }
        Tok::Ident(self.s[start..self.pos].to_string())
    }

    fn lex_ident(&mut self) -> Tok {
        let start = self.pos;
        while matches!(self.peek(), b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_') {
            self.pos += 1;
        }
        // A leading single-letter scope prefix (`a:`/`b:`/`g:`/`l:`/`s:`/`t:`/
        // `v:`/`w:`) absorbs its `:` and the name after it. Only the real scope
        // letters do this — otherwise `z:1` (a no-space ternary `?z:1`) or a
        // literal-Dict key `#{z:1}` would wrongly merge.
        if self.peek() == b':'
            && (self.pos - start) == 1
            && matches!(
                self.src[start],
                b'a' | b'b' | b'g' | b'l' | b's' | b't' | b'v' | b'w'
            )
        {
            self.pos += 1;
            while matches!(self.peek(), b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_') {
                self.pos += 1;
            }
        }
        while self.peek() == b'#'
            && matches!(self.peek2(), b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_')
        {
            self.pos += 1;
            while matches!(self.peek(), b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_') {
                self.pos += 1;
            }
        }
        Tok::Ident(self.s[start..self.pos].to_string())
    }

    fn lex_sigil_name(&mut self, ctor: fn(String) -> Tok) -> Tok {
        self.pos += 1;
        let start = self.pos;
        if matches!(self.peek(), b'l' | b'g') && self.peek2() == b':' {
            self.pos += 2;
        }
        while matches!(self.peek(), b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_') {
            self.pos += 1;
        }
        ctor(self.s[start..self.pos].to_string())
    }

    fn lex_operator(&mut self) -> Result<Tok, VimlError> {
        let c = self.peek();
        let c2 = self.peek2();
        macro_rules! cmp {
            ($op:expr, $len:expr) => {{
                self.pos += $len;
                let flag = match self.peek() {
                    b'#' => {
                        self.pos += 1;
                        CaseFlag::MatchCase
                    }
                    b'?' => {
                        self.pos += 1;
                        CaseFlag::IgnoreCase
                    }
                    _ => CaseFlag::Default,
                };
                return Ok(Tok::Cmp($op, flag));
            }};
        }
        match (c, c2) {
            (b'?', b'?') => {
                self.pos += 2;
                Ok(Tok::QuestionQuestion)
            }
            (b'?', _) => {
                self.pos += 1;
                Ok(Tok::Question)
            }
            (b':', _) => {
                self.pos += 1;
                Ok(Tok::Colon)
            }
            (b'|', b'|') => {
                self.pos += 2;
                Ok(Tok::OrOr)
            }
            (b'&', b'&') => {
                self.pos += 2;
                Ok(Tok::AndAnd)
            }
            (b'=', b'=') => cmp!(CmpOp::Equal, 2),
            (b'=', b'~') => cmp!(CmpOp::Match, 2),
            // `=>` is the vim9 lambda arrow (`(a, b) => a + b`).
            (b'=', b'>') => {
                self.pos += 2;
                Ok(Tok::FatArrow)
            }
            (b'=', _) => {
                self.pos += 1;
                Ok(Tok::Assign)
            }
            (b'!', b'=') => cmp!(CmpOp::NotEqual, 2),
            (b'!', b'~') => cmp!(CmpOp::NoMatch, 2),
            (b'!', _) => {
                self.pos += 1;
                Ok(Tok::Bang)
            }
            (b'>', b'=') => cmp!(CmpOp::GreaterEqual, 2),
            (b'>', _) => cmp!(CmpOp::Greater, 1),
            (b'<', b'=') => cmp!(CmpOp::LessEqual, 2),
            (b'<', _) => cmp!(CmpOp::Less, 1),
            (b'-', b'>') => {
                self.pos += 2;
                Ok(Tok::Arrow)
            }
            (b'+', _) => {
                self.pos += 1;
                Ok(Tok::Plus)
            }
            (b'-', _) => {
                self.pos += 1;
                Ok(Tok::Minus)
            }
            (b'.', b'.') => {
                self.pos += 2;
                Ok(Tok::DotDot)
            }
            (b'.', _) => {
                self.pos += 1;
                Ok(Tok::Dot)
            }
            (b'*', _) => {
                self.pos += 1;
                Ok(Tok::Star)
            }
            (b'/', _) => {
                self.pos += 1;
                Ok(Tok::Slash)
            }
            (b'%', _) => {
                self.pos += 1;
                Ok(Tok::Percent)
            }
            (b'(', _) => {
                self.pos += 1;
                Ok(Tok::LParen)
            }
            (b')', _) => {
                self.pos += 1;
                Ok(Tok::RParen)
            }
            (b'[', _) => {
                self.pos += 1;
                Ok(Tok::LBracket)
            }
            (b']', _) => {
                self.pos += 1;
                Ok(Tok::RBracket)
            }
            (b'{', _) => {
                self.pos += 1;
                Ok(Tok::LBrace)
            }
            (b'}', _) => {
                self.pos += 1;
                Ok(Tok::RBrace)
            }
            (b',', _) => {
                self.pos += 1;
                Ok(Tok::Comma)
            }
            _ => Err(VimlError::msg(format!(
                "E15: Invalid expression: unexpected '{}'",
                c as char
            ))),
        }
    }

    fn next_char(&mut self) -> char {
        let ch = self.s[self.pos..].chars().next().unwrap_or('\0');
        self.pos += ch.len_utf8();
        ch
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn kinds(src: &str) -> Vec<Tok> {
        lex(src).unwrap().into_iter().map(|t| t.kind).collect()
    }

    #[test]
    fn blob_literals() {
        assert_eq!(
            kinds("0z00112233"),
            vec![Tok::Blob(vec![0, 17, 34, 51]), Tok::Eof]
        );
        assert_eq!(
            kinds("0zDEADBEEF"),
            vec![Tok::Blob(vec![0xde, 0xad, 0xbe, 0xef]), Tok::Eof]
        );
        assert_eq!(kinds("0z00.11"), vec![Tok::Blob(vec![0, 17]), Tok::Eof]);
        assert_eq!(kinds("0z"), vec![Tok::Blob(vec![]), Tok::Eof]);
    }

    #[test]
    // `3.14` here is a lexer fixture, not an attempt to express π.
    #[allow(clippy::approx_constant)]
    fn numbers_and_floats() {
        assert_eq!(kinds("0xff"), vec![Tok::Number(255), Tok::Eof]);
        assert_eq!(kinds("3.14"), vec![Tok::Float(3.14), Tok::Eof]);
        assert_eq!(
            kinds("1 . 2"),
            vec![Tok::Number(1), Tok::Dot, Tok::Number(2), Tok::Eof]
        );
    }

    #[test]
    fn octal_literals() {
        // Leading 0 + only octal digits → octal (Vim semantics).
        assert_eq!(kinds("010"), vec![Tok::Number(8), Tok::Eof]);
        assert_eq!(kinds("0777"), vec![Tok::Number(511), Tok::Eof]);
        assert_eq!(kinds("017"), vec![Tok::Number(15), Tok::Eof]);
        // A 8/9 digit makes it decimal; bare 0 stays 0; floats untouched.
        assert_eq!(kinds("08"), vec![Tok::Number(8), Tok::Eof]);
        assert_eq!(kinds("0129"), vec![Tok::Number(129), Tok::Eof]);
        assert_eq!(kinds("0"), vec![Tok::Number(0), Tok::Eof]);
        assert_eq!(kinds("0.5"), vec![Tok::Float(0.5), Tok::Eof]);
    }

    #[test]
    fn strings_and_ops() {
        assert_eq!(kinds("'a''b'"), vec![Tok::Str("a'b".into()), Tok::Eof]);
        assert_eq!(
            kinds("==#"),
            vec![Tok::Cmp(CmpOp::Equal, CaseFlag::MatchCase), Tok::Eof]
        );
    }
}