brrrty 0.1.12

Brrrty, a fast static site generator compatible with Eleventy (11ty) projects.
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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
//! A tokenizer for 11ty config files, enough to read them without running them.
//!
//! It splits JavaScript into tokens and statements and reads literal values
//! (strings, numbers, booleans, `null`, arrays and objects of those). Anything
//! else is left for a person to port, so it only has to know where code starts
//! and ends, not what it means.

/// What a token is.
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Tok {
    /// A name or keyword.
    Ident(String),
    /// A string, or a template literal with no `${}`, decoded. `None` when an
    /// escape can't be decoded exactly (a lone surrogate).
    Str(Option<String>),
    /// A template literal with substitutions.
    Template,
    /// A number, as written.
    Num(String),
    /// A regular expression literal.
    Regex,
    /// Punctuation.
    Punct(&'static str),
}

/// A token and where it is in the source.
#[derive(Debug, Clone)]
pub(crate) struct Token {
    pub tok: Tok,
    /// Byte offsets.
    pub start: usize,
    pub end: usize,
    /// A line break comes between this token and the one before.
    pub nl_before: bool,
    /// The bracket that closes or opens this one, for `(`, `[`, `{` and their pairs.
    pub pair: Option<usize>,
    /// A template literal (which on a new line continues the statement as a tag).
    pub tick: bool,
    /// For a template literal: the names its `${}` code uses, other than as a property.
    pub inner: Vec<String>,
}

/// How deep brackets and template literals may nest before Brrrty stops reading.
pub(crate) const MAX_DEPTH: usize = 64;

impl Token {
    pub(crate) fn is(&self, p: &str) -> bool {
        matches!(&self.tok, Tok::Punct(q) if *q == p)
    }

    pub(crate) fn ident(&self) -> Option<&str> {
        match &self.tok {
            Tok::Ident(s) => Some(s),
            _ => None,
        }
    }

    pub(crate) fn is_ident(&self, name: &str) -> bool {
        self.ident() == Some(name)
    }
}

const PUNCTS: [&str; 52] = [
    ">>>=", "...", "===", "!==", "**=", "<<=", ">>=", ">>>", "&&=", "||=", "??=", "=>", "==", "!=",
    "<=", ">=", "&&", "||", "??", "?.", "++", "--", "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=",
    "**", "<<", ">>", "{", "}", "(", ")", "[", "]", ";", ",", "<", ">", "+", "-", "*", "%", "&",
    "|", "^", "!", "~",
];
const MORE_PUNCTS: [&str; 6] = ["?", ":", "=", ".", "/", "@"];

/// Keywords after which a `/` starts a regular expression.
const REGEX_AFTER: [&str; 12] = [
    "return",
    "typeof",
    "instanceof",
    "in",
    "of",
    "new",
    "delete",
    "void",
    "throw",
    "case",
    "do",
    "else",
];

fn is_ident_start(b: u8) -> bool {
    b.is_ascii_alphabetic() || b == b'_' || b == b'$'
}

fn is_ident_part(b: u8) -> bool {
    is_ident_start(b) || b.is_ascii_digit()
}

/// Split `src` into tokens, or say why it can't be.
/// JavaScript's line terminators: LF, CR, U+2028 and U+2029.
pub(crate) fn is_line_terminator(c: char) -> bool {
    matches!(c, '\n' | '\r' | '\u{2028}' | '\u{2029}')
}

/// [`is_line_terminator`] for a byte. Exact once [`unusual_terminator`] has
/// passed: then the only terminators left are LF and CR (in CR LF), both ASCII.
fn is_line_break(b: u8) -> bool {
    is_line_terminator(char::from(b))
}

/// The first line terminator in `src` other than LF or CR LF: a lone CR,
/// U+2028 or U+2029. Real configs don't have these, and every place they
/// could hide code is a place for the reader to get wrong, so a file with
/// one is not read at all.
fn unusual_terminator(src: &str) -> Option<&'static str> {
    let b = src.as_bytes();
    for (i, c) in src.char_indices() {
        if !is_line_terminator(c) || c == '\n' {
            continue;
        }
        if c == '\r' && b.get(i + 1) == Some(&b'\n') {
            continue;
        }
        return Some(match c {
            '\r' => "a carriage return without a line feed",
            '\u{2028}' => "a U+2028 line separator",
            _ => "a U+2029 paragraph separator",
        });
    }
    None
}

