apollo_parser/cst/
node_ext.rs

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
use crate::cst;
use crate::cst::CstNode;
use crate::SyntaxNode;
use crate::TokenText;
use rowan::GreenToken;
use rowan::SyntaxKind;
use std::num::ParseFloatError;
use std::num::ParseIntError;

impl cst::Name {
    pub fn text(&self) -> TokenText {
        text_of_first_token(self.syntax())
    }
}

impl cst::Variable {
    pub fn text(&self) -> TokenText {
        self.name()
            .expect("Cannot get variable's NAME token")
            .text()
    }
}

impl cst::EnumValue {
    pub fn text(&self) -> TokenText {
        self.name()
            .expect("Cannot get enum value's NAME token")
            .text()
    }
}

impl cst::DirectiveLocation {
    pub fn text(self) -> Option<TokenText> {
        let txt = if self.query_token().is_some() {
            Some("QUERY")
        } else if self.mutation_token().is_some() {
            Some("MUTATION")
        } else if self.subscription_token().is_some() {
            Some("SUBSCRIPTION")
        } else if self.field_token().is_some() {
            Some("FIELD")
        } else if self.fragment_definition_token().is_some() {
            Some("FRAGMENT_DEFINITION")
        } else if self.fragment_spread_token().is_some() {
            Some("FRAGMENT_SPREAD")
        } else if self.inline_fragment_token().is_some() {
            Some("INLINE_FRAGMENT")
        } else if self.variable_definition_token().is_some() {
            Some("VARIABLE_DEFINITION")
        } else if self.schema_token().is_some() {
            Some("SCHEMA")
        } else if self.scalar_token().is_some() {
            Some("SCALAR")
        } else if self.object_token().is_some() {
            Some("OBJECT")
        } else if self.field_definition_token().is_some() {
            Some("FIELD_DEFINITION")
        } else if self.argument_definition_token().is_some() {
            Some("ARGUMENT_DEFINITION")
        } else if self.interface_token().is_some() {
            Some("INTERFACE")
        } else if self.union_token().is_some() {
            Some("UNION")
        } else if self.enum_token().is_some() {
            Some("ENUM")
        } else if self.enum_value_token().is_some() {
            Some("ENUM_VALUE")
        } else if self.input_object_token().is_some() {
            Some("INPUT_OBJECT")
        } else if self.input_field_definition_token().is_some() {
            Some("INPUT_FIELD_DEFINITION")
        } else {
            None
        };

        txt.map(|txt| {
            TokenText(GreenToken::new(
                SyntaxKind(crate::SyntaxKind::DIRECTIVE_LOCATION as u16),
                txt,
            ))
        })
    }
}

impl cst::Definition {
    /// Return the name of this definition, if any. Schema definitions are unnamed and always
    /// return `None`.
    pub fn name(&self) -> Option<cst::Name> {
        match self {
            Self::OperationDefinition(it) => it.name(),
            Self::FragmentDefinition(it) => it.fragment_name()?.name(),
            Self::DirectiveDefinition(it) => it.name(),
            Self::SchemaDefinition(_) => None,
            Self::ScalarTypeDefinition(it) => it.name(),
            Self::ObjectTypeDefinition(it) => it.name(),
            Self::InterfaceTypeDefinition(it) => it.name(),
            Self::UnionTypeDefinition(it) => it.name(),
            Self::EnumTypeDefinition(it) => it.name(),
            Self::InputObjectTypeDefinition(it) => it.name(),
            Self::SchemaExtension(_) => None,
            Self::ScalarTypeExtension(it) => it.name(),
            Self::ObjectTypeExtension(it) => it.name(),
            Self::InterfaceTypeExtension(it) => it.name(),
            Self::UnionTypeExtension(it) => it.name(),
            Self::EnumTypeExtension(it) => it.name(),
            Self::InputObjectTypeExtension(it) => it.name(),
        }
    }

