Skip to content

Commit

Permalink
Update syntax.md
Browse files Browse the repository at this point in the history
 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]
  • Loading branch information
odersky authored and WojciechMazur committed Jul 1, 2024
1 parent b49f016 commit 91f0501
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
42 changes: 29 additions & 13 deletions docs/_docs/internals/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ productions map to AST nodes.
The following description of Scala tokens uses literal characters `‘c’` when
referring to the ASCII fragment `\u0000``\u007F`.

Informal descriptions are typeset as `“some comment”`.

## Lexical Syntax

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

## Optional Braces

The lexical analyzer also inserts `indent` and `outdent` tokens that represent regions of indented code [at certain points](../reference/other-new-features/indentation.md)
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 `:`.
(Allowing an optional `:` would be counterproductive since it would introduce several ways to do the same thing.)

The lexical analyzer inserts `indent` and `outdent` tokens that represent regions of indented code [at certain points](./other-new-features/indentation.md).

In the context-free productions below we use the notation `<<< ts >>>`
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
Expand Down Expand Up @@ -181,7 +186,7 @@ FunTypeArgs ::= InfixType
| ‘(’ [ FunArgTypes ] ‘)’
| FunParamClause
FunParamClause ::= ‘(’ TypedFunParam {‘,’ TypedFunParam } ‘)’
TypedFunParam ::= [`erased`] id ‘:’ Type
TypedFunParam ::= [`erased`] id ‘:’ IntoType
MatchType ::= InfixType `match` <<< TypeCaseClauses >>>
InfixType ::= RefinedType {id [nl] RefinedType} InfixOp(t1, op, t2)
RefinedType ::= AnnotType {[nl] Refinement} RefinedTypeTree(t, ds)
Expand All @@ -201,14 +206,17 @@ SimpleType1 ::= id
Singleton ::= SimpleRef
| SimpleLiteral
| Singleton ‘.’ id
FunArgType ::= [`erased`] Type
| [`erased`] ‘=>’ Type PrefixOp(=>, t)
FunArgType ::= IntoType
| ‘=>’ IntoType PrefixOp(=>, t)
FunArgTypes ::= FunArgType { ‘,’ FunArgType }
ParamType ::= [‘=>’] ParamValueType
ParamValueType ::= [‘into’] ExactParamType Into(t)
ExactParamType ::= ParamValueType [‘*’] PostfixOp(t, "*")
ParamValueType ::= IntoType [‘*’] PostfixOp(t, "*")
IntoType ::= [‘into’] IntoTargetType Into(t)
| ‘(’ ‘into’ IntoTargetType ‘)’
IntoTargetType ::= Type
| FunTypeArgs (‘=>’ | ‘?=>’) IntoType
TypeArgs ::= ‘[’ Types ‘]’ ts
Refinement ::= :<<< [RefineDef] {semi [RefineDef]} >>> ds
Refinement ::= :<<< [RefineDef] {semi [RefineDcl]} >>> ds
TypeBounds ::= [‘>:’ Type] [‘<:’ Type] TypeBoundsTree(lo, hi)
TypeParamBounds ::= TypeBounds {‘:’ Type} ContextBounds(typeBounds, tps)
Types ::= Type {‘,’ Type}
Expand All @@ -223,7 +231,7 @@ BlockResult ::= FunParams (‘=>’ | ‘?=>’) Block
| HkTypeParamClause ‘=>’ Block
| Expr1
FunParams ::= Bindings
| [`erased`] id
| id
| ‘_’
Expr1 ::= [‘inline’] ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[semi] ‘else’ Expr] If(Parens(cond), thenp, elsep?)
| [‘inline’] ‘if’ Expr ‘then’ Expr [[semi] ‘else’ Expr] If(cond, thenp, elsep?)
Expand Down Expand Up @@ -272,7 +280,7 @@ ColonArgument ::= colon [LambdaStart]
LambdaStart ::= FunParams (‘=>’ | ‘?=>’)
| HkTypeParamClause ‘=>’
Quoted ::= ‘'’ ‘{’ Block ‘}’
| ‘'’ ‘[’ Type ‘]’
| ‘'’ ‘[’ TypeBlock ‘]’
ExprSplice ::= spliceId -- if inside quoted block
| ‘$’ ‘{’ Block ‘}’ -- unless inside quoted pattern
| ‘$’ ‘{’ Pattern ‘}’ -- when inside quoted pattern
Expand All @@ -294,6 +302,8 @@ BlockStat ::= Import
| Extension
| Expr1
| EndMarker
TypeBlock ::= {TypeBlockStat semi} Type
TypeBlockStat ::= ‘type’ {nl} TypeDcl
ForExpr ::= ‘for’ ‘(’ Enumerators0 ‘)’ {nl} [‘do‘ | ‘yield’] Expr ForYield(enums, expr) / ForDo(enums, expr)
| ‘for’ ‘{’ Enumerators0 ‘}’ {nl} [‘do‘ | ‘yield’] Expr
Expand Down Expand Up @@ -376,8 +386,8 @@ Param ::= id ‘:’ ParamType [‘=’ Expr]