pub(crate) fn tokenize(src: &str) -> Result<Vec<Token>, String> {
    if let Some(what) = unusual_terminator(src) {
        return Err(format!("it has {what}, which Brrrty doesn't read"));
    }
    let b = src.as_bytes();
    let mut i = 0;
    if src.starts_with("#!") {
        while i < b.len() && !is_line_break(b[i]) {
            i += 1;
        }
    }
    let (mut out, _) = lex(src, i, false, 0)?;
    pair_up(&mut out)?;
    Ok(out)
}

/// Tokens from `i` to the end, or, in a template literal's `${}` (`subst`), to
/// the `}` that closes it: then also the offset after that `}`.
fn lex(src: &str, mut i: usize, subst: bool, depth: usize) -> Result<(Vec<Token>, usize), String> {
    let b = src.as_bytes();
    let mut out: Vec<Token> = Vec::new();
    let mut nl = false;
    // For each open `(`: whether it holds the condition of `if`, `while`,
    // `for` or `with`, after which a `/` could start a regular expression.
    let mut parens: Vec<bool> = Vec::new();
    let mut closed_condition = false;
    // Braces open in this `${}`.
    let mut braces = 0usize;
    while i < b.len() {
        let c = b[i];
        if is_line_break(c) {
            nl = true;
            i += 1;
            continue;
        }
        if c.is_ascii_whitespace() {
            i += 1;
            continue;
        }
        // A byte order mark at the very start.
        if i == 0 && src.starts_with('\u{feff}') {
            i += 3;
            continue;
        }
        // JavaScript reads some non-ASCII characters as spaces or line breaks
        // and others as parts of names. Outside strings and comments, none is
        // read at all.
        if c >= 0x80 {
            return Err("there is a non-ASCII character outside a string or comment".into());
        }
        if src[i..].starts_with("//") {
            i = line_comment(src, i)?;
            continue;
        }
        if src[i..].starts_with("/*") {
            let Some(end) = src[i + 2..].find("*/") else {
                return Err("a /* comment is never closed".into());
            };
            if src[i..i + 2 + end].chars().any(is_line_terminator) {
                nl = true;
            }
            i += end + 4;
            continue;
        }
        if subst && c == b'}' && braces == 0 {
            return Ok((out, i + 1));
        }
        let start = i;
        let mut inner = Vec::new();
        let tok = if c == b'"' || c == b'\'' {
            let (end, value) = string(src, i)?;
            i = end;
            Tok::Str(value)
        } else if c == b'`' {
            let (end, value) = template(src, i, depth, &mut inner)?;
            i = end;
            match value {
                Some(v) => Tok::Str(v),
                None => Tok::Template,
            }
        } else if c.is_ascii_digit() || (c == b'.' && b.get(i + 1).is_some_and(u8::is_ascii_digit))
        {
            while i < b.len() && (b[i].is_ascii_alphanumeric() || b[i] == b'.' || b[i] == b'_') {
                // An exponent's sign.
                if (b[i] == b'e' || b[i] == b'E')
                    && !src[start..].starts_with("0x")
                    && matches!(b.get(i + 1), Some(b'+' | b'-'))
                {
                    i += 1;
                }
                i += 1;
            }
            Tok::Num(src[start..i].to_string())
        } else if is_ident_start(c) || c == b'#' || c == b'\\' {
            i += 1;
            while i < b.len() && (is_ident_part(b[i]) || b[i] == b'\\') {
                i += 1;
            }
            // `\u0065` in a name means `e`: never compare such names as text.
            if src[start..i].contains('\\') {
                return Err("a name is written with a `\\` escape".into());
            }
            Tok::Ident(src[start..i].to_string())
        } else if c == b'/' && slash(out.last(), closed_condition)? {
            i = regex(b, i)?;
            Tok::Regex
        } else {
            let p = PUNCTS
                .iter()
                .chain(MORE_PUNCTS.iter())
                .find(|p| src[i..].starts_with(**p));
            match p {
                Some(p) => {
                    i += p.len();
                    Tok::Punct(p)
                }
                None => {
                    let ch = src[i..].chars().next().unwrap_or('?');
                    return Err(format!("unexpected `{ch}`"));
                }
            }
        };
        closed_condition = false;
        match tok {
            Tok::Punct("{") => braces += 1,
            Tok::Punct("}") => braces = braces.saturating_sub(1),
            Tok::Punct("(") => parens.push(
                out.last()
                    .and_then(Token::ident)
                    .is_some_and(|w| matches!(w, "if" | "while" | "for" | "with")),
            ),
            Tok::Punct(")") => closed_condition = parens.pop().unwrap_or(false),
            _ => {}
        }
        out.push(Token {
            tok,
            start,
            end: i,
            nl_before: nl,
            pair: None,
            tick: c == b'`',
            inner,
        });
        nl = false;
    }
    if subst {
        return Err("a template literal is never closed".into());
    }
    Ok((out, i))
}

