title | layout | chapter |
---|---|---|
Syntax Summary | default | 13 |
The following descriptions 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’ | ‘$’ andanycharacterinUnicodecategoriesLu, LtorNl, andanycharacterinUnicodecategoriesLoandLmthatdoesn't have contributory property Other_Lowercaselower::= ‘a’ | ... | ‘z’ | ‘_’ andanycharacterinUnicodecategoryLl, andanycharacterinUnicodecategoriesLoorLmthathascontributorypropertyOther_Lowercaseletter::=upper|lowerdigit::= ‘0’ | ... | ‘9’ paren::= ‘(’ | ‘)’ | ‘[’ | ‘]’ | ‘{’ | ‘}’ delim::= ‘`’ | ‘'’| ‘"’| ‘.’ | ‘;’ | ‘,’opchar::= ‘!’ | ‘#’ | ‘%’ | ‘&’ | ‘*’ | ‘+’ | ‘-’ | ‘/’ | ‘:’ | ‘<’ | ‘=’ | ‘>’ | ‘?’ | ‘@’ | ‘\’ | ‘^’ | ‘|’ | ‘~’ andanycharacterinUnicodecategoriesSmorSoprintableChar::=allcharactersin [\u0020, \u007E] inclusiveUnicodeEscape::= ‘\’ ‘u’ {‘u’} hexDigithexDigithexDigithexDigithexDigit::= ‘0’ | ... | ‘9’ | ‘A’ | ... | ‘F’ | ‘a’ | ... | ‘f’ charEscapeSeq::= ‘\’ (‘b’ | ‘t’ | ‘n’ | ‘f’ | ‘r’ | ‘"’| ‘'’| ‘\’) escapeSeq::=UnicodeEscape|charEscapeSeqop::=opchar {opchar} varid::=loweridrestboundvarid::=varid| ‘`’ varid ‘`’ alphaid::=upperidrest|varidplainid::=alphaid|opid::=plainid| ‘`’ { charNoBackQuoteOrNewline|escapeSeq } ‘`’ idrest::= {letter|digit} [‘_’ op] quoteId::= ‘'’ alphaidspliceId::= ‘$’ 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] |decimalNumeralexponentPart [floatType] |decimalNumeralfloatTypeexponentPart::= (‘E’ | ‘e’) [‘+’ | ‘-’] digit [{digit| ‘_’} digit] floatType::= ‘F’ | ‘f’ | ‘D’ | ‘d’ booleanLiteral::= ‘true’ | ‘false’ characterLiteral::= ‘'’ (charNoQuoteOrNewline|escapeSeq) ‘'’stringLiteral::= ‘"’ {stringElement} ‘"’ | ‘"""’ multiLineChars ‘"""’ stringElement::=charNoDoubleQuoteOrNewline|escapeSeqmultiLineChars::= {[‘"’] [‘"’] charNoDoubleQuote} {‘"’}interpolatedString ::= alphaid ‘"’ {[‘\’] interpolatedStringPart| ‘\\’ | ‘\"’} ‘"’ |alphaid ‘"""’ {[‘"’] [‘"’] char \ (‘"’| ‘\$’) |escape} {‘"’} ‘"""’ interpolatedStringPart::=printableChar \ (‘"’| ‘$’ | ‘\’) |escapeescape::= ‘\$\$’ | ‘\$"’| ‘\$’ alphaid| ‘\$’ BlockExprcomment::= ‘/*’ “anysequenceofcharacters; nestedcommentsareallowed” ‘*/’ | ‘//’ “any sequence of characters up to end of line”nl::= ´\mathit{“newlinecharacter”}´ semi::= ‘;’ | nl {nl}
colon ::= ':' -- with side conditions explained in 01-literal-syntax.md <<< ts >>> ::= ‘{’ ts ‘}’ | indent ts outdent :<<< ts >>> ::= [nl] ‘{’ ts ‘}’ | colon indent ts outdent
´\color{red}{\text{TODO SCALA3: Once we're done porting the spec, make sure that the references to grammar productions in the rest of the spec match this.}}´
The context-free syntax of Scala is given by the following EBNF grammar:
RefineDcl::= ‘val’ ValDcl| ‘def’ DefDcl| ‘type’ {nl} TypeDclDcl::=RefineDcl| ‘var’ VarDclValDcl::=ids ‘:’ TypeVarDcl::=ids ‘:’ TypeDefDcl::=DefSig ‘:’ TypeDefSig::=id [DefTypeParamClause] [TypelessClauses] [DefImplicitClause] TypeDcl::=id [TypeParamClause] {FunParamClause} TypeBoundsDef::= ‘val’ PatDef| ‘var’ PatDef| ‘def’ DefDef| ‘type’ {nl} TypeDcl|TmplDefPatDef::=ids [‘:’ Type] ‘=’ Expr|Pattern2 [‘:’ Type] ‘=’ ExprDefDef::=DefSig [‘:’ Type] ‘=’ Expr| ‘this’ TypelessClauses [DefImplicitClause] ‘=’ ConstrExprTmplDef::= ([‘case’] ‘class’ | ‘trait’) ClassDef| [‘case’] ‘object’ ObjectDef| ‘enum’ EnumDef| ‘given’ GivenDefClassDef::=idClassConstr [Template] ClassConstr::= [ClsTypeParamClause] [ConstrMods] ClsParamClausesConstrMods::= {Annotation} [AccessModifier] ObjectDef::=id [Template] EnumDef::=idClassConstrInheritClausesEnumBodyGivenDef::= [GivenSig] (AnnotType [‘=’ Expr] |StructuralInstance) GivenSig::= [id] [DefTypeParamClause] {UsingParamClause} ‘:’ --oneof `id`, `DefTypeParamClause`, `UsingParamClause` mustbepresentStructuralInstance::=ConstrApp {‘with’ ConstrApp} [‘with’ WithTemplateBody] Extension::= ‘extension’ [DefTypeParamClause] {UsingParamClause} ‘(’ DefTermParam ‘)’ {UsingParamClause} ExtMethodsExtMethods::=ExtMethod| [nl] <<< ExtMethod {semiExtMethod} >>> ExtMethod::= {Annotation [nl]} {Modifier} ‘def’ DefDef|ExportTemplate::=InheritClauses [TemplateBody] InheritClauses::= [‘extends’ ConstrApps] [‘derives’ QualId {‘,’ QualId}] ConstrApps::=ConstrApp ({‘,’ ConstrApp} | {‘with’ ConstrApp}) ConstrApp::=SimpleType1 {Annotation} {ParArgumentExprs} ConstrExpr::=SelfInvocation| <<< SelfInvocation {semiBlockStat} >>> SelfInvocation::= ‘this’ ArgumentExprs {ArgumentExprs} WithTemplateBody::= <<< [SelfType] TemplateStat {semiTemplateStat} >>> TemplateBody::= :<<< [SelfType] TemplateStat {semiTemplateStat} >>> TemplateStat::=Import|Export| {Annotation [nl]} {Modifier} Def| {Annotation [nl]} {Modifier} Dcl|Extension|Expr1|EndMarker|SelfType::=id [‘:’ InfixType] ‘=>’ | ‘this’ ‘:’ InfixType ‘=>’ EnumBody::= :<<< [SelfType] EnumStat {semiEnumStat} >>> EnumStat::=TemplateStat| {Annotation [nl]} {Modifier} EnumCaseEnumCase::= ‘case’ (idClassConstr [‘extends’ ConstrApps]] |ids) TopStats::=TopStat {semiTopStat} TopStat::=Import|Export| {Annotation [nl]} {Modifier} Def|Extension|Packaging|PackageObject|EndMarker|Packaging::= ‘package’ QualId :<<< TopStats >>> PackageObject::= ‘package’ ‘object’ ObjectDefCompilationUnit::= {‘package’ QualIdsemi} TopStats