@@ -89,242 +89,87 @@ semi ::= ‘;’ | nl {nl}
89
89
90
90
## Context-free Syntax
91
91
92
+ ´\color{red}{\text{TODO SCALA3: Once we're done porting the spec, make sure that
93
+ the references to grammar productions in the rest of the spec match this.}}´
94
+
92
95
The context-free syntax of Scala is given by the following EBNF grammar:
93
96
94
97
``` ebnf
95
- Literal ::= [‘-’] integerLiteral
96
- | [‘-’] floatingPointLiteral
97
- | booleanLiteral
98
- | characterLiteral
99
- | stringLiteral
100
- | interpolatedString
101
- | symbolLiteral
102
- | ‘null’
103
-
104
- QualId ::= id {‘.’ id}
105
- ids ::= id {‘,’ id}
106
-
107
- Path ::= StableId
108
- | [id ‘.’] ‘this’
109
- StableId ::= id
110
- | Path ‘.’ id
111
- | [id ‘.’] ‘super’ [ClassQualifier] ‘.’ id
112
- ClassQualifier ::= ‘[’ id ‘]’
113
-
114
- Type ::= FunctionArgTypes ‘=>’ Type
115
- | InfixType [ExistentialClause]
116
- FunctionArgTypes ::= InfixType
117
- | ‘(’ [ ParamType {‘,’ ParamType } ] ‘)’
118
- ExistentialClause ::= ‘forSome’ ‘{’ ExistentialDcl {semi ExistentialDcl} ‘}’
119
- ExistentialDcl ::= ‘type’ TypeDcl
120
- | ‘val’ ValDcl
121
- InfixType ::= CompoundType {id [nl] CompoundType}
122
- CompoundType ::= AnnotType {‘with’ AnnotType} [Refinement]
123
- | Refinement
124
- AnnotType ::= SimpleType {Annotation}
125
- SimpleType ::= SimpleType TypeArgs
126
- | SimpleType ‘#’ id
127
- | StableId
128
- | Path ‘.’ ‘type’
129
- | ‘(’ Types ‘)’
130
- TypeArgs ::= ‘[’ Types ‘]’
131
- Types ::= Type {‘,’ Type}
132
- Refinement ::= [nl] ‘{’ RefineStat {semi RefineStat} ‘}’
133
- RefineStat ::= Dcl
134
- | ‘type’ TypeDef
135
- |
136
- TypePat ::= Type
137
-
138
- Ascription ::= ‘:’ InfixType
139
- | ‘:’ Annotation {Annotation}
140
- | ‘:’ ‘_’ ‘*’
141
-
142
- Expr ::= (Bindings | [‘implicit’] id | ‘_’) ‘=>’ Expr
143
- | Expr1
144
- Expr1 ::= ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[semi] ‘else’ Expr]
145
- | ‘while’ ‘(’ Expr ‘)’ {nl} Expr
146
- | ‘try’ Expr [‘catch’ Expr] [‘finally’ Expr]
147
- | ‘do’ Expr [semi] ‘while’ ‘(’ Expr ‘)’
148
- | ‘for’ (‘(’ Enumerators ‘)’ | ‘{’ Enumerators ‘}’) {nl} [‘yield’] Expr
149
- | ‘throw’ Expr
150
- | ‘return’ [Expr]
151
- | [SimpleExpr ‘.’] id ‘=’ Expr
152
- | PrefixOperator SimpleExpr ‘=’ Expr
153
- | SimpleExpr1 ArgumentExprs ‘=’ Expr
154
- | PostfixExpr
155
- | PostfixExpr Ascription
156
- | PostfixExpr ‘match’ ‘{’ CaseClauses ‘}’
157
- PostfixExpr ::= InfixExpr [id [nl]]
158
- InfixExpr ::= PrefixExpr
159
- | InfixExpr id [nl] InfixExpr
160
- PrefixExpr ::= [PrefixOperator] SimpleExpr
161
- PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’
162
- SimpleExpr ::= ‘new’ (ClassTemplate | TemplateBody)
163
- | BlockExpr
164
- | SimpleExpr1 [‘_’]
165
- SimpleExpr1 ::= Literal
166
- | Path
167
- | ‘_’
168
- | ‘(’ [Exprs] ‘)’
169
- | SimpleExpr ‘.’ id
170
- | SimpleExpr TypeArgs
171
- | SimpleExpr1 ArgumentExprs
172
- | XmlExpr
173
- Exprs ::= Expr {‘,’ Expr}
174
- ArgumentExprs ::= ‘(’ [Exprs] ‘)’
175
- | ‘(’ [Exprs ‘,’] PostfixExpr ‘:’ ‘_’ ‘*’ ‘)’
176
- | [nl] BlockExpr
177
- BlockExpr ::= ‘{’ CaseClauses ‘}’
178
- | ‘{’ Block ‘}’
179
- Block ::= BlockStat {semi BlockStat} [ResultExpr]
180
- BlockStat ::= Import
181
- | {Annotation} [‘implicit’] [‘lazy’] Def
182
- | {Annotation} {LocalModifier} TmplDef
183
- | Expr1
184
- |
185
- ResultExpr ::= Expr1
186
- | (Bindings | ([‘implicit’] id | ‘_’) ‘:’ CompoundType) ‘=>’ Block
187
-
188
- Enumerators ::= Generator {semi Generator}
189
- Generator ::= [‘case’] Pattern1 ‘<-’ Expr {[semi] Guard | semi Pattern1 ‘=’ Expr}
190
-
191
- CaseClauses ::= CaseClause { CaseClause }
192
- CaseClause ::= ‘case’ Pattern [Guard] ‘=>’ Block
193
- Guard ::= ‘if’ PostfixExpr
194
-
195
- Pattern ::= Pattern1 { ‘|’ Pattern1 }
196
- Pattern1 ::= boundvarid ‘:’ TypePat
197
- | ‘_’ ‘:’ TypePat
198
- | Pattern2
199
- Pattern2 ::= id [‘@’ Pattern3]
200
- | Pattern3
201
- Pattern3 ::= SimplePattern
202
- | SimplePattern { id [nl] SimplePattern }
203
- SimplePattern ::= ‘_’
204
- | varid
205
- | Literal
206
- | StableId
207
- | StableId ‘(’ [Patterns] ‘)’
208
- | StableId ‘(’ [Patterns ‘,’] [id ‘@’] ‘_’ ‘*’ ‘)’
209
- | ‘(’ [Patterns] ‘)’
210
- | XmlPattern
211
- Patterns ::= Pattern [‘,’ Patterns]
212
- | ‘_’ ‘*’
213
-
214
- TypeParamClause ::= ‘[’ VariantTypeParam {‘,’ VariantTypeParam} ‘]’
215
- FunTypeParamClause::= ‘[’ TypeParam {‘,’ TypeParam} ‘]’
216
- VariantTypeParam ::= {Annotation} [‘+’ | ‘-’] TypeParam
217
- TypeParam ::= (id | ‘_’) [TypeParamClause] [‘>:’ Type] [‘<:’ Type]
218
- {‘<%’ Type} {‘:’ Type}
219
- ParamClauses ::= {ParamClause} [[nl] ‘(’ ‘implicit’ Params ‘)’]
220
- ParamClause ::= [nl] ‘(’ [Params] ‘)’
221
- Params ::= Param {‘,’ Param}
222
- Param ::= {Annotation} id [‘:’ ParamType] [‘=’ Expr]
223
- ParamType ::= Type
224
- | ‘=>’ Type
225
- | Type ‘*’
226
- ClassParamClauses ::= {ClassParamClause}
227
- [[nl] ‘(’ ‘implicit’ ClassParams ‘)’]
228
- ClassParamClause ::= [nl] ‘(’ [ClassParams] ‘)’
229
- ClassParams ::= ClassParam {‘,’ ClassParam}
230
- ClassParam ::= {Annotation} {Modifier} [(‘val’ | ‘var’)]
231
- id ‘:’ ParamType [‘=’ Expr]
232
- Bindings ::= ‘(’ Binding {‘,’ Binding} ‘)’
233
- Binding ::= (id | ‘_’) [‘:’ Type]
234
-
235
- Modifier ::= LocalModifier
236
- | AccessModifier
237
- | ‘override’
238
- LocalModifier ::= ‘abstract’
239
- | ‘final’
240
- | ‘sealed’
241
- | ‘implicit’
242
- | ‘lazy’
243
- AccessModifier ::= (‘private’ | ‘protected’) [AccessQualifier]
244
- AccessQualifier ::= ‘[’ (id | ‘this’) ‘]’
245
-
246
- Annotation ::= ‘@’ SimpleType {ArgumentExprs}
247
- ConstrAnnotation ::= ‘@’ SimpleType ArgumentExprs
248
-
249
- TemplateBody ::= [nl] ‘{’ [SelfType] TemplateStat {semi TemplateStat} ‘}’
250
- TemplateStat ::= Import
251
- | {Annotation [nl]} {Modifier} Def
252
- | {Annotation [nl]} {Modifier} Dcl
253
- | Expr
254
- |
255
- SelfType ::= id [‘:’ Type] ‘=>’
256
- | ‘this’ ‘:’ Type ‘=>’
257
-
258
- Import ::= ‘import’ ImportExpr {‘,’ ImportExpr}
259
- ImportExpr ::= StableId ‘.’ (id | ‘_’ | ImportSelectors)
260
- ImportSelectors ::= ‘{’ {ImportSelector ‘,’} (ImportSelector | ‘_’) ‘}’
261
- ImportSelector ::= id [‘=>’ id | ‘=>’ ‘_’]
262
-
263
- Dcl ::= ‘val’ ValDcl
264
- | ‘var’ VarDcl
265
- | ‘def’ FunDcl
266
- | ‘type’ {nl} TypeDcl
267
-
268
- ValDcl ::= ids ‘:’ Type
269
- VarDcl ::= ids ‘:’ Type
270
- FunDcl ::= FunSig [‘:’ Type]
271
- FunSig ::= id [FunTypeParamClause] ParamClauses
272
- TypeDcl ::= id [TypeParamClause] [‘>:’ Type] [‘<:’ Type]
273
-
274
- PatVarDef ::= ‘val’ PatDef
275
- | ‘var’ VarDef
276
- Def ::= PatVarDef
277
- | ‘def’ FunDef
278
- | ‘type’ {nl} TypeDef
279
- | TmplDef
280
- PatDef ::= Pattern2 {‘,’ Pattern2} [‘:’ Type] ‘=’ Expr
281
- VarDef ::= PatDef
282
- | ids ‘:’ Type ‘=’ ‘_’
283
- FunDef ::= FunSig [‘:’ Type] ‘=’ Expr
284
- | FunSig [nl] ‘{’ Block ‘}’
285
- | ‘this’ ParamClause ParamClauses
286
- (‘=’ ConstrExpr | [nl] ConstrBlock)
287
- TypeDef ::= id [TypeParamClause] ‘=’ Type
288
-
289
- TmplDef ::= [‘case’] ‘class’ ClassDef
290
- | [‘case’] ‘object’ ObjectDef
291
- | ‘trait’ TraitDef
292
- ClassDef ::= id [TypeParamClause] {ConstrAnnotation} [AccessModifier]
293
- ClassParamClauses ClassTemplateOpt
294
- TraitDef ::= id [TypeParamClause] TraitTemplateOpt
295
- ObjectDef ::= id ClassTemplateOpt
296
- ClassTemplateOpt ::= ‘extends’ ClassTemplate | [[‘extends’] TemplateBody]
297
- TraitTemplateOpt ::= ‘extends’ TraitTemplate | [[‘extends’] TemplateBody]
298
- ClassTemplate ::= [EarlyDefs] ClassParents [TemplateBody]
299
- TraitTemplate ::= [EarlyDefs] TraitParents [TemplateBody]
300
- ClassParents ::= Constr {‘with’ AnnotType}
301
- TraitParents ::= AnnotType {‘with’ AnnotType}
302
- Constr ::= AnnotType {ArgumentExprs}
303
- EarlyDefs ::= ‘{’ [EarlyDef {semi EarlyDef}] ‘}’ ‘with’
304
- EarlyDef ::= {Annotation [nl]} {Modifier} PatVarDef
305
-
306
- ConstrExpr ::= SelfInvocation
307
- | ConstrBlock
308
- ConstrBlock ::= ‘{’ SelfInvocation {semi BlockStat} ‘}’
309
- SelfInvocation ::= ‘this’ ArgumentExprs {ArgumentExprs}
310
-
311
- TopStatSeq ::= TopStat {semi TopStat}
312
- TopStat ::= {Annotation [nl]} {Modifier} TmplDef
313
- | Import
314
- | Packaging
315
- | PackageObject
316
- |
317
- Packaging ::= ‘package’ QualId [nl] ‘{’ TopStatSeq ‘}’
318
- PackageObject ::= ‘package’ ‘object’ ObjectDef
319
-
320
- CompilationUnit ::= {‘package’ QualId semi} TopStatSeq
98
+ RefineDcl ::= ‘val’ ValDcl
99
+ | ‘def’ DefDcl
100
+ | ‘type’ {nl} TypeDcl
101
+ Dcl ::= RefineDcl
102
+ | ‘var’ VarDcl
103
+ ValDcl ::= ids ‘:’ Type
104
+ VarDcl ::= ids ‘:’ Type
105
+ DefDcl ::= DefSig ‘:’ Type
106
+ DefSig ::= id [DefTypeParamClause] [TypelessClauses] [DefImplicitClause]
107
+ TypeDcl ::= id [TypeParamClause] {FunParamClause} TypeBounds
108
+
109
+ Def ::= ‘val’ PatDef
110
+ | ‘var’ PatDef
111
+ | ‘def’ DefDef
112
+ | ‘type’ {nl} TypeDcl
113
+ | TmplDef
114
+ PatDef ::= ids [‘:’ Type] ‘=’ Expr
115
+ | Pattern2 [‘:’ Type] ‘=’ Expr
116
+ DefDef ::= DefSig [‘:’ Type] ‘=’ Expr
117
+ | ‘this’ TypelessClauses [DefImplicitClause] ‘=’ ConstrExpr
118
+
119
+ TmplDef ::= ([‘case’] ‘class’ | ‘trait’) ClassDef
120
+ | [‘case’] ‘object’ ObjectDef
121
+ | ‘enum’ EnumDef
122
+ | ‘given’ GivenDef
123
+ ClassDef ::= id ClassConstr [Template]
124
+ ClassConstr ::= [ClsTypeParamClause] [ConstrMods] ClsParamClauses
125
+ ConstrMods ::= {Annotation} [AccessModifier]
126
+ ObjectDef ::= id [Template]
127
+ EnumDef ::= id ClassConstr InheritClauses EnumBody
128
+ GivenDef ::= [GivenSig] (AnnotType [‘=’ Expr] | StructuralInstance)
129
+ GivenSig ::= [id] [DefTypeParamClause] {UsingParamClause} ‘:’ -- one of `id`, `DefTypeParamClause`, `UsingParamClause` must be present
130
+ StructuralInstance ::= ConstrApp {‘with’ ConstrApp} [‘with’ WithTemplateBody]
131
+ Extension ::= ‘extension’ [DefTypeParamClause] {UsingParamClause}
132
+ ‘(’ DefTermParam ‘)’ {UsingParamClause} ExtMethods
133
+ ExtMethods ::= ExtMethod | [nl] <<< ExtMethod {semi ExtMethod} >>>
134
+ ExtMethod ::= {Annotation [nl]} {Modifier} ‘def’ DefDef
135
+ | Export
136
+ Template ::= InheritClauses [TemplateBody]
137
+ InheritClauses ::= [‘extends’ ConstrApps] [‘derives’ QualId {‘,’ QualId}]
138
+ ConstrApps ::= ConstrApp ({‘,’ ConstrApp} | {‘with’ ConstrApp})
139
+ ConstrApp ::= SimpleType1 {Annotation} {ParArgumentExprs}
140
+ ConstrExpr ::= SelfInvocation
141
+ | <<< SelfInvocation {semi BlockStat} >>>
142
+ SelfInvocation ::= ‘this’ ArgumentExprs {ArgumentExprs}
143
+
144
+ WithTemplateBody ::= <<< [SelfType] TemplateStat {semi TemplateStat} >>>
145
+ TemplateBody ::= :<<< [SelfType] TemplateStat {semi TemplateStat} >>>
146
+ TemplateStat ::= Import
147
+ | Export
148
+ | {Annotation [nl]} {Modifier} Def
149
+ | {Annotation [nl]} {Modifier} Dcl
150
+ | Extension
151
+ | Expr1
152
+ | EndMarker
153
+ |
154
+ SelfType ::= id [‘:’ InfixType] ‘=>’
155
+ | ‘this’ ‘:’ InfixType ‘=>’
156
+
157
+ EnumBody ::= :<<< [SelfType] EnumStat {semi EnumStat} >>>
158
+ EnumStat ::= TemplateStat
159
+ | {Annotation [nl]} {Modifier} EnumCase
160
+ EnumCase ::= ‘case’ (id ClassConstr [‘extends’ ConstrApps]] | ids)
161
+
162
+ TopStats ::= TopStat {semi TopStat}
163
+ TopStat ::= Import
164
+ | Export
165
+ | {Annotation [nl]} {Modifier} Def
166
+ | Extension
167
+ | Packaging
168
+ | PackageObject
169
+ | EndMarker
170
+ |
171
+ Packaging ::= ‘package’ QualId :<<< TopStats >>>
172
+ PackageObject ::= ‘package’ ‘object’ ObjectDef
173
+
174
+ CompilationUnit ::= {‘package’ QualId semi} TopStats
321
175
```
322
-
323
- <!-- TODO add:
324
- SeqPattern ::= ...
325
-
326
- SimplePattern ::= StableId [TypePatArgs] [‘(’ [SeqPatterns] ‘)’]
327
- TypePatArgs ::= ‘[’ TypePatArg {‘,’ TypePatArg} ‘]’
328
- TypePatArg ::= ‘_’ | varid}
329
-
330
- -->
0 commit comments