Skip to content

Commit f7c4641

Browse files
committed
Expand explanation of whitespace and other ignored source.
This should fix #42 and hopefully be a general improvement in understanding the GraphQL source grammar.
1 parent 7fcf062 commit f7c4641

File tree

1 file changed

+60
-47
lines changed

1 file changed

+60
-47
lines changed

Section 8 -- Grammar.md

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,62 @@ Example_param :
6969
- D
7070

7171

72+
## Ignored Source
73+
74+
Before and after every lexical token may be any amount of ignored source
75+
characters including whitespace and comments. No ignored regions of a source
76+
document are significant, however ignored source characters may appear within a
77+
lexical token, for example a {String} may contain whitespace.
78+
79+
*Ignoring commas*
80+
81+
GraphQL ignores the comma `,` character. This ensures that the absence or
82+
presence of a comma does not meaningfully alter the interpreted syntax of the
83+
document, as this can be a common user-error in other languages. It also allows
84+
for the stylistic use of either trailing commas or line-terminators as
85+
delimiters which are often desired for legibility and maintainability of source
86+
code. The use of commas, whitespace, and line-terminators is encouraged when
87+
they improve the legibility of GraphQL documents.
88+
89+
GraphQL ignores these character sequences:
90+
91+
Ignored :
92+
- WhiteSpace
93+
- LineTerminator
94+
- Comment
95+
- ,
96+
97+
WhiteSpace :
98+
- "Horizontal Tab (U+0009)"
99+
- "Vertical Tab (U+000B)"
100+
- "Form Feed (U+000C)"
101+
- "Space (U+0020)"
102+
- "No-break Space (U+00A0)"
103+
104+
LineTerminator :
105+
- "New Line (U+000A)"
106+
- "Carriage Return (U+000D)"
107+
- "Line Separator (U+2028)"
108+
- "Paragraph Separator (U+2029)"
109+
110+
Comment :
111+
- `#` CommentChar+?
112+
113+
CommentChar : "Any character" but not LineTerminator
114+
115+
72116
## Tokens
73117

74118
A GraphQL document is comprised of several kinds of source tokens defined here
75-
in a lexical grammar and used as terminal symbols in GraphQL's syntactic
76-
grammar. This lexical grammar defines patterns of source characters by specifying
77-
character patterns in {`monospace`} or as {/regular_expressions/}. Non-terminal
78-
patterns are defined as {Italics}.
119+
in a lexical grammar. This lexical grammar defines patterns of source characters
120+
by specifying character patterns in {`monospace`} or as {/regular_expressions/}.
121+
Non-terminal patterns are defined as {Italics}.
122+
123+
No characters are ignored while parsing a given token, for example no whitespace
124+
is allowed between the characters defining a {FloatValue}, however ignored
125+
characters are skipped before and after each well-formed Token.
126+
127+
Tokens are later used as terminal symbols in GraphQL's syntactic grammar.
79128

80129
The GraphQL document syntactic grammar is defined in terms of these
81130
lexical tokens:
@@ -124,55 +173,19 @@ EscapedUnicode : u /[0-9A-Fa-f]{4}/
124173
EscapedCharacter : one of `"` \ `/` b f n r t
125174

126175

127-
128-
### Ignored Source
129-
130-
Before every lexical token may be any amount of ignored source characters
131-
such as whitespace and comments. Whitespace, other than within strings, is
132-
not significant.
133-
134-
GraphQL treats comma `,` similarly to whitespace. This ensures that the absence
135-
or presence of a comma does not meaningfully alter the interpreted syntax of
136-
the document which is a common user-error in other languages. It also allows for
137-
the stylistic use of trailing commas or line-breaks as delimiters which are
138-
often desired for legibility and maintainability of source code. The use of
139-
commas and other whitespace is suggested when it improves legibility.
140-
141-
GraphQL ignores these character sequences:
142-
143-
Ignored :
144-
- WhiteSpace
145-
- LineTerminator
146-
- Comment
147-
- ,
148-
149-
WhiteSpace :
150-
- "Horizontal Tab (U+0009)"
151-
- "Vertical Tab (U+000B)"
152-
- "Form Feed (U+000C)"
153-
- "Space (U+0020)"
154-
- "No-break Space (U+00A0)"
155-
156-
LineTerminator :
157-
- "New Line (U+000A)"
158-
- "Carriage Return (U+000D)"
159-
- "Line Separator (U+2028)"
160-
- "Paragraph Separator (U+2029)"
161-
162-
Comment :
163-
- `#` CommentChar+?
164-
165-
CommentChar : "Any character" but not LineTerminator
166-
167-
168-
169176
## Syntax
170177

171178
A GraphQL document is defined in a syntactic grammar where terminal symbols are
172179
expressed as either an italicized token (ex. {Document}) or as
173-
monospaced short-hand for a {Punctuator} (ex. {`:`}) or {Name}
180+
monospaced short-hand for a {Punctuator} (ex. {`:`}) or short-hand for a {Name}
174181
(ex. {`query`}).
175182

183+
Since whitespace, comments, and other ignored source is skipped between each
184+
well-formed token, this ignored source can appear at any point between the
185+
terminal tokens in the syntactic grammars defined below. However GraphQL
186+
source documents are encouraged to use ignored source only to
187+
improve legibility.
188+
176189

177190
### Document
178191

0 commit comments

Comments
 (0)