typeql 3.8.4-rc0

TypeQL Language for Rust
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
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

use std::fmt::{self, Formatter};

use crate::{
    common::{error::TypeQLError, Span, Spanned},
    pretty::Pretty,
    Result,
};

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct BooleanLiteral {
    pub value: String,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct StringLiteral {
    pub value: String,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct IntegerLiteral {
    pub value: String,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct NumericLiteral {
    pub value: String,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Sign {
    Plus,
    Minus,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SignedIntegerLiteral {
    pub sign: Option<Sign>,
    pub integral: String,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SignedDoubleLiteral {
    pub sign: Option<Sign>,
    pub double: String,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SignedDecimalLiteral {
    pub sign: Option<Sign>,
    pub decimal: String,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct DateFragment {
    pub year: String,
    pub month: String,
    pub day: String,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct TimeFragment {
    pub hour: String,
    pub minute: String,
    pub second: Option<String>,
    pub second_fraction: Option<String>,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct DateTimeTZLiteral {
    pub date: DateFragment,
    pub time: TimeFragment,
    pub timezone: TimeZone,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct DateTimeLiteral {
    pub date: DateFragment,
    pub time: TimeFragment,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct DateLiteral {
    pub date: DateFragment,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum TimeZone {
    IANA(String),
    ISO(String),
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum DurationLiteral {
    Weeks(IntegerLiteral),
    DateAndTime(DurationDate, Option<DurationTime>),
    Time(DurationTime),
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct StructLiteral {
    pub inner: String, // TODO
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct DurationDate {
    pub years: Option<IntegerLiteral>,
    pub months: Option<IntegerLiteral>,
    pub days: Option<IntegerLiteral>,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct DurationTime {
    pub hours: Option<IntegerLiteral>,
    pub minutes: Option<IntegerLiteral>,
    pub seconds: Option<NumericLiteral>,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ValueLiteral {
    Boolean(BooleanLiteral),
    Integer(SignedIntegerLiteral),
    Decimal(SignedDecimalLiteral),
    Double(SignedDoubleLiteral),
    Date(DateLiteral),
    DateTime(DateTimeLiteral),
    DateTimeTz(DateTimeTZLiteral),
    Duration(DurationLiteral),
    String(StringLiteral),
    Struct(StructLiteral),
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Literal {
    pub span: Option<Span>,
    pub inner: ValueLiteral,
}

impl Literal {
    pub(crate) fn new(span: Option<Span>, inner: ValueLiteral) -> Self {
        Self { span, inner }
    }
}

impl Spanned for Literal {
    fn span(&self) -> Option<Span> {
        self.span
    }
}

impl Pretty for Literal {}

impl fmt::Display for Literal {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.inner, f)
    }
}

impl fmt::Display for ValueLiteral {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            ValueLiteral::Boolean(value) => fmt::Display::fmt(value, f),
            ValueLiteral::Integer(value) => fmt::Display::fmt(value, f),
            ValueLiteral::Decimal(value) => fmt::Display::fmt(value, f),
            ValueLiteral::Double(value) => fmt::Display::fmt(value, f),
            ValueLiteral::Date(value) => fmt::Display::fmt(value, f),
            ValueLiteral::DateTime(value) => fmt::Display::fmt(value, f),
            ValueLiteral::DateTimeTz(value) => fmt::Display::fmt(value, f),
            ValueLiteral::Duration(value) => fmt::Display::fmt(value, f),
            ValueLiteral::String(value) => fmt::Display::fmt(value, f),
            ValueLiteral::Struct(value) => fmt::Display::fmt(value, f),
        }
    }
}

impl fmt::Display for IntegerLiteral {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(self.value.as_str())
    }
}

impl fmt::Display for NumericLiteral {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(self.value.as_str())
    }
}

impl fmt::Display for StringLiteral {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(self.value.as_str())
    }
}

impl fmt::Display for Sign {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Sign::Plus => f.write_str("+"),
            Sign::Minus => f.write_str("-"),
        }
    }
}

impl fmt::Display for DateFragment {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}-{}-{}", self.year, self.month, self.day)
    }
}

impl fmt::Display for TimeFragment {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let (hour, minute) = (self.hour.as_str(), self.minute.as_str());
        match &self.second {
            None => write!(f, "T{hour}:{minute}"),
            Some(second) => match &self.second_fraction {
                None => write!(f, "T{hour}:{minute}:{second}"),
                Some(second_fraction) => write!(f, "T{hour}:{minute}:{second}.{second_fraction}"),
            },
        }
    }
}

impl fmt::Display for TimeZone {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            TimeZone::IANA(value) => f.write_str(value),
            TimeZone::ISO(value) => f.write_str(value),
        }
    }
}

impl fmt::Display for DurationDate {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        if let Some(years) = &self.years {
            write!(f, "{years}Y")?;
        }
        if let Some(months) = &self.months {
            write!(f, "{months}M")?;
        }
        if let Some(days) = &self.days {
            write!(f, "{days}D")?;
        }
        Ok(())
    }
}

impl fmt::Display for DurationTime {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        if let Some(hours) = &self.hours {
            write!(f, "{hours}H")?;
        }
        if let Some(minutes) = &self.minutes {
            write!(f, "{minutes}M")?;
        }
        if let Some(seconds) = &self.seconds {
            write!(f, "{seconds}S")?;
        }
        Ok(())
    }
}

impl fmt::Display for BooleanLiteral {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(self.value.as_str())
    }
}

impl fmt::Display for SignedIntegerLiteral {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        if let Some(sign) = &self.sign {
            fmt::Display::fmt(sign, f)?;
        }
        f.write_str(self.integral.as_str())
    }
}

impl fmt::Display for SignedDecimalLiteral {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        if let Some(sign) = &self.sign {
            fmt::Display::fmt(sign, f)?;
        }
        write!(f, "{}dec", self.decimal.as_str())
    }
}

