|
| 1 | +# A. Appendix: Notation Conventions |
| 2 | + |
| 3 | +This specification uses a number of notation conventions to describe the language |
| 4 | +grammar. This appendix explains those notations to avoid ambiguity. |
| 5 | + |
| 6 | +## Context-Free Grammar |
| 7 | + |
| 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 | + |
| 12 | +Starting from a single goal non-terminal ({Type} for TypeLang), the grammar |
| 13 | +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. |
| 16 | + |
| 17 | +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)"}). |
| 20 | + |
| 21 | +A production with a single definition is written on one line: |
| 22 | + |
| 23 | +NonTerminalWithSingleDefinition : NonTerminal `terminal` |
| 24 | + |
| 25 | +A production with several alternative definitions is written as a list: |
| 26 | + |
| 27 | +NonTerminalWithManyDefinitions : |
| 28 | + |
| 29 | +- OtherNonTerminal `terminal` |
| 30 | +- `terminal` |
| 31 | + |
| 32 | +A definition may refer to itself to describe a repetitive sequence: |
| 33 | + |
| 34 | +ListOfLetterA : |
| 35 | + |
| 36 | +- ListOfLetterA `a` |
| 37 | +- `a` |
| 38 | + |
| 39 | +## Lexical and Syntactic Grammar |
| 40 | + |
| 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. |
| 44 | + |
| 45 | +A lexical grammar production is distinguished by a double colon `::`. No {Ignored} |
| 46 | +characters may appear between the terminals of a lexical production. |
| 47 | + |
| 48 | +Word :: Letter+ |
| 49 | + |
| 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. |
| 52 | + |
| 53 | +Phrase : Word+ |
| 54 | + |
| 55 | +## Grammar Notation |
| 56 | + |
| 57 | +**one of.** A production whose alternatives are each a single terminal may be |
| 58 | +written compactly with the phrase "one of": |
| 59 | + |
| 60 | +Operator : one of `|` `&` `?` |
| 61 | + |
| 62 | +is shorthand for |
| 63 | + |
| 64 | +Operator : |
| 65 | + |
| 66 | +- `|` |
| 67 | +- `&` |
| 68 | +- `?` |
| 69 | + |
| 70 | +**Optionality.** A subscript-style suffix `?` denotes an optional symbol — one |
| 71 | +sequence including it and one excluding it. |
| 72 | + |
| 73 | +Nullable : `?`? Type |
| 74 | + |
| 75 | +is shorthand for |
| 76 | + |
| 77 | +Nullable : |
| 78 | + |
| 79 | +- `?` Type |
| 80 | +- Type |
| 81 | + |
| 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}. |
| 85 | + |
| 86 | +**Constraints (but not).** The phrase "but not" excludes certain expansions that |
| 87 | +would otherwise be permitted. |
| 88 | + |
| 89 | +NonReserved : Name but not `true` or `false` or `null` |
| 90 | + |
| 91 | +means a {NonReserved} may be any {Name} except those three sequences. |
| 92 | + |
| 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: |
| 97 | + |
| 98 | +Name :: NameStart NameContinue\* [lookahead != NameContinue] |
| 99 | + |
| 100 | +makes explicit that a {Name} is always the longest possible sequence and cannot be |
| 101 | +followed by another {NameContinue} character. |
| 102 | + |
| 103 | +## Grammar Semantics |
| 104 | + |
| 105 | +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. |
| 110 | + |
| 111 | +## Examples and Counter-Examples |
| 112 | + |
| 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. |
0 commit comments