Skip to content

Commit 704e8df

Browse files
committed
Update Parsers.scala to accomodate new syntax
Interweaved methods still fail at use-cite, see next commit
1 parent 290ab7b commit 704e8df

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
@@ -3079,6 +3079,61 @@ object Parsers {
30793079

30803080
/* -------- PARAMETERS ------------------------------------------- */
30813081

3082+
/** DefParamClause ::= DefTypeParamClause
3083+
* | DefTermParamClause
3084+
* | UsingParamClause
3085+
*/
3086+
def typeOrTermParamClause(nparams: Int, // number of parameters preceding this clause
3087+
ofClass: Boolean = false, // owner is a class
3088+
ofCaseClass: Boolean = false, // owner is a case class
3089+
prefix: Boolean = false, // clause precedes name of an extension method
3090+
givenOnly: Boolean = false, // only given parameters allowed
3091+
firstClause: Boolean = false, // clause is the first in regular list of clauses
3092+
ownerKind: ParamOwner
3093+
): List[TypeDef] | List[ValDef] =
3094+
if (in.token == LPAREN)
3095+
paramClause(nparams, ofClass, ofCaseClass, prefix, givenOnly, firstClause)
3096+
else if (in.token == LBRACKET)
3097+
typeParamClause(ownerKind)
3098+
else
3099+
Nil
3100+
3101+
end typeOrTermParamClause
3102+
3103+
/** DefParamClauses ::= DefParamClause { DefParamClause }
3104+
*/
3105+
def typeOrTermParamClauses(
3106+
ownerKind: ParamOwner,
3107+
ofClass: Boolean = false,
3108+
ofCaseClass: Boolean = false,
3109+
givenOnly: Boolean = false,
3110+
numLeadParams: Int = 0
3111+
): List[List[TypeDef] | List[ValDef]] =
3112+
3113+
def recur(firstClause: Boolean, nparams: Int): List[List[TypeDef] | List[ValDef]] =
3114+
newLineOptWhenFollowedBy(LPAREN)
3115+
newLineOptWhenFollowedBy(LBRACKET)
3116+
if in.token == LPAREN then
3117+
val paramsStart = in.offset
3118+
val params = paramClause(
3119+
nparams,
3120+
ofClass = ofClass,
3121+
ofCaseClass = ofCaseClass,
3122+
givenOnly = givenOnly,
3123+
firstClause = firstClause)
3124+
val lastClause = params.nonEmpty && params.head.mods.flags.is(Implicit)
3125+
params :: (
3126+
if lastClause then Nil
3127+
else recur(firstClause = false, nparams + params.length))
3128+
else if in.token == LBRACKET then
3129+
typeParamClause(ownerKind) :: recur(firstClause, nparams)
3130+
else Nil
3131+
end recur
3132+
3133+
recur(firstClause = true, nparams = numLeadParams)
3134+
end typeOrTermParamClauses
3135+
3136+
30823137
/** ClsTypeParamClause::= ‘[’ ClsTypeParam {‘,’ ClsTypeParam} ‘]’
30833138
* ClsTypeParam ::= {Annotation} [‘+’ | ‘-’]
30843139
* id [HkTypeParamClause] TypeParamBounds
@@ -3143,11 +3198,15 @@ object Parsers {
31433198
* UsingClsParamClause::= ‘(’ ‘using’ [‘erased’] (ClsParams | ContextTypes) ‘)’
31443199
* ClsParams ::= ClsParam {‘,’ ClsParam}
31453200
* ClsParam ::= {Annotation}
3201+
*
3202+
* TypelessClause ::= DefTermParamClause
3203+
* | UsingParamClause
31463204
*
3147-
* DefParamClause ::= ‘(’ [‘erased’] DefParams ‘)’ | UsingParamClause
3148-
* UsingParamClause ::= ‘(’ ‘using’ [‘erased’] (DefParams | ContextTypes) ‘)’
3149-
* DefParams ::= DefParam {‘,’ DefParam}
3150-
* DefParam ::= {Annotation} [‘inline’] Param
3205+
* DefTermParamClause::= [nl] ‘(’ [DefTermParams] ‘)’
3206+
* UsingParamClause ::= ‘(’ ‘using’ [‘erased’] (DefTermParams | ContextTypes) ‘)’
3207+
* DefImplicitClause ::= [nl] ‘(’ ‘implicit’ DefTermParams ‘)’
3208+
* DefTermParams ::= DefTermParam {‘,’ DefTermParam}
3209+
* DefTermParam ::= {Annotation} [‘inline’] Param
31513210
*
31523211
* Param ::= id `:' ParamType [`=' Expr]
31533212
*
@@ -3246,7 +3305,7 @@ object Parsers {
32463305
}
32473306

32483307
/** ClsParamClauses ::= {ClsParamClause} [[nl] ‘(’ [‘implicit’] ClsParams ‘)’]
3249-
* DefParamClauses ::= {DefParamClause} [[nl] ‘(’ [‘implicit’] DefParams ‘)’]
3308+
* TypelessClauses ::= TypelessClause {TypelessClause}
32503309
*
32513310
* @return The parameter definitions
32523311
*/
@@ -3515,9 +3574,9 @@ object Parsers {
35153574
}
35163575

35173576
/** DefDef ::= DefSig [‘:’ Type] ‘=’ Expr
3518-
* | this ParamClause ParamClauses `=' ConstrExpr
3577+
* | this TypelessClauses [DefImplicitClause] `=' ConstrExpr
35193578
* DefDcl ::= DefSig `:' Type
3520-
* DefSig ::= id [DefTypeParamClause] DefParamClauses
3579+
* DefSig ::= id [DefParamClauses] [DefImplicitClause]
35213580
* | ExtParamClause [nl] [‘.’] id DefParamClauses
35223581
*/
35233582
def defDefOrDcl(start: Offset, mods: Modifiers, numLeadParams: Int = 0): DefDef = atSpan(start, nameStart) {
@@ -3555,8 +3614,7 @@ object Parsers {
35553614
val mods1 = addFlag(mods, Method)
35563615
val ident = termIdent()
35573616
var name = ident.name.asTermName
3558-
val tparams = typeParamClauseOpt(ParamOwner.Def)
3559-
val vparamss = paramClauses(numLeadParams = numLeadParams)
3617+
val paramss = typeOrTermParamClauses(ParamOwner.Def, numLeadParams = numLeadParams)
35603618
var tpt = fromWithinReturnType { typedOpt() }
35613619
if (migrateTo3) newLineOptWhenFollowedBy(LBRACE)
35623620
val rhs =
@@ -3574,7 +3632,7 @@ object Parsers {
35743632
accept(EQUALS)
35753633
expr()
35763634

3577-
val ddef = DefDef(name, joinParams(tparams, vparamss), tpt, rhs)
3635+
val ddef = DefDef(name, paramss, tpt, rhs)
35783636
if (isBackquoted(ident)) ddef.pushAttachment(Backquoted, ())
35793637
finalizeDef(ddef, mods1, start)
35803638
}
@@ -3837,7 +3895,7 @@ object Parsers {
38373895
finalizeDef(gdef, mods1, start)
38383896
}
38393897

3840-
/** Extension ::= ‘extension’ [DefTypeParamClause] {UsingParamClause} ‘(’ DefParam ‘)’
3898+
/** Extension ::= ‘extension’ [DefTypeParamClause] {UsingParamClause} ‘(’ DefTermParam ‘)’
38413899
* {UsingParamClause} ExtMethods
38423900
*/
38433901
def extension(): ExtMethods =

0 commit comments

Comments
 (0)