@@ -1085,9 +1085,19 @@ object Parsers {
1085
1085
}
1086
1086
1087
1087
/** Is the token sequence following the current `:` token classified as a lambda?
1088
- * This is the case if the input starts with an identifier, a wildcard, or
1089
- * something enclosed in (...) or [...], and this is followed by a `=>` or `?=>`
1090
- * and an INDENT.
1088
+ * If yes return a defined parsing function to parse the lambda body, if not
1089
+ * return None. The case is triggered in two :if the input starts with an identifier,
1090
+ * a wildcard, or something enclosed in (...) or [...], this is followed by a
1091
+ * `=>` or `?=>`, one one of the following two cases applies:
1092
+ * 1. The next token is an indent. In this case the return parsing function parses
1093
+ * an Expr in location Location.InColonArg.
1094
+ * 2. The next token is on the same line and the enclosing region is not `(...)`.
1095
+ * In this case the parsing function parses an Expr in location Location.InColonArg
1096
+ * enclosed in a SingleLineLambda region, and then eats the ENDlambda token
1097
+ * generated by the Scanner at the end of that region.
1098
+ * The reason for excluding (2) in regions enclosed in parentheses is to avoid
1099
+ * an ambiguity with type ascription `(x: A => B)`, where function types are only
1100
+ * allowed inside parentheses.
1091
1101
*/
1092
1102
def followingIsLambdaAfterColon (): Option [() => Tree ] =
1093
1103
val lookahead = in.LookaheadScanner (allowIndent = true )
@@ -1101,7 +1111,7 @@ object Parsers {
1101
1111
Some : () =>
1102
1112
val t = inSepRegion(SingleLineLambda (_)):
1103
1113
expr(Location .InColonArg )
1104
- accept(ENDLAMBDA )
1114
+ accept(ENDlambda )
1105
1115
t
1106
1116
else None
1107
1117
else None
@@ -1178,11 +1188,14 @@ object Parsers {
1178
1188
case _ => infixOp
1179
1189
}
1180
1190
1181
- /** True if we are seeing a lambda argument after a colon of the form:
1191
+ /** Optionally, if we are seeing a lambda argument after a colon of the form
1182
1192
* : (params) =>
1183
1193
* body
1194
+ * or a single-line lambda
1195
+ * : (params) => body
1196
+ * then return the function used to parse `body`.
1184
1197
*/
1185
- def isColonLambda : Option [() => Tree ] =
1198
+ def detectColonLambda : Option [() => Tree ] =
1186
1199
if sourceVersion.enablesFewerBraces && in.token == COLONfollow
1187
1200
then followingIsLambdaAfterColon()
1188
1201
else None
@@ -1209,7 +1222,7 @@ object Parsers {
1209
1222
opStack = OpInfo (top1, op, in.offset) :: opStack
1210
1223
colonAtEOLOpt()
1211
1224
newLineOptWhenFollowing(canStartOperand)
1212
- isColonLambda match
1225
+ detectColonLambda match
1213
1226
case Some (parseExpr) =>
1214
1227
in.nextToken()
1215
1228
recur(parseExpr())
@@ -2778,6 +2791,7 @@ object Parsers {
2778
2791
* | SimpleExpr1 ColonArgument
2779
2792
* ColonArgument ::= colon [LambdaStart]
2780
2793
* indent (CaseClauses | Block) outdent
2794
+ * | colon LambdaStart expr ENDlambda -- under experimental.relaxedLambdaSyntax
2781
2795
* LambdaStart ::= FunParams (‘=>’ | ‘?=>’)
2782
2796
* | TypTypeParamClause ‘=>’
2783
2797
* ColonArgBody ::= indent (CaseClauses | Block) outdent
@@ -2860,7 +2874,7 @@ object Parsers {
2860
2874
makeParameter(name.asTermName, typedOpt(), Modifiers (), isBackquoted = isBackquoted(id))
2861
2875
}
2862
2876
case _ => t
2863
- else isColonLambda match
2877
+ else detectColonLambda match
2864
2878
case Some (parseExpr) =>
2865
2879
val app =
2866
2880
atSpan(startOffset(t), in.skipToken()):
0 commit comments