@@ -48,9 +48,13 @@ object Parsers {
48
48
def nonePositive : Boolean = parCounts forall (_ <= 0 )
49
49
}
50
50
51
- @ sharable object Location extends Enumeration {
52
- val InParens, InBlock, InPattern, ElseWhere : Value = Value
53
- }
51
+ enum Location (val inParens : Boolean , val inPattern : Boolean , val inArgs : Boolean ):
52
+ case InParens extends Location (true , false , false )
53
+ case InArgs extends Location (true , false , true )
54
+ case InPattern extends Location (false , true , false )
55
+ case InPatternArgs extends Location (false , true , true ) // InParens not true, since it might be an alternative
56
+ case InBlock extends Location (false , false , false )
57
+ case ElseWhere extends Location (false , false , false )
54
58
55
59
@ sharable object ParamOwner extends Enumeration {
56
60
val Class, Type, TypeParam, Def : Value = Value
@@ -1754,9 +1758,9 @@ object Parsers {
1754
1758
else TypeTree ().withSpan(Span (in.lastOffset))
1755
1759
}
1756
1760
1757
- def typeDependingOn (location : Location . Value ): Tree =
1758
- if ( location == Location . InParens ) typ()
1759
- else if ( location == Location . InPattern ) refinedType()
1761
+ def typeDependingOn (location : Location ): Tree =
1762
+ if location.inParens then typ()
1763
+ else if location.inPattern then refinedType()
1760
1764
else infixType()
1761
1765
1762
1766
/* ----------- EXPRESSIONS ------------------------------------------------ */
@@ -1843,7 +1847,7 @@ object Parsers {
1843
1847
1844
1848
def subExpr () = subPart(expr)
1845
1849
1846
- def expr (location : Location . Value ): Tree = {
1850
+ def expr (location : Location ): Tree = {
1847
1851
val start = in.offset
1848
1852
def isSpecialClosureStart =
1849
1853
val lookahead = in.LookaheadScanner ()
@@ -1876,7 +1880,7 @@ object Parsers {
1876
1880
}
1877
1881
}
1878
1882
1879
- def expr1 (location : Location . Value = Location .ElseWhere ): Tree = in.token match
1883
+ def expr1 (location : Location = Location .ElseWhere ): Tree = in.token match
1880
1884
case IF =>
1881
1885
in.endMarkerScope(IF ) { ifExpr(in.offset, If ) }
1882
1886
case WHILE =>
@@ -1989,11 +1993,14 @@ object Parsers {
1989
1993
else expr1Rest(postfixExpr(), location)
1990
1994
end expr1
1991
1995
1992
- def expr1Rest (t : Tree , location : Location . Value ): Tree = in.token match
1996
+ def expr1Rest (t : Tree , location : Location ): Tree = in.token match
1993
1997
case EQUALS =>
1994
1998
t match
1995
1999
case Ident (_) | Select (_, _) | Apply (_, _) =>
1996
- atSpan(startOffset(t), in.skipToken()) { Assign (t, subExpr()) }
2000
+ atSpan(startOffset(t), in.skipToken()) {
2001
+ val loc = if location.inArgs then location else Location .ElseWhere
2002
+ Assign (t, subPart(() => expr(loc)))
2003
+ }
1997
2004
case _ =>
1998
2005
t
1999
2006
case COLON =>
@@ -2003,24 +2010,29 @@ object Parsers {
2003
2010
t
2004
2011
end expr1Rest
2005
2012
2006
- def ascription (t : Tree , location : Location . Value ): Tree = atSpan(startOffset(t)) {
2013
+ def ascription (t : Tree , location : Location ): Tree = atSpan(startOffset(t)) {
2007
2014
in.token match {
2008
2015
case USCORE =>
2009
2016
val uscoreStart = in.skipToken()
2010
- if ( isIdent(nme.raw.STAR )) {
2017
+ if isIdent(nme.raw.STAR ) then
2011
2018
in.nextToken()
2012
- if (in.token != RPAREN ) syntaxError(SeqWildcardPatternPos (), uscoreStart)
2019
+ if ! (location.inArgs && in.token == RPAREN ) then
2020
+ if opStack.nonEmpty
2021
+ ctx.errorOrMigrationWarning(
2022
+ em """ `_*` can be used only for last argument of method application.
2023
+ |It is no longer allowed in operands of infix operations. """ ,
2024
+ in.sourcePos(uscoreStart))
2025
+ else
2026
+ syntaxError(SeqWildcardPatternPos (), uscoreStart)
2013
2027
Typed (t, atSpan(uscoreStart) { Ident (tpnme.WILDCARD_STAR ) })
2014
- }
2015
- else {
2028
+ else
2016
2029
syntaxErrorOrIncomplete(IncorrectRepeatedParameterSyntax ())
2017
2030
t
2018
- }
2019
- case AT if location != Location .InPattern =>
2031
+ case AT if ! location.inPattern =>
2020
2032
annotations().foldLeft(t)(Annotated )
2021
2033
case _ =>
2022
2034
val tpt = typeDependingOn(location)
2023
- if (isWildcard(t) && location != Location . InPattern ) {
2035
+ if (isWildcard(t) && ! location.inPattern ) {
2024
2036
val vd :: rest = placeholderParams
2025
2037
placeholderParams =
2026
2038
cpy.ValDef (vd)(tpt = tpt).withSpan(vd.span.union(tpt.span)) :: rest
@@ -2063,7 +2075,7 @@ object Parsers {
2063
2075
* | `_'
2064
2076
* Bindings ::= `(' [[‘using’] [‘erased’] Binding {`,' Binding}] `)'
2065
2077
*/
2066
- def funParams (mods : Modifiers , location : Location . Value ): List [Tree ] =
2078
+ def funParams (mods : Modifiers , location : Location ): List [Tree ] =
2067
2079
if in.token == LPAREN then
2068
2080
in.nextToken()
2069
2081
if in.token == RPAREN then
@@ -2117,10 +2129,10 @@ object Parsers {
2117
2129
/** Expr ::= [‘implicit’] FunParams `=>' Expr
2118
2130
* BlockResult ::= implicit id [`:' InfixType] `=>' Block // Scala2 only
2119
2131
*/
2120
- def closure (start : Int , location : Location . Value , implicitMods : Modifiers ): Tree =
2132
+ def closure (start : Int , location : Location , implicitMods : Modifiers ): Tree =
2121
2133
closureRest(start, location, funParams(implicitMods, location))
2122
2134
2123
- def closureRest (start : Int , location : Location . Value , params : List [Tree ]): Tree =
2135
+ def closureRest (start : Int , location : Location , params : List [Tree ]): Tree =
2124
2136
atSpan(start, in.offset) {
2125
2137
if in.token == CTXARROW then in.nextToken() else accept(ARROW )
2126
2138
Function (params, if (location == Location .InBlock ) block() else expr())
@@ -2295,10 +2307,9 @@ object Parsers {
2295
2307
if args._2 then res.setUsingApply()
2296
2308
res
2297
2309
2298
- val argumentExpr : () => Tree = () => exprInParens( ) match {
2310
+ val argumentExpr : () => Tree = () => expr( Location . InArgs ) match
2299
2311
case arg @ Assign (Ident (id), rhs) => cpy.NamedArg (arg)(id, rhs)
2300
2312
case arg => arg
2301
- }
2302
2313
2303
2314
/** ArgumentExprss ::= {ArgumentExprs}
2304
2315
*/
@@ -2535,21 +2546,20 @@ object Parsers {
2535
2546
2536
2547
/** Pattern ::= Pattern1 { `|' Pattern1 }
2537
2548
*/
2538
- val pattern : () => Tree = () => {
2539
- val pat = pattern1()
2549
+ def pattern ( location : Location = Location . InPattern ) : Tree =
2550
+ val pat = pattern1(location )
2540
2551
if (isIdent(nme.raw.BAR ))
2541
- atSpan(startOffset(pat)) { Alternative (pat :: patternAlts()) }
2552
+ atSpan(startOffset(pat)) { Alternative (pat :: patternAlts(location )) }
2542
2553
else pat
2543
- }
2544
2554
2545
- def patternAlts (): List [Tree ] =
2546
- if (isIdent(nme.raw.BAR )) { in.nextToken(); pattern1() :: patternAlts() }
2555
+ def patternAlts (location : Location ): List [Tree ] =
2556
+ if (isIdent(nme.raw.BAR )) { in.nextToken(); pattern1(location ) :: patternAlts(location ) }
2547
2557
else Nil
2548
2558
2549
2559
/** Pattern1 ::= Pattern2 [Ascription]
2550
2560
* | ‘given’ PatVar ‘:’ RefinedType
2551
2561
*/
2552
- def pattern1 (): Tree =
2562
+ def pattern1 (location : Location = Location . InPattern ): Tree =
2553
2563
if (in.token == GIVEN ) {
2554
2564
val givenMod = atSpan(in.skipToken())(Mod .Given ())
2555
2565
atSpan(in.offset) {
@@ -2558,7 +2568,7 @@ object Parsers {
2558
2568
val name = in.name
2559
2569
in.nextToken()
2560
2570
accept(COLON )
2561
- val typed = ascription(Ident (nme.WILDCARD ), Location . InPattern )
2571
+ val typed = ascription(Ident (nme.WILDCARD ), location )
2562
2572
Bind (name, typed).withMods(addMod(Modifiers (), givenMod))
2563
2573
case _ =>
2564
2574
syntaxErrorOrIncomplete(" pattern variable expected" )
@@ -2570,7 +2580,7 @@ object Parsers {
2570
2580
val p = pattern2()
2571
2581
if (in.token == COLON ) {
2572
2582
in.nextToken()
2573
- ascription(p, Location . InPattern )
2583
+ ascription(p, location )
2574
2584
}
2575
2585
else p
2576
2586
}
@@ -2661,17 +2671,17 @@ object Parsers {
2661
2671
2662
2672
/** Patterns ::= Pattern [`,' Pattern]
2663
2673
*/
2664
- def patterns (): List [Tree ] = commaSeparated(pattern)
2665
-
2666
- def patternsOpt (): List [Tree ] =
2667
- if (in.token == RPAREN ) Nil else patterns()
2674
+ def patterns (location : Location = Location .InPattern ): List [Tree ] =
2675
+ commaSeparated(() => pattern(location))
2668
2676
2677
+ def patternsOpt (location : Location = Location .InPattern ): List [Tree ] =
2678
+ if (in.token == RPAREN ) Nil else patterns(location)
2669
2679
2670
2680
/** ArgumentPatterns ::= ‘(’ [Patterns] ‘)’
2671
2681
* | ‘(’ [Patterns ‘,’] Pattern2 ‘:’ ‘_’ ‘*’ ‘)’
2672
2682
*/
2673
2683
def argumentPatterns (): List [Tree ] =
2674
- inParens(patternsOpt())
2684
+ inParens(patternsOpt(Location . InPatternArgs ))
2675
2685
2676
2686
/* -------- MODIFIERS and ANNOTATIONS ------------------------------------------- */
2677
2687
0 commit comments