Skip to content

Commit c04e041

Browse files
committed
Allow toplevel definitions in syntax and parsing
1 parent a1ffafc commit c04e041

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ object Parsers {
506506
recur(top)
507507
}
508508

509-
/** operand { infixop operand | ‘with’ (operand | ParArgumentExprs) } [postfixop],
509+
/** operand { infixop operand | ‘given’ (operand | ParArgumentExprs) } [postfixop],
510510
*
511511
* respecting rules of associativity and precedence.
512512
* @param notAnOperator a token that does not count as operator.
@@ -1410,7 +1410,7 @@ object Parsers {
14101410
/** PostfixExpr ::= InfixExpr [id [nl]]
14111411
* InfixExpr ::= PrefixExpr
14121412
* | InfixExpr id [nl] InfixExpr
1413-
* | InfixExpr ‘with’ (InfixExpr | ParArgumentExprs)
1413+
* | InfixExpr ‘given’ (InfixExpr | ParArgumentExprs)
14141414
*/
14151415
def postfixExpr(): Tree =
14161416
infixOps(prefixExpr(), canStartExpressionTokens, prefixExpr, maybePostfix = true)
@@ -2017,7 +2017,7 @@ object Parsers {
20172017

20182018
/** ClsParamClause ::= [nl | ‘with’] `(' [FunArgMods] [ClsParams] ')'
20192019
* ClsParams ::= ClsParam {`' ClsParam}
2020-
* ClsParam ::= {Annotation} [{Modifier} (`val' | `var') | `inline'] Param
2020+
* ClsParam ::= {Annotation} [{ParamModifier} (`val' | `var') | `inline'] Param
20212021
* DefParamClause ::= [nl] `(' [FunArgMods] [DefParams] ')' | InferParamClause
20222022
* InferParamClause ::= ‘given’ (‘(’ DefParams ‘)’ | ContextTypes)
20232023
* ContextTypes ::= RefinedType {`,' RefinedType}
@@ -2675,7 +2675,7 @@ object Parsers {
26752675
}
26762676

26772677
/** TopStatSeq ::= TopStat {semi TopStat}
2678-
* TopStat ::= Annotations Modifiers TmplDef
2678+
* TopStat ::= Annotations Modifiers Def
26792679
* | Packaging
26802680
* | package object objectDef
26812681
* | Import
@@ -2695,15 +2695,13 @@ object Parsers {
26952695
}
26962696
else if (in.token == IMPORT)
26972697
stats ++= importClause()
2698-
else if (in.token == AT || isTemplateIntro || isModifier)
2699-
stats +++= tmplDef(in.offset, defAnnotsMods(modifierTokens))
2698+
else if (in.token == AT || isDefIntro(modifierTokens))
2699+
stats +++= defOrDcl(in.offset, defAnnotsMods(modifierTokens))
27002700
else if (!isStatSep) {
27012701
if (in.token == CASE)
27022702
syntaxErrorOrIncomplete(OnlyCaseClassOrCaseObjectAllowed())
27032703
else
27042704
syntaxErrorOrIncomplete(ExpectedClassOrObjectDef())
2705-
if (mustStartStat) // do parse all definitions even if they are probably local (i.e. a "}" has been forgotten)
2706-
defOrDcl(in.offset, defAnnotsMods(modifierTokens))
27072705
}
27082706
acceptStatSepUnlessAtEnd()
27092707
}

docs/docs/internals/syntax.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,10 +402,11 @@ EnumStat ::= TemplateStat
402402
EnumCase ::= ‘case’ (id ClassConstr [‘extends’ ConstrApps]] | ids)
403403
404404
TopStatSeq ::= TopStat {semi TopStat}
405-
TopStat ::= {Annotation [nl]} {Modifier} TmplDef
406-
| Import
405+
TopStat ::= Import
406+
| {Annotation [nl]} {Modifier} Def
407407
| Packaging
408408
| PackageObject
409+
|
409410
Packaging ::= ‘package’ QualId [nl] ‘{’ TopStatSeq ‘}’ Package(qid, stats)
410411
PackageObject ::= ‘package’ ‘object’ ObjectDef object with package in mods.
411412

0 commit comments

Comments
 (0)