impl fmt::Display for SignedDoubleLiteral {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        if let Some(sign) = &self.sign {
            fmt::Display::fmt(sign, f)?;
        }
        f.write_str(self.double.as_str())
    }
}

impl fmt::Display for DateLiteral {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.date, f)
    }
}

impl fmt::Display for DateTimeLiteral {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}{}", &self.date, &self.time)
    }
}

impl fmt::Display for DateTimeTZLiteral {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.date, f)?;
        fmt::Display::fmt(&self.time, f)?;
        fmt::Display::fmt(&self.timezone, f)?;
        Ok(())
    }
}

impl fmt::Display for DurationLiteral {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str("P")?;
        match self {
            DurationLiteral::Weeks(weeks) => write!(f, "{weeks}W")?,
            DurationLiteral::DateAndTime(date, time) => {
                fmt::Display::fmt(date, f)?;
                match time {
                    None => {}
                    Some(time) => write!(f, "T{time}")?,
                }
            }
            DurationLiteral::Time(time) => write!(f, "T{time}")?,
        }
        Ok(())
    }
}

impl fmt::Display for StructLiteral {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(self.inner.as_str())
    }
}

impl StringLiteral {
    pub fn unescape(&self) -> Result<String> {
        self.process_unescape(|bytes| {
            if bytes.len() < 2 {
                return Err(1);
            }
            match bytes[1] {
                BSP => Ok(('\x08', 2)),
                TAB => Ok(('\x09', 2)),
                LF_ => Ok(('\x0a', 2)),
                FF_ => Ok(('\x0c', 2)),
                CR_ => Ok(('\x0d', 2)),
                c @ (b'"' | b'\'' | b'\\') => Ok((c as char, 2)),
                b'u' => match decode_unicode_hex_escape(&bytes[2..]) {
                    Ok((ch, consumed)) => Ok((ch, consumed + 2)),
                    Err(consumed) => Err(consumed + 2),
                },
                _ => Err(2),
            }
        })
    }

    pub fn unescape_regex(&self) -> Result<String> {
        self.process_unescape(|bytes| match bytes.get(1) {
            Some(b'"') => Ok(('"', 2)),
            _ => Ok(('\\', 1)),
        })
    }

    fn process_unescape<F>(&self, escape_handler: F) -> Result<String>
    where
        F: Fn(&[u8]) -> std::result::Result<(char, usize), usize>,
    {
        let bytes = self.value.as_bytes();
        assert_eq!(bytes[0], bytes[bytes.len() - 1]);
        assert!(matches!(bytes[0], b'\'' | b'"'));

        let escaped_string = &self.value[1..self.value.len() - 1];
        let mut buf = Vec::with_capacity(escaped_string.len());
        let mut rest = escaped_string.as_bytes();
        while !rest.is_empty() {
            if rest[0] == b'\\' {
                match escape_handler(rest) {
                    Ok((char, escaped_len)) => {
                        let start = buf.len();
                        buf.resize(buf.len() + char.len_utf8(), 0);
                        char.encode_utf8(&mut buf[start..]);
                        rest = &rest[escaped_len..];
                    }
                    Err(considered_byte_length) => {
                        let offset = escaped_string.len() - rest.len();
                        let mut end = std::cmp::min(offset + considered_byte_length, escaped_string.len());
                        while !escaped_string.is_char_boundary(end) {
                            end += 1;
                        }
                        return Err(TypeQLError::InvalidStringEscape {
                            full_string: escaped_string.to_owned(),
                            escape: escaped_string[offset..end].to_owned(),
                        }
                        .into());
                    }
                }
            } else {
                buf.push(rest[0]);
                rest = &rest[1..];
            }
        }
        Ok(String::from_utf8(buf).expect("Expected valid utf8").to_owned())
    }
}

