Skip to content

Commit 667e17f

Browse files
committed
Update Parsers.scala to accomodate new syntax
Interweaved methods still fail at use-cite, see next commit
1 parent bf9bf1c commit 667e17f

File tree

1 file changed

+69
-11
lines changed

1 file changed

+69
-11
lines changed

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

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2904,6 +2904,61 @@ object Parsers {
29042904

29052905
/* -------- PARAMETERS ------------------------------------------- */
29062906

2907+
/** DefParamClause ::= DefTypeParamClause
2908+
* | DefTermParamClause
2909+
* | UsingParamClause
2910+
*/
2911+
def typeOrTermParamClause(nparams: Int, // number of parameters preceding this clause
2912+
ofClass: Boolean = false, // owner is a class
2913+
ofCaseClass: Boolean = false, // owner is a case class
2914+
prefix: Boolean = false, // clause precedes name of an extension method
2915+
givenOnly: Boolean = false, // only given parameters allowed
2916+
firstClause: Boolean = false, // clause is the first in regular list of clauses
2917+
ownerKind: ParamOwner.Value
2918+
): List[TypeDef] | List[ValDef] =
2919+
if (in.token == LPAREN)
2920+
paramClause(nparams, ofClass, ofCaseClass, prefix, givenOnly, firstClause)
2921+
else if (in.token == LBRACKET)
2922+
typeParamClause(ownerKind)
2923+
else
2924+
Nil
2925+
2926+
end typeOrTermParamClause
2927+
2928+
/** DefParamClauses ::= DefParamClause { DefParamClause }
2929+
*/
2930+
def typeOrTermParamClauses(
2931+
ownerKind: ParamOwner.Value,
2932+
ofClass: Boolean = false,
2933+
ofCaseClass: Boolean = false,
2934+
givenOnly: Boolean = false,
2935+
numLeadParams: Int = 0
2936+
): List[List[TypeDef] | List[ValDef]] =
2937+
2938+
def recur(firstClause: Boolean, nparams: Int): List[List[TypeDef] | List[ValDef]] =
2939+
newLineOptWhenFollowedBy(LPAREN)
2940+
newLineOptWhenFollowedBy(LBRACKET)
2941+
if in.token == LPAREN then
2942+
val paramsStart = in.offset
2943+
val params = paramClause(
2944+
nparams,
2945+
ofClass = ofClass,
2946+
ofCaseClass = ofCaseClass,
2947+
givenOnly = givenOnly,
2948+
firstClause = firstClause)
2949+
val lastClause = params.nonEmpty && params.head.mods.flags.is(Implicit)
2950+
params :: (
2951+
if lastClause then Nil
2952+
else recur(firstClause = false, nparams + params.length))
2953+
else if in.token == LBRACKET then
2954+
typeParamClause(ownerKind) :: recur(firstClause, nparams)
2955+
else Nil
2956+
end recur
2957+
2958+
recur(firstClause = true, nparams = numLeadParams)
2959+
end typeOrTermParamClauses
2960+
2961+
29072962
/** ClsTypeParamClause::= ‘[’ ClsTypeParam {‘,’ ClsTypeParam} ‘]’
29082963
* ClsTypeParam ::= {Annotation} [‘+’ | ‘-’]
29092964
* id [HkTypeParamClause] TypeParamBounds
@@ -2968,11 +3023,15 @@ object Parsers {
29683023
* UsingClsParamClause::= ‘(’ ‘using’ [‘erased’] (ClsParams | ContextTypes) ‘)’
29693024
* ClsParams ::= ClsParam {‘,’ ClsParam}
29703025
* ClsParam ::= {Annotation}
3026+
*
3027+
* TypelessClause ::= DefTermParamClause
3028+
* | UsingParamClause
29713029
*
2972-
* DefParamClause ::= ‘(’ [‘erased’] DefParams ‘)’ | UsingParamClause
2973-
* UsingParamClause ::= ‘(’ ‘using’ [‘erased’] (DefParams | ContextTypes) ‘)’
2974-
* DefParams ::= DefParam {‘,’ DefParam}
2975-
* DefParam ::= {Annotation} [‘inline’] Param
3030+
* DefTermParamClause::= [nl] ‘(’ [DefTermParams] ‘)’
3031+
* UsingParamClause ::= ‘(’ ‘using’ [‘erased’] (DefTermParams | ContextTypes) ‘)’
3032+
* DefImplicitClause ::= [nl] ‘(’ ‘implicit’ DefTermParams ‘)’
3033+
* DefTermParams ::= DefTermParam {‘,’ DefTermParam}
3034+
* DefTermParam ::= {Annotation} [‘inline’] Param
29763035
*
29773036
* Param ::= id `:' ParamType [`=' Expr]
29783037
*
@@ -3070,7 +3129,7 @@ object Parsers {
30703129
}
30713130

30723131
/** ClsParamClauses ::= {ClsParamClause} [[nl] ‘(’ [‘implicit’] ClsParams ‘)’]
3073-
* DefParamClauses ::= {DefParamClause} [[nl] ‘(’ [‘implicit’] DefParams ‘)’]
3132+
* TypelessClauses ::= TypelessClause {TypelessClause}
30743133
*
30753134
* @return The parameter definitions
30763135
*/
@@ -3322,9 +3381,9 @@ object Parsers {
33223381
}
33233382

33243383
/** DefDef ::= DefSig [‘:’ Type] ‘=’ Expr
3325-
* | this ParamClause ParamClauses `=' ConstrExpr
3384+
* | this TypelessClauses [DefImplicitClause] `=' ConstrExpr
33263385
* DefDcl ::= DefSig `:' Type
3327-
* DefSig ::= id [DefTypeParamClause] DefParamClauses
3386+
* DefSig ::= id [DefParamClauses] [DefImplicitClause]
33283387
* | ExtParamClause [nl] [‘.’] id DefParamClauses
33293388
*/
33303389
def defDefOrDcl(start: Offset, mods: Modifiers, numLeadParams: Int = 0): DefDef = atSpan(start, nameStart) {
@@ -3362,8 +3421,7 @@ object Parsers {
33623421
val mods1 = addFlag(mods, Method)
33633422
val ident = termIdent()
33643423
var name = ident.name.asTermName
3365-
val tparams = typeParamClauseOpt(ParamOwner.Def)
3366-
val vparamss = paramClauses(numLeadParams = numLeadParams)
3424+
val paramss = typeOrTermParamClauses(ParamOwner.Def, numLeadParams = numLeadParams)
33673425
var tpt = fromWithinReturnType { typedOpt() }
33683426
if (migrateTo3) newLineOptWhenFollowedBy(LBRACE)
33693427
val rhs =
@@ -3381,7 +3439,7 @@ object Parsers {
33813439
accept(EQUALS)
33823440
expr()
33833441

3384-
val ddef = DefDef(name, joinParams(tparams, vparamss), tpt, rhs)
3442+
val ddef = DefDef(name, paramss, tpt, rhs)
33853443
if (isBackquoted(ident)) ddef.pushAttachment(Backquoted, ())
33863444
finalizeDef(ddef, mods1, start)
33873445
}
@@ -3643,7 +3701,7 @@ object Parsers {
36433701
finalizeDef(gdef, mods1, start)
36443702
}
36453703

3646-
/** Extension ::= ‘extension’ [DefTypeParamClause] {UsingParamClause} ‘(’ DefParam ‘)’
3704+
/** Extension ::= ‘extension’ [DefTypeParamClause] {UsingParamClause} ‘(’ DefTermParam ‘)’
36473705
* {UsingParamClause} ExtMethods
36483706
*/
36493707
def extension(): ExtMethods =

0 commit comments

Comments
 (0)