/// Fill in `pair` for every bracket, in one pass.
fn pair_up(toks: &mut [Token]) -> Result<(), String> {
    let mut stack: Vec<usize> = Vec::new();
    for k in 0..toks.len() {
        match toks[k].tok {
            Tok::Punct("(" | "[" | "{") => {
                if stack.len() >= MAX_DEPTH {
                    return Err(format!("brackets nest more than {MAX_DEPTH} deep"));
                }
                stack.push(k);
            }
            Tok::Punct(close @ (")" | "]" | "}")) => {
                let Some(o) = stack.pop() else {
                    return Err(format!("a `{close}` closes nothing"));
                };
                let want = match toks[o].tok {
                    Tok::Punct("(") => ")",
                    Tok::Punct("[") => "]",
                    _ => "}",
                };
                if want != close {
                    return Err(format!(
                        "a `{close}` closes a bracket opened with something else"
                    ));
                }
                toks[o].pair = Some(k);
                toks[k].pair = Some(o);
            }
            _ => {}
        }
    }
    if !stack.is_empty() {
        return Err("a bracket is never closed".into());
    }
    Ok(())
}

/// Whether a `/` after `prev` starts a regular expression (`true`) or is
/// division (`false`). Where that can't be known without parsing the
/// statement around it, the file is unreadable rather than guessed at.
fn slash(prev: Option<&Token>, after_condition: bool) -> Result<bool, String> {
    let unsure = || {
        Err(
            "Brrrty can't tell whether a `/` here starts a regular expression or divides"
                .to_string(),
        )
    };
    match prev.map(|t| &t.tok) {
        None => Ok(true),
        Some(Tok::Ident(k)) => match k.as_str() {
            "of" | "yield" | "await" | "let" | "async" | "get" | "set" | "static" => unsure(),
            k if REGEX_AFTER.contains(&k) => Ok(true),
            _ => Ok(false),
        },
        Some(Tok::Punct(")")) if after_condition => unsure(),
        Some(Tok::Punct(")" | "]")) => Ok(false),
        Some(Tok::Punct("}" | "++" | "--")) => unsure(),
        Some(Tok::Punct(_)) => Ok(true),
        Some(_) => Ok(false),
    }
}

/// Skip a `//` comment at `i`; returns where its line break is.
fn line_comment(src: &str, mut i: usize) -> Result<usize, String> {
    let b = src.as_bytes();
    while i < b.len() && !is_line_break(b[i]) {
        i += 1;
    }
    Ok(i)
}

