Skip to content

Latest commit

 

History

History
186 lines (159 loc) · 8.51 KB

13-syntax-summary.md

File metadata and controls

186 lines (159 loc) · 8.51 KB
titlelayoutchapter
Syntax Summary
default
13

Syntax Summary

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”.

Lexical Syntax

The lexical syntax of Scala is given by the following grammar in EBNF form:

whiteSpace::= ‘\u0020| ‘\u0009| ‘\u000D| ‘\u000Aupper::=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| ... |fcharEscapeSeq::= ‘\’ (‘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|dbooleanLiteral::=true|falsecharacterLiteral::='’ (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}

Optional Braces

colon ::= ':' -- with side conditions explained in 01-literal-syntax.md <<< ts >>> ::= ‘{’ ts ‘}’ | indent ts outdent :<<< ts >>> ::= [nl] ‘{’ ts ‘}’ | colon indent ts outdent 

Context-free Syntax

´\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::=valValDcl|defDefDcl|type’ {nl} TypeDclDcl::=RefineDcl|varVarDclValDcl::=ids ‘:’ TypeVarDcl::=ids ‘:’ TypeDefDcl::=DefSig ‘:’ TypeDefSig::=id [DefTypeParamClause] [TypelessClauses] [DefImplicitClause] TypeDcl::=id [TypeParamClause] {FunParamClause} TypeBoundsDef::=valPatDef|varPatDef|defDefDef|type’ {nl} TypeDcl|TmplDefPatDef::=ids [‘:’ Type] ‘=’ Expr|Pattern2 [‘:’ Type] ‘=’ ExprDefDef::=DefSig [‘:’ Type] ‘=’ Expr|thisTypelessClauses [DefImplicitClause] ‘=’ ConstrExprTmplDef::= ([‘case’] ‘class|trait’) ClassDef| [‘case’] ‘objectObjectDef|enumEnumDef|givenGivenDefClassDef::=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 {‘withConstrApp} [‘withWithTemplateBody] Extension::=extension’ [DefTypeParamClause] {UsingParamClause} ‘(’ DefTermParam ‘)’ {UsingParamClause} ExtMethodsExtMethods::=ExtMethod| [nl] <<< ExtMethod {semiExtMethod} >>> ExtMethod::= {Annotation [nl]} {Modifier} ‘defDefDef|ExportTemplate::=InheritClauses [TemplateBody] InheritClauses::= [‘extendsConstrApps] [‘derivesQualId {‘,’ QualId}] ConstrApps::=ConstrApp ({‘,’ ConstrApp} | {‘withConstrApp}) ConstrApp::=SimpleType1 {Annotation} {ParArgumentExprs} ConstrExpr::=SelfInvocation| <<< SelfInvocation {semiBlockStat} >>> SelfInvocation::=thisArgumentExprs {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 [‘extendsConstrApps]] |ids) TopStats::=TopStat {semiTopStat} TopStat::=Import|Export| {Annotation [nl]} {Modifier} Def|Extension|Packaging|PackageObject|EndMarker|Packaging::=packageQualId :<<< TopStats >>> PackageObject::=package’ ‘objectObjectDefCompilationUnit::= {‘packageQualIdsemi} TopStats
close