1- package dotty .tools
1+ package dotty .tools
22package dotc
33package parsing
44
@@ -14,6 +14,7 @@ import Flags._
1414import Contexts ._
1515import Names ._
1616import NameKinds .WildcardParamName
17+ import NameOps ._
1718import ast .{Positioned , Trees }
1819import ast .Trees ._
1920import StdNames ._
@@ -202,7 +203,7 @@ object Parsers {
202203 } && ! in.isSoftModifierInModifierPosition
203204
204205 def isExprIntro : Boolean =
205- if (in.token == IMPLIED || in.token == GIVEN ) in.lookaheadIn(BitSet (MATCH ))
206+ if (in.token == GIVEN ) in.lookaheadIn(BitSet (MATCH ))
206207 else (canStartExpressionTokens.contains(in.token) && ! in.isSoftModifierInModifierPosition)
207208
208209 def isDefIntro (allowedMods : BitSet , excludedSoftModifiers : Set [TermName ] = Set .empty): Boolean =
@@ -1645,7 +1646,7 @@ object Parsers {
16451646 if (in.token == MATCH ) impliedMatch(start, imods)
16461647 else implicitClosure(start, location, imods)
16471648 }
1648- else if (in.token == IMPLIED || in.token == GIVEN ) {
1649+ else if (in.token == GIVEN ) {
16491650 in.nextToken()
16501651 if (in.token == MATCH )
16511652 impliedMatch(start, EmptyModifiers )
@@ -2350,16 +2351,34 @@ object Parsers {
23502351 if (isIdent(nme.raw.BAR )) { in.nextToken(); pattern1() :: patternAlts() }
23512352 else Nil
23522353
2353- /** Pattern1 ::= Pattern2 [Ascription]
2354+ /** Pattern1 ::= Pattern2 [Ascription]
2355+ * | ‘given’ PatVar ‘:’ RefinedType
23542356 */
2355- def pattern1 (): Tree = {
2356- val p = pattern2()
2357- if (in.token == COLON ) {
2358- in.nextToken()
2359- ascription(p, Location .InPattern )
2357+ def pattern1 (): Tree =
2358+ if (in.token == GIVEN ) {
2359+ val givenMod = atSpan(in.skipToken())(Mod .Given ())
2360+ atSpan(in.offset) {
2361+ in.token match {
2362+ case IDENTIFIER | USCORE if in.name.isVariableName =>
2363+ val name = in.name
2364+ in.nextToken()
2365+ accept(COLON )
2366+ val typed = ascription(Ident (nme.WILDCARD ), Location .InPattern )
2367+ Bind (name, typed).withMods(addMod(Modifiers (), givenMod))
2368+ case _ =>
2369+ syntaxErrorOrIncomplete(" pattern variable expected" )
2370+ errorTermTree
2371+ }
2372+ }
2373+ }
2374+ else {
2375+ val p = pattern2()
2376+ if (in.token == COLON ) {
2377+ in.nextToken()
2378+ ascription(p, Location .InPattern )
2379+ }
2380+ else p
23602381 }
2361- else p
2362- }
23632382
23642383 /** Pattern2 ::= [id `@'] InfixPattern
23652384 */
@@ -2850,7 +2869,7 @@ object Parsers {
28502869 */
28512870 def importClause (leading : Token , mkTree : ImportConstr ): List [Tree ] = {
28522871 val offset = accept(leading)
2853- val importGiven = in.token == IMPLIED || in.token == GIVEN
2872+ val importGiven = in.token == GIVEN
28542873 if (importGiven) in.nextToken()
28552874 commaSeparated(importExpr(importGiven, mkTree)) match {
28562875 case t :: rest =>
@@ -3203,8 +3222,8 @@ object Parsers {
32033222 objectDef(start, posMods(start, mods | Case | Module ))
32043223 case ENUM =>
32053224 enumDef(start, posMods(start, mods | Enum ))
3206- case IMPLIED | GIVEN =>
3207- instanceDef(in.token == GIVEN , start, mods, atSpan(in.skipToken()) { Mod .Given () })
3225+ case GIVEN =>
3226+ instanceDef(start, mods, atSpan(in.skipToken()) { Mod .Given () })
32083227 case _ =>
32093228 syntaxErrorOrIncomplete(ExpectedStartOfTopLevelDefinition ())
32103229 EmptyTree
@@ -3297,14 +3316,21 @@ object Parsers {
32973316 Template (constr, parents, Nil , EmptyValDef , Nil )
32983317 }
32993318
3300- /** GivenDef ::= [id] [DefTypeParamClause] GivenBody
3301- * GivenBody ::= [‘as ConstrApp {‘,’ ConstrApp }] {GivenParamClause} [TemplateBody]
3302- * | ‘as’ Type {GivenParamClause} ‘=’ Expr
3303- * | ‘(’ DefParam ‘)’ TemplateBody
3319+ /** OLD:
3320+ * GivenDef ::= [id] [DefTypeParamClause] GivenBody
3321+ * GivenBody ::= [‘as ConstrApp {‘,’ ConstrApp }] {GivenParamClause} [TemplateBody]
3322+ * | ‘as’ Type {GivenParamClause} ‘=’ Expr
3323+ * | ‘(’ DefParam ‘)’ TemplateBody
3324+ * NEW:
3325+ * GivenDef ::= [GivenSig (‘:’ | <:)] Type ‘=’ Expr
3326+ * | [GivenSig ‘:’] [ConstrApp {‘,’ ConstrApp }] [TemplateBody]
3327+ * // | [id ‘:’] [ExtParamClause] TemplateBody (not yet implemented)
3328+ * ExtParamClause ::= [DefTypeParamClause] DefParamClause {GivenParamClause}
3329+ * GivenSig ::= [id] [DefTypeParamClause] {GivenParamClause}
33043330 */
3305- def instanceDef (newStyle : Boolean , start : Offset , mods : Modifiers , instanceMod : Mod ) = atSpan(start, nameStart) {
3331+ def instanceDef (start : Offset , mods : Modifiers , instanceMod : Mod ) = atSpan(start, nameStart) {
33063332 var mods1 = addMod(mods, instanceMod)
3307- val name = if (isIdent && ! (newStyle && isIdent(nme.as) )) ident() else EmptyTermName
3333+ val name = if (isIdent && ! isIdent(nme.as)) ident() else EmptyTermName
33083334 indentRegion(name) {
33093335 val tparams = typeParamClauseOpt(ParamOwner .Def )
33103336 var leadingParamss =
@@ -3316,7 +3342,7 @@ object Parsers {
33163342 }
33173343 else Nil
33183344 val parents =
3319- if (! newStyle && in.token == FOR || isIdent(nme.as)) { // for the moment, accept both `given for` and `given as`
3345+ if (isIdent(nme.as)) {
33203346 in.nextToken()
33213347 tokenSeparated(COMMA , constrApp)
33223348 }
@@ -3634,7 +3660,7 @@ object Parsers {
36343660 val mods = modifiers(closureMods)
36353661 mods.mods match {
36363662 case givenMod :: Nil if ! isBindingIntro =>
3637- stats += instanceDef(true , start, EmptyModifiers , Mod .Given ().withSpan(givenMod.span))
3663+ stats += instanceDef(start, EmptyModifiers , Mod .Given ().withSpan(givenMod.span))
36383664 case _ =>
36393665 stats += implicitClosure(in.offset, Location .InBlock , mods)
36403666 }
0 commit comments