mangle-parse 0.7.0

Rust implementation of Mangle, a logic programming language
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
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use anyhow::{Result, anyhow};
use std::io;

use crate::error::{ErrorContext, ScanError};
use crate::quote::{DecodedSequence, unquote};
use crate::token::Token;

// Scanner turns a stream of bytes into a stream of tokens.
pub struct Scanner<R>
where
    R: io::Read,
{
    iter: io::Bytes<io::BufReader<R>>,

    // Peeked char.
    ch: Option<char>,

    pub line: usize,
    pub col: usize,
    /// Byte offset of start of current line.
    pub start_of_line: usize,

    // Text for the last token.
    text: String,

    // Path to file we are parsing. Used for error messages only.
    path: String,
}

impl<R> Scanner<R>
where
    R: io::Read,
{
    pub fn new<P: ToString>(reader: R, path: P) -> Self {
        use io::Read;
        let buf_reader = io::BufReader::new(reader);
        Self {
            iter: buf_reader.bytes(),
            ch: None,
            line: 1,
            col: 0,
            start_of_line: 0,
            text: String::new(),
            path: path.to_string(),
        }
    }

    pub fn get_error_context(&self) -> ErrorContext {
        ErrorContext {
            path: self.path.clone(),
            line: self.line,
            col: self.col,
            start_of_line: self.start_of_line,
        }
    }

    pub fn next_token(&mut self) -> Result<Token> {
        self.next_token_internal()
    }

    fn next_token_internal(&mut self) -> Result<Token> {
        match self.next_char_skip()? {
            Some('=') => Ok(Token::Eq),
            Some(';') => Ok(Token::Semi),
            Some(',') => Ok(Token::Comma),
            Some('!') => match self.peek()? {
                Some('=') => {
                    let _ = self.next_char()?;
                    Ok(Token::BangEq)
                }
                _ => Ok(Token::Bang),
            },
            Some('(') => Ok(Token::LParen),
            Some(')') => Ok(Token::RParen),
            Some('{') => Ok(Token::LBrace),
            Some('}') => Ok(Token::RBrace),
            Some('[') => Ok(Token::LBracket),
            Some(']') => Ok(Token::RBracket),
            Some('') => Ok(Token::Le), // unicode \u2264
            Some('<') => match self.peek()? {
                Some('=') => {
                    let _ = self.next_char()?;
                    Ok(Token::Le)
                }
                _ => Ok(Token::Lt),
            },
            Some('') => Ok(Token::Ge), // unicode \u2265
            Some('>') => match self.peek()? {
                Some('=') => {
                    let _ = self.next_char()?;
                    Ok(Token::Ge)
                }
                _ => Ok(Token::Gt),
            },
            Some(':') => match self.peek()? {
                Some('-') => {
                    let _ = self.next_char()?;
                    Ok(Token::ColonDash)
                }
                Some(c) if is_ident_start(c) => {
                    // Built-in predicate name like :string:contains
                    self.builtin_predicate()
                }
                _ => Ok(Token::Colon),
            },
            Some('|') => match self.peek()? {
                Some('>') => {
                    let _ = self.next_char()?;
                    Ok(Token::PipeGt)
                }
                _ => Ok(Token::Pipe),
            },
            Some('.') => match self.peek()? {
                Some('A'..='Z') => {
                    let first = self.next_char()?.expect("could not get peeked character.");
                    self.ident_or_dot_ident(first, true)
                }
                _ => Ok(Token::Dot),
            },
            Some('/') => self.name(),
            Some('@') => Ok(Token::At),
            Some('') => Ok(Token::LongLeftDoubleArrow),
            Some(delim @ '\'') => self.string(delim, false),
            Some(delim @ '"') => self.string(delim, false),
            Some(first @ '0'..='9') => self.numeric(first),
            Some('-') => match self.peek()? {
                Some('0'..='9' | '.') => self.numeric('-'),
                _ => Err(anyhow!(ScanError::Unexpected(self.get_error_context(), '-'))),
            },
            Some(ch) if is_ident_start(ch) => {
                if ch == 'b'
                    && let Some(delim @ ('\'' | '"')) = self.peek()?
                {
                    let _ = self.next_char()?;
                    return self.string(delim, true);
                }
                self.ident(ch)
            }
            Some(ch) => Err(anyhow!(ScanError::Unexpected(self.get_error_context(), ch))),
            None => Ok(Token::Eof),
        }
    }

    /// Scans a built-in predicate name starting with `:`, e.g. `:string:contains`.
    fn builtin_predicate(&mut self) -> Result<Token> {
        self.text.clear();
        self.text.push(':');
        loop {
            match self.peek()? {
                Some(ch) if is_ident(ch) => {
                    self.next_char()?;
                    self.text.push(ch);
                }
                Some(':') => {
                    self.next_char()?;
                    self.text.push(':');
                }
                _ => break,
            }
        }
        Ok(Token::Ident {
            name: self.text.clone(),
        })
    }

    fn name(&mut self) -> Result<Token> {
        self.text.clear();
        self.text.push('/');
        let mut seen_char = false;
        loop {
            match self.peek()? {
                Some(c) if is_name_char(c) => {
                    self.next_char()?;
                    self.text.push(c);
                    seen_char = true;
                }
                Some('/') => {
                    self.next_char()?;
                    if !seen_char {
                        anyhow::bail!("name constant: expected char after {}", self.text)
                    }
                    self.text.push('/');
                    seen_char = false;
                }
                _ => break,
            }
        }
        if !seen_char {
            anyhow::bail!("name constant: expected name char after {}", self.text)
        }
        Ok(Token::Name {
            name: self.text.to_string(),
        })
    }

    // TODO: this only handles single-double quoted (short. not long).
    fn string(&mut self, delim: char, is_byte: bool) -> Result<Token> {
        self.text.clear();
        if is_byte {
            self.text.push('b');
        }
        self.text.push(delim); // TODO
        loop {
            match self.next_char()? {
                Some(c) if c == delim => break,
                Some(c) => self.text.push(c),
                _ => break,
            }
        }
        self.text.push(delim); // TODO
        match unquote(self.text.as_str())? {
            DecodedSequence::String(decoded) => Ok(Token::String { decoded }),
            DecodedSequence::Bytes(decoded) => Ok(Token::Bytes { decoded }),
        }
    }

    fn numeric(&mut self, first: char) -> Result<Token> {
        self.text.clear();
        self.text.push(first);
        let mut is_float = false;
        loop {
            match self.peek()? {
                Some(c @ '0'..='9') => {
                    self.next_char()?;
                    self.text.push(c)
                }
                Some(c @ '.') => {
                    self.next_char()?;
                    is_float = true;
                    self.text.push(c)
                }
                _ => break,
            }
        }

        // Check for timestamp: exactly 4 digits followed by '-'
        if !is_float && self.text.len() == 4 && first != '-' {
            if let Some('-') = self.peek()? {
                return self.timestamp();
            }
        }

        // Check for duration suffix: digits followed by d, h, m, s, or ms
        if !is_float && first != '-' {
            if let Some(c @ ('d' | 'h' | 'm' | 's')) = self.peek()? {
                return self.duration_literal(c);
            }
        }

        if is_float {
            let num = self.text.parse::<f64>()?;
            return Ok(Token::Float { decoded: num });
        }
        let num = self.text.parse::<i64>()?;
        Ok(Token::Int { decoded: num })
    }

    /// Scan a timestamp literal: YYYY-MM-DDTHH:MM:SS[.frac][Z]
    /// `self.text` already contains the 4-digit year.
    fn timestamp(&mut self) -> Result<Token> {
        // Consume '-'
        self.next_char()?;
        self.text.push('-');

        // Month (2 digits)
        self.scan_n_digits(2, "timestamp month")?;
        self.expect_char('-', "timestamp")?;

        // Day (2 digits)
        self.scan_n_digits(2, "timestamp day")?;

        // Optional time part starting with 'T'
        let mut has_time = false;
        if let Some('T') = self.peek()? {
            has_time = true;
            self.next_char()?;
            self.text.push('T');

            // Hour
            self.scan_n_digits(2, "timestamp hour")?;
            self.expect_char(':', "timestamp")?;

            // Minute
            self.scan_n_digits(2, "timestamp minute")?;
            self.expect_char(':', "timestamp")?;

            // Second
            self.scan_n_digits(2, "timestamp second")?;

            // Optional fractional seconds
            if let Some('.') = self.peek()? {
                self.next_char()?;
                self.text.push('.');
                let start = self.text.len();
                loop {
                    match self.peek()? {
                        Some(c @ '0'..='9') => {
                            self.next_char()?;
                            self.text.push(c);
                        }
                        _ => break,
                    }
                }
                if self.text.len() == start {
                    return Err(anyhow!("timestamp: expected digits after '.'"));
                }
            }

            // Optional 'Z'
            if let Some('Z') = self.peek()? {
                self.next_char()?;
                self.text.push('Z');
            }
        }

        let nanos = parse_timestamp_to_nanos(&self.text, has_time)?;
        Ok(Token::Timestamp { nanos })
    }

    /// Scan a duration literal. `self.text` contains the digits, `suffix_start` is the first suffix char.
    fn duration_literal(&mut self, suffix_start: char) -> Result<Token> {
        self.next_char()?;
        let unit = if suffix_start == 'm' {
            // Could be 'm' (minutes) or 'ms' (milliseconds)
            if let Some('s') = self.peek()? {
                self.next_char()?;
                "ms"
            } else {
                "m"
            }
        } else {
            match suffix_start {
                'd' => "d",
                'h' => "h",
                's' => "s",
                _ => unreachable!(),
            }
        };

        let amount: i64 = self.text.parse()?;
        let nanos = match unit {
            "d" => amount.checked_mul(24 * 60 * 60 * 1_000_000_000),
            "h" => amount.checked_mul(60 * 60 * 1_000_000_000),
            "m" => amount.checked_mul(60 * 1_000_000_000),
            "s" => amount.checked_mul(1_000_000_000),
            "ms" => amount.checked_mul(1_000_000),
            _ => unreachable!(),
        }
        .ok_or_else(|| anyhow!("duration overflow"))?;
        Ok(Token::Duration { nanos })
    }

    /// Scan exactly `n` digits and append to `self.text`.
    fn scan_n_digits(&mut self, n: usize, context: &str) -> Result<()> {
        for _ in 0..n {
            match self.next_char()? {
                Some(c @ '0'..='9') => self.text.push(c),
                Some(c) => {
                    return Err(anyhow!(
                        "{context}: expected digit, got '{c}'"
                    ))
                }
                None => return Err(anyhow!("{context}: unexpected end of input")),
            }
        }
        Ok(())
    }

    /// Expect a specific character and append to `self.text`.
    fn expect_char(&mut self, expected: char, context: &str) -> Result<()> {
        match self.next_char()? {
            Some(c) if c == expected => {
                self.text.push(c);
                Ok(())
            }
            Some(c) => Err(anyhow!(
                "{context}: expected '{expected}', got '{c}'"
            )),
            None => Err(anyhow!(
                "{context}: expected '{expected}', got end of input"
            )),
        }
    }

    fn ident(&mut self, first: char) -> Result<Token> {
        self.ident_or_dot_ident(first, false)
    }

    fn ident_or_dot_ident(&mut self, first: char, dot_ident: bool) -> Result<Token> {
        self.text.clear();
        self.text.push(first);
        loop {
            match self.peek()? {
                Some(ch) if is_ident(ch) => {
                    self.next_char()?;
                    self.text.push(ch);
                }
                Some(':')
                    if (self.text.starts_with("fn:") || self.text == "fn")
                        && !self.text.ends_with(':') =>
                {
                    self.next_char()?;
                    self.text.push(':');
                }
                _ => {
                    return match self.text.as_str() {
                        "Package" => Ok(Token::Package),
                        "Use" => Ok(Token::Use),
                        "Decl" => Ok(Token::Decl),
                        "bound" => Ok(Token::Bound),
                        "inclusion" => Ok(Token::Inclusion),
                        "do" => Ok(Token::Do),
                        "descr" => Ok(Token::Descr),
                        "let" => Ok(Token::Let),
                        _ if dot_ident => {
                            let mut fn_name = String::new();
                            fn_name.push_str("fn:");
                            fn_name.push_str(&self.text);
                            Ok(Token::DotIdent { name: fn_name })
                        }
                        _ => Ok(Token::Ident {
                            name: self.text.clone(),
                        }),
                    };
                }
            }
        }
    }

    #[inline]
    fn next_char(&mut self) -> Result<Option<char>> {
        if let Some(c) = self.ch.take() {
            return Ok(Some(c));
        }
        macro_rules! next_byte_or_incomplete {
            ($self:expr) => {
                $self
                    .next_byte()?
                    .ok_or_else(|| anyhow!(ScanError::IncompleteUtf8(self.get_error_context())))
            };
        }
        let b = self.next_byte()?;
        match b {
            None => Ok(None),
            Some(b @ 0x00..=0x7F) => Ok(Some(unsafe { char::from_u32_unchecked(b.into()) })),
            Some(b1 @ 0xC0..=0xDF) => {
                let b2 = next_byte_or_incomplete!(self)?;
                let bytes = [b1, b2];
                let s = std::str::from_utf8(&bytes)?;
                Ok(s.chars().next())
            }
            Some(b1 @ 0xE0..=0xEF) => {
                let b2 = next_byte_or_incomplete!(self)?;
                let b3 = next_byte_or_incomplete!(self)?;
                let bytes = [b1, b2, b3];
                let s = std::str::from_utf8(&bytes)?;
                Ok(s.chars().next())
            }
            Some(b1 @ 0xF0..=0xF4) => {
                let b2 = next_byte_or_incomplete!(self)?;
                let b3 = next_byte_or_incomplete!(self)?;
                let b4 = next_byte_or_incomplete!(self)?;
                let bytes = [b1, b2, b3, b4];
                let s = std::str::from_utf8(&bytes)?;
                Ok(s.chars().next())
            }
            _ => Err(anyhow!("invalid utf8")),
        }
    }

    /// Advance to next non-whitespace byte. Skip comments.
    #[inline]
    fn next_char_skip(&mut self) -> Result<Option<char>> {
        loop {
            match self.next_char()? {
                Some(' ' | '\t' | '\n') => {}
                Some('#') => self.skip_line()?,
                z => return Ok(z),
            };
        }
    }

    // Skip bytes until newline (included).
    fn skip_line(&mut self) -> Result<()> {
        loop {
            match self.next_byte()? {
                Some(b'\n') | None => return Ok(()),
                _ => {}
            }
        }
    }

    // Advance exactly one byte.
    fn next_byte(&mut self) -> Result<Option<u8>> {
        match self.iter.next() {
            None => Ok(None),
            Some(Ok(b'\n')) => {
                self.start_of_line += self.col + 1;
                self.line += 1;
                self.col = 0;
                Ok(Some(b'\n'))
            }
            Some(Ok(c)) => {
                self.col += 1;
                Ok(Some(c))
            }
            Some(Err(e)) => Err(e.into()),
        }
    }

    #[inline]
    pub fn peek(&mut self) -> Result<Option<char>> {
        Ok(match self.ch {
            Some(ch) => Some(ch),
            None => {
                self.ch = self.next_char()?;
                self.ch
            }
        })
    }
}

fn is_ident_start(c: char) -> bool {
    matches!(c, 'a'..='z' | 'A'..='Z' | '_' )
}

fn is_ident(c: char) -> bool {
    matches!(c, 'a'..='z' | 'A'..='Z' | '0'..='9' | '_' )
}

fn is_name_char(c: char) -> bool {
    matches!(c, 'a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '~' | '.' | '%')
}

/// Parse a timestamp string like "2024-01-15" or "2024-01-15T10:30:00.123Z" into nanoseconds since Unix epoch.
fn parse_timestamp_to_nanos(s: &str, has_time: bool) -> Result<i64> {
    let year: i64 = s[0..4].parse()?;
    let month: u32 = s[5..7].parse()?;
    let day: u32 = s[8..10].parse()?;

    let (hour, minute, second, frac_nanos) = if has_time {
        let h: u32 = s[11..13].parse()?;
        let m: u32 = s[14..16].parse()?;
        let sec: u32 = s[17..19].parse()?;

        let frac = if s.len() > 19 && s.as_bytes()[19] == b'.' {
            let end = if s.ends_with('Z') {
                s.len() - 1
            } else {
                s.len()
            };
            let frac_str = &s[20..end];
            // Pad or truncate to 9 digits (nanoseconds)
            let padded = format!("{frac_str:0<9}");
            padded[..9].parse::<i64>()?
        } else {
            0
        };
        (h, m, sec, frac)
    } else {
        (0, 0, 0, 0)
    };

    // Convert date to days since epoch using Howard Hinnant's algorithm
    let y = if month <= 2 { year - 1 } else { year };
    let era = (if y >= 0 { y } else { y - 399 }) / 400;
    let yoe = (y - era * 400) as u32;
    let m_adj = if month > 2 { month - 3 } else { month + 9 };
    let doy = (153 * m_adj + 2) / 5 + day - 1;
    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
    let days = era * 146097 + doe as i64 - 719468;

    let total_seconds = days * 86400 + hour as i64 * 3600 + minute as i64 * 60 + second as i64;
    Ok(total_seconds * 1_000_000_000 + frac_nanos)
}

#[cfg(test)]
mod test {

    use super::*;

    #[test]
    fn test_ident() -> Result<()> {
        let mut sc = Scanner::new("hello".as_bytes(), "test");
        let token = sc.next_token()?;
        match token {
            Token::Ident { name } if name == "hello" => {}
            _ => panic!("did not match"),
        }
        Ok(())
    }

    fn scan_all(s: &str) -> Result<Vec<Token>> {
        let mut sc = Scanner::new(s.as_bytes(), "test");
        let mut got = vec![];
        loop {
            let token = sc.next_token()?;
            if let Token::Eof = token {
                return Ok(got);
            }
            got.push(token.clone());
        }
    }

    #[test]
    fn test_keywords() -> Result<()> {
        let got = scan_all("do ⟸ let bound descr inclusion Package Use")?;
        use Token::*;
        let want = vec![
            Do,
            LongLeftDoubleArrow,
            Let,
            Bound,
            Descr,
            Inclusion,
            Package,
            Use,
        ];
        assert!(want == got, "want {:?} got {:?}", want, got);
        Ok(())
    }

    #[test]
    fn test_values() -> Result<()> {
        let got = scan_all("1 3.14 'foo🤖' b'foo👷‍♀️' \"bar\" b\"bar\" ")?;
        let want = vec![
            Token::Int { decoded: 1 },
            Token::Float { decoded: 3.14 },
            Token::String {
                decoded: "foo🤖".to_string(),
            },
            Token::Bytes {
                decoded: "foo👷‍♀️".as_bytes().into(),
            },
            Token::String {
                decoded: "bar".to_string(),
            },
            Token::Bytes {
                decoded: "bar".as_bytes().into(),
            },
        ];
        assert!(want == got, "want {:?} got {:?}", want, got);
        Ok(())
    }

    #[test]
    fn test_punctuation() -> Result<()> {
        let got = scan_all(".=!!=()[]{}::-|>")?;
        use Token::*;
        let want = vec![
            Dot, Eq, Bang, BangEq, LParen, RParen, LBracket, RBracket, LBrace, RBrace, Colon,
            ColonDash, PipeGt,
        ];
        assert!(want == got, "want {:?} got {:?}", want, got);
        Ok(())
    }

    #[test]
    fn test_names() -> Result<()> {
        let got = scan_all("/foo /foo/bar")?;
        let want = vec![
            Token::Name {
                name: "/foo".to_string(),
            },
            Token::Name {
                name: "/foo/bar".to_string(),
            },
        ];
        assert!(want == got, "want {:?} got {:?}", want, got);
        Ok(())
    }

    #[test]
    fn test_names_negative() -> Result<()> {
        scan_all("/").unwrap_err();
        scan_all("/foo/").unwrap_err();
        Ok(())
    }

    #[test]
    fn test_negative_numbers() -> Result<()> {
        let got = scan_all("-42 -3.14 -.5")?;
        let want = vec![
            Token::Int { decoded: -42 },
            Token::Float { decoded: -3.14 },
            Token::Float { decoded: -0.5 },
        ];
        assert!(want == got, "want {:?} got {:?}", want, got);
        Ok(())
    }

    #[test]
    fn test_timestamp_date_only() -> Result<()> {
        let got = scan_all("2024-01-15")?;
        assert_eq!(got.len(), 1);
        match &got[0] {
            Token::Timestamp { nanos } => {
                // 2024-01-15T00:00:00Z in nanos
                assert_eq!(*nanos, 1705276800_000_000_000);
            }
            _ => panic!("expected Timestamp, got {:?}", got[0]),
        }
        Ok(())
    }

    #[test]
    fn test_timestamp_full() -> Result<()> {
        let got = scan_all("2024-01-15T10:30:00Z")?;
        assert_eq!(got.len(), 1);
        match &got[0] {
            Token::Timestamp { nanos } => {
                // 2024-01-15T10:30:00Z = 2024-01-15T00:00:00Z + 10*3600 + 30*60
                assert_eq!(*nanos, 1705276800_000_000_000 + (10 * 3600 + 30 * 60) * 1_000_000_000);
            }
            _ => panic!("expected Timestamp, got {:?}", got[0]),
        }
        Ok(())
    }

    #[test]
    fn test_timestamp_fractional() -> Result<()> {
        let got = scan_all("2024-01-15T10:30:00.123Z")?;
        assert_eq!(got.len(), 1);
        match &got[0] {
            Token::Timestamp { nanos } => {
                let base = 1705276800_000_000_000i64 + (10 * 3600 + 30 * 60) * 1_000_000_000;
                assert_eq!(*nanos, base + 123_000_000);
            }
            _ => panic!("expected Timestamp, got {:?}", got[0]),
        }
        Ok(())
    }

    #[test]
    fn test_duration_literals() -> Result<()> {
        let got = scan_all("1d 2h 30m 10s 500ms")?;
        let want = vec![
            Token::Duration { nanos: 24 * 60 * 60 * 1_000_000_000 },
            Token::Duration { nanos: 2 * 60 * 60 * 1_000_000_000 },
            Token::Duration { nanos: 30 * 60 * 1_000_000_000 },
            Token::Duration { nanos: 10 * 1_000_000_000 },
            Token::Duration { nanos: 500 * 1_000_000 },
        ];
        assert!(want == got, "want {:?} got {:?}", want, got);
        Ok(())
    }

    #[test]
    fn test_int_not_duration() -> Result<()> {
        // Bare integer followed by non-duration ident should be two tokens
        let got = scan_all("42 x")?;
        assert_eq!(got[0], Token::Int { decoded: 42 });
        assert_eq!(
            got[1],
            Token::Ident {
                name: "x".to_string()
            }
        );
        Ok(())
    }
}