    pub fn kind(&self) -> &'static str {
        match self {
            cst::Definition::OperationDefinition(_) => "OperationDefinition",
            cst::Definition::FragmentDefinition(_) => "FragmentDefinition",
            cst::Definition::DirectiveDefinition(_) => "DirectiveDefinition",
            cst::Definition::ScalarTypeDefinition(_) => "ScalarTypeDefinition",
            cst::Definition::ObjectTypeDefinition(_) => "ObjectTypeDefinition",
            cst::Definition::InterfaceTypeDefinition(_) => "InterfaceTypeDefinition",
            cst::Definition::UnionTypeDefinition(_) => "UnionTypeDefinition",
            cst::Definition::EnumTypeDefinition(_) => "EnumTypeDefinition",
            cst::Definition::InputObjectTypeDefinition(_) => "InputObjectTypeDefinition",
            cst::Definition::SchemaDefinition(_) => "SchemaDefinition",
            cst::Definition::SchemaExtension(_) => "SchemaExtension",
            cst::Definition::ScalarTypeExtension(_) => "ScalarTypeExtension",
            cst::Definition::ObjectTypeExtension(_) => "ObjectTypeExtension",
            cst::Definition::InterfaceTypeExtension(_) => "InterfaceTypeExtension",
            cst::Definition::UnionTypeExtension(_) => "UnionTypeExtension",
            cst::Definition::EnumTypeExtension(_) => "EnumTypeExtension",
            cst::Definition::InputObjectTypeExtension(_) => "InputObjectTypeExtension",
        }
    }

    pub fn is_executable_definition(&self) -> bool {
        matches!(
            self,
            Self::OperationDefinition(_) | Self::FragmentDefinition(_)
        )
    }

    pub fn is_extension_definition(&self) -> bool {
        matches!(
            self,
            Self::SchemaExtension(_)
                | Self::ScalarTypeExtension(_)
                | Self::ObjectTypeExtension(_)
                | Self::InterfaceTypeExtension(_)
                | Self::UnionTypeExtension(_)
                | Self::EnumTypeExtension(_)
                | Self::InputObjectTypeExtension(_)
        )
    }
}

impl From<cst::StringValue> for String {
    fn from(val: cst::StringValue) -> Self {
        Self::from(&val)
    }
}

/// Handle escaped characters in a StringValue.
///
/// Panics on invalid escape sequences. Those should be rejected in the lexer already.
fn unescape_string(input: &str) -> String {
    let mut output = String::with_capacity(input.len());

    let mut iter = input.chars();
    while let Some(c) = iter.next() {
        match c {
            '\\' => {
                let Some(c2) = iter.next() else {
                    output.push(c);
                    break;
                };

                // TODO: https://github.com/apollographql/apollo-rs/issues/657 needs
                // changes both here and in `lexer/mod.rs`
                let mut unicode = || {
                    // 1. Let value be the 16-bit hexadecimal value represented
                    // by the sequence of hexadecimal digits within EscapedUnicode.
                    let value = iter.by_ref().take(4).fold(0, |acc, c| {
                        let digit = c.to_digit(16).unwrap();
                        (acc << 4) + digit
                    });
                    // 2. Return the code point value.
                    char::from_u32(value).unwrap()
                };

                match c2 {
                    '"' | '\\' | '/' => output.push(c2),
                    'b' => output.push('\u{0008}'),
                    'f' => output.push('\u{000c}'),
                    'n' => output.push('\n'),
                    'r' => output.push('\r'),
                    't' => output.push('\t'),
                    'u' => output.push(unicode()),
                    _ => (),
                }
            }
            _ => output.push(c),
        }
    }

    output
}

const ESCAPED_TRIPLE_QUOTE: &str = r#"\""""#;
const TRIPLE_QUOTE: &str = r#"""""#;

fn is_block_string(input: &str) -> bool {
    input.starts_with(TRIPLE_QUOTE)
}

/// Iterator over the lines in a GraphQL string, using GraphQL's definition of newlines
/// (\r\n, \n, or just \r).
struct GraphQLLines<'a> {
    input: &'a str,
    finished: bool,
}

impl<'a> GraphQLLines<'a> {
    fn new(input: &'a str) -> Self {
        Self {
            input,
            finished: false,
        }
    }
}

impl<'a> Iterator for GraphQLLines<'a> {
    type Item = &'a str;
    fn next(&mut self) -> Option<Self::Item> {
        // Can't just check for the input string being empty, as an empty string should still
        // produce one line.
        if self.finished {
            return None;
        }

        let Some(index) = memchr::memchr2(b'\r', b'\n', self.input.as_bytes()) else {
            self.finished = true;
            return Some(self.input);
        };
        let line = &self.input[..index];
        let rest = match self.input.get(index..=index + 1) {
            Some("\r\n") => &self.input[index + 2..],
            _ => &self.input[index + 1..],
        };
        self.input = rest;
        Some(line)
    }
}

/// Split lines on \n, \r\n, and just \r
fn split_lines(input: &str) -> impl Iterator<Item = &str> {
    GraphQLLines::new(input)
}

/// Replace a literal pattern in a string but push the output to an existing string.
///
/// Like `str::replace`, but doesn't allocate if there's enough space in the provided output.
fn replace_into(input: &str, pattern: &str, replace: &str, output: &mut String) {
    let mut last_index = 0;
    for index in memchr::memmem::find_iter(input.as_bytes(), pattern.as_bytes()) {
        output.push_str(&input[last_index..index]);
        output.push_str(replace);
        last_index = index + pattern.len();
    }
    if last_index < input.len() {
        output.push_str(&input[last_index..]);
    }
}