fn regex(b: &[u8], mut i: usize) -> Result<usize, String> {
    i += 1;
    let mut class = false;
    loop {
        match b.get(i) {
            None => return Err("a regular expression is never closed".into()),
            Some(&c) if is_line_break(c) => {
                return Err("a regular expression is never closed".into());
            }
            // Any other character is part of the pattern: the line
            // terminators that could end one early were refused up front.
            Some(b'\\') => i += 2,
            Some(b'[') => {
                class = true;
                i += 1;
            }
            Some(b']') => {
                class = false;
                i += 1;
            }
            Some(b'/') if !class => {
                i += 1;
                while i < b.len() && is_ident_part(b[i]) {
                    i += 1;
                }
                return Ok(i);
            }
            Some(_) => i += 1,
        }
    }
}

/// A quoted string from `i`: where it ends, and its value.
fn string(src: &str, i: usize) -> Result<(usize, Option<String>), String> {
    let b = src.as_bytes();
    let quote = b[i];
    let mut j = i + 1;
    let mut units: Vec<u16> = Vec::new();
    let mut exact = true;
    loop {
        match b.get(j) {
            None => return Err("a string is never closed".into()),
            Some(&c) if is_line_break(c) => return Err("a string is never closed".into()),
            Some(&q) if q == quote => break,
            Some(b'\\') => {
                let (next, ok) = escape(src, j, &mut units);
                exact &= ok;
                j = next;
            }
            Some(_) => {
                let ch = src[j..].chars().next().unwrap_or('\u{fffd}');
                let mut buf = [0u16; 2];
                units.extend_from_slice(ch.encode_utf16(&mut buf));
                j += ch.len_utf8();
            }
        }
    }
    let value = if exact {
        String::from_utf16(&units).ok()
    } else {
        None
    };
    Ok((j + 1, value))
}

/// The escape at `j` (a backslash): where it ends, pushing its UTF-16 units.
/// `false` when it isn't one that can be read exactly.
fn escape(src: &str, j: usize, units: &mut Vec<u16>) -> (usize, bool) {
    let b = src.as_bytes();
    let Some(&c) = b.get(j + 1) else {
        return (j + 1, false);
    };
    let simple = match c {
        b'n' => Some(0x0a),
        b'r' => Some(0x0d),
        b't' => Some(0x09),
        b'b' => Some(0x08),
        b'f' => Some(0x0c),
        b'v' => Some(0x0b),
        b'0' if !b.get(j + 2).is_some_and(u8::is_ascii_digit) => Some(0),
        _ => None,
    };
    if let Some(u) = simple {
        units.push(u);
        return (j + 2, true);
    }
    let hex = |s: &str| u32::from_str_radix(s, 16).ok();
    match c {
        b'\n' => (j + 2, true),
        b'\r' => (
            if b.get(j + 2) == Some(&b'\n') {
                j + 3
            } else {
                j + 2
            },
            true,
        ),
        b'x' => match src.get(j + 2..j + 4).and_then(hex) {
            Some(v) => {
                units.push(v as u16);
                (j + 4, true)
            }
            None => (j + 2, false),
        },
        b'u' if b.get(j + 2) == Some(&b'{') => {
            let Some(close) = src[j + 3..].find('}') else {
                return (j + 2, false);
            };
            match hex(&src[j + 3..j + 3 + close]).and_then(char::from_u32) {
                Some(ch) => {
                    let mut buf = [0u16; 2];
                    units.extend_from_slice(ch.encode_utf16(&mut buf));
                    (j + 4 + close, true)
                }
                None => (j + 4 + close, false),
            }
        }
        b'u' => match src.get(j + 2..j + 6).and_then(hex) {
            Some(v) => {
                units.push(v as u16);
                (j + 6, true)
            }
            None => (j + 2, false),
        },
        b'1'..=b'9' | b'0' => (j + 2, false),
        _ => {
            let ch = src[j + 1..].chars().next().unwrap_or('\u{fffd}');
            let mut buf = [0u16; 2];
            units.extend_from_slice(ch.encode_utf16(&mut buf));
            (j + 1 + ch.len_utf8(), true)
        }
    }
}