### Bindings and Imports
```ebnf
Bindings ::= ‘(’[`erased`] [Binding {‘,’ [`erased`] Binding}] ‘)’
Binding ::= (id | ‘_’) [‘:’ Type] ValDef(_, id, tpe, EmptyTree)
Bindings ::= ‘(’ [Binding {‘,’ Binding}] ‘)’
Binding ::= [`erased`] (id | ‘_’) [‘:’ Type] ValDef(_, id, tpe, EmptyTree)
Modifier ::= LocalModifier
| AccessModifier
Expand All @@ -390,6 +400,10 @@ LocalModifier ::= ‘abstract’
| ‘implicit’
| ‘lazy’
| ‘inline’
| ‘transparent’
| ‘infix’
| ‘erased’
AccessModifier ::= (‘private’ | ‘protected’) [AccessQualifier]
AccessQualifier ::= ‘[’ id ‘]’
Expand All @@ -414,9 +428,11 @@ EndMarkerTag ::= id | ‘if’ | ‘while’ | ‘for’ | ‘match’ |

### Definitions
```ebnf
RefineDef ::= ‘val’ ValDef
| ‘def’ DefDef
RefineDcl ::= ‘val’ ValDcl
| ‘def’ DefDcl
| ‘type’ {nl} TypeDef
ValDcl ::= ids ‘:’ Type
DefDcl ::= DefSig ‘:’ Type
Def ::= ‘val’ PatDef
| ‘var’ PatDef
Expand Down
20 changes: 10 additions & 10 deletions docs/_docs/reference/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ LocalModifier ::= ‘abstract’
| ‘implicit’
| ‘lazy’
| ‘inline’
| ‘transparent’
| ‘infix’
AccessModifier ::= (‘private’ | ‘protected’) [AccessQualifier]
AccessQualifier ::= ‘[’ id ‘]’
Expand All @@ -407,24 +409,22 @@ EndMarkerTag ::= id | ‘if’ | ‘while’ | ‘for’ | ‘match’ |
```
RefineDcl ::= ‘val’ ValDcl
| ‘def’ DefDcl
| ‘type’ {nl} TypeDcl
Dcl ::= RefineDcl
| ‘var’ VarDcl
| ‘type’ {nl} TypeDef
ValDcl ::= ids ‘:’ Type
VarDcl ::= ids ‘:’ Type
DefDcl ::= DefSig ‘:’ Type
DefSig ::= id [DefTypeParamClause] [TypelessClauses] [DefImplicitClause]
TypeDcl ::= id [TypeParamClause] {FunParamClause} TypeBounds
Def ::= ‘val’ PatDef
| ‘var’ PatDef
| ‘def’ DefDef
| ‘type’ {nl} TypeDcl
| TmplDef
PatDef ::= ids [‘:’ Type] ‘=’ Expr
| Pattern2 [‘:’ Type] ‘=’ Expr
DefDef ::= DefSig [‘:’ Type] ‘=’ Expr
| ‘this’ TypelessClauses [DefImplicitClause] ‘=’ ConstrExpr
PatDef ::= ids [‘:’ Type] [‘=’ Expr]
| Pattern2 [‘:’ Type] [‘=’ Expr] PatDef(_, pats, tpe?, expr)
DefDef ::= DefSig [‘:’ Type] [‘=’ Expr] DefDef(_, name, paramss, tpe, expr)
| ‘this’ TypelessClauses [DefImplicitClause] ‘=’ ConstrExpr DefDef(_, <init>, vparamss, EmptyTree, expr | Block)
DefSig ::= id [DefParamClauses] [DefImplicitClause]
TypeDef ::= id [TypeParamClause] {FunParamClause} TypeBounds TypeDefTree(_, name, tparams, bound
[‘=’ Type]
TmplDef ::= ([‘case’] ‘class’ | ‘trait’) ClassDef
| [‘case’] ‘object’ ObjectDef
Expand Down

0 comments on commit 91f0501

Please sign in to comment.