File tree 2 files changed +66
-5
lines changed
src/compiler/scala/tools/nsc/ast/parser 2 files changed +66
-5
lines changed Original file line number Diff line number Diff line change @@ -665,6 +665,15 @@ self =>
665
665
}
666
666
def isLiteral = isLiteralToken(in.token)
667
667
668
+ def isSimpleExprIntroToken (token : Token ): Boolean = isLiteralToken(token) || (token match {
669
+ case IDENTIFIER | BACKQUOTED_IDENT |
670
+ THIS | SUPER | NEW | USCORE |
671
+ LPAREN | LBRACE | XMLSTART => true
672
+ case _ => false
673
+ })
674
+
675
+ def isSimpleExprIntro : Boolean = isExprIntroToken(in.token)
676
+
668
677
def isExprIntroToken (token : Token ): Boolean = isLiteralToken(token) || (token match {
669
678
case IDENTIFIER | BACKQUOTED_IDENT |
670
679
THIS | SUPER | IF | FOR | NEW | USCORE | TRY | WHILE |
@@ -1565,11 +1574,14 @@ self =>
1565
1574
def prefixExpr (): Tree = {
1566
1575
if (isUnaryOp) {
1567
1576
atPos(in.offset) {
1568
- val name = nme.toUnaryName(rawIdent().toTermName)
1569
- if (name == nme.UNARY_- && isNumericLit)
1570
- simpleExprRest(literal(isNegated = true ), canApply = true )
1571
- else
1572
- Select (stripParens(simpleExpr()), name)
1577
+ if (lookingAhead(isSimpleExprIntro)) {
1578
+ val uname = nme.toUnaryName(rawIdent().toTermName)
1579
+ if (uname == nme.UNARY_- && isNumericLit)
1580
+ simpleExprRest(literal(isNegated = true ), canApply = true )
1581
+ else
1582
+ Select (stripParens(simpleExpr()), uname)
1583
+ }
1584
+ else simpleExpr()
1573
1585
}
1574
1586
}
1575
1587
else simpleExpr()
Original file line number Diff line number Diff line change
1
+
2
+
3
+ import scala .tools .partest .SessionTest
4
+
5
+ // Taking unary ids as plain
6
+ object Test extends SessionTest {
7
+ def session =
8
+ """ Type in expressions to have them evaluated.
9
+ Type :help for more information.
10
+
11
+ scala> val - = 42
12
+ -: Int = 42
13
+
14
+ scala> val i = -
15
+ i: Int = 42
16
+
17
+ scala> - { 42 }
18
+ res0: Int = -42
19
+
20
+ scala> - if (true) 1 else 2
21
+ <console>:1: error: illegal start of simple expression
22
+ - if (true) 1 else 2
23
+ ^
24
+
25
+ scala> - - 1
26
+ <console>:1: error: ';' expected but integer literal found.
27
+ - - 1
28
+ ^
29
+
30
+ scala> -.-(1)
31
+ res1: Int = 41
32
+
33
+ scala> -
34
+ res2: Int = 42
35
+
36
+ scala> - -
37
+ res3: Int = -42
38
+
39
+ scala> + -
40
+ res4: Int = 42
41
+
42
+ scala> object X { def -(i: Int) = 42 - i ; def f(g: Int => Int) = g(7) ; def j = f(-) }
43
+ defined object X
44
+
45
+ scala> X.j
46
+ res5: Int = 35
47
+
48
+ scala> :quit"""
49
+ }
You can’t perform that action at this time.
0 commit comments