/// A template literal from `i`: where it ends, and its value when it has no
/// substitutions.
fn template(
    src: &str,
    i: usize,
    depth: usize,
    inner: &mut Vec<String>,
) -> Result<(usize, Option<Option<String>>), String> {
    if depth > MAX_DEPTH {
        return Err(format!("template literals nest more than {MAX_DEPTH} deep"));
    }
    let b = src.as_bytes();
    let mut j = i + 1;
    let mut units: Vec<u16> = Vec::new();
    let mut exact = true;
    let mut plain = true;
    loop {
        match b.get(j) {
            None => return Err("a template literal is never closed".into()),
            Some(b'`') => break,
            Some(b'\\') => {
                let (next, ok) = escape(src, j, &mut units);
                exact &= ok;
                j = next;
            }
            Some(b'$') if b.get(j + 1) == Some(&b'{') => {
                plain = false;
                let (mut toks, end) = lex(src, j + 2, true, depth + 1)?;
                pair_up(&mut toks)?;
                for (k, t) in toks.iter().enumerate() {
                    if let Some(name) = t.ident()
                        && !(k > 0 && (toks[k - 1].is(".") || toks[k - 1].is("?.")))
                    {
                        inner.push(name.to_string());
                    }
                    inner.extend(t.inner.iter().cloned());
                }
                j = end;
            }
            Some(b'\r') => {
                // A template's CR LF and CR read as LF.
                units.push(0x0a);
                j += if b.get(j + 1) == Some(&b'\n') { 2 } else { 1 };
            }
            Some(_) => {
                let ch = src[j..].chars().next().unwrap_or('\u{fffd}');
                let mut buf = [0u16; 2];
                units.extend_from_slice(ch.encode_utf16(&mut buf));
                j += ch.len_utf8();
            }
        }
    }
    let value = if !plain {
        None
    } else if exact {
        Some(String::from_utf16(&units).ok())
    } else {
        Some(None)
    };
    Ok((j + 1, value))
}

/// The index of the token that closes the bracket at `open`.
pub(crate) fn matching(toks: &[Token], open: usize) -> Option<usize> {
    let t = toks.get(open)?;
    if !matches!(t.tok, Tok::Punct("(" | "[" | "{")) {
        return None;
    }
    t.pair
}

/// Keywords that end their statement at a line break (JavaScript's
/// restricted productions): `return\n{...}` returns `undefined`.
const RESTRICTED: [&str; 5] = ["return", "throw", "break", "continue", "yield"];

fn can_end(t: &Token) -> bool {
    match &t.tok {
        Tok::Punct(p) => matches!(*p, ")" | "]" | "}" | "++" | "--"),
        _ => true,
    }
}

fn continues(t: &Token) -> bool {
    if t.tick {
        return true;
    }
    match &t.tok {
        Tok::Punct(p) => !matches!(*p, "!" | "~" | "++" | "--"),
        Tok::Ident(k) => matches!(k.as_str(), "else" | "catch" | "finally"),
        _ => false,
    }
}

/// Split `toks[from..to]` into statements, as index ranges without their `;`.
pub(crate) fn statements(toks: &[Token], from: usize, to: usize) -> Vec<(usize, usize)> {
    let mut out = Vec::new();
    let mut depth = 0i32;
    let mut start = from;
    for k in from..to {
        let t = &toks[k];
        if depth == 0
            && k > start
            && t.nl_before
            && (toks[k - 1].ident().is_some_and(|w| RESTRICTED.contains(&w))
                || (can_end(&toks[k - 1]) && !continues(t)))
        {
            out.push((start, k));
            start = k;
        }
        match t.tok {
            Tok::Punct("(" | "[" | "{") => depth += 1,
            Tok::Punct(")" | "]" | "}") => depth -= 1,
            Tok::Punct(";") if depth == 0 => {
                if k > start {
                    out.push((start, k));
                }
                start = k + 1;
            }
            _ => {}
        }
    }
    if start < to {
        out.push((start, to));
    }
    out
}

