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
#![allow(clippy::needless_return)]

use crate::{
    parser::grammar::{argument, description, directive, name, selection, ty},
    Parser, SyntaxKind, TokenKind, S, T,
};

/// See: https://spec.graphql.org/October2021/#Field
///
/// *Field*:
///     Alias? Name Arguments? Directives? SelectionSet?
pub(crate) fn field(p: &mut Parser) {
    let _guard = p.start_node(SyntaxKind::FIELD);

    if let Some(TokenKind::Name) = p.peek() {
        if let Some(T![:]) = p.peek_n(2) {
            name::alias(p)
        }
        name::name(p)
    } else {
        p.err("expected a Name");
    }

    if let Some(T!['(']) = p.peek() {
        argument::arguments(p);
    }

    if let Some(T![@]) = p.peek() {
        directive::directives(p);
    }

    if let Some(T!['{']) = p.peek() {
        selection::selection_set(p);
    }
}

/// See: https://spec.graphql.org/October2021/#FieldsDefinition
///
/// *FieldsDefinition*:
///     **{** FieldDefinition* **}**
pub(crate) fn fields_definition(p: &mut Parser) {
    let _g = p.start_node(SyntaxKind::FIELDS_DEFINITION);
    p.bump(S!['{']);
    while let Some(TokenKind::Name | TokenKind::StringValue) = p.peek() {
        // Guaranteed to eat at least one token if the next token is a Name or StringValue
        field_definition(p);
    }
    p.expect(T!['}'], S!['}']);
}

/// See: https://spec.graphql.org/October2021/#FieldDefinition
///
/// *FieldDefinition*:
///     Description? Name ArgumentsDefinition? **:** Type Directives?
pub(crate) fn field_definition(p: &mut Parser) {
    let _guard = p.start_node(SyntaxKind::FIELD_DEFINITION);

    if let Some(TokenKind::StringValue) = p.peek() {
        description::description(p);
    }

    name::name(p);

    if let Some(T!['(']) = p.peek() {
        argument::arguments_definition(p);
    }

    if let Some(T![:]) = p.peek() {
        p.bump(S![:]);
        match p.peek() {
            Some(TokenKind::Name) | Some(T!['[']) => {
                ty::ty(p);
                if let Some(T![@]) = p.peek() {
                    directive::directives(p);
                }
                if p.peek().is_some() {
                    return;
                }
            }
            _ => {
                p.err("expected a Type");
            }
        }
    } else {
        p.err("expected a type");
    }
}