/// Implementation of the spec function `BlockStringValue(rawValue)`. In addition to handling
/// indents and newline normalization, this also handles escape sequences (strictly not part of
/// BlockStringValue in the spec, but more efficient to do it at the same time).
///
/// Spec: https://spec.graphql.org/October2021/#BlockStringValue()
fn unescape_block_string(raw_value: &str) -> String {
    /// WhiteSpace :: Horizontal Tab (U+0009) Space (U+0020)
    fn is_whitespace(c: char) -> bool {
        matches!(c, ' ' | '\t')
    }
    /// Check if a string is all WhiteSpace. This expects a single line of input.
    fn is_whitespace_line(line: &str) -> bool {
        line.chars().all(is_whitespace)
    }
    /// Count the indentation of a single line (how many WhiteSpace characters are at the start).
    fn count_indent(line: &str) -> usize {
        line.chars().take_while(|&c| is_whitespace(c)).count()
    }

    // 1. Let lines be the result of splitting rawValue by LineTerminator.
    // 2. Let commonIndent be null.
    // 3. For each line in lines:
    let common_indent = split_lines(raw_value)
        // 3.a. If line is the first item in lines, continue to the next line.
        .skip(1)
        .filter_map(|line| {
            // 3.b. Let length be the number of characters in line.
            // We will compare this byte length to a character length below, but
            // `count_indent` only ever counts one-byte characters, so it's equivalent.
            let length = line.len();
            // 3.c. Let indent be the number of leading consecutive WhiteSpace characters in line.
            let indent = count_indent(line);
            // 3.d. If indent is less than length:
            (indent < length).then_some(indent)
        })
        .min()
        .unwrap_or(0);

    let mut lines = split_lines(raw_value)
        .enumerate()
        // 4.a. For each line in lines:
        .map(|(index, line)| {
            // 4.a.i. If line is the first item in lines, continue to the next line.
            if index == 0 {
                line
            } else {
                // 4.a.ii. Remove commonIndent characters from the beginning of line.
                &line[common_indent.min(line.len())..]
            }
        })
        // 5. While the first item line in lines contains only WhiteSpace:
        // 5.a. Remove the first item from lines.
        .skip_while(|line| is_whitespace_line(line));

    // (Step 6 is done at the end so we don't need an intermediate allocation.)

    // 7. Let formatted be the empty character sequence.
    let mut formatted = String::with_capacity(raw_value.len());

    // 8. For each line in lines:
    // 8.a. If line is the first item in lines:
    if let Some(line) = lines.next() {
        // 8.a.i. Append formatted with line.
        replace_into(line, ESCAPED_TRIPLE_QUOTE, TRIPLE_QUOTE, &mut formatted);
    };

    let mut final_char_index = formatted.len();

    // 8.b. Otherwise:
    for line in lines {
        // 8.b.i. Append formatted with a line feed character (U+000A).
        formatted.push('\n');
        // 8.b.ii. Append formatted with line.
        replace_into(line, ESCAPED_TRIPLE_QUOTE, TRIPLE_QUOTE, &mut formatted);

        // Track the last non-whitespace line for implementing step 6 in the spec.
        if !is_whitespace_line(line) {
            final_char_index = formatted.len();
        }
    }

    // 6. Implemented differently: remove WhiteSpace-only lines from the end.
    formatted.truncate(final_char_index);

    // 9. Return formatted.
    formatted
}

// TODO(@goto-bus-stop) As this handles escaping, which can fail in theory, it should be TryFrom
impl From<&'_ cst::StringValue> for String {
    fn from(val: &'_ cst::StringValue) -> Self {
        let text = text_of_first_token(val.syntax());
        // These slices would panic if the contents are invalid, but the lexer already guarantees that the
        // string is valid.
        if is_block_string(&text) {
            unescape_block_string(&text[3..text.len() - 3])
        } else {
            unescape_string(&text[1..text.len() - 1])
        }
    }
}

impl TryFrom<cst::IntValue> for i32 {
    type Error = ParseIntError;

    fn try_from(val: cst::IntValue) -> Result<Self, Self::Error> {
        Self::try_from(&val)
    }
}

impl TryFrom<&'_ cst::IntValue> for i32 {
    type Error = ParseIntError;

    fn try_from(val: &'_ cst::IntValue) -> Result<Self, Self::Error> {
        let text = text_of_first_token(val.syntax());
        text.parse()
    }
}

impl TryFrom<cst::IntValue> for f64 {
    type Error = ParseFloatError;

    fn try_from(val: cst::IntValue) -> Result<Self, Self::Error> {
        Self::try_from(&val)
    }
}