/// Split `toks[from..to]` at its top-level commas.
pub(crate) fn split_commas(toks: &[Token], from: usize, to: usize) -> Vec<(usize, usize)> {
    let mut out = Vec::new();
    let mut depth = 0i32;
    let mut start = from;
    for (k, t) in toks.iter().enumerate().take(to).skip(from) {
        match t.tok {
            Tok::Punct("(" | "[" | "{") => depth += 1,
            Tok::Punct(")" | "]" | "}") => depth -= 1,
            Tok::Punct(",") if depth == 0 => {
                out.push((start, k));
                start = k + 1;
            }
            _ => {}
        }
    }
    if start < to {
        out.push((start, to));
    }
    out
}

/// A literal value read exactly.
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Lit {
    Str(String),
    /// As written: a whole number or a plain decimal.
    Num(String),
    Bool(bool),
    Null,
    Array(Vec<Lit>),
    /// Keys in the order written.
    Object(Vec<(String, Lit)>),
}

/// One property of an object literal: its key and where its value is.
pub(crate) struct Prop {
    pub key: String,
    /// The whole property.
    pub range: (usize, usize),
    /// The value.
    pub value: (usize, usize),
}

/// The properties of the object literal that is exactly `toks[from..to]`,
/// when each is a plain `key: value`.
pub(crate) fn object_props(toks: &[Token], from: usize, to: usize) -> Option<Vec<Prop>> {
    if to <= from || !toks[from].is("{") || matching(toks, from)? != to - 1 {
        return None;
    }
    let mut out = Vec::new();
    for (a, b) in split_commas(toks, from + 1, to - 1) {
        if b < a + 3 || !toks[a + 1].is(":") {
            return None;
        }
        let key = match &toks[a].tok {
            Tok::Ident(s) => s.clone(),
            Tok::Str(Some(s)) => s.clone(),
            Tok::Num(n) if n.bytes().all(|c| c.is_ascii_digit()) => n.clone(),
            _ => return None,
        };
        out.push(Prop {
            key,
            range: (a, b),
            value: (a + 2, b),
        });
    }
    Some(out)
}

/// The literal that is exactly `toks[from..to]`.
pub(crate) fn literal(toks: &[Token], from: usize, to: usize) -> Option<Lit> {
    let first = toks.get(from)?;
    if to == from + 1 {
        return match &first.tok {
            Tok::Str(Some(s)) => Some(Lit::Str(s.clone())),
            Tok::Num(n) => number(n, false),
            Tok::Ident(k) => match k.as_str() {
                "true" => Some(Lit::Bool(true)),
                "false" => Some(Lit::Bool(false)),
                "null" => Some(Lit::Null),
                _ => None,
            },
            _ => None,
        };
    }
    if to == from + 2 && first.is("-") {
        if let Tok::Num(n) = &toks[from + 1].tok {
            return number(n, true);
        }
        return None;
    }
    if first.is("[") {
        if matching(toks, from)? != to - 1 {
            return None;
        }
        let mut items = Vec::new();
        for (a, b) in split_commas(toks, from + 1, to - 1) {
            items.push(literal(toks, a, b)?);
        }
        return Some(Lit::Array(items));
    }
    if first.is("{") {
        let mut pairs: Vec<(String, Lit)> = Vec::new();
        for p in object_props(toks, from, to)? {
            let v = literal(toks, p.value.0, p.value.1)?;
            // A repeated key: JavaScript keeps the first place and the last value.
            if let Some(slot) = pairs.iter_mut().find(|(k, _)| *k == p.key) {
                slot.1 = v;
            } else {
                pairs.push((p.key, v));
            }
        }
        return Some(Lit::Object(pairs));
    }
    None
}

fn number(n: &str, negative: bool) -> Option<Lit> {
    let (whole, frac) = match n.split_once('.') {
        Some((w, f)) => (w, Some(f)),
        None => (n, None),
    };
    let digits = |s: &str| !s.is_empty() && s.bytes().all(|c| c.is_ascii_digit());
    if !digits(whole) || (whole.len() > 1 && whole.starts_with('0')) {
        return None;
    }
    if let Some(f) = frac
        && !digits(f)
    {
        return None;
    }
    // Rhai's integers are 64-bit; JavaScript's numbers are doubles, exact to 2^53.
    if frac.is_none() && whole.len() > 15 {
        return None;
    }
    Some(Lit::Num(if negative {
        format!("-{n}")
    } else {
        n.to_string()
    }))
}

