Syntax Summary
The following descriptions of Scala tokens uses literal characters βcβ
when referring to the ASCII fragment \u0000
β \u007F
.
The nine Bidirectional explicit formatting
characters \u202a - \u202e
and \u2066 - \u2069
(inclusive) are forbidden
from appearing in source files. Note that they can be represented using
unicode escapes in string and character literals.
Lexical Syntax
The lexical syntax of Scala is given by the following grammar in EBNF form:
whiteSpace ::= β\u0020β | β\u0009β | β\u000Dβ | β\u000Aβ
upper ::= βAβ | β¦ | βZβ | β$β and any character in Unicode categories Lu, Lt or Nl,
and any character in Unicode categories Lo and Lm that doesn't have
contributory property Other_Lowercase
lower ::= βaβ | β¦ | βzβ | β_β and any character in Unicode category Ll,
and any character in Unicode categories Lo or Lm that has contributory
property Other_Lowercase
letter ::= upper | lower
digit ::= β0β | β¦ | β9β
paren ::= β(β | β)β | β[β | β]β | β{β | β}β
delim ::= β`β | β'β | β"β | β.β | β;β | β,β
opchar ::= β!β | β#β | β%β | β&β | β*β | β+β | β-β | β/β | β:β |
β<β | β=β | β>β | β?β | β@β | β\β | β^β | β|β | β~β
and any character in Unicode categories Sm or So
printableChar ::= all characters in [\u0020, \u007E] inclusive
UnicodeEscape ::= β\β βuβ {βuβ} hexDigit hexDigit hexDigit hexDigit
hexDigit ::= β0β | β¦ | β9β | βAβ | β¦ | βFβ | βaβ | β¦ | βfβ
binaryDigit ::= β0β | β1β
charEscapeSeq ::= β\β (βbβ | βtβ | βnβ | βfβ | βrβ | β"β | β'β | β\β)
escapeSeq ::= UnicodeEscape | charEscapeSeq
op ::= opchar {opchar}
varid ::= lower idrest
boundvarid ::= varid
| β`β varid β`β
plainid ::= upper idrest
| varid
| op
id ::= plainid
| β`β { charNoBackQuoteOrNewline | escapeSeq } β`β
idrest ::= {letter | digit} [β_β op]
integerLiteral ::= (decimalNumeral | hexNumeral) [βLβ | βlβ]
decimalNumeral ::= digit {digit}
hexNumeral ::= β0β (βxβ | βXβ) hexDigit {hexDigit}
binaryNumeral ::= β0β (βbβ | βBβ) binaryDigit {binaryDigit}
floatingPointLiteral
::= digit {digit} β.β digit {digit} [exponentPart] [floatType]
| β.β digit {digit} [exponentPart] [floatType]
| digit {digit} exponentPart [floatType]
| digit {digit} [exponentPart] floatType
exponentPart ::= (βEβ | βeβ) [β+β | β-β] digit {digit}
floatType ::= βFβ | βfβ | βDβ | βdβ
booleanLiteral ::= βtrueβ | βfalseβ
characterLiteral ::= β'β (charNoQuoteOrNewline | escapeSeq) β'β
stringLiteral ::= β"β {stringElement} β"β
| β"""β multiLineChars β"""β
stringElement ::= charNoDoubleQuoteOrNewline
| escapeSeq
multiLineChars ::= {[β"β] [β"β] charNoDoubleQuote} {β"β}
interpolatedString
::= alphaid β"β {[β\β] interpolatedStringPart | β\\β | β\"β} β"β
| alphaid β"""β {[β"β] [β"β] char \ (β"β | β\$β) | escape} {β"β} β"""β
interpolatedStringPart
::= printableChar \ (β"β | β$β | β\β) | escape
escape ::= β\$\$β
| β\$"β
| β\$β alphaid
| β\$β BlockExpr
alphaid ::= upper idrest
| varid
symbolLiteral ::= β'β plainid
comment ::= β/*β βany sequence of characters; nested comments are allowedβ β*/β
| β//β βany sequence of characters up to end of lineβ
nl ::= Β΄\mathit{βnew line characterβ}Β΄
semi ::= β;β | nl {nl}
Context-free Syntax
The context-free syntax of Scala is given by the following EBNF grammar:
Literal ::= [β-β] integerLiteral
| [β-β] floatingPointLiteral
| booleanLiteral
| characterLiteral
| stringLiteral
| interpolatedString
| symbolLiteral
| βnullβ
QualId ::= id {β.β id}
ids ::= id {β,β id}
Path ::= StableId
| [id β.β] βthisβ
StableId ::= id
| Path β.β id
| [id β.β] βsuperβ [ClassQualifier] β.β id
ClassQualifier ::= β[β id β]β
Type ::= FunctionArgTypes β=>β Type
| InfixType [ExistentialClause]
FunctionArgTypes ::= InfixType
| β(β [ ParamType {β,β ParamType } ] β)β
ExistentialClause ::= βforSomeβ β{β ExistentialDcl {semi ExistentialDcl} β}β
ExistentialDcl ::= βtypeβ TypeDcl
| βvalβ ValDcl
InfixType ::= CompoundType {id [nl] CompoundType}
CompoundType ::= AnnotType {βwithβ AnnotType} [Refinement]
| Refinement
AnnotType ::= SimpleType {Annotation}
SimpleType ::= SimpleType TypeArgs
| SimpleType β#β id
| StableId
| Path β.β βtypeβ
| β(β Types β)β
TypeArgs ::= β[β Types β]β
Types ::= Type {β,β Type}
Refinement ::= [nl] β{β RefineStat {semi RefineStat} β}β
RefineStat ::= Dcl
| βtypeβ TypeDef
|
TypePat ::= Type
Ascription ::= β:β InfixType
| β:β Annotation {Annotation}
| β:β β_β β*β
Expr ::= (Bindings | [βimplicitβ] (id | β_β)) β=>β Expr
| Expr1
Expr1 ::= βifβ β(β Expr β)β {nl} Expr [[semi] βelseβ Expr]
| βwhileβ β(β Expr β)β {nl} Expr
| βtryβ Expr [βcatchβ Expr] [βfinallyβ Expr]
| βdoβ Expr [semi] βwhileβ β(β Expr β)β
| βforβ (β(β Enumerators β)β | β{β Enumerators β}β) {nl} [βyieldβ] Expr
| βthrowβ Expr
| βreturnβ [Expr]
| [SimpleExpr β.β] id β=β Expr
| PrefixOperator SimpleExpr β=β Expr
| SimpleExpr1 ArgumentExprs β=β Expr
| PostfixExpr
| PostfixExpr Ascription
| PostfixExpr βmatchβ β{β CaseClauses β}β
PostfixExpr ::= InfixExpr [id [nl]]
InfixExpr ::= PrefixExpr
| InfixExpr id [nl] InfixExpr
PrefixExpr ::= [PrefixOperator] SimpleExpr
PrefixOperator ::= β-β | β+β | β~β | β!β
SimpleExpr ::= βnewβ (ClassTemplate | TemplateBody)
| BlockExpr
| SimpleExpr1 [β_β]
SimpleExpr1 ::= Literal
| Path
| β_β
| β(β [Exprs] β)β
| SimpleExpr β.β id
| SimpleExpr TypeArgs
| SimpleExpr1 ArgumentExprs
| XmlExpr
Exprs ::= Expr {β,β Expr}
ArgumentExprs ::= β(β [Exprs] β)β
| β(β [Exprs β,β] PostfixExpr β:β β_β β*β β)β
| [nl] BlockExpr
BlockExpr ::= β{β CaseClauses β}β
| β{β Block β}β
Block ::= BlockStat {semi BlockStat} [ResultExpr]
BlockStat ::= Import
| {Annotation} [βimplicitβ] [βlazyβ] Def
| {Annotation} {LocalModifier} TmplDef
| Expr1
|
ResultExpr ::= Expr1
| (Bindings | [βimplicitβ] (id | β_β) [β:β CompoundType]) β=>β Block
Enumerators ::= Generator {semi Generator}
Generator ::= [βcaseβ] Pattern1 β<-β Expr {[semi] Guard | semi Pattern1 β=β Expr}
CaseClauses ::= CaseClause { CaseClause }
CaseClause ::= βcaseβ Pattern [Guard] β=>β Block
Guard ::= βifβ PostfixExpr
Pattern ::= Pattern1 { β|β Pattern1 }
Pattern1 ::= boundvarid β:β TypePat
| β_β β:β TypePat
| Pattern2
Pattern2 ::= id [β@β Pattern3]
| Pattern3
Pattern3 ::= SimplePattern
| SimplePattern { id [nl] SimplePattern }
SimplePattern ::= β_β
| varid
| Literal
| StableId
| StableId β(β [Patterns] β)β
| StableId β(β [Patterns β,β] [id β@β] β_β β*β β)β
| β(β [Patterns] β)β
| XmlPattern
Patterns ::= Pattern [β,β Patterns]
| β_β β*β
TypeParamClause ::= β[β VariantTypeParam {β,β VariantTypeParam} β]β
FunTypeParamClause::= β[β TypeParam {β,β TypeParam} β]β
VariantTypeParam ::= {Annotation} [β+β | β-β] TypeParam
TypeParam ::= (id | β_β) [TypeParamClause] [β>:β Type] [β<:β Type]
{β<%β Type} {β:β Type}
ParamClauses ::= {ParamClause} [[nl] β(β βimplicitβ Params β)β]
ParamClause ::= [nl] β(β [Params] β)β
Params ::= Param {β,β Param}
Param ::= {Annotation} id [β:β ParamType] [β=β Expr]
ParamType ::= Type
| β=>β Type
| Type β*β
ClassParamClauses ::= {ClassParamClause}
[[nl] β(β βimplicitβ ClassParams β)β]
ClassParamClause ::= [nl] β(β [ClassParams] β)β
ClassParams ::= ClassParam {β,β ClassParam}
ClassParam ::= {Annotation} {Modifier} [(βvalβ | βvarβ)]
id β:β ParamType [β=β Expr]
Bindings ::= β(β Binding {β,β Binding} β)β
Binding ::= (id | β_β) [β:β Type]
Modifier ::= LocalModifier
| AccessModifier
| βoverrideβ
LocalModifier ::= βabstractβ
| βfinalβ
| βsealedβ
| βimplicitβ
| βlazyβ
AccessModifier ::= (βprivateβ | βprotectedβ) [AccessQualifier]
AccessQualifier ::= β[β (id | βthisβ) β]β
Annotation ::= β@β SimpleType {ArgumentExprs}
ConstrAnnotation ::= β@β SimpleType ArgumentExprs
TemplateBody ::= [nl] β{β [SelfType] TemplateStat {semi TemplateStat} β}β
TemplateStat ::= Import
| {Annotation [nl]} {Modifier} Def
| {Annotation [nl]} {Modifier} Dcl
| Expr
|
SelfType ::= id [β:β Type] β=>β
| βthisβ β:β Type β=>β
Import ::= βimportβ ImportExpr {β,β ImportExpr}
ImportExpr ::= StableId β.β (id | β_β | ImportSelectors)
ImportSelectors ::= β{β {ImportSelector β,β} (ImportSelector | β_β) β}β
ImportSelector ::= id [β=>β id | β=>β β_β]
Dcl ::= βvalβ ValDcl
| βvarβ VarDcl
| βdefβ FunDcl
| βtypeβ {nl} TypeDcl
ValDcl ::= ids β:β Type
VarDcl ::= ids β:β Type
FunDcl ::= FunSig [β:β Type]
FunSig ::= id [FunTypeParamClause] ParamClauses
TypeDcl ::= id [TypeParamClause] [β>:β Type] [β<:β Type]
PatVarDef ::= βvalβ PatDef
| βvarβ VarDef
Def ::= PatVarDef
| βdefβ FunDef
| βtypeβ {nl} TypeDef
| TmplDef
PatDef ::= Pattern2 {β,β Pattern2} [β:β Type] β=β Expr
VarDef ::= PatDef
| ids β:β Type β=β β_β
FunDef ::= FunSig [β:β Type] β=β Expr
| FunSig [nl] β{β Block β}β
| βthisβ ParamClause ParamClauses
(β=β ConstrExpr | [nl] ConstrBlock)
TypeDef ::= id [TypeParamClause] β=β Type
TmplDef ::= [βcaseβ] βclassβ ClassDef
| [βcaseβ] βobjectβ ObjectDef
| βtraitβ TraitDef
ClassDef ::= id [TypeParamClause] {ConstrAnnotation} [AccessModifier]
ClassParamClauses ClassTemplateOpt
TraitDef ::= id [TypeParamClause] TraitTemplateOpt
ObjectDef ::= id ClassTemplateOpt
ClassTemplateOpt ::= βextendsβ ClassTemplate | [[βextendsβ] TemplateBody]
TraitTemplateOpt ::= βextendsβ TraitTemplate | [[βextendsβ] TemplateBody]
ClassTemplate ::= [EarlyDefs] ClassParents [TemplateBody]
TraitTemplate ::= [EarlyDefs] TraitParents [TemplateBody]
ClassParents ::= Constr {βwithβ AnnotType}
TraitParents ::= AnnotType {βwithβ AnnotType}
Constr ::= AnnotType {ArgumentExprs}
EarlyDefs ::= β{β [EarlyDef {semi EarlyDef}] β}β βwithβ
EarlyDef ::= {Annotation [nl]} {Modifier} PatVarDef
ConstrExpr ::= SelfInvocation
| ConstrBlock
ConstrBlock ::= β{β SelfInvocation {semi BlockStat} β}β
SelfInvocation ::= βthisβ ArgumentExprs {ArgumentExprs}
TopStatSeq ::= TopStat {semi TopStat}
TopStat ::= {Annotation [nl]} {Modifier} TmplDef
| Import
| Packaging
| PackageObject
|
Packaging ::= βpackageβ QualId [nl] β{β TopStatSeq β}β
PackageObject ::= βpackageβ βobjectβ ObjectDef
CompilationUnit ::= {βpackageβ QualId semi} TopStatSeq