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
|
# A simple grammar for GraphQL
%grammar graphql
%version 0.01
%include pegex-atoms
# canonical: https://github.com/facebook/graphql/blob/master/spec/Appendix B -- Grammar Summary.md
# IDL RFC: https://github.com/facebook/graphql/pull/90
# inspiration drawn from the slightly obsolete https://github.com/antlr/grammars-v4/blob/master/graphql/GraphQL.g4
# string and number from https://github.com/ingydotnet/json-pgx/blob/master/json.pgx
# in order to capture comments *before* a given rule-group *into* it,
# can't suck up whitespace *after* rule-groups as part of them
graphql: definition
definition: (operationDefinition | fragment | typeSystemDefinition | .ws2)
operationDefinition: selectionSet | operationType .(-) name? .(-) variableDefinitions? .(-) directives? selectionSet
operationType: /(query|mutation|subscription)/
selectionSet: .(/- LCURLY -/) ( selection | `Expected name` ) .(/- RCURLY -/)
selection: (field | inline_fragment | fragment_spread) .(-)
field: alias? name .(-) arguments? .(-) directives? selectionSet?
alias: name .(/- COLON -/)
arguments: .(/- LPAREN -/) argument .(/- RPAREN -/)
argument: name .(/- COLON -/) value
fragment_spread: .spread fragmentName .(-) directives?
inline_fragment: .spread typeCondition? .(-) directives? selectionSet
fragment: .(/- 'fragment' -/) fragmentName .(-) (typeCondition | `Expected "on"`) .(-) directives? selectionSet
fragmentName: ('on' `Unexpected Name "on"` | name)
typeCondition: .(/'on' -/) namedType
value: (variable | float | int | string | boolean | null | enumValue | listValue | objectValue) .(-)
value_const: (float | int | string | boolean | null | enumValue | listValue_const | objectValue_const) .(-)
boolean: /(true|false)/
null: /(null)/
enumValue: (/(true|false|null)/ `Invalid enum value` | name)
listValue: .(/- LSQUARE -/) value* .(/- RSQUARE -/)
listValue_const: .(/- LSQUARE -/) value_const* .(/- RSQUARE -/)
objectValue: .(/- LCURLY -/) ( objectField | `Expected name` ) .(/- RCURLY -/)
objectValue_const: .(/- LCURLY -/) ( objectField_const | `Expected name or constant` ) .(/- RCURLY -/)
objectField: name .(/- COLON -/) value .(-)
objectField_const: name .(/- COLON -/) value_const
variableDefinitions: .(/- LPAREN -/) ( variableDefinition | `Expected $argument: Type` ) .(/- RPAREN -/)
variableDefinition: variable .(/- COLON -/) typedef defaultValue? .(-)
variable: .(/- DOLLAR/) name
defaultValue: .(/- EQUAL -/) value_const
# not "type" as using that for "objectTypeDefinition"
typedef: nonNullType | namedType | listType
namedType: name .(-)
listType: LSQUARE typedef RSQUARE
nonNullType: namedType /- BANG/ | listType /- BANG/
directives: directiveactual
directiveactual: .(/- AT/) name arguments?
string: blockStringValue | stringValue
stringValue: /
DOUBLE
(
(:
BACK (: # Backslash escapes
[
DOUBLE # Double Quote
BACK # Back Slash
SLASH # Foreward Slash
'b' # Back Space
'f' # Form Feed
'n' # New Line
'r' # Carriage Return
't' # Horizontal Tab
]
|
'u' HEX{4} # Unicode octet pair
)
|
[^ DOUBLE CONTROLS BACK ] # Anything else
)*
)
DOUBLE
/
blockStringValue: /
DOUBLE DOUBLE DOUBLE
(
(:
(: BACK DOUBLE DOUBLE DOUBLE ) |
[^ CONTROLS DOUBLE ] |
[ TAB NL CR ] |
(: DOUBLE (?!"") )
)*
)
DOUBLE DOUBLE DOUBLE
/
float: /(
DASH?
(: 0 | [1-9] DIGIT* )
(:
# one or other or both. not neither
(: DOT DIGIT ) (: [eE] [ DASH PLUS ]? DIGIT ) |
(: DOT DIGIT ) |
(: [eE] [ DASH PLUS ]? DIGIT )
)
)/
int: /(
DASH?
(: 0 | [1-9] DIGIT* )
)/
name: /([ UNDER ALPHAS ] [ WORDS ]*)/
spread: /\.{3}/ .(-)
ws: / (: WS | \x{FEFF} | COMMA | comment ) /
comment: / BLANK* HASH BLANK* [^\r\n]* (: EOL | CR !NL | EOS ) / # CR is because MacOS 9
description: .(/ [ WS NL ]* /) string .(/ [ WS NL ]* /) | .(-)
typeSystemDefinition: description? (schema | typeDefinition | typeExtensionDefinition | directive)
schema: .(/'schema' -/) directives? ( .(/- LCURLY -/) operationTypeDefinition .(/- RCURLY /) )?
operationTypeDefinition: operationType .(/- COLON -/) namedType .(-)
typeDefinition: scalar | type | interface | union | enumTypeDefinition | input
# aka scalarTypeDefinition
scalar: .(/'scalar' -/) name .(-) directives?
# aka objectTypeDefinition
type: .(/'type' -/) name .(-) implementsInterfaces? .(-) directives? ( .(/- LCURLY /) fieldDefinition .(/- RCURLY /) )?
implementsInterfaces: .(/'implements' -/) .(/- AMP -/)? (namedType % .(/- AMP -/))
fieldDefinition: description? name .(-) argumentsDefinition? .(/- COLON -/) typedef .(-) directives?
argumentsDefinition: .(/- LPAREN /) inputValueDefinition .(/- RPAREN /)
inputValueDefinition: description? name .(/- COLON -/) typedef .(-) defaultValue? .(-) directives? .(-)
interface: .(/'interface' -/) name .(-) directives? ( .(/- LCURLY /) fieldDefinition .(/- RCURLY /) )?
union: .(/'union' -/) name .(-) directives? ( .(/- EQUAL -/) unionMembers )?
unionMembers: .(/- PIPE -/)? namedType % .(/- PIPE -/)
enumTypeDefinition: .(/'enum' -/) name .(-) directives? ( .(/- LCURLY /) enumValueDefinition .(/- RCURLY /) )?
enumValueDefinition: description? enumValue (.(-) directives)?
input: .(/'input' -/) name .(-) directives? ( .(/- LCURLY /) inputValueDefinition .(/- RCURLY /) )?
typeExtensionDefinition: .(/'extend' -/) (schema | typeDefinition)
directive: .(/'directive' - AT -/) name .(-) argumentsDefinition? .(/- 'on' -/) directiveLocations
directiveLocations: .(/- PIPE -/)? name % .(/- PIPE -/)
|