/// A Rhai string literal for `s`.
pub(crate) fn rhai_string(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    out.push('"');
    for ch in s.chars() {
        match ch {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if (c as u32) < 0x20 || c == '\u{7f}' => {
                out.push_str(&format!("\\x{:02x}", c as u32));
            }
            '\u{85}' | '\u{2028}' | '\u{2029}' => {
                out.push_str(&format!("\\u{:04x}", ch as u32));
            }
            c => out.push(c),
        }
    }
    out.push('"');
    out
}

/// Rhai's reserved words, which a map key can't be written as bare.
const RHAI_RESERVED: &[&str] = &[
    "true",
    "false",
    "let",
    "const",
    "if",
    "else",
    "switch",
    "do",
    "while",
    "until",
    "loop",
    "for",
    "in",
    "continue",
    "break",
    "return",
    "throw",
    "try",
    "catch",
    "import",
    "export",
    "as",
    "global",
    "private",
    "fn",
    "this",
    "is",
    "var",
    "static",
    "shared",
    "sync",
    "async",
    "await",
    "yield",
    "default",
    "void",
    "null",
    "nil",
    "spawn",
    "thread",
    "go",
    "exit",
    "goto",
    "match",
    "case",
    "public",
    "protected",
    "new",
    "use",
    "with",
    "module",
    "package",
    "super",
    "type_of",
    "print",
    "debug",
    "eval",
    "Fn",
    "call",
    "curry",
];

fn rhai_key(k: &str) -> String {
    let bare = k.bytes().next().is_some_and(|c| c.is_ascii_alphabetic())
        && k.bytes().all(|c| c.is_ascii_alphanumeric() || c == b'_')
        && !RHAI_RESERVED.contains(&k);
    if bare { k.to_string() } else { rhai_string(k) }
}

/// The Rhai for `lit`, starting at `indent` levels.
pub(crate) fn rhai(lit: &Lit, indent: usize) -> String {
    let flat = rhai_flat(lit);
    if indent * 4 + flat.len() <= 76 {
        return flat;
    }
    let pad = "    ".repeat(indent + 1);
    let close = "    ".repeat(indent);
    match lit {
        Lit::Array(items) => {
            let mut s = "[\n".to_string();
            for v in items {
                s += &format!("{pad}{},\n", rhai(v, indent + 1));
            }
            format!("{s}{close}]")
        }
        Lit::Object(pairs) => {
            let mut s = "#{\n".to_string();
            for (k, v) in pairs {
                s += &format!("{pad}{}: {},\n", rhai_key(k), rhai(v, indent + 1));
            }
            format!("{s}{close}}}")
        }
        _ => flat,
    }
}

fn rhai_flat(lit: &Lit) -> String {
    match lit {
        Lit::Str(s) => rhai_string(s),
        Lit::Num(n) => n.clone(),
        Lit::Bool(b) => b.to_string(),
        Lit::Null => "null_value()".into(),
        Lit::Array(items) => format!(
            "[{}]",
            items.iter().map(rhai_flat).collect::<Vec<_>>().join(", ")
        ),
        Lit::Object(pairs) if pairs.is_empty() => "#{}".into(),
        Lit::Object(pairs) => format!(
            "#{{ {} }}",
            pairs
                .iter()
                .map(|(k, v)| format!("{}: {}", rhai_key(k), rhai_flat(v)))
                .collect::<Vec<_>>()
                .join(", ")
        ),
    }
}

