Skip to content

Commit 919f3f2

Browse files
committed
Clarify that lexing is greedy
GraphQL syntactical grammars intend to be unambiguous. While lexical grammars should also be - there has historically been an assumption that lexical parsing is greedy. This is obvious for numbers and words, but less obvious for empty block strings. Either way, the additional clarity removes ambiguity from the spec Partial fix for #564 Specifically addresses #564 (comment)
1 parent dfd7571 commit 919f3f2

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

spec/Appendix A -- Notation Conventions.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ ListOfLetterA :
4848

4949
The GraphQL language is defined in a syntactic grammar where terminal symbols
5050
are tokens. Tokens are defined in a lexical grammar which matches patterns of
51-
source characters. The result of parsing a sequence of source Unicode characters
52-
produces a GraphQL AST.
51+
source characters. The result of parsing a source text sequence of Unicode
52+
characters first produces a sequence of lexical tokens according to the lexical
53+
grammar which then produces abstract syntax tree (AST) according to the
54+
syntactical grammar.
5355

54-
A Lexical grammar production describes non-terminal "tokens" by
56+
A lexical grammar production describes non-terminal "tokens" by
5557
patterns of terminal Unicode characters. No "whitespace" or other ignored
5658
characters may appear between any terminal Unicode characters in the lexical
5759
grammar production. A lexical grammar production is distinguished by a two colon

spec/Section 2 -- Language.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ common unit of composition allowing for query reuse.
77

88
A GraphQL document is defined as a syntactic grammar where terminal symbols are
99
tokens (indivisible lexical units). These tokens are defined in a lexical
10-
grammar which matches patterns of source characters (defined by a
11-
double-colon `::`).
10+
grammar which matches patterns of source characters. In this document, syntactic
11+
grammar productions are distinguished with a colon `:` while lexical grammar
12+
productions are distinguished with a double-colon `::`.
1213

13-
Note: See [Appendix A](#sec-Appendix-Notation-Conventions) for more details about the definition of lexical and syntactic grammar and other notational conventions
14-
used in this document.
14+
Note: See [Appendix A](#sec-Appendix-Notation-Conventions) for more information
15+
about the lexical and syntactic grammar and other notational conventions used
16+
throughout this document.
1517

1618

1719
## Source Text
@@ -25,6 +27,19 @@ ASCII range so as to be as widely compatible with as many existing tools,
2527
languages, and serialization formats as possible and avoid display issues in
2628
text editors and source control.
2729

30+
**Greedy Lexical Parsing**
31+
32+
The source text of a GraphQL document is first converted into a sequence of
33+
lexical tokens, {Token}, and ignored tokens, {Ignored}. The source text is
34+
scanned from left to right, repeatedly taking the longest possible sequence of
35+
unicode characters as the next token.
36+
37+
For example, the sequence `123` is always interpreted as a single {IntValue},
38+
and `""""""` is always interpreted as a single block {StringValue}.
39+
40+
This sequence of lexical tokens are then scanned from left to right to produce
41+
an abstract syntax tree (AST) according to the {Document} syntactical grammar.
42+
2843

2944
### Unicode
3045

@@ -118,8 +133,7 @@ Token ::
118133
A GraphQL document is comprised of several kinds of indivisible lexical tokens
119134
defined here in a lexical grammar by patterns of source Unicode characters.
120135

121-
Tokens are later used as terminal symbols in a GraphQL Document
122-
syntactic grammars.
136+
Tokens are later used as terminal symbols in GraphQL syntactic grammar rules.
123137

124138

125139
### Ignored Tokens

0 commit comments

Comments
 (0)