1
- package dotty .tools
1
+ package dotty .tools
2
2
package dotc
3
3
package parsing
4
4
@@ -14,6 +14,7 @@ import Flags._
14
14
import Contexts ._
15
15
import Names ._
16
16
import NameKinds .WildcardParamName
17
+ import NameOps ._
17
18
import ast .{Positioned , Trees }
18
19
import ast .Trees ._
19
20
import StdNames ._
@@ -202,7 +203,7 @@ object Parsers {
202
203
} && ! in.isSoftModifierInModifierPosition
203
204
204
205
def isExprIntro : Boolean =
205
- if (in.token == IMPLIED || in.token == GIVEN ) in.lookaheadIn(BitSet (MATCH ))
206
+ if (in.token == GIVEN ) in.lookaheadIn(BitSet (MATCH ))
206
207
else (canStartExpressionTokens.contains(in.token) && ! in.isSoftModifierInModifierPosition)
207
208
208
209
def isDefIntro (allowedMods : BitSet , excludedSoftModifiers : Set [TermName ] = Set .empty): Boolean =
@@ -1645,7 +1646,7 @@ object Parsers {
1645
1646
if (in.token == MATCH ) impliedMatch(start, imods)
1646
1647
else implicitClosure(start, location, imods)
1647
1648
}
1648
- else if (in.token == IMPLIED || in.token == GIVEN ) {
1649
+ else if (in.token == GIVEN ) {
1649
1650
in.nextToken()
1650
1651
if (in.token == MATCH )
1651
1652
impliedMatch(start, EmptyModifiers )
@@ -2350,16 +2351,34 @@ object Parsers {
2350
2351
if (isIdent(nme.raw.BAR )) { in.nextToken(); pattern1() :: patternAlts() }
2351
2352
else Nil
2352
2353
2353
- /** Pattern1 ::= Pattern2 [Ascription]
2354
+ /** Pattern1 ::= Pattern2 [Ascription]
2355
+ * | ‘given’ PatVar ‘:’ RefinedType
2354
2356
*/
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
2360
2381
}
2361
- else p
2362
- }
2363
2382
2364
2383
/** Pattern2 ::= [id `@'] InfixPattern
2365
2384
*/
@@ -2850,7 +2869,7 @@ object Parsers {
2850
2869
*/
2851
2870
def importClause (leading : Token , mkTree : ImportConstr ): List [Tree ] = {
2852
2871
val offset = accept(leading)
2853
- val importGiven = in.token == IMPLIED || in.token == GIVEN
2872
+ val importGiven = in.token == GIVEN
2854
2873
if (importGiven) in.nextToken()
2855
2874
commaSeparated(importExpr(importGiven, mkTree)) match {
2856
2875
case t :: rest =>
@@ -3203,8 +3222,8 @@ object Parsers {
3203
3222
objectDef(start, posMods(start, mods | Case | Module ))
3204
3223
case ENUM =>
3205
3224
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 () })
3208
3227
case _ =>
3209
3228
syntaxErrorOrIncomplete(ExpectedStartOfTopLevelDefinition ())
3210
3229
EmptyTree
@@ -3297,14 +3316,21 @@ object Parsers {
3297
3316
Template (constr, parents, Nil , EmptyValDef , Nil )
3298
3317
}
3299
3318
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}
3304
3330
*/
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) {
3306
3332
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
3308
3334
indentRegion(name) {
3309
3335
val tparams = typeParamClauseOpt(ParamOwner .Def )
3310
3336
var leadingParamss =
@@ -3316,7 +3342,7 @@ object Parsers {
3316
3342
}
3317
3343
else Nil
3318
3344
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)) {
3320
3346
in.nextToken()
3321
3347
tokenSeparated(COMMA , constrApp)
3322
3348
}
@@ -3634,7 +3660,7 @@ object Parsers {
3634
3660
val mods = modifiers(closureMods)
3635
3661
mods.mods match {
3636
3662
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))
3638
3664
case _ =>
3639
3665
stats += implicitClosure(in.offset, Location .InBlock , mods)
3640
3666
}
0 commit comments