Skip to content

Commit 91f0501

Browse files
oderskyWojciechMazur
authored andcommitted
Update syntax.md
1. Add missing soft modifiers 2. Port changes from multiple type parameter sections to reference. Now that multiple type parameter sections are merged, this is the official syntax. This also covers extension method declarations. Fixes #19667 Fixes #19668 [Cherry-picked e06b831]
1 parent b49f016 commit 91f0501

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

docs/_docs/internals/syntax.md

+29-13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ productions map to AST nodes.
2020
The following description of Scala tokens uses literal characters `‘c’` when
2121
referring to the ASCII fragment `\u0000``\u007F`.
2222

23+
Informal descriptions are typeset as `“some comment”`.
24+
2325
## Lexical Syntax
2426

2527
The lexical syntax of Scala is given by the following grammar in EBNF form:
@@ -99,7 +101,10 @@ semi ::= ‘;’ | nl {nl}
99101

100102
## Optional Braces
101103

102-
The lexical analyzer also inserts `indent` and `outdent` tokens that represent regions of indented code [at certain points](../reference/other-new-features/indentation.md)
104+
The principle of optional braces is that any keyword that can be followed by `{` can also be followed by an indented block, without needing an intervening `:`.
105+
(Allowing an optional `:` would be counterproductive since it would introduce several ways to do the same thing.)
106+
107+
The lexical analyzer inserts `indent` and `outdent` tokens that represent regions of indented code [at certain points](./other-new-features/indentation.md).
103108

104109
In the context-free productions below we use the notation `<<< ts >>>`
105110
to indicate a token sequence `ts` that is either enclosed in a pair of braces `{ ts }` or that constitutes an indented region `indent ts outdent`. Analogously, the
@@ -181,7 +186,7 @@ FunTypeArgs ::= InfixType
181186
| ‘(’ [ FunArgTypes ] ‘)’
182187
| FunParamClause
183188
FunParamClause ::= ‘(’ TypedFunParam {‘,’ TypedFunParam } ‘)’
184-
TypedFunParam ::= [`erased`] id ‘:’ Type
189+
TypedFunParam ::= [`erased`] id ‘:’ IntoType
185190
MatchType ::= InfixType `match` <<< TypeCaseClauses >>>
186191
InfixType ::= RefinedType {id [nl] RefinedType} InfixOp(t1, op, t2)
187192
RefinedType ::= AnnotType {[nl] Refinement} RefinedTypeTree(t, ds)
@@ -201,14 +206,17 @@ SimpleType1 ::= id
201206
Singleton ::= SimpleRef
202207
| SimpleLiteral
203208
| Singleton ‘.’ id
204-
FunArgType ::= [`erased`] Type
205-
| [`erased`] ‘=>’ Type PrefixOp(=>, t)
209+
FunArgType ::= IntoType
210+
| ‘=>’ IntoType PrefixOp(=>, t)
206211
FunArgTypes ::= FunArgType { ‘,’ FunArgType }
207212
ParamType ::= [‘=>’] ParamValueType
208-
ParamValueType ::= [‘into’] ExactParamType Into(t)
209-
ExactParamType ::= ParamValueType [‘*’] PostfixOp(t, "*")
213+
ParamValueType ::= IntoType [‘*’] PostfixOp(t, "*")
214+
IntoType ::= [‘into’] IntoTargetType Into(t)
215+
| ‘(’ ‘into’ IntoTargetType ‘)’
216+
IntoTargetType ::= Type
217+
| FunTypeArgs (‘=>’ | ‘?=>’) IntoType
210218
TypeArgs ::= ‘[’ Types ‘]’ ts
211-
Refinement ::= :<<< [RefineDef] {semi [RefineDef]} >>> ds
219+
Refinement ::= :<<< [RefineDef] {semi [RefineDcl]} >>> ds
212220
TypeBounds ::= [‘>:’ Type] [‘<:’ Type] TypeBoundsTree(lo, hi)
213221
TypeParamBounds ::= TypeBounds {‘:’ Type} ContextBounds(typeBounds, tps)
214222
Types ::= Type {‘,’ Type}
@@ -223,7 +231,7 @@ BlockResult ::= FunParams (‘=>’ | ‘?=>’) Block
223231
| HkTypeParamClause ‘=>’ Block
224232
| Expr1
225233
FunParams ::= Bindings
226-
| [`erased`] id
234+
| id
227235
| ‘_’
228236
Expr1 ::= [‘inline’] ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[semi] ‘else’ Expr] If(Parens(cond), thenp, elsep?)
229237
| [‘inline’] ‘if’ Expr ‘then’ Expr [[semi] ‘else’ Expr] If(cond, thenp, elsep?)
@@ -272,7 +280,7 @@ ColonArgument ::= colon [LambdaStart]
272280
LambdaStart ::= FunParams (‘=>’ | ‘?=>’)
273281
| HkTypeParamClause ‘=>’
274282
Quoted ::= ‘'’ ‘{’ Block ‘}’
275-
| ‘'’ ‘[’ Type ‘]’
283+
| ‘'’ ‘[’ TypeBlock ‘]’
276284
ExprSplice ::= spliceId -- if inside quoted block
277285
| ‘$’ ‘{’ Block ‘}’ -- unless inside quoted pattern
278286
| ‘$’ ‘{’ Pattern ‘}’ -- when inside quoted pattern
@@ -294,6 +302,8 @@ BlockStat ::= Import
294302
| Extension
295303
| Expr1
296304
| EndMarker
305+
TypeBlock ::= {TypeBlockStat semi} Type
306+
TypeBlockStat ::= ‘type’ {nl} TypeDcl
297307
298308
ForExpr ::= ‘for’ ‘(’ Enumerators0 ‘)’ {nl} [‘do‘ | ‘yield’] Expr ForYield(enums, expr) / ForDo(enums, expr)
299309
| ‘for’ ‘{’ Enumerators0 ‘}’ {nl} [‘do‘ | ‘yield’] Expr
@@ -376,8 +386,8 @@ Param ::= id ‘:’ ParamType [‘=’ Expr]
376386