const BSP: u8 = b'b';
const TAB: u8 = b't';
const LF_: u8 = b'n';
const FF_: u8 = b'f';
const CR_: u8 = b'r';

#[allow(arithmetic_overflow)]
fn decode_unicode_hex_escape(bytes: &[u8]) -> std::result::Result<(char, usize), usize> {
    if bytes.is_empty() {
        Err(0)
    } else if bytes[0] == b'{' {
        let safe_len = std::cmp::min(bytes.len(), 8);
        if let Some(i) = bytes[..safe_len].iter().position(|b| *b == b'}') {
            unicode_char_from_hex(&bytes[1..i]).map(|c| (c, i + 1)).ok_or(i + 1)
        } else {
            Err(safe_len)
        }
    } else {
        if bytes.len() >= 4 {
            unicode_char_from_hex(&bytes[0..4]).map(|c| (c, 4)).ok_or(4)
        } else {
            Err(std::cmp::min(bytes.len(), 4))
        }
    }
}

fn unicode_char_from_hex(bytes: &[u8]) -> Option<char> {
    if bytes.is_empty() || bytes.len() > 6 {
        return None;
    }
    let mut as_u32 = 0u32;
    // from_ascii_radix is still experimental
    for b in bytes {
        as_u32 = (as_u32 << 4) | (*b as char).to_digit(16)?;
    }
    char::from_u32(as_u32)
}

#[cfg(test)]
pub mod tests {
    use crate::{
        value::{StringLiteral, TypeQLError},
        Result,
    };

    fn parse_to_string_literal(escaped: &str) -> StringLiteral {
        let crate::ValueLiteral::String(parsed) = crate::parse_value(escaped).unwrap() else {
            panic!("Not parsed as string");
        };
        parsed
    }

