@@ -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,8 +203,8 @@ object Parsers {
202203 } && ! in.isSoftModifierInModifierPosition
203204
204205 def isExprIntro : Boolean =
205- if (in.token == IMPLIED || in.token == GIVEN ) in.lookaheadIn( BitSet ( MATCH ))
206- else ( canStartExpressionTokens.contains(in.token) && ! in.isSoftModifierInModifierPosition)
206+ if (in.token == GIVEN ) false
207+ else canStartExpressionTokens.contains(in.token) && ! in.isSoftModifierInModifierPosition
207208
208209 def isDefIntro (allowedMods : BitSet , excludedSoftModifiers : Set [TermName ] = Set .empty): Boolean =
209210 in.token == AT ||
@@ -1645,15 +1646,6 @@ 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- in.nextToken()
1650- if (in.token == MATCH )
1651- impliedMatch(start, EmptyModifiers )
1652- else {
1653- syntaxError(" `match` expected" )
1654- EmptyTree
1655- }
1656- }
16571649 else {
16581650 val saved = placeholderParams
16591651 placeholderParams = Nil
@@ -2350,16 +2342,34 @@ object Parsers {
23502342 if (isIdent(nme.raw.BAR )) { in.nextToken(); pattern1() :: patternAlts() }
23512343 else Nil
23522344
2353- /** Pattern1 ::= Pattern2 [Ascription]
2345+ /** Pattern1 ::= Pattern2 [Ascription]
2346+ * | ‘given’ PatVar ‘:’ RefinedType
23542347 */
2355- def pattern1 (): Tree = {
2356- val p = pattern2()
2357- if (in.token == COLON ) {
2358- in.nextToken()
2359- ascription(p, Location .InPattern )
2348+ def pattern1 (): Tree =
2349+ if (in.token == GIVEN ) {
2350+ val givenMod = atSpan(in.skipToken())(Mod .Given ())
2351+ atSpan(in.offset) {
2352+ in.token match {
2353+ case IDENTIFIER | USCORE if in.name.isVariableName =>
2354+ val name = in.name
2355+ in.nextToken()
2356+ accept(COLON )
2357+ val typed = ascription(Ident (nme.WILDCARD ), Location .InPattern )
2358+ Bind (name, typed).withMods(addMod(Modifiers (), givenMod))
2359+ case _ =>
2360+ syntaxErrorOrIncomplete(" pattern variable expected" )
2361+ errorTermTree
2362+ }
2363+ }
2364+ }
2365+ else {
2366+ val p = pattern2()
2367+ if (in.token == COLON ) {
2368+ in.nextToken()
2369+ ascription(p, Location .InPattern )
2370+ }
2371+ else p
23602372 }
2361- else p
2362- }
23632373
23642374 /** Pattern2 ::= [id `@'] InfixPattern
23652375 */
@@ -3297,10 +3307,17 @@ object Parsers {
32973307 Template (constr, parents, Nil , EmptyValDef , Nil )
32983308 }
32993309
3300- /** GivenDef ::= [id] [DefTypeParamClause] GivenBody
3301- * GivenBody ::= [‘as ConstrApp {‘,’ ConstrApp }] {GivenParamClause} [TemplateBody]
3302- * | ‘as’ Type {GivenParamClause} ‘=’ Expr
3303- * | ‘(’ DefParam ‘)’ TemplateBody
3310+ /** OLD:
3311+ * GivenDef ::= [id] [DefTypeParamClause] GivenBody
3312+ * GivenBody ::= [‘as ConstrApp {‘,’ ConstrApp }] {GivenParamClause} [TemplateBody]
3313+ * | ‘as’ Type {GivenParamClause} ‘=’ Expr
3314+ * | ‘(’ DefParam ‘)’ TemplateBody
3315+ * NEW:
3316+ * GivenDef ::= [GivenSig (‘:’ | <:)] Type ‘=’ Expr
3317+ * | [GivenSig ‘:’] [ConstrApp {‘,’ ConstrApp }] [TemplateBody]
3318+ * // | [id ‘:’] [ExtParamClause] TemplateBody (not yet implemented)
3319+ * ExtParamClause ::= [DefTypeParamClause] DefParamClause {GivenParamClause}
3320+ * GivenSig ::= [id] [DefTypeParamClause] {GivenParamClause}
33043321 */
33053322 def instanceDef (newStyle : Boolean , start : Offset , mods : Modifiers , instanceMod : Mod ) = atSpan(start, nameStart) {
33063323 var mods1 = addMod(mods, instanceMod)
0 commit comments