377387
### Bindings and Imports
378388
```ebnf
379-
Bindings ::= ‘(’[`erased`] [Binding {‘,’ [`erased`] Binding}] ‘)’
380-
Binding ::= (id | ‘_’) [‘:’ Type] ValDef(_, id, tpe, EmptyTree)
389+
Bindings ::= ‘(’ [Binding {‘,’ Binding}] ‘)’
390+
Binding ::= [`erased`] (id | ‘_’) [‘:’ Type] ValDef(_, id, tpe, EmptyTree)
381391
382392
Modifier ::= LocalModifier
383393
| AccessModifier
@@ -390,6 +400,10 @@ LocalModifier ::= ‘abstract’
390400
| ‘implicit’
391401
| ‘lazy’
392402
| ‘inline’
403+
| ‘transparent’
404+
| ‘infix’
405+
| ‘erased’
406+
393407
AccessModifier ::= (‘private’ | ‘protected’) [AccessQualifier]
394408
AccessQualifier ::= ‘[’ id ‘]’
395409
@@ -414,9 +428,11 @@ EndMarkerTag ::= id | ‘if’ | ‘while’ | ‘for’ | ‘match’ |
414428

415429
### Definitions
416430
```ebnf
417-
RefineDef ::= ‘val’ ValDef
418-
| ‘def’ DefDef
431+
RefineDcl ::= ‘val’ ValDcl
432+
| ‘def’ DefDcl
419433
| ‘type’ {nl} TypeDef
434+
ValDcl ::= ids ‘:’ Type
435+
DefDcl ::= DefSig ‘:’ Type
420436
421437
Def ::= ‘val’ PatDef
422438
| ‘var’ PatDef

docs/_docs/reference/syntax.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ LocalModifier ::= ‘abstract’
381381
| ‘implicit’
382382
| ‘lazy’
383383
| ‘inline’
384+
| ‘transparent’
385+
| ‘infix’
384386
AccessModifier ::= (‘private’ | ‘protected’) [AccessQualifier]
385387
AccessQualifier ::= ‘[’ id ‘]’
386388
@@ -407,24 +409,22 @@ EndMarkerTag ::= id | ‘if’ | ‘while’ | ‘for’ | ‘match’ |
407409
```
408410
RefineDcl ::= ‘val’ ValDcl
409411
| ‘def’ DefDcl
410-
| ‘type’ {nl} TypeDcl
411-
Dcl ::= RefineDcl
412-
| ‘var’ VarDcl
412+
| ‘type’ {nl} TypeDef
413413
ValDcl ::= ids ‘:’ Type
414-
VarDcl ::= ids ‘:’ Type
415414
DefDcl ::= DefSig ‘:’ Type
416-
DefSig ::= id [DefTypeParamClause] [TypelessClauses] [DefImplicitClause]
417-
TypeDcl ::= id [TypeParamClause] {FunParamClause} TypeBounds
418415
419416
Def ::= ‘val’ PatDef
420417
| ‘var’ PatDef
421418
| ‘def’ DefDef
422419
| ‘type’ {nl} TypeDcl
423420
| TmplDef
424-
PatDef ::= ids [‘:’ Type] ‘=’ Expr
425-
| Pattern2 [‘:’ Type] ‘=’ Expr
426-
DefDef ::= DefSig [‘:’ Type] ‘=’ Expr
427-
| ‘this’ TypelessClauses [DefImplicitClause] ‘=’ ConstrExpr
421+
PatDef ::= ids [‘:’ Type] [‘=’ Expr]
422+
| Pattern2 [‘:’ Type] [‘=’ Expr] PatDef(_, pats, tpe?, expr)
423+
DefDef ::= DefSig [‘:’ Type] [‘=’ Expr] DefDef(_, name, paramss, tpe, expr)
424+
| ‘this’ TypelessClauses [DefImplicitClause] ‘=’ ConstrExpr DefDef(_, <init>, vparamss, EmptyTree, expr | Block)
425+
DefSig ::= id [DefParamClauses] [DefImplicitClause]
426+
TypeDef ::= id [TypeParamClause] {FunParamClause} TypeBounds TypeDefTree(_, name, tparams, bound
427+
[‘=’ Type]
428428
429429
TmplDef ::= ([‘case’] ‘class’ | ‘trait’) ClassDef
430430
| [‘case’] ‘object’ ObjectDef

0 commit comments

Comments
 (0)