Skip to content

Commit c045c5f

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

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

29782978
/* -------- PARAMETERS ------------------------------------------- */
29792979

2980+
/** DefParamClause ::= DefTypeParamClause
2981+
* | DefTermParamClause
2982+
* | UsingParamClause
2983+
*/
2984+
def typeOrTermParamClause(nparams: Int, // number of parameters preceding this clause
2985+
ofClass: Boolean = false, // owner is a class
2986+
ofCaseClass: Boolean = false, // owner is a case class
2987+
prefix: Boolean = false, // clause precedes name of an extension method
2988+
givenOnly: Boolean = false, // only given parameters allowed
2989+
firstClause: Boolean = false, // clause is the first in regular list of clauses
2990+
ownerKind: ParamOwner
2991+
): List[TypeDef] | List[ValDef] =
2992+
if (in.token == LPAREN)
2993+
paramClause(nparams, ofClass, ofCaseClass, prefix, givenOnly, firstClause)
2994+
else if (in.token == LBRACKET)
2995+
typeParamClause(ownerKind)
2996+
else
2997+
Nil
2998+
2999+
end typeOrTermParamClause
3000+
3001+
/** DefParamClauses ::= DefParamClause { DefParamClause }
3002+
*/
3003+
def typeOrTermParamClauses(
3004+
ownerKind: ParamOwner,
3005+
ofClass: Boolean = false,
3006+
ofCaseClass: Boolean = false,
3007+
givenOnly: Boolean = false,
3008+
numLeadParams: Int = 0
3009+
): List[List[TypeDef] | List[ValDef]] =
3010+
3011+
def recur(firstClause: Boolean, nparams: Int): List[List[TypeDef] | List[ValDef]] =
3012+
newLineOptWhenFollowedBy(LPAREN)
3013+
newLineOptWhenFollowedBy(LBRACKET)
3014+
if in.token == LPAREN then
3015+
val paramsStart = in.offset
3016+
val params = paramClause(
3017+
nparams,
3018+
ofClass = ofClass,
3019+
ofCaseClass = ofCaseClass,
3020+
givenOnly = givenOnly,
3021+
firstClause = firstClause)
3022+
val lastClause = params.nonEmpty && params.head.mods.flags.is(Implicit)
3023+
params :: (
3024+
if lastClause then Nil
3025+
else recur(firstClause = false, nparams + params.length))
3026+
else if in.token == LBRACKET then
3027+
typeParamClause(ownerKind) :: recur(firstClause, nparams)
3028+
else Nil
3029+
end recur
3030+
3031+
recur(firstClause = true, nparams = numLeadParams)
3032+
end typeOrTermParamClauses
3033+
3034+
29803035
/** ClsTypeParamClause::= ‘[’ ClsTypeParam {‘,’ ClsTypeParam} ‘]’
29813036
* ClsTypeParam ::= {Annotation} [‘+’ | ‘-’]
29823037
* id [HkTypeParamClause] TypeParamBounds
@@ -3041,11 +3096,15 @@ object Parsers {
30413096
* UsingClsParamClause::= ‘(’ ‘using’ [‘erased’] (ClsParams | ContextTypes) ‘)’
30423097
* ClsParams ::= ClsParam {‘,’ ClsParam}
30433098
* ClsParam ::= {Annotation}
3099+
*
3100+
* TypelessClause ::= DefTermParamClause
3101+
* | UsingParamClause
30443102
*
3045-
* DefParamClause ::= ‘(’ [‘erased’] DefParams ‘)’ | UsingParamClause
3046-
* UsingParamClause ::= ‘(’ ‘using’ [‘erased’] (DefParams | ContextTypes) ‘)’
3047-
* DefParams ::= DefParam {‘,’ DefParam}
3048-
* DefParam ::= {Annotation} [‘inline’] Param
3103+
* DefTermParamClause::= [nl] ‘(’ [DefTermParams] ‘)’
3104+
* UsingParamClause ::= ‘(’ ‘using’ [‘erased’] (DefTermParams | ContextTypes) ‘)’
3105+
* DefImplicitClause ::= [nl] ‘(’ ‘implicit’ DefTermParams ‘)’
3106+
* DefTermParams ::= DefTermParam {‘,’ DefTermParam}
3107+
* DefTermParam ::= {Annotation} [‘inline’] Param
30493108
*
30503109
* Param ::= id `:' ParamType [`=' Expr]
30513110
*
@@ -3143,7 +3202,7 @@ object Parsers {
31433202
}
31443203

31453204
/** ClsParamClauses ::= {ClsParamClause} [[nl] ‘(’ [‘implicit’] ClsParams ‘)’]
3146-
* DefParamClauses ::= {DefParamClause} [[nl] ‘(’ [‘implicit’] DefParams ‘)’]
3205+
* TypelessClauses ::= TypelessClause {TypelessClause}
31473206
*
31483207
* @return The parameter definitions
31493208
*/
@@ -3411,9 +3470,9 @@ object Parsers {
34113470
}
34123471

34133472
/** DefDef ::= DefSig [‘:’ Type] ‘=’ Expr
3414-
* | this ParamClause ParamClauses `=' ConstrExpr
3473+
* | this TypelessClauses [DefImplicitClause] `=' ConstrExpr
34153474
* DefDcl ::= DefSig `:' Type
3416-
* DefSig ::= id [DefTypeParamClause] DefParamClauses
3475+
* DefSig ::= id [DefParamClauses] [DefImplicitClause]
34173476
* | ExtParamClause [nl] [‘.’] id DefParamClauses
34183477
*/
34193478
def defDefOrDcl(start: Offset, mods: Modifiers, numLeadParams: Int = 0): DefDef = atSpan(start, nameStart) {
@@ -3451,8 +3510,7 @@ object Parsers {
34513510
val mods1 = addFlag(mods, Method)
34523511
val ident = termIdent()
34533512
var name = ident.name.asTermName
3454-
val tparams = typeParamClauseOpt(ParamOwner.Def)
3455-
val vparamss = paramClauses(numLeadParams = numLeadParams)
3513+
val paramss = typeOrTermParamClauses(ParamOwner.Def, numLeadParams = numLeadParams)
34563514
var tpt = fromWithinReturnType { typedOpt() }
34573515
if (migrateTo3) newLineOptWhenFollowedBy(LBRACE)
34583516
val rhs =
@@ -3470,7 +3528,7 @@ object Parsers {
34703528
accept(EQUALS)
34713529
expr()
34723530

3473-
val ddef = DefDef(name, joinParams(tparams, vparamss), tpt, rhs)
3531+
val ddef = DefDef(name, paramss, tpt, rhs)
34743532
if (isBackquoted(ident)) ddef.pushAttachment(Backquoted, ())
34753533
finalizeDef(ddef, mods1, start)
34763534
}
@@ -3733,7 +3791,7 @@ object Parsers {
37333791
finalizeDef(gdef, mods1, start)
37343792
}
37353793

3736-
/** Extension ::= ‘extension’ [DefTypeParamClause] {UsingParamClause} ‘(’ DefParam ‘)’
3794+
/** Extension ::= ‘extension’ [DefTypeParamClause] {UsingParamClause} ‘(’ DefTermParam ‘)’
37373795
* {UsingParamClause} ExtMethods
37383796
*/
37393797
def extension(): ExtMethods =

0 commit comments

Comments
 (0)