@@ -83,6 +83,14 @@ object Parsers {
83
83
def atPos [T <: Positioned ](start : Offset )(t : T ): T =
84
84
atPos(start, start)(t)
85
85
86
+ /** Defensive version of Position#start */
87
+ def startPos (t : Positioned ): Int =
88
+ if (t.pos.exists) t.pos.start else in.offset
89
+
90
+ /** Defensive version of Position#end */
91
+ def endPos (t : Positioned ): Int =
92
+ if (t.pos.exists) t.pos.end else in.lastOffset
93
+
86
94
def nameStart : Offset =
87
95
if (in.token == BACKQUOTED_IDENT ) in.offset + 1 else in.offset
88
96
@@ -448,7 +456,7 @@ object Parsers {
448
456
val topInfo = opStack.head
449
457
opStack = opStack.tail
450
458
val od = reduceStack(base, topInfo.operand, 0 , true )
451
- return atPos(od.pos.start , topInfo.offset) {
459
+ return atPos(startPos(od) , topInfo.offset) {
452
460
PostfixOp (od, topInfo.operator)
453
461
}
454
462
}
@@ -492,7 +500,7 @@ object Parsers {
492
500
493
501
/** Accept identifier acting as a selector on given tree `t`. */
494
502
def selector (t : Tree ): Tree =
495
- atPos(t.pos.start , in.offset) { Select (t, ident()) }
503
+ atPos(startPos(t) , in.offset) { Select (t, ident()) }
496
504
497
505
/** Selectors ::= ident { `.' ident()
498
506
*
@@ -727,7 +735,7 @@ object Parsers {
727
735
728
736
def refinedTypeRest (t : Tree ): Tree = {
729
737
newLineOptWhenFollowedBy(LBRACE )
730
- if (in.token == LBRACE ) refinedTypeRest(atPos(t.pos.start ) { RefinedTypeTree (t, refinement()) })
738
+ if (in.token == LBRACE ) refinedTypeRest(atPos(startPos(t) ) { RefinedTypeTree (t, refinement()) })
731
739
else t
732
740
}
733
741
@@ -748,7 +756,7 @@ object Parsers {
748
756
def annotType (): Tree = annotTypeRest(simpleType())
749
757
750
758
def annotTypeRest (t : Tree ): Tree =
751
- if (in.token == AT ) annotTypeRest(atPos(t.pos.start ) { Annotated (t, annot()) })
759
+ if (in.token == AT ) annotTypeRest(atPos(startPos(t) ) { Annotated (t, annot()) })
752
760
else t
753
761
754
762
/** SimpleType ::= SimpleType TypeArgs
@@ -779,19 +787,19 @@ object Parsers {
779
787
val handleSingletonType : Tree => Tree = t =>
780
788
if (in.token == TYPE ) {
781
789
in.nextToken()
782
- atPos(t.pos.start ) { SingletonTypeTree (t) }
790
+ atPos(startPos(t) ) { SingletonTypeTree (t) }
783
791
} else t
784
792
785
793
private def simpleTypeRest (t : Tree ): Tree = in.token match {
786
794
case HASH => simpleTypeRest(typeProjection(t))
787
- case LBRACKET => simpleTypeRest(atPos(t.pos.start ) { AppliedTypeTree (t, typeArgs(namedOK = true )) })
795
+ case LBRACKET => simpleTypeRest(atPos(startPos(t) ) { AppliedTypeTree (t, typeArgs(namedOK = true )) })
788
796
case _ => t
789
797
}
790
798
791
799
private def typeProjection (t : Tree ): Tree = {
792
800
accept(HASH )
793
801
val id = typeIdent()
794
- atPos(t.pos.start, id.pos.start ) { Select (t, id.name) }
802
+ atPos(startPos(t), startPos(id) ) { Select (t, id.name) }
795
803
}
796
804
797
805
/** NamedTypeArg ::= id `=' Type
@@ -845,7 +853,7 @@ object Parsers {
845
853
val t = toplevelTyp()
846
854
if (isIdent(nme.raw.STAR )) {
847
855
in.nextToken()
848
- atPos(t.pos.start ) { PostfixOp (t, nme.raw.STAR ) }
856
+ atPos(startPos(t) ) { PostfixOp (t, nme.raw.STAR ) }
849
857
} else t
850
858
}
851
859
@@ -970,7 +978,7 @@ object Parsers {
970
978
val t = expr1(location)
971
979
if (in.token == ARROW ) {
972
980
placeholderParams = saved
973
- closureRest(t.pos.start , location, convertToParams(t))
981
+ closureRest(startPos(t) , location, convertToParams(t))
974
982
}
975
983
else if (isWildcard(t)) {
976
984
placeholderParams = placeholderParams ::: saved
@@ -1024,7 +1032,7 @@ object Parsers {
1024
1032
assert(handlerStart != - 1 )
1025
1033
syntaxError(
1026
1034
new EmptyCatchBlock (body),
1027
- Position (handlerStart, handler.pos.end )
1035
+ Position (handlerStart, endPos( handler) )
1028
1036
)
1029
1037
case _ =>
1030
1038
}
@@ -1034,7 +1042,7 @@ object Parsers {
1034
1042
else {
1035
1043
if (handler.isEmpty) warning(
1036
1044
EmptyCatchAndFinallyBlock (body),
1037
- source atPos Position (tryOffset, body.pos.end )
1045
+ source atPos Position (tryOffset, endPos( body) )
1038
1046
)
1039
1047
EmptyTree
1040
1048
}
@@ -1056,21 +1064,21 @@ object Parsers {
1056
1064
case EQUALS =>
1057
1065
t match {
1058
1066
case Ident (_) | Select (_, _) | Apply (_, _) =>
1059
- atPos(t.pos.start , in.skipToken()) { Assign (t, expr()) }
1067
+ atPos(startPos(t) , in.skipToken()) { Assign (t, expr()) }
1060
1068
case _ =>
1061
1069
t
1062
1070
}
1063
1071
case COLON =>
1064
1072
ascription(t, location)
1065
1073
case MATCH =>
1066
- atPos(t.pos.start , in.skipToken()) {
1074
+ atPos(startPos(t) , in.skipToken()) {
1067
1075
inBraces(Match (t, caseClauses()))
1068
1076
}
1069
1077
case _ =>
1070
1078
t
1071
1079
}
1072
1080
1073
- def ascription (t : Tree , location : Location .Value ) = atPos(t.pos.start , in.skipToken()) {
1081
+ def ascription (t : Tree , location : Location .Value ) = atPos(startPos(t) , in.skipToken()) {
1074
1082
in.token match {
1075
1083
case USCORE =>
1076
1084
val uscoreStart = in.skipToken()
@@ -1104,7 +1112,7 @@ object Parsers {
1104
1112
val id = termIdent()
1105
1113
val paramExpr =
1106
1114
if (location == Location .InBlock && in.token == COLON )
1107
- atPos(id.pos.start , in.skipToken()) { Typed (id, infixType()) }
1115
+ atPos(startPos(id) , in.skipToken()) { Typed (id, infixType()) }
1108
1116
else
1109
1117
id
1110
1118
closureRest(start, location, convertToParam(paramExpr, mods) :: Nil )
@@ -1193,13 +1201,13 @@ object Parsers {
1193
1201
in.nextToken()
1194
1202
simpleExprRest(selector(t), canApply = true )
1195
1203
case LBRACKET =>
1196
- val tapp = atPos(t.pos.start , in.offset) { TypeApply (t, typeArgs(namedOK = true )) }
1204
+ val tapp = atPos(startPos(t) , in.offset) { TypeApply (t, typeArgs(namedOK = true )) }
1197
1205
simpleExprRest(tapp, canApply = true )
1198
1206
case LPAREN | LBRACE if canApply =>
1199
- val app = atPos(t.pos.start , in.offset) { Apply (t, argumentExprs()) }
1207
+ val app = atPos(startPos(t) , in.offset) { Apply (t, argumentExprs()) }
1200
1208
simpleExprRest(app, canApply = true )
1201
1209
case USCORE =>
1202
- atPos(t.pos.start , in.skipToken()) { PostfixOp (t, nme.WILDCARD ) }
1210
+ atPos(startPos(t) , in.skipToken()) { PostfixOp (t, nme.WILDCARD ) }
1203
1211
case _ =>
1204
1212
t
1205
1213
}
@@ -1283,7 +1291,7 @@ object Parsers {
1283
1291
if (in.token == IF ) guard()
1284
1292
else {
1285
1293
val pat = pattern1()
1286
- if (in.token == EQUALS ) atPos(pat.pos.start , in.skipToken()) { GenAlias (pat, expr()) }
1294
+ if (in.token == EQUALS ) atPos(startPos( pat) , in.skipToken()) { GenAlias (pat, expr()) }
1287
1295
else generatorRest(pat)
1288
1296
}
1289
1297
@@ -1292,7 +1300,7 @@ object Parsers {
1292
1300
def generator (): Tree = generatorRest(pattern1())
1293
1301
1294
1302
def generatorRest (pat : Tree ) =
1295
- atPos(pat.pos.start , accept(LARROW )) { GenFrom (pat, expr()) }
1303
+ atPos(startPos( pat) , accept(LARROW )) { GenFrom (pat, expr()) }
1296
1304
1297
1305
/** ForExpr ::= `for' (`(' Enumerators `)' | `{' Enumerators `}')
1298
1306
* {nl} [`yield'] Expr
@@ -1356,7 +1364,7 @@ object Parsers {
1356
1364
val pattern = () => {
1357
1365
val pat = pattern1()
1358
1366
if (isIdent(nme.raw.BAR ))
1359
- atPos(pat.pos.start ) { Alternative (pat :: patternAlts()) }
1367
+ atPos(startPos( pat) ) { Alternative (pat :: patternAlts()) }
1360
1368
else pat
1361
1369
}
1362
1370
@@ -1382,15 +1390,15 @@ object Parsers {
1382
1390
// compatibility for Scala2 `x @ _*` syntax
1383
1391
infixPattern() match {
1384
1392
case pt @ Ident (tpnme.WILDCARD_STAR ) =>
1385
- migrationWarningOrError(" The syntax `x @ _*' is no longer supported; use `x : _*' instead" , p.pos.start )
1386
- atPos(p.pos.start , offset) { Typed (p, pt) }
1393
+ migrationWarningOrError(" The syntax `x @ _*' is no longer supported; use `x : _*' instead" , startPos(p) )
1394
+ atPos(startPos(p) , offset) { Typed (p, pt) }
1387
1395
case p =>
1388
- atPos(p.pos.start , offset) { Bind (name, p) }
1396
+ atPos(startPos(p) , offset) { Bind (name, p) }
1389
1397
}
1390
1398
case p @ Ident (tpnme.WILDCARD_STAR ) =>
1391
1399
// compatibility for Scala2 `_*` syntax
1392
- migrationWarningOrError(" The syntax `_*' is no longer supported; use `x : _*' instead" , p.pos.start )
1393
- atPos(p.pos.start ) { Typed (Ident (nme.WILDCARD ), p) }
1400
+ migrationWarningOrError(" The syntax `_*' is no longer supported; use `x : _*' instead" , startPos(p) )
1401
+ atPos(startPos(p) ) { Typed (Ident (nme.WILDCARD ), p) }
1394
1402
case p =>
1395
1403
p
1396
1404
}
@@ -1414,7 +1422,7 @@ object Parsers {
1414
1422
val simplePattern = () => in.token match {
1415
1423
case IDENTIFIER | BACKQUOTED_IDENT | THIS =>
1416
1424
path(thisOK = true ) match {
1417
- case id @ Ident (nme.raw.MINUS ) if isNumericLit => literal(id.pos.start )
1425
+ case id @ Ident (nme.raw.MINUS ) if isNumericLit => literal(startPos(id) )
1418
1426
case t => simplePatternRest(t)
1419
1427
}
1420
1428
case USCORE =>
@@ -1444,9 +1452,9 @@ object Parsers {
1444
1452
def simplePatternRest (t : Tree ): Tree = {
1445
1453
var p = t
1446
1454
if (in.token == LBRACKET )
1447
- p = atPos(t.pos.start , in.offset) { TypeApply (p, typeArgs()) }
1455
+ p = atPos(startPos(t) , in.offset) { TypeApply (p, typeArgs()) }
1448
1456
if (in.token == LPAREN )
1449
- p = atPos(t.pos.start , in.offset) { Apply (p, argumentPatterns()) }
1457
+ p = atPos(startPos(t) , in.offset) { Apply (p, argumentPatterns()) }
1450
1458
p
1451
1459
}
1452
1460
@@ -1572,7 +1580,8 @@ object Parsers {
1572
1580
case Select (qual, name) => cpy.Select (tree)(adjustStart(start)(qual), name)
1573
1581
case _ => tree
1574
1582
}
1575
- if (start < tree1.pos.start) tree1.withPos(tree1.pos.withStart(start))
1583
+ if (tree1.pos.exists && start < tree1.pos.start)
1584
+ tree1.withPos(tree1.pos.withStart(start))
1576
1585
else tree1
1577
1586
}
1578
1587
@@ -1803,7 +1812,7 @@ object Parsers {
1803
1812
def importSelector (): Tree = {
1804
1813
val from = termIdentOrWildcard()
1805
1814
if (from.name != nme.WILDCARD && in.token == ARROW )
1806
- atPos(from.pos.start , in.skipToken()) {
1815
+ atPos(startPos( from) , in.skipToken()) {
1807
1816
Thicket (from, termIdentOrWildcard())
1808
1817
}
1809
1818
else from
0 commit comments