Skip to content

Commit e06b831

Browse files
committed
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
1 parent 551eae4 commit e06b831

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
@@ -383,6 +383,8 @@ LocalModifier ::= ‘abstract’
383383
| ‘implicit’
384384
| ‘lazy’
385385
| ‘inline’
386+
| ‘transparent’
387+
| ‘infix’
386388
AccessModifier ::= (‘private’ | ‘protected’) [AccessQualifier]
387389
AccessQualifier ::= ‘[’ id ‘]’
388390
@@ -409,24 +411,22 @@ EndMarkerTag ::= id | ‘if’ | ‘while’ | ‘for’ | ‘match’ |
409411
```
410412
RefineDcl ::= ‘val’ ValDcl
411413
| ‘def’ DefDcl
412-
| ‘type’ {nl} TypeDcl
413-
Dcl ::= RefineDcl
414-
| ‘var’ VarDcl
414+
| ‘type’ {nl} TypeDef
415415
ValDcl ::= ids ‘:’ Type
416-
VarDcl ::= ids ‘:’ Type
417416
DefDcl ::= DefSig ‘:’ Type
418-
DefSig ::= id [DefTypeParamClause] [TypelessClauses] [DefImplicitClause]
419-
TypeDcl ::= id [TypeParamClause] {FunParamClause} TypeBounds
420417
421418
Def ::= ‘val’ PatDef
422419
| ‘var’ PatDef
423420
| ‘def’ DefDef
424421
| ‘type’ {nl} TypeDcl
425422
| TmplDef
426-
PatDef ::= ids [‘:’ Type] ‘=’ Expr
427-
| Pattern2 [‘:’ Type] ‘=’ Expr
428-
DefDef ::= DefSig [‘:’ Type] ‘=’ Expr
429-
| ‘this’ TypelessClauses [DefImplicitClause] ‘=’ ConstrExpr
423+
PatDef ::= ids [‘:’ Type] [‘=’ Expr]
424+
| Pattern2 [‘:’ Type] [‘=’ Expr] PatDef(_, pats, tpe?, expr)
425+
DefDef ::= DefSig [‘:’ Type] [‘=’ Expr] DefDef(_, name, paramss, tpe, expr)
426+
| ‘this’ TypelessClauses [DefImplicitClause] ‘=’ ConstrExpr DefDef(_, <init>, vparamss, EmptyTree, expr | Block)
427+
DefSig ::= id [DefParamClauses] [DefImplicitClause]
428+
TypeDef ::= id [TypeParamClause] {FunParamClause} TypeBounds TypeDefTree(_, name, tparams, bound
429+
[‘=’ Type]
430430
431431
TmplDef ::= ([‘case’] ‘class’ | ‘trait’) ClassDef
432432
| [‘case’] ‘object’ ObjectDef

0 commit comments

Comments
 (0)