impl TryFrom<&'_ cst::IntValue> for f64 {
    type Error = ParseFloatError;

    fn try_from(val: &'_ cst::IntValue) -> Result<Self, Self::Error> {
        let text = text_of_first_token(val.syntax());
        text.parse()
    }
}

impl TryFrom<cst::FloatValue> for f64 {
    type Error = ParseFloatError;

    fn try_from(val: cst::FloatValue) -> Result<Self, Self::Error> {
        Self::try_from(&val)
    }
}

impl TryFrom<&'_ cst::FloatValue> for f64 {
    type Error = ParseFloatError;

    fn try_from(val: &'_ cst::FloatValue) -> Result<Self, Self::Error> {
        let text = text_of_first_token(val.syntax());
        text.parse()
    }
}

impl TryFrom<cst::BooleanValue> for bool {
    type Error = std::str::ParseBoolError;

    fn try_from(val: cst::BooleanValue) -> Result<Self, Self::Error> {
        Self::try_from(&val)
    }
}

impl TryFrom<&'_ cst::BooleanValue> for bool {
    type Error = std::str::ParseBoolError;

    fn try_from(val: &'_ cst::BooleanValue) -> Result<Self, Self::Error> {
        let text = text_of_first_token(val.syntax());
        text.parse()
    }
}

fn text_of_first_token(node: &SyntaxNode) -> TokenText {
    let first_token = node
        .green()
        .children()
        .next()
        .and_then(|it| it.into_token())
        .unwrap()
        .to_owned();

    TokenText(first_token)
}

#[cfg(test)]
mod string_tests {
    use super::unescape_string;

    #[test]
    fn it_parses_strings() {
        assert_eq!(unescape_string(r"simple"), "simple");
        assert_eq!(unescape_string(r" white space "), " white space ");
    }

    #[test]
    fn it_unescapes_strings() {
        assert_eq!(unescape_string(r#"quote \""#), "quote \"");
        assert_eq!(
            unescape_string(r"escaped \n\r\b\t\f"),
            "escaped \n\r\u{0008}\t\u{000c}"
        );
        assert_eq!(unescape_string(r"slashes \\ \/"), r"slashes \ /");
        assert_eq!(
            unescape_string("unescaped unicode outside BMP 😀"),
            "unescaped unicode outside BMP 😀"
        );
        assert_eq!(
            unescape_string(r"unicode \u1234\u5678\u90AB\uCDEF"),
            "unicode \u{1234}\u{5678}\u{90AB}\u{CDEF}"
        );
    }
}

#[cfg(test)]
mod block_string_tests {
    use super::split_lines;
    use super::unescape_block_string;

    #[test]
    fn it_splits_lines_by_graphql_newline_definition() {
        let plain_newlines: Vec<_> = split_lines(
            r#"source text
    with some
    new


    lines
        "#,
        )
        .collect();

        assert_eq!(
            plain_newlines,
            [
                "source text",
                "    with some",
                "    new",
                "",
                "",
                "    lines",
                "        ",
            ]
        );

        let different_endings: Vec<_> =
            split_lines("with\nand\r\nand\rall in the same\r\nstring").collect();
        assert_eq!(
            different_endings,
            ["with", "and", "and", "all in the same", "string",]
        );

        let empty_string: Vec<_> = split_lines("").collect();
        assert_eq!(empty_string, [""]);

        let empty_line: Vec<_> = split_lines("\n\r\r\n").collect();
        assert_eq!(empty_line, ["", "", "", ""]);
    }

    #[test]
    fn it_normalizes_block_string_newlines() {
        assert_eq!(unescape_block_string("multi\nline"), "multi\nline");
        assert_eq!(unescape_block_string("multi\r\nline"), "multi\nline");
        assert_eq!(unescape_block_string("multi\rline"), "multi\nline");
    }

    #[test]
    fn it_does_not_unescape_block_strings() {
        assert_eq!(
            unescape_block_string(r"escaped \n\r\b\t\f"),
            r"escaped \n\r\b\t\f"
        );
        assert_eq!(unescape_block_string(r"slashes \\ \/"), r"slashes \\ \/");
        assert_eq!(
            unescape_block_string("unescaped unicode outside BMP \u{1f600}"),
            "unescaped unicode outside BMP \u{1f600}"
        );
    }

    #[test]
    fn it_dedents_block_strings() {
        assert_eq!(
            unescape_block_string("  intact whitespace with one line  "),
            "  intact whitespace with one line  "
        );

        assert_eq!(
            unescape_block_string(
                r"
            This is
            indented
            quite a lot
    "
            ),
            r"This is
indented
quite a lot"
        );

        assert_eq!(
            unescape_block_string(
                r"

        spans
          multiple
            lines

    "
            ),
            r"spans
  multiple
    lines"
        );
    }
}