@@ -902,7 +902,6 @@ object Parsers {
902902
903903 /** Are the next tokens a prefix of a formal parameter or given type?
904904 * @pre: current token is LPAREN
905- * TODO: Drop once syntax has stabilized
906905 */
907906 def followingIsParamOrGivenType () =
908907 val lookahead = in.LookaheadScanner ()
@@ -920,22 +919,6 @@ object Parsers {
920919 else false
921920 else false
922921
923- /** Are the next tokens a prefix of a formal parameter?
924- * @pre: current token is LPAREN
925- */
926- def followingIsParam () =
927- val lookahead = in.LookaheadScanner ()
928- lookahead.nextToken()
929- if startParamTokens.contains(lookahead.token) then true
930- else if lookahead.token == IDENTIFIER then
931- if lookahead.name == nme.inline then
932- lookahead.nextToken()
933- if lookahead.token == IDENTIFIER then
934- lookahead.nextToken()
935- lookahead.token == COLON
936- else false
937- else false
938-
939922 /** Are the next token the "GivenSig" part of a given definition,
940923 * i.e. an identifier followed by type and value parameters, followed by `:`?
941924 * @pre The current token is an identifier
@@ -2788,18 +2771,16 @@ object Parsers {
27882771 def typeParamClauseOpt (ownerKind : ParamOwner .Value ): List [TypeDef ] =
27892772 if (in.token == LBRACKET ) typeParamClause(ownerKind) else Nil
27902773
2791- def typesToGivenParams (tps : List [Tree ], ofClass : Boolean , nparams : Int ): List [ValDef ] =
2774+ /** OLD: GivenTypes ::= AnnotType {‘,’ AnnotType}
2775+ * NEW: GivenTypes ::= Type {‘,’ Type}
2776+ */
2777+ def givenTypes (nparams : Int , ofClass : Boolean ): List [ValDef ] =
2778+ val tps = commaSeparated(typ)
27922779 var counter = nparams
27932780 def nextIdx = { counter += 1 ; counter }
27942781 val paramFlags = if ofClass then Private | Local | ParamAccessor else Param
27952782 tps.map(makeSyntheticParameter(nextIdx, _, paramFlags | Synthetic | Given ))
27962783
2797- /** OLD: GivenTypes ::= AnnotType {‘,’ AnnotType}
2798- * NEW: GivenTypes ::= Type {‘,’ Type}
2799- */
2800- def givenTypes (ofClass : Boolean , nparams : Int ): List [ValDef ] =
2801- typesToGivenParams(commaSeparated(typ), ofClass, nparams)
2802-
28032784 /** ClsParamClause ::= ‘(’ [‘erased’] ClsParams ‘)’
28042785 * GivenClsParamClause::= ‘(’ ‘given’ [‘erased’] (ClsParams | GivenTypes) ‘)’
28052786 * ClsParams ::= ClsParam {‘,’ ClsParam}
@@ -2897,7 +2878,7 @@ object Parsers {
28972878 || startParamTokens.contains(in.token)
28982879 || isIdent && (in.name == nme.inline || in.lookaheadIn(BitSet (COLON )))
28992880 if isParams then commaSeparated(() => param())
2900- else givenTypes(ofClass, nparams )
2881+ else givenTypes(nparams, ofClass )
29012882 checkVarArgsRules(clause)
29022883 clause
29032884 }
@@ -3423,12 +3404,11 @@ object Parsers {
34233404 syntaxError(i " extension clause can only define methods " , stat.span)
34243405 }
34253406
3426- /** GivenDef ::= [GivenSig (‘:’ | <:)] {FunArgTypes ‘=>’} AnnotType ‘=’ Expr
3427- * | [GivenSig ‘:’] {FunArgTypes ‘=>’} ConstrApps [TemplateBody]
3407+ /** GivenDef ::= [GivenSig (‘:’ | <:)] Type ‘=’ Expr
3408+ * | [GivenSig ‘:’] ConstrApps [TemplateBody]
34283409 * GivenSig ::= [id] [DefTypeParamClause] {GivenParamClause}
34293410 * ExtParamClause ::= [DefTypeParamClause] DefParamClause
34303411 * ExtMethods ::= [nl] ‘{’ ‘def’ DefDef {semi ‘def’ DefDef} ‘}’
3431- * TODO: cleanup once syntax has stabilized
34323412 */
34333413 def givenDef (start : Offset , mods : Modifiers , instanceMod : Mod ) = atSpan(start, nameStart) {
34343414 var mods1 = addMod(mods, instanceMod)
@@ -3453,60 +3433,30 @@ object Parsers {
34533433 templ.body.foreach(checkExtensionMethod(tparams, _))
34543434 ModuleDef (name, templ)
34553435 else
3456- var hasLabel = false
3457- def skipColon () =
3458- if ! hasLabel && in.token == COLON then
3459- hasLabel = true
3460- in.nextToken()
3461- if ! name.isEmpty then skipColon()
3436+ val hasLabel = ! name.isEmpty && in.token == COLON
3437+ if hasLabel then in.nextToken()
34623438 val tparams = typeParamClauseOpt(ParamOwner .Def )
3463- if ! tparams.isEmpty then skipColon()
34643439 val paramsStart = in.offset
3465- var vparamss =
3440+ val vparamss =
34663441 if in.token == LPAREN && followingIsParamOrGivenType()
3467- then paramClauses() // todo: ONLY admit a single paramClause
3442+ then paramClauses()
34683443 else Nil
34693444 def checkAllGivens (vparamss : List [List [ValDef ]], what : String ) =
34703445 vparamss.foreach(_.foreach(vparam =>
34713446 if ! vparam.mods.is(Given ) then syntaxError(em " $what must be `given` " , vparam.span)))
3472- def makeGiven (params : List [ValDef ]): List [ValDef ] =
3473- params.map(param => param.withMods(param.mods | Given ))
3474- def conditionalParents (): List [Tree ] =
3475- accept(ARROW )
3476- if in.token == LPAREN && followingIsParam() then
3477- vparamss = vparamss :+ makeGiven(paramClause(vparamss.flatten.length))
3478- conditionalParents()
3479- else
3480- val constrs = constrApps(commaOK = true , templateCanFollow = true )
3481- if in.token == ARROW && constrs.forall(_.isType) then
3482- vparamss = vparamss
3483- :+ typesToGivenParams(constrs, ofClass = false , vparamss.flatten.length)
3484- conditionalParents()
3485- else constrs
3486-
3487- val isConditional =
3488- in.token == ARROW
3489- && vparamss.length == 1
3490- && (hasLabel || name.isEmpty && tparams.isEmpty)
3491- if ! isConditional then checkAllGivens(vparamss, " parameter of given instance" )
3447+ checkAllGivens(vparamss, " parameter of given instance" )
34923448 val parents =
3493- if in.token == SUBTYPE && ! hasLabel then
3449+ if hasLabel then
3450+ constrApps(commaOK = true , templateCanFollow = true )
3451+ else if in.token == SUBTYPE then
34943452 if ! mods.is(Inline ) then
34953453 syntaxError(" `<:` is only allowed for given with `inline` modifier" )
34963454 in.nextToken()
3497- TypeBoundsTree (EmptyTree , annotType()) :: Nil
3498- else if isConditional then
3499- vparamss = vparamss.map(makeGiven)
3500- conditionalParents()
3455+ TypeBoundsTree (EmptyTree , toplevelTyp()) :: Nil
35013456 else
3502- if ! hasLabel && ! (name.isEmpty && tparams.isEmpty && vparamss.isEmpty) then
3457+ if ! (name.isEmpty && tparams.isEmpty && vparamss.isEmpty) then
35033458 accept(COLON )
3504- val constrs = constrApps(commaOK = true , templateCanFollow = true )
3505- if in.token == ARROW && vparamss.isEmpty && constrs.forall(_.isType) then
3506- vparamss = typesToGivenParams(constrs, ofClass = false , 0 ) :: Nil
3507- conditionalParents()
3508- else
3509- constrs
3459+ constrApps(commaOK = true , templateCanFollow = true )
35103460
35113461 if in.token == EQUALS && parents.length == 1 && parents.head.isType then
35123462 in.nextToken()
0 commit comments