Skip to content

Commit 11e0d62

Browse files
committed
Improve spec (add more formal RFC-like descriptions)
1 parent 3a65a53 commit 11e0d62

7 files changed

Lines changed: 578 additions & 407 deletions
Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
# A. Appendix: Notation Conventions
22

3-
This specification uses a number of notation conventions to describe the language
4-
grammar. This appendix explains those notations to avoid ambiguity.
3+
This specification uses a number of notation conventions to describe the
4+
language grammar. This appendix explains those notations so as to avoid
5+
ambiguity, and is itself non-normative: it describes how to read the
6+
productions found elsewhere in this document, but introduces no new
7+
requirement of its own.
58

69
## Context-Free Grammar
710

8-
A context-free grammar consists of a number of _productions_. Each production has
9-
an abstract symbol called a _non-terminal_ as its left-hand side, and one or more
10-
sequences of non-terminal symbols and terminal characters as its right-hand side.
11+
A context-free grammar consists of a number of _productions_. Each production
12+
has an abstract symbol, called a _non-terminal_, as its left-hand side, and
13+
one or more sequences of non-terminal symbols and terminal characters as its
14+
right-hand side.
1115

12-
Starting from a single goal non-terminal ({Type} for TypeLang), the grammar
16+
Starting from a single goal non-terminal ({Type}, for TypeLang), the grammar
1317
describes a language: the set of character sequences obtained by repeatedly
14-
replacing a non-terminal with one of its right-hand sides until only terminals
15-
remain.
18+
replacing a non-terminal with one of its right-hand sides until only
19+
terminals remain.
1620

1721
Terminals are written in a monospace font, either as a specific character or
18-
sequence (for example {`|`} or {`is`}), or as prose describing a code point (for
19-
example {"New Line (U+000A)"}).
22+
sequence (for example {`|`} or {`is`}), or as prose describing a code point
23+
(for example {"New Line (U+000A)"}).
2024

2125
A production with a single definition is written on one line:
2226

@@ -38,17 +42,19 @@ ListOfLetterA :
3842

3943
## Lexical and Syntactic Grammar
4044

41-
TypeLang is defined by two grammars. The _lexical grammar_ matches patterns of
42-
source characters into _tokens_; the _syntactic grammar_ matches patterns of
43-
tokens into the abstract syntax tree.
45+
TypeLang is defined by two grammars. The _lexical grammar_ matches patterns
46+
of source characters into _tokens_; the _syntactic grammar_ matches patterns
47+
of tokens into the abstract syntax tree.
4448

45-
A lexical grammar production is distinguished by a double colon `::`. No {Ignored}
46-
characters may appear between the terminals of a lexical production.
49+
A lexical grammar production is distinguished by a double colon `::`. No
50+
{Ignored} characters may appear between the terminals of a lexical
51+
production.
4752

4853
Word :: Letter+
4954

50-
A syntactic grammar production is distinguished by a single colon `:`. {Ignored}
51-
tokens may appear before or after any terminal token of a syntactic production.
55+
A syntactic grammar production is distinguished by a single colon `:`.
56+
{Ignored} tokens may appear before or after any terminal token of a syntactic
57+
production.
5258

5359
Phrase : Word+
5460

@@ -67,8 +73,8 @@ Operator :
6773
- `&`
6874
- `?`
6975

70-
**Optionality.** A subscript-style suffix `?` denotes an optional symbol one
71-
sequence including it and one excluding it.
76+
**Optionality.** A subscript-style suffix `?` denotes an optional symbol: one
77+
sequence including it, and one excluding it.
7278

7379
Nullable : `?`? Type
7480

@@ -79,39 +85,56 @@ Nullable :
7985
- `?` Type
8086
- Type
8187

82-
**Lists.** A suffix `*` denotes zero or more repetitions of a symbol; a suffix `+`
83-
denotes one or more. For example {Identifier+} matches a non-empty run of
84-
{Identifier}.
88+
**Lists.** A suffix `*` denotes zero or more repetitions of a symbol; a
89+
suffix `+` denotes one or more. For example, {Identifier+} matches a
90+
non-empty run of {Identifier}.
8591

86-
**Constraints (but not).** The phrase "but not" excludes certain expansions that
87-
would otherwise be permitted.
92+
**Constraints (but not).** The phrase "but not" excludes certain expansions
93+
that would otherwise be permitted.
8894

8995
NonReserved : Name but not `true` or `false` or `null`
9096