/// `#{ }` maps sort their keys, and JavaScript objects keep integer-like keys
/// first. True when an object's keys come out in the same order either way.
pub(crate) fn order_safe(lit: &Lit) -> bool {
    match lit {
        Lit::Object(pairs) => {
            pairs
                .iter()
                .all(|(k, _)| !k.bytes().all(|c| c.is_ascii_digit()))
                && pairs.windows(2).all(|w| w[0].0 < w[1].0)
                && pairs.iter().all(|(_, v)| order_safe(v))
        }
        Lit::Array(items) => items.iter().all(order_safe),
        _ => true,
    }
}

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

    fn lit(src: &str) -> Option<Lit> {
        let t = tokenize(src).unwrap();
        literal(&t, 0, t.len())
    }

    #[test]
    fn reads_literals() {
        assert_eq!(lit("'a\\'b'"), Some(Lit::Str("a'b".into())));
        assert_eq!(lit("`plain`"), Some(Lit::Str("plain".into())));
        assert_eq!(lit("`x${y}`"), None);
        assert_eq!(lit("-2"), Some(Lit::Num("-2".into())));
        assert_eq!(lit("0x10"), None);
        assert_eq!(lit("1e3"), None);
        assert_eq!(
            lit("{ a: [1, 'x'], 'b-c': null, }"),
            Some(Lit::Object(vec![
                (
                    "a".into(),
                    Lit::Array(vec![Lit::Num("1".into()), Lit::Str("x".into())])
                ),
                ("b-c".into(), Lit::Null),
            ]))
        );
        assert_eq!(lit("{ a }"), None);
        assert_eq!(lit("{ ...a }"), None);
        assert_eq!(lit("f()"), None);
        assert_eq!(lit("'\\ud83d\\ude00'"), Some(Lit::Str("😀".into())));
        assert_eq!(lit("'\\ud83d'"), None);
    }

    #[test]
    fn splits_statements_with_and_without_semicolons() {
        let src = "a.b(1)\nc.d({\n x: 1\n})\nif (x) {\n} else {\n}\nreturn { y: 2 };";
        let t = tokenize(src).unwrap();
        let s = statements(&t, 0, t.len());
        let text: Vec<&str> = s
            .iter()
            .map(|&(a, b)| &src[t[a].start..t[b - 1].end])
            .collect();
        assert_eq!(
            text,
            [
                "a.b(1)",
                "c.d({\n x: 1\n})",
                "if (x) {\n} else {\n}",
                "return { y: 2 }"
            ]
        );
    }

    #[test]
    fn regex_and_comments() {
        let t = tokenize("x = /a\\/[/]b/g; // c\n/* d */ y = 4 / 2").unwrap();
        assert!(t.iter().any(|t| t.tok == Tok::Regex));
        assert_eq!(t.iter().filter(|t| t.is("/")).count(), 1);
        assert!(tokenize("'open").is_err());
    }

    #[test]
    fn template_substitutions_are_read_as_code() {
        // A regex holding `}` or a quote, division, and nested templates.
        let t = tokenize("x = `a${s.replace(/[}'`]/g, '')}b${n / 2}c${`${cfg.d}`}`; y").unwrap();
        assert_eq!(t.iter().filter(|t| t.tok == Tok::Template).count(), 1);
        let tpl = t.iter().find(|t| t.tok == Tok::Template).unwrap();
        assert_eq!(tpl.inner, ["s", "n", "cfg"]);
        assert!(t.last().unwrap().is_ident("y"));
        // A `/` that could be either is refused there too.
        assert!(tokenize("`${(x) /'/.test(s)}`").is_err());
        assert!(tokenize("`${a").is_err());
        // Non-ASCII is part of a regular expression's pattern, not code.
        assert!(tokenize("x = /é\\u{a0}/u").is_ok());
        assert!(tokenize("`${é}`").is_err());
    }

    #[test]
    fn writes_rhai() {
        let v = lit("{ name: 'a\"b', type: 'x', 'data-x': 1, n: null }").unwrap();
        assert_eq!(
            rhai(&v, 0),
            "#{ name: \"a\\\"b\", type: \"x\", \"data-x\": 1, n: null_value() }"
        );
        assert!(!order_safe(&lit("{ b: 1, a: 2 }").unwrap()));
        assert!(!order_safe(&lit("{ 2: 1 }").unwrap()));
        assert!(order_safe(&lit("{ a: 1, b: { c: 1 } }").unwrap()));
    }
}