Skip to content

Commit f7833df

Browse files
committed
Fix documentation about indentation syntax
The fixes omit documenting `:` in extension blocks. These will be disabled in a separate commit. There are two reasons for omitted in `:` in extensions: - they are not needed, and there should be one way to do things - they are confusing since everywhere else a `:` at eol starts a new scope, but here it doesn't.
1 parent bbdbdf2 commit f7833df

File tree

2 files changed

+19
-28
lines changed

2 files changed

+19
-28
lines changed

docs/docs/internals/syntax.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ comment ::= ‘/*’ “any sequence of characters; nested comments ar
8383
8484
nl ::= “new line character”
8585
semi ::= ‘;’ | nl {nl}
86-
colonEol ::= ": at end of line that can start a tenmplate body"
86+
colonEol ::= ": at end of line that can start a template body"
8787
```
8888

8989
## Keywords
@@ -151,7 +151,7 @@ FunParamClause ::= ‘(’ TypedFunParam {‘,’ TypedFunParam } ‘)’
151151
TypedFunParam ::= id ‘:’ Type
152152
MatchType ::= InfixType `match` ‘{’ TypeCaseClauses ‘}’
153153
InfixType ::= RefinedType {id [nl] RefinedType} InfixOp(t1, op, t2)
154-
RefinedType ::= WithType {[nl | colonEol] Refinement} RefinedTypeTree(t, ds)
154+
RefinedType ::= WithType {[nl] Refinement} RefinedTypeTree(t, ds)
155155
WithType ::= AnnotType {‘with’ AnnotType} (deprecated)
156156
AnnotType ::= SimpleType {Annotation} Annotated(t, annot)
157157
@@ -224,8 +224,9 @@ SimpleExpr ::= SimpleRef
224224
| ‘$’ ‘{’ Block ‘}’
225225
| Quoted
226226
| quoteId // only inside splices
227-
| ‘new’ ConstrApp {‘with’ ConstrApp} [TemplateBody] New(constr | templ)
228-
| ‘new’ TemplateBody
227+
| ‘new’ ConstrApp {‘with’ ConstrApp} New(constr | templ)
228+
[[colonEol] TemplateBody
229+
| ‘new’ [colonEol] TemplateBody
229230
| ‘(’ ExprsInParens ‘)’ Parens(exprs)
230231
| SimpleExpr ‘.’ id Select(expr, id)
231232
| SimpleExpr ‘.’ MatchClause
@@ -397,23 +398,22 @@ ClassDef ::= id ClassConstr [Template]
397398
ClassConstr ::= [ClsTypeParamClause] [ConstrMods] ClsParamClauses with DefDef(_, <init>, Nil, vparamss, EmptyTree, EmptyTree) as first stat
398399
ConstrMods ::= {Annotation} [AccessModifier]
399400
ObjectDef ::= id [Template] ModuleDef(mods, name, template) // no constructor
400-
EnumDef ::= id ClassConstr InheritClauses EnumBody EnumDef(mods, name, tparams, template)
401+
EnumDef ::= id ClassConstr InheritClauses [colonEol] EnumBody
401402
GivenDef ::= [GivenSig] (Type [‘=’ Expr] | StructuralInstance)
402-
GivenSig ::= [id] [DefTypeParamClause] {UsingParamClause} ‘:’ -- one of `id`, `DefParamClause`, `UsingParamClause` must be present
403+
GivenSig ::= [id] [DefTypeParamClause] {UsingParamClause} ‘:’ -- one of `id`, `DefParamClause`, `UsingParamClause` must be present
403404
Extension ::= ‘extension’ [DefTypeParamClause] ‘(’ DefParam ‘)’
404405
{UsingParamClause}] ExtMethods
405406
ExtMethods ::= ExtMethod | [nl] ‘{’ ExtMethod {semi ExtMethod ‘}’
406407
ExtMethod ::= {Annotation [nl]} {Modifier} ‘def’ DefDef
407-
Template ::= InheritClauses [TemplateBody] Template(constr, parents, self, stats)
408+
Template ::= InheritClauses [colonEol] [TemplateBody] Template(constr, parents, self, stats)
408409
InheritClauses ::= [‘extends’ ConstrApps] [‘derives’ QualId {‘,’ QualId}]
409410
ConstrApps ::= ConstrApp {(‘,’ | ‘with’) ConstrApp}
410411
ConstrApp ::= SimpleType1 {Annotation} {ParArgumentExprs} Apply(tp, args)
411412
ConstrExpr ::= SelfInvocation
412413
| ‘{’ SelfInvocation {semi BlockStat} ‘}’
413414
SelfInvocation ::= ‘this’ ArgumentExprs {ArgumentExprs}
414415
415-
TemplateBody ::= [nl | colonEol]
416-
‘{’ [SelfType] TemplateStat {semi TemplateStat} ‘}’ (self, stats)
416+
TemplateBody ::= [nl] ‘{’ [SelfType] TemplateStat {semi TemplateStat} ‘}’
417417
TemplateStat ::= Import
418418
| Export
419419
| {Annotation [nl]} {Modifier} Def
@@ -425,8 +425,7 @@ TemplateStat ::= Import
425425
SelfType ::= id [‘:’ InfixType] ‘=>’ ValDef(_, name, tpt, _)
426426
| ‘this’ ‘:’ InfixType ‘=>’
427427
428-
EnumBody ::= [nl | colonEol]
429-
‘{’ [SelfType] EnumStat {semi EnumStat} ‘}’
428+
EnumBody ::= [nl] ‘{’ [SelfType] EnumStat {semi EnumStat} ‘}’
430429
EnumStat ::= TemplateStat
431430
| {Annotation [nl]} {Modifier} EnumCase
432431
EnumCase ::= ‘case’ (id ClassConstr [‘extends’ ConstrApps]] | ids)

docs/docs/reference/other-new-features/indentation.md

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ There are two rules:
5858

5959
An indentation region can start
6060

61-
- after the condition of an `if-else`, or
6261
- after the leading parameters of an `extension`, or
62+
- after a `with` in a given instance, or
6363
- after a ": at end of line" token (see below)
6464
- after one of the following tokens:
6565

@@ -93,7 +93,7 @@ There are two rules:
9393
It is an error if the indentation width of the token following an `<outdent>` does not match the indentation of some previous line in the enclosing indentation region. For instance, the following would be rejected.
9494

9595
```scala
96-
if x < 0
96+
if x < 0 then
9797
-x
9898
else // error: `else` does not align correctly
9999
x
@@ -104,14 +104,14 @@ at the toplevel, inside braces `{...}`, but not inside parentheses `(...)`, patt
104104

105105
### Optional Braces Around Template Bodies
106106

107-
The Scala grammar uses the term _template body_ for the definitions of a class, trait, object or given instance that are normally enclosed in braces. The braces around a template body can also be omitted by means of the following rule
107+
The Scala grammar uses the term _template body_ for the definitions of a class, trait, or object that are normally enclosed in braces. The braces around a template body can also be omitted by means of the following rule
108108

109109
If at the point where a template body can start there is a `:` that occurs at the end
110110
of a line, and that is followed by at least one indented statement, the recognized
111111
token is changed from ":" to ": at end of line". The latter token is one of the tokens
112112
that can start an indentation region. The Scala grammar is changed so an optional ": at end of line" is allowed in front of a template body.
113113

114-
Analogous rules apply for enum bodies, type refinements, definitions in an instance creation expressions, and local packages containing nested definitions.
114+
Analogous rules apply for enum bodies and local packages containing nested definitions.
115115

116116
With these new rules, the following constructs are all valid:
117117

@@ -128,15 +128,6 @@ object O:
128128
enum Color:
129129
case Red, Green, Blue
130130

131-
type T = A:
132-
def f: Int
133-
134-
given [T](using Ord[T]): Ord[List[T]] with
135-
def compare(x: List[T], y: List[T]) = ???
136-
137-
extension (xs: List[Int])
138-
def second: Int = xs.tail.head
139-
140131
new A:
141132
def f = 3
142133

@@ -145,14 +136,15 @@ package p:
145136
package q:
146137
def b = 2
147138
```
139+
In each case, the `:` at the end of line can be replaced without change of meaning by a pair of braces that enclose the following indented definition(s).
148140

149141
The syntax changes allowing this are as follows:
150142

151143
```
152-
TemplateBody ::= [colonEol] ‘{’ [SelfType] TemplateStat {semi TemplateStat} ‘}’
153-
EnumBody ::= [colonEol] ‘{’ [SelfType] EnumStat {semi EnumStat} ‘}’
154-
Packaging ::= ‘package’ QualId [colonEol] ‘{’ TopStatSeq ‘}’
155-
RefinedType ::= AnnotType {[colonEol] Refinement}
144+
Template ::= InheritClauses [colonEol] [TemplateBody]
145+
EnumDef ::= id ClassConstr InheritClauses [colonEol] EnumBody
146+
Packaging ::= ‘package’ QualId [nl | colonEol] ‘{’ TopStatSeq ‘}’
147+
SimpleExpr ::= newConstrApp {‘withConstrApp} [[colonEol] TemplateBody]
156148
```
157149

158150
Here, `colonEol` stands for ": at end of line", as described above.

0 commit comments

Comments
 (0)