91-
means a {NonReserved} may be any {Name} except those three sequences.
97+
means that a {NonReserved} may be any {Name} except those three sequences.
9298

93-
**Lookahead Restrictions.** A restriction of the form `[lookahead != X]` states
94-
that the production must not be followed by `X`. Lookahead restrictions remove
95-
ambiguity and, together with longest-match scanning, ensure a single valid lexical
96-
analysis. For example:
99+
**Lookahead Restrictions.** A restriction of the form `[lookahead != X]`
100+
states that the production must not be followed by `X`. Lookahead
101+
restrictions remove ambiguity and, together with longest-match scanning,
102+
ensure a single valid lexical analysis. For example:
97103

98-
Name :: NameStart NameContinue\* [lookahead != NameContinue]
104+
NameToken :: NameStart NameContinue\* [lookahead != NameContinue]
99105

100-
makes explicit that a {Name} is always the longest possible sequence and cannot be
101-
followed by another {NameContinue} character.
106+
makes explicit that a {NameToken} is always the longest possible sequence,
107+
and cannot be followed by another {NameContinue} character.
108+
109+
**Ordered Choice.** Unless a production states otherwise, its alternatives
110+
are attempted in the order in which they are written, and the first
111+
alternative that matches at the current position is selected; a later
112+
alternative is attempted only once every earlier alternative has failed to
113+
match. This discipline applies uniformly to every alternation in both the
114+
lexical and the syntactic grammar and, in particular, is what allows this
115+
specification to describe an unambiguous grammar despite alternatives whose
116+
languages overlap — for example, the five alternatives of
117+
[PrimaryType](#sec-Primary-Types), or the five alternatives of
118+
[ShapeKey](#sec-Shape-Fields). This is analogous to the ordered choice of a
119+
parsing expression grammar, and is a stronger guarantee than the mere absence
120+
of ambiguity provided by a classical context-free grammar.
102121

103122
## Grammar Semantics
104123

105124
Some productions are accompanied by a **Static Semantics** description, which
106-
explains how a conforming parser should interpret the matched source beyond merely
107-
accepting it — for example, how the radix of an integer literal is determined, or
108-
how out-of-range values are clamped. Static semantics never alter which documents
109-
are accepted; they only describe the value or node that a valid document denotes.
125+
explains how a conforming implementation is to interpret the matched source
126+
beyond merely accepting it — for example, how the radix of an integer
127+
literal is determined, or how an out-of-range value is clamped. Static
128+
semantics never alter which documents are accepted; they only describe the
129+
value or node that a valid document denotes.
110130

111131
## Examples and Counter-Examples
112132

113-
Code blocks in this document illustrate the grammar. A block presented without
114-
qualification denotes a **valid** document. A block explicitly introduced as a
115-
_counter-example_ denotes an **invalid** document, and is followed by the kind of
116-
error a conforming parser is expected to raise. Error messages are illustrative;
117-
the exact wording is not normative.
133+
Code blocks in this document illustrate the grammar. A block presented
134+
without qualification denotes a **valid** document. A block explicitly
135+
introduced as a _counter-example_ denotes an **invalid** document, and is
136+
typically followed by the kind of error a conforming implementation is
137+
expected to raise. Such error messages are illustrative and non-normative;
138+
their exact wording is not prescribed by this specification, and an
139+
implementation MAY report a different message, provided that it rejects the
140+
document (see [Conformance](#sec-Conformance)).

Specification/Appendix B -- Grammar Summary.md

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# B. Appendix: Grammar Summary
22

3-
This appendix consolidates every grammar production defined in this specification.
4-
Lexical productions (double colon `::`) appear first, followed by syntactic
5-
productions (single colon `:`).
3+
This appendix consolidates every grammar production defined in this
4+
specification. Lexical productions (double colon `::`) appear first, followed
5+
by syntactic productions (single colon `:`). It is provided for convenience
6+
and is non-normative; in the event of any discrepancy between this appendix
7+
and the body of this specification, the body governs.
68

7-
## Source Text
9+
**Source Text**
810

911
SourceCharacter :: "Any Unicode code point"
1012

@@ -18,7 +20,7 @@ Letter :: one of
1820

1921
Digit :: one of `0` `1` `2` `3` `4` `5` `6` `7` `8` `9`
2022

21-
## Ignored Tokens
23+
**Ignored Tokens**
2224

2325
Ignored ::
2426

@@ -44,12 +46,12 @@ BlockComment :: `/*` BlockCommentChar\* `*/`
4446

4547
BlockCommentChar :: SourceCharacter but not `*/`
4648

47-
## Lexical Tokens
49+
**Lexical Tokens**
4850

4951
Token ::
5052

5153
- Punctuator
52-
- Name
54+
- NameToken
5355
- Variable
5456
- IntLiteral
5557
- FloatLiteral
@@ -59,12 +61,12 @@ Token ::
5961

6062
Punctuator :: one of
6163

62-
- `?` `|` `&` `*` `,` `:` `;` `=` `!`
64+
- `?` `|` `&` `*` `,` `:` `=`
6365
- `(` `)` `[` `]` `{` `}`
6466
- `<` `>` `<=` `>=`
6567
- `::` `\` `...` `#[`
6668

67-
Name :: NameStart NameContinue\* [lookahead != NameContinue]
69+
NameToken :: NameStart NameContinue\* [lookahead != NameContinue]
6870

6971
NameStart ::
7072

@@ -84,7 +86,7 @@ Variable :: `$` NameStart NameContinue\*
8486

8587
ThisVariable :: `$this` [lookahead != NameContinue]
8688

87-
## Literal Tokens
89+
**Literal Tokens**
8890

8991
BoolLiteral :: one of `true` `false` [lookahead != NameContinue]
9092

@@ -172,7 +174,7 @@ HexEscape :: `x` HexDigit HexDigit?
172174

173175
UnicodeEscape :: `u` `{` HexDigit+ `}`
174176

175-
## Types
177+
**Types**
176178

177179
Type : Expression
178180

@@ -192,7 +194,7 @@ ConditionalOperand :
192194

193195
ConditionalOperator : one of `is` `is not` `>=` `<=` `<` `>`
194196

195-
## Logical Types
197+
**Logical Types**
196198

197199
LogicalType : UnionType
198200

@@ -204,7 +206,7 @@ UnaryType : NullableType
204206

205207
NullableType : `?`? PostfixType
206208

207-
## List and Offset Access Types
209+
**List and Offset Access Types**
208210

209211
PostfixType : PrimaryType TypeSuffix\*
210212

@@ -217,7 +219,7 @@ ListSuffix : `[` `]`
217219

218220
OffsetSuffix : `[` Type `]`
219221

220-
## Primary Types
222+
**Primary Types**
221223

222224
PrimaryType :
223225

@@ -227,7 +229,7 @@ PrimaryType :
227229
- CallableType
228230
- NamedType
229231

230-
## Names
232+
**Names**
231233

232234
Name :
233235

@@ -240,10 +242,10 @@ RelativeName : Identifier (`\` Identifier)\*
240242

241243
Identifier :
242244

243-
- Name
245+
- NameToken
244246
- ReservedWord
245247

246-
## Named and Generic Types
248+
**Named and Generic Types**
247249

248250
NamedType : Name (TemplateArguments | ShapeFields)?
249251

@@ -255,7 +257,7 @@ TemplateArgumentHint : Identifier Type
255257

256258
TemplateArgumentType : Type
257259

258-
## Literal and Constant Types
260+
**Literal and Constant Types**
259261

260262
LiteralType :
261263

@@ -264,11 +266,6 @@ LiteralType :
264266
- IntLiteral
265267
- FloatLiteral
266268
- StringLiteral
267-
- ConstantType
268-
269-
ConstantType :
270-
271-
- Name
272269
- ClassConstant
273270
- ConstantMask
274271

@@ -280,7 +277,7 @@ ConstantMask :
280277
- Name `::` Identifier `*`
281278
- Name `::` `*`
282279

283-
## Shape Types
280+
**Shape Types**
284281

285282
ShapeFields : `{` ShapeBody? `,`? `}`
286283

@@ -309,7 +306,7 @@ ShapeValue : Type
309306

310307
UnsealedShape : `...` TemplateArguments?
311308

312-
## Callable Types
309+
**Callable Types**
313310

314311
CallableType : Name `(` CallableParameters? `)` CallableReturnType?
315312

@@ -329,7 +326,7 @@ ParameterModifiers :
329326
- `&` `...`?
330327
- `...` `&`?
331328

332-
## Attributes
329+
**Attributes**
333330

334331
AttributeGroups : AttributeGroup+
335332

0 commit comments

Comments
 (0)