Skip to content

Commit b1a04d6

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 f334e3f commit b1a04d6

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
@@ -220,8 +220,9 @@ SimpleExpr ::= SimpleRef
220220
| ‘$’ ‘{’ Block ‘}’
221221
| Quoted
222222
| quoteId // only inside splices
223-
| ‘new’ ConstrApp {‘with’ ConstrApp} [TemplateBody] New(constr | templ)
224-
| ‘new’ TemplateBody
223+
| ‘new’ ConstrApp {‘with’ ConstrApp} New(constr | templ)
224+
[[colonEol] TemplateBody
225+
| ‘new’ [colonEol] TemplateBody
225226
| ‘(’ ExprsInParens ‘)’ Parens(exprs)
226227
| SimpleExpr ‘.’ id Select(expr, id)
227228
| SimpleExpr ‘.’ MatchClause
@@ -393,24 +394,23 @@ ClassDef ::= id ClassConstr [Template]
393394
ClassConstr ::= [ClsTypeParamClause] [ConstrMods] ClsParamClauses with DefDef(_, <init>, Nil, vparamss, EmptyTree, EmptyTree) as first stat
394395
ConstrMods ::= {Annotation} [AccessModifier]
395396
ObjectDef ::= id [Template] ModuleDef(mods, name, template) // no constructor
396-
EnumDef ::= id ClassConstr InheritClauses EnumBody EnumDef(mods, name, tparams, template)
397+
EnumDef ::= id ClassConstr InheritClauses [colonEol] EnumBody
397398
GivenDef ::= [GivenSig] (Type [‘=’ Expr] | StructuralInstance)
398-
GivenSig ::= [id] [DefTypeParamClause] {UsingParamClause} ‘:’ -- one of `id`, `DefParamClause`, `UsingParamClause` must be present
399+
GivenSig ::= [id] [DefTypeParamClause] {UsingParamClause} ‘:’ -- one of `id`, `DefParamClause`, `UsingParamClause` must be present
399400
StructuralInstance ::= ConstrApp {‘with’ ConstrApp} ‘with’ TemplateBody
400401
Extension ::= ‘extension’ [DefTypeParamClause] ‘(’ DefParam ‘)’
401402
{UsingParamClause}] ExtMethods
402403
ExtMethods ::= ExtMethod | [nl] ‘{’ ExtMethod {semi ExtMethod ‘}’
403404
ExtMethod ::= {Annotation [nl]} {Modifier} ‘def’ DefDef
404-
Template ::= InheritClauses [TemplateBody] Template(constr, parents, self, stats)
405+
Template ::= InheritClauses [colonEol] [TemplateBody] Template(constr, parents, self, stats)
405406
InheritClauses ::= [‘extends’ ConstrApps] [‘derives’ QualId {‘,’ QualId}]
406407
ConstrApps ::= ConstrApp {(‘,’ | ‘with’) ConstrApp}
407408
ConstrApp ::= SimpleType1 {Annotation} {ParArgumentExprs} Apply(tp, args)
408409
ConstrExpr ::= SelfInvocation
409410
| ‘{’ SelfInvocation {semi BlockStat} ‘}’
410411
SelfInvocation ::= ‘this’ ArgumentExprs {ArgumentExprs}
411412
412-
TemplateBody ::= [nl | colonEol]
413-
‘{’ [SelfType] TemplateStat {semi TemplateStat} ‘}’ (self, stats)
413+
TemplateBody ::= [nl] ‘{’ [SelfType] TemplateStat {semi TemplateStat} ‘}’
414414
TemplateStat ::= Import
415415
| Export
416416
| {Annotation [nl]} {Modifier} Def
@@ -422,8 +422,7 @@ TemplateStat ::= Import
422422
SelfType ::= id [‘:’ InfixType] ‘=>’ ValDef(_, name, tpt, _)
423423
| ‘this’ ‘:’ InfixType ‘=>’
424424
425-
EnumBody ::= [nl | colonEol]
426-
‘{’ [SelfType] EnumStat {semi EnumStat} ‘}’
425+
EnumBody ::= [nl] ‘{’ [SelfType] EnumStat {semi EnumStat} ‘}’
427426
EnumStat ::= TemplateStat
428427
| {Annotation [nl]} {Modifier} EnumCase
429428
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)