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â
.
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â
charEscapeSeq ::= â\â (âbâ | âtâ | ânâ | âfâ | ârâ | â"â | â'â | â\â)
escapeSeq ::= UnicodeEscape | charEscapeSeq
op ::= opchar {opchar}
varid ::= lower idrest
boundvarid ::= varid
| â`â varid â`â
plainid ::= alphaid
| op
id ::= plainid
| â`â { charNoBackQuoteOrNewline | escapeSeq } â`â
idrest ::= {letter | digit} [â_â op]
quoteId ::= â'â alphaid
spliceId ::= â$â alphaid ;
integerLiteral ::= (decimalNumeral | hexNumeral | binaryNumeral) [âLâ | âlâ]
decimalNumeral ::= â0â | digit [{digit | â_â} digit]
hexNumeral ::= â0â (âxâ | âXâ) hexDigit [{hexDigit | â_â} hexDigit]
binaryNumeral ::= â0â (âbâ | âBâ) binaryDigit [{binaryDigit | â_â} binaryDigit]
floatingPointLiteral
::= [decimalNumeral] â.â digit [{digit | â_â} digit] [exponentPart] [floatType]
| decimalNumeral exponentPart [floatType]
| decimalNumeral floatType
exponentPart ::= (âEâ | âeâ) [â+â | â-â] digit [{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
comment ::= â/*â âany sequence of characters; nested comments are allowedâ â*/â
| â//â âany sequence of characters up to end of lineâ
nl ::= ânew line characterâ
semi ::= â;â | nl {nl}
Optional Braces
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.
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 notation :<<< ts >>>
indicates a token sequence ts
that is either enclosed in a pair of braces { ts }
or that constitutes an indented region indent ts outdent
that follows a colon
token.
A colon
token reads as the standard colon ":
" but is generated instead of it where colon
is legal according to the context free syntax, but only if the previous token is an alphanumeric identifier, a backticked identifier, or one of the tokens this
, super
, new
, ")
", and "]
".
colon ::= ':' -- with side conditions explained above
<<< ts >>> ::= â{â ts â}â
| indent ts outdent
:<<< ts >>> ::= [nl] â{â ts â}â
| colon indent ts outdent
Keywords Regular keywords
abstract case catch class def do else
enum export extends false final finally for
given if implicit import lazy match new
null object override package private protected return
sealed super then throw trait true try
type val var while with yield
: = <- => <: >: #
@ =>> ?=>
Soft keywords
as derives end extension infix inline opaque open transparent using | * + -
See the separate section on soft keywords for additional details on where a soft keyword is recognized.
Context-free SyntaxThe context-free syntax of Scala is given by the following EBNF grammar:
Literals and PathsSimpleLiteral ::= [â-â] integerLiteral
| [â-â] floatingPointLiteral
| booleanLiteral
| characterLiteral
| stringLiteral
Literal ::= SimpleLiteral
| interpolatedStringLiteral
| symbolLiteral
| ânullâ
QualId ::= id {â.â id}
ids ::= id {â,â id}
SimpleRef ::= id
| [id â.â] âthisâ
| [id â.â] âsuperâ [ClassQualifier] â.â id
ClassQualifier ::= â[â id â]â
Types
Type ::= FunType
| TypTypeParamClause â=>>â Type
| MatchType
| InfixType
FunType ::= FunTypeArgs (â=>â | â?=>â) Type
| TypTypeParamClause '=>' Type
FunTypeArgs ::= InfixType
| â(â [ FunArgTypes ] â)â
| FunParamClause
FunParamClause ::= â(â TypedFunParam {â,â TypedFunParam } â)â
TypedFunParam ::= id â:â Type
MatchType ::= InfixType `match` <<< TypeCaseClauses >>>
InfixType ::= RefinedType {id [nl] RefinedType}
RefinedType ::= AnnotType {[nl] Refinement}
AnnotType ::= SimpleType {Annotation}
SimpleType ::= SimpleLiteral
| â?â TypeBounds
| id
| Singleton â.â id
| Singleton â.â âtypeâ
| â(â [Types] â)â
| Refinement
| SimpleType TypeArgs
| SimpleType â#â id
Singleton ::= SimpleRef
| SimpleLiteral
| Singleton â.â id
FunArgType ::= Type
| â=>â Type
FunArgTypes ::= FunArgType { â,â FunArgType }
ParamType ::= [â=>â] ParamValueType
ParamValueType ::= Type [â*â]
TypeArgs ::= â[â Types â]â
Refinement ::= :<<< [RefineDcl] {semi [RefineDcl]} >>>
TypeBounds ::= [â>:â Type] [â<:â Type]
TypeAndCtxBounds ::= TypeBounds [':' ContextBounds]
ContextBounds ::= ContextBound
| ContextBound ':' ContextBounds -- to be deprecated
| '{' ContextBound {',' ContextBound} '}'
ContextBound ::= Type ['as' id]
Types ::= Type {â,â Type}
Expressions
Expr ::= FunParams (â=>â | â?=>â) Expr
| TypTypeParamClause â=>â Expr
| Expr1
BlockResult ::= FunParams (â=>â | â?=>â) Block
| TypTypeParamClause â=>â Block
| Expr1
FunParams ::= Bindings
| id
| â_â
Expr1 ::= [âinlineâ] âifâ â(â Expr â)â {nl} Expr [[semi] âelseâ Expr]
| [âinlineâ] âifâ Expr âthenâ Expr [[semi] âelseâ Expr]
| âwhileâ â(â Expr â)â {nl} Expr
| âwhileâ Expr âdoâ Expr
| âtryâ Expr Catches [âfinallyâ Expr]
| âtryâ Expr [âfinallyâ Expr]
| âthrowâ Expr
| âreturnâ [Expr]
| ForExpr
| [SimpleExpr â.â] id â=â Expr
| PrefixOperator SimpleExpr â=â Expr
| SimpleExpr ArgumentExprs â=â Expr
| PostfixExpr [Ascription]
| âinlineâ InfixExpr MatchClause
Ascription ::= â:â InfixType
| â:â Annotation {Annotation}
Catches ::= âcatchâ (Expr | ExprCaseClause)
PostfixExpr ::= InfixExpr [id] -- only if language.postfixOperators is enabled
InfixExpr ::= PrefixExpr
| InfixExpr id [nl] InfixExpr
| InfixExpr id ColonArgument
| InfixExpr MatchClause
MatchClause ::= âmatchâ <<< CaseClauses >>>
PrefixExpr ::= [PrefixOperator] SimpleExpr
PrefixOperator ::= â-â | â+â | â~â | â!â -- unless backquoted
SimpleExpr ::= SimpleRef
| Literal
| â_â
| BlockExpr
| ExprSplice
| Quoted
| quoteId -- only inside splices
| ânewâ ConstrApp {âwithâ ConstrApp} [TemplateBody]
| ânewâ TemplateBody
| â(â [ExprsInParens] â)â
| SimpleExpr â.â id
| SimpleExpr â.â MatchClause
| SimpleExpr TypeArgs
| SimpleExpr ArgumentExprs
| SimpleExpr ColonArgument
ColonArgument ::= colon [LambdaStart]
indent (CaseClauses | Block) outdent
LambdaStart ::= FunParams (â=>â | â?=>â)
| TypTypeParamClause â=>â
Quoted ::= â'â â{â Block â}â
| â'â â[â TypeBlock â]â
ExprSplice ::= spliceId -- if inside quoted block
| â$â â{â Block â}â -- unless inside quoted pattern
| â$â â{â Pattern â}â -- when inside quoted pattern
ExprsInParens ::= ExprInParens {â,â ExprInParens}
ExprInParens ::= PostfixExpr â:â Type | Expr
ParArgumentExprs ::= â(â [ExprsInParens] â)â
| â(â âusingâ ExprsInParens â)â
| â(â [ExprsInParens â,â] PostfixExpr â*â â)â
ArgumentExprs ::= ParArgumentExprs
| BlockExpr
BlockExpr ::= <<< (CaseClauses | Block) >>>
Block ::= {BlockStat semi} [BlockResult]
BlockStat ::= Import
| {Annotation {nl}} {LocalModifier} Def
| Extension
| Expr1
| EndMarker
TypeBlock ::= {TypeBlockStat semi} Type
TypeBlockStat ::= âtypeâ {nl} TypeDef
ForExpr ::= âforâ â(â Enumerators0 â)â {nl} [âdoâ | âyieldâ] Expr
| âforâ â{â Enumerators0 â}â {nl} [âdoâ | âyieldâ] Expr
| âforâ Enumerators0 (âdoâ | âyieldâ) Expr
Enumerators0 ::= {nl} Enumerators [semi]
Enumerators ::= Generator {semi Enumerator | Guard}
Enumerator ::= Generator
| Guard {Guard}
| Pattern1 â=â Expr
Generator ::= [âcaseâ] Pattern1 â<-â Expr
Guard ::= âifâ PostfixExpr
CaseClauses ::= CaseClause { CaseClause }
CaseClause ::= âcaseâ Pattern [Guard] â=>â Block
ExprCaseClause ::= âcaseâ Pattern [Guard] â=>â Expr
TypeCaseClauses ::= TypeCaseClause { TypeCaseClause }
TypeCaseClause ::= âcaseâ (InfixType | â_â) â=>â Type [semi]
Pattern ::= Pattern1 { â|â Pattern1 }
Pattern1 ::= PatVar â:â RefinedType
| [â-â] integerLiteral â:â RefinedType
| [â-â] floatingPointLiteral â:â RefinedType
| Pattern2
Pattern2 ::= [id â@â] InfixPattern
InfixPattern ::= SimplePattern { id [nl] SimplePattern }
SimplePattern ::= PatVar
| Literal
| â(â [Patterns] â)â
| Quoted
| SimplePattern1 [TypeArgs] [ArgumentPatterns]
| âgivenâ RefinedType
SimplePattern1 ::= SimpleRef
| SimplePattern1 â.â id
PatVar ::= varid
| â_â
Patterns ::= Pattern {â,â Pattern}
ArgumentPatterns ::= â(â [Patterns] â)â
| â(â [Patterns â,â] PatVar â*â â)â
Type and Value Parameters
ClsTypeParamClause::= â[â ClsTypeParam {â,â ClsTypeParam} â]â
ClsTypeParam ::= {Annotation} [â+â | â-â] id [HkTypeParamClause] TypeAndCtxBounds
DefTypeParamClause::= [nl] â[â DefTypeParam {â,â DefTypeParam} â]â
DefTypeParam ::= {Annotation} id [HkTypeParamClause] TypeAndCtxBounds
TypTypeParamClause::= â[â TypTypeParam {â,â TypTypeParam} â]â
TypTypeParam ::= {Annotation} (id | â_â) [HkTypeParamClause] TypeBounds
HkTypeParamClause ::= â[â HkTypeParam {â,â HkTypeParam} â]â
HkTypeParam ::= {Annotation} [â+â | â-â] (id | â_â) [HkTypeParamClause] TypeBounds
ClsParamClauses ::= {ClsParamClause} [[nl] â(â [âimplicitâ] ClsParams â)â]
ClsParamClause ::= [nl] â(â ClsParams â)â
| [nl] â(â âusingâ (ClsParams | FunArgTypes) â)â
ClsParams ::= ClsParam {â,â ClsParam}
ClsParam ::= {Annotation} [{Modifier} (âvalâ | âvarâ)] Param
DefParamClauses ::= DefParamClause { DefParamClause } -- and two DefTypeParamClause cannot be adjacent
DefParamClause ::= DefTypeParamClause
| DefTermParamClause
| UsingParamClause
ConstrParamClauses::= ConstrParamClause {ConstrParamClause}
ConstrParamClause ::= DefTermParamClause
| UsingParamClause
DefTermParamClause::= [nl] â(â [DefTermParams] â)â
UsingParamClause ::= [nl] â(â âusingâ (DefTermParams | FunArgTypes) â)â
DefImplicitClause ::= [nl] â(â âimplicitâ DefTermParams â)â
DefTermParams ::= DefTermParam {â,â DefTermParam}
DefTermParam ::= {Annotation} [âinlineâ] Param
Param ::= id â:â ParamType [â=â Expr]
Bindings and Imports
Bindings ::= â(â [Binding {â,â Binding}] â)â
Binding ::= (id | â_â) [â:â Type]
Modifier ::= LocalModifier
| AccessModifier
| âoverrideâ
| âopaqueâ
LocalModifier ::= âabstractâ
| âfinalâ
| âsealedâ
| âopenâ
| âimplicitâ
| âlazyâ
| âinlineâ
| âtransparentâ
| âinfixâ
AccessModifier ::= (âprivateâ | âprotectedâ) [AccessQualifier]
AccessQualifier ::= â[â id â]â
Annotation ::= â@â SimpleType {ParArgumentExprs}
Import ::= âimportâ ImportExpr {â,â ImportExpr}
Export ::= âexportâ ImportExpr {â,â ImportExpr}
ImportExpr ::= SimpleRef {â.â id} â.â ImportSpec
| SimpleRef âasâ id
ImportSpec ::= NamedSelector
| WildCardSelector
| â{â ImportSelectors) â}â
NamedSelector ::= id [âasâ (id | â_â)]
WildCardSelector ::= â*â | âgivenâ [InfixType]
ImportSelectors ::= NamedSelector [â,â ImportSelectors]
| WildCardSelector {â,â WildCardSelector}
EndMarker ::= âendâ EndMarkerTag -- when followed by EOL
EndMarkerTag ::= id | âifâ | âwhileâ | âforâ | âmatchâ | âtryâ
| ânewâ | âthisâ | âgivenâ | âextensionâ | âvalâ
Declarations and Definitions
RefineDcl ::= âvalâ ValDcl
| âdefâ DefDcl
| âvarâ ValDcl
| âtypeâ {nl} TypeDef
ValDcl ::= ids â:â Type
DefDcl ::= DefSig â:â Type
Def ::= âvalâ PatDef
| âvarâ PatDef
| âdefâ DefDef
| âtypeâ {nl} TypeDef
| TmplDef
PatDef ::= ids [â:â Type] [â=â Expr]
| Pattern2 [â:â Type] [â=â Expr] PatDef(_, pats, tpe?, expr)
DefDef ::= DefSig [â:â Type] [â=â Expr] DefDef(_, name, paramss, tpe, expr)
| âthisâ ConstrParamClauses [DefImplicitClause] â=â ConstrExpr DefDef(_, <init>, vparamss, EmptyTree, expr | Block)
DefSig ::= id [DefParamClauses] [DefImplicitClause]
TypeDef ::= id [HkTypeParamClause] {FunParamClause}TypeBounds TypeDefTree(_, name, tparams, bound
[â=â Type]
TmplDef ::= ([âcaseâ] âclassâ | âtraitâ) ClassDef
| [âcaseâ] âobjectâ ObjectDef
| âenumâ EnumDef
| 'given' (GivenDef | OldGivenDef)
ClassDef ::= id ClassConstr [Template]
ClassConstr ::= [ClsTypeParamClause] [ConstrMods] ClsParamClauses
ConstrMods ::= {Annotation} [AccessModifier]
ObjectDef ::= id [Template]
EnumDef ::= id ClassConstr InheritClauses EnumBody
GivenDef ::= [id ':'] GivenSig
GivenSig ::= GivenImpl
| '(' ')' '=>' GivenImpl
| GivenConditional '=>' GivenSig
GivenImpl ::= GivenType ([â=â Expr] | TemplateBody)
| ConstrApps TemplateBody
GivenConditional ::= DefTypeParamClause
| DefTermParamClause
| '(' FunArgTypes ')'
| GivenType
GivenType ::= AnnotType1 {id [nl] AnnotType1}
OldGivenDef ::= [OldGivenSig] (AnnotType [â=â Expr] | StructuralInstance) -- syntax up to Scala 3.5, to be deprecated in the future
OldGivenSig ::= [id] [DefTypeParamClause] {UsingParamClause} â:â -- one of `id`, `DefTypeParamClause`, `UsingParamClause` must be present
StructuralInstance ::= ConstrApp {âwithâ ConstrApp} [âwithâ WithTemplateBody]
Extension ::= âextensionâ [DefTypeParamClause] {UsingParamClause}
â(â DefTermParam â)â {UsingParamClause} ExtMethods
ExtMethods ::= ExtMethod | [nl] <<< ExtMethod {semi ExtMethod} >>>
ExtMethod ::= {Annotation [nl]} {Modifier} âdefâ DefDef
| Export
Template ::= InheritClauses [TemplateBody]
InheritClauses ::= [âextendsâ ConstrApps] [âderivesâ QualId {â,â QualId}]
ConstrApps ::= ConstrApp ({â,â ConstrApp} | {âwithâ ConstrApp})
ConstrApp ::= SimpleType {Annotation} {ParArgumentExprs}
ConstrExpr ::= SelfInvocation
| <<< SelfInvocation {semi BlockStat} >>>
SelfInvocation ::= âthisâ ArgumentExprs {ArgumentExprs}
WithTemplateBody ::= <<< [SelfType] TemplateStat {semi TemplateStat} >>>
TemplateBody ::= :<<< [SelfType] TemplateStat {semi TemplateStat} >>>
TemplateStat ::= Import
| Export
| {Annotation [nl]} {Modifier} Def
| Extension
| Expr1
| EndMarker
|
SelfType ::= id [â:â InfixType] â=>â
| âthisâ â:â InfixType â=>â
EnumBody ::= :<<< [SelfType] EnumStat {semi EnumStat} >>>
EnumStat ::= TemplateStat
| {Annotation [nl]} {Modifier} EnumCase
EnumCase ::= âcaseâ (id ClassConstr [âextendsâ ConstrApps]] | ids)
TopStats ::= TopStat {semi TopStat}
TopStat ::= Import
| Export
| {Annotation [nl]} {Modifier} Def
| Extension
| Packaging
| PackageObject
| EndMarker
|
Packaging ::= âpackageâ QualId :<<< TopStats >>>
PackageObject ::= âpackageâ âobjectâ ObjectDef
CompilationUnit ::= {âpackageâ QualId semi} TopStats
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4