Skip to content

Commit 326ba1b

Browse files
committed
Parser refactorings
1 parent 21aee87 commit 326ba1b

File tree

1 file changed

+66
-62
lines changed

1 file changed

+66
-62
lines changed

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

Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,8 +1360,8 @@ object Parsers {
13601360
*/
13611361
def typ(): Tree = {
13621362
val start = in.offset
1363-
var imods = Modifiers()
1364-
def functionRest(params: List[Tree]): Tree =
1363+
def functionRest(params: List[Tree], mods: Modifiers): Tree =
1364+
var imods = mods
13651365
atSpan(start, in.offset) {
13661366
if in.token == TLARROW then
13671367
if !imods.flags.isEmpty || params.isEmpty then
@@ -1389,61 +1389,11 @@ object Parsers {
13891389
Function(params, t)
13901390
}
13911391
}
1392-
def funArgTypesRest(first: Tree, following: () => Tree) = {
1393-
val buf = new ListBuffer[Tree] += first
1394-
while (in.token == COMMA) {
1395-
in.nextToken()
1396-
buf += following()
1397-
}
1398-
buf.toList
1399-
}
1400-
var isValParamList = false
14011392

14021393
val t =
1403-
if (in.token == LPAREN) {
1404-
in.nextToken()
1405-
if (in.token == RPAREN) {
1406-
in.nextToken()
1407-
functionRest(Nil)
1408-
}
1409-
else {
1410-
openParens.change(LPAREN, 1)
1411-
imods = modifiers(funTypeArgMods)
1412-
val paramStart = in.offset
1413-
val ts = funArgType() match {
1414-
case Ident(name) if name != tpnme.WILDCARD && in.token == COLON =>
1415-
isValParamList = true
1416-
funArgTypesRest(
1417-
typedFunParam(paramStart, name.toTermName, imods),
1418-
() => typedFunParam(in.offset, ident(), imods))
1419-
case t =>
1420-
funArgTypesRest(t, funArgType)
1421-
}
1422-
openParens.change(LPAREN, -1)
1423-
accept(RPAREN)
1424-
if isValParamList || in.token == ARROW || in.token == CTXARROW then
1425-
functionRest(ts)
1426-
else {
1427-
val ts1 =
1428-
for (t <- ts) yield
1429-
t match {
1430-
case t@ByNameTypeTree(t1) =>
1431-
syntaxError(ByNameParameterNotSupported(t), t.span)
1432-
t1
1433-
case _ =>
1434-
t
1435-
}
1436-
val tuple = atSpan(start) { makeTupleOrParens(ts1) }
1437-
infixTypeRest(
1438-
refinedTypeRest(
1439-
withTypeRest(
1440-
annotTypeRest(
1441-
simpleTypeRest(tuple)))))
1442-
}
1443-
}
1444-
}
1394+
if in.token == LPAREN then
1395+
typAfterParens(functionRest)
14451396
else if (in.token == LBRACKET) {
1446-
val start = in.offset
14471397
val tparams = typeParamClause(ParamOwner.TypeParam)
14481398
if (in.token == TLARROW)
14491399
atSpan(start, in.skipToken())(LambdaTypeTree(tparams, toplevelTyp()))
@@ -1465,16 +1415,64 @@ object Parsers {
14651415
else infixType()
14661416

14671417
in.token match {
1468-
case ARROW | CTXARROW => functionRest(t :: Nil)
1418+
case ARROW | CTXARROW => functionRest(t :: Nil, Modifiers())
14691419
case MATCH => matchType(t)
14701420
case FORSOME => syntaxError(ExistentialTypesNoLongerSupported()); t
14711421
case _ =>
1472-
if (imods.is(Erased) && !t.isInstanceOf[FunctionWithMods])
1473-
syntaxError(ErasedTypesCanOnlyBeFunctionTypes(), implicitKwPos(start))
1422+
//if (imods.is(Erased) && !t.isInstanceOf[FunctionWithMods])
1423+
// syntaxError(ErasedTypesCanOnlyBeFunctionTypes(), implicitKwPos(start))
14741424
t
14751425
}
14761426
}
14771427

1428+
def funArgTypesRest(first: Tree, following: () => Tree): List[Tree] =
1429+
val buf = new ListBuffer[Tree] += first
1430+
while in.token == COMMA do
1431+
in.nextToken()
1432+
buf += following()
1433+
buf.toList
1434+
1435+
def typAfterParens(parseRest: (List[Tree], Modifiers) => Tree) =
1436+
val start = in.offset
1437+
in.nextToken()
1438+
var imods = Modifiers()
1439+
var isValParamList = false
1440+
if in.token == RPAREN then
1441+
in.nextToken()
1442+
parseRest(Nil, imods)
1443+
else
1444+
openParens.change(LPAREN, 1)
1445+
imods = modifiers(funTypeArgMods)
1446+
val paramStart = in.offset
1447+
val ts = funArgType() match
1448+
case Ident(name) if name != tpnme.WILDCARD && in.token == COLON =>
1449+
isValParamList = true
1450+
funArgTypesRest(
1451+
typedFunParam(paramStart, name.toTermName, imods),
1452+
() => typedFunParam(in.offset, ident(), imods))
1453+
case t =>
1454+
funArgTypesRest(t, funArgType)
1455+
openParens.change(LPAREN, -1)
1456+
accept(RPAREN)
1457+
if isValParamList || in.token == ARROW || in.token == CTXARROW then
1458+
parseRest(ts, imods)
1459+
else
1460+
val ts1 =
1461+
for t <- ts yield
1462+
t match
1463+
case t@ByNameTypeTree(t1) =>
1464+
syntaxError(ByNameParameterNotSupported(t), t.span)
1465+
t1
1466+
case _ =>
1467+
t
1468+
val tuple = atSpan(start) { makeTupleOrParens(ts1) }
1469+
infixTypeRest(
1470+
refinedTypeRest(
1471+
withTypeRest(
1472+
annotTypeRest(
1473+
simpleTypeRest(tuple)))))
1474+
end typAfterParens
1475+
14781476
private def makeKindProjectorTypeDef(name: TypeName): TypeDef =
14791477
TypeDef(name, WildcardTypeBoundsTree()).withFlags(Param)
14801478

@@ -3607,24 +3605,30 @@ object Parsers {
36073605

36083606
/* -------- TEMPLATES ------------------------------------------- */
36093607

3608+
def constrOrSimpleType() =
3609+
rejectWildcardType(
3610+
annotTypeRest(simpleType1()),
3611+
fallbackTree = Ident(tpnme.ERROR))
3612+
// Using Ident(tpnme.ERROR) to avoid causing cascade errors on non-user-written code
3613+
36103614
/** ConstrApp ::= SimpleType1 {Annotation} {ParArgumentExprs}
36113615
*/
36123616
val constrApp: () => Tree = () =>
3613-
val t = rejectWildcardType(annotTypeRest(simpleType1()),
3614-
fallbackTree = Ident(tpnme.ERROR))
3615-
// Using Ident(tpnme.ERROR) to avoid causing cascade errors on non-user-written code
3617+
val t = constrOrSimpleType()
36163618
if in.token == LPAREN then parArgumentExprss(wrapNew(t)) else t
36173619

36183620
/** ConstrApps ::= ConstrApp {(‘,’ | ‘with’) ConstrApp}
36193621
*/
36203622
def constrApps(commaOK: Boolean): List[Tree] =
3621-
val t = constrApp()
3623+
constrAppsRest(constrApp(), commaOK)
3624+
3625+
def constrAppsRest(app: Tree, commaOK: Boolean): List[Tree] =
36223626
val ts =
36233627
if in.token == WITH || commaOK && in.token == COMMA then
36243628
in.nextToken()
36253629
constrApps(commaOK)
36263630
else Nil
3627-
t :: ts
3631+
app :: ts
36283632

36293633
/** Template ::= InheritClauses [TemplateBody]
36303634
* InheritClauses ::= [‘extends’ ConstrApps] [‘derives’ QualId {‘,’ QualId}]

0 commit comments

Comments
 (0)