    #[test]
    fn test_unescape_regex() {
        {
            let escaped = r#""a\"b\"c""#;
            let unescaped = parse_to_string_literal(escaped).unescape_regex().unwrap();
            assert_eq!(unescaped.as_str(), r#"a"b"c"#);
        }
        {
            let escaped = r#""abc\123""#;
            let unescaped = parse_to_string_literal(escaped).unescape_regex().unwrap();
            assert_eq!(unescaped.as_str(), r#"abc\123"#);
        }
        // Cases that fail at parsing
        {
            let escaped = r#""abc\""#;
            assert!(crate::parse_value(escaped).is_err()); // Parsing fails as incomplete string literal
            let string_literal = StringLiteral { value: escaped.to_owned() };
            let unescaped = string_literal.unescape_regex().unwrap();
            assert_eq!(unescaped.as_str(), r#"abc\"#);
        }
    }

    macro_rules! assert_unescapes_to {
        ($escaped: expr, $expected: expr) => {
            let unescaped = parse_to_string_literal($escaped).unescape().unwrap();
            assert_eq!(unescaped, $expected);
        };
    }

    macro_rules! assert_unescape_errors {
        ($escaped: expr, $expected_escape_sequence: expr) => {
            let error = parse_to_string_literal($escaped).unescape().unwrap_err();
            let TypeQLError::InvalidStringEscape { escape, .. } = &error.errors()[0] else {
                panic!("Wrong error type. Was {error:?}")
            };
            assert_eq!(escape, $expected_escape_sequence);
        };
    }

    #[test]
    fn test_unescape() {
        // Succeeds
        assert_unescapes_to!(r#""a\tb\tc""#, "a\tb\tc"); // works
        assert_unescapes_to!(r#""a\"b\"c""#, r#"a"b"c"#); // works
        assert_unescapes_to!(r#""a\'b\'c""#, r#"a'b'c"#); // works
        assert_unescapes_to!(r#""a\\b\\c""#, r#"a\b\c"#); // works
                                                          //  - Unicode
        assert_unescapes_to!(r#""abc \u0ca0\u005f\u0ca0""#, "abc ಠ_ಠ"); // works
        assert_unescapes_to!(r#""abc \u0CA0\u005F\u0CA0""#, "abc ಠ_ಠ"); // caps
        assert_unescapes_to!(r#""abc \u0CA01234""#, "abc ಠ1234"); // consumes only 4
        assert_unescapes_to!(r#""abc \u{0CA0}1234""#, "abc ಠ1234"); // braces with only 4
        assert_unescapes_to!(r#""abc \u{130ED}\u{13153}1234""#, "abc 𓃭𓅓1234"); // braces with 6

        // Errors
        assert_unescape_errors!(r#""ab\c""#, r"\c"); // Invalid escape

        //  - Unicode
        assert_unescape_errors!(r#""abc \u""#, r"\u"); // Not enough bytes
        assert_unescape_errors!(r#""abc \u012""#, r"\u012"); // Not enough bytes
        assert_unescape_errors!(r#""abc \uwu/ abc""#, r"\uwu/ "); // Invalid hex
        assert_unescape_errors!(r#""abc \uΣ12Σ abc""#, r"\uΣ12"); // Invalid hex, 3 chars more than 4 bytes
        assert_unescape_errors!(r#""abc \u123Σ abc""#, r"\u123Σ"); // Invalid hex, 4 chars more than 4 bytes
        assert_unescape_errors!(r#""abc \u{""#, r"\u{"); // Not enough bytes
        assert_unescape_errors!(r#""abc \u{123Σ} abc""#, r"\u{123Σ}"); // Invalid hex with braces
        assert_unescape_errors!(r#""abc \u{1234567} abc""#, r"\u{1234567"); // Too many characters, stop at 8
        assert_unescape_errors!(r#""abc \u{213456} abc""#, r"\u{213456}"); // Above valid range

        // Cases that fail at parsing
        {
            let escaped = r#""abc\""#;
            assert!(crate::parse_value(escaped).is_err()); // Parsing fails as incomplete string literal
            let string_literal = StringLiteral { value: escaped.to_owned() };
            let error = string_literal.unescape().unwrap_err();
            let TypeQLError::InvalidStringEscape { escape, .. } = &error.errors()[0] else {
                panic!("Wrong error type. Was {error:?}")
            };
            assert_eq!(escape, r#"\"#);
        }
    }

    #[ignore]
    #[test]
    fn time_unescape_ascii() {
        let text = generate_string(TIME_UNESCAPE_TEXT_LEN, |x| 32 + (x % 94));
        time_unescape(text);
    }

    #[ignore]
    #[test]
    fn time_unescape_unicode() {
        // assert_eq!(None, (0..0x07ff).filter(|x| char::from_u32(*x).is_none()).next());
        let text = generate_string(TIME_UNESCAPE_TEXT_LEN, move |x| x & 0x07ff);
        time_unescape(text);
    }

    const TIME_UNESCAPE_TEXT_LEN: usize = 100000;
    fn time_unescape(text: String) {
        use std::time::Instant;
        let iters = 10000;

        let string_literal = StringLiteral { value: text };
        let start = Instant::now();
        for _ in 0..iters {
            string_literal.unescape().unwrap();
        }
        let end = Instant::now();
        println!(
            "{iters} on string of length {} iters in {}",
            string_literal.value.as_str().len(),
            (end - start).as_secs_f64()
        )
    }

    fn generate_string(length: usize, mapper: fn(u32) -> u32) -> String {
        use rand::{thread_rng, Rng, RngCore};
        let mut rng = thread_rng();
        let capacity: i64 = (1.2 * length as f64).ceil() as i64;
        let mut text = String::with_capacity(capacity as usize);
        text.push('"');

        for _ in 0..capacity {
            if text.len() > length {
                break;
            }
            match char::from_u32(mapper(rng.next_u32())) {
                Some('\\') => text += r"\\",
                Some('\'') => text += r"\'",
                Some('\"') => text += r#"\""#,
                Some('\x08') => text += r"\b",
                Some('\x09') => text += r"\t",
                Some('\x0a') => text += r"\n",
                Some('\x0c') => text += r"\f",
                Some('\x0d') => text += r"\r",
                Some(ch) => text.push(ch),
                None => (),
            }
        }
        text.push('"');
        assert!(text.len() > length && text.len() < length + 10);
        text
    }
}