Appearance
| 1 | namespace Syntax.Parsers.Statements is | |
| 2 | use IO.Std | |
| 3 | use Ghul.Disposable | |
| 4 | ||
| 5 | use Ghul.Pipes | |
| 6 | ||
| 7 | class STATEMENT( | |
| 8 | labellable_statement_tokens: Collections.LIST[Lexical.TOKEN], | |
| 9 | identifier_parser: Parser[Trees.Identifiers.Identifier], | |
| 10 | expression_parser: Parser[Trees.Expressions.Expression], | |
| 11 | expression_list_parser: Parser[Trees.Expressions.LIST], | |
| 12 | variable_parser: Parser[Trees.Variables.VARIABLE], | |
| 13 | variable_list_parser: Parser[Trees.Variables.LIST], | |
| 14 | statement_list_parser: Parser[Trees.Statements.LIST], | |
| 15 | pragma_parser: Parser[Trees.Statements.PRAGMA] | |
| 16 | ): Base[Trees.Statements.Statement] is | |
| 17 | description: string => "statement" | |
| 18 | ||
| 19 | super() | |
| 20 | ||
| 21 | init(..) is | |
| 22 | add_parsers() | |
| 23 | si | |
| 24 | ||
| 25 | add_parsers() is | |
| 26 | // note: this has to be the exact same set as accepted by the primary expression parsers | |
| 27 | // plus TOKEN.OPERAOR. Otherwise return statements with an expression, but without a semicolon, | |
| 28 | // won't recognize the expression as a primary expression. | |
| 29 | let primary_expression_tokens = [ | |
| 30 | Lexical.TOKEN.IDENTIFIER, | |
| 31 | Lexical.TOKEN.SQUARE_OPEN, | |
| 32 | Lexical.TOKEN.ARRAY_DEF, | |
| 33 | Lexical.TOKEN.PAREN_OPEN, | |
| 34 | Lexical.TOKEN.NEW, | |
| 35 | Lexical.TOKEN.CAST, | |
| 36 | Lexical.TOKEN.ISA, | |
| 37 | Lexical.TOKEN.TYPEOF, | |
| 38 | Lexical.TOKEN.INT_LITERAL, | |
| 39 | Lexical.TOKEN.FLOAT_LITERAL, | |
| 40 | Lexical.TOKEN.STRING_LITERAL, | |
| 41 | Lexical.TOKEN.ENTER_STRING, | |
| 42 | Lexical.TOKEN.CHAR_LITERAL, | |
| 43 | Lexical.TOKEN.TRUE, | |
| 44 | Lexical.TOKEN.FALSE, | |
| 45 | Lexical.TOKEN.NULL, | |
| 46 | Lexical.TOKEN.SELF, | |
| 47 | Lexical.TOKEN.SUPER, | |
| 48 | Lexical.TOKEN.REC, | |
| 49 | Lexical.TOKEN.IF, | |
| 50 | Lexical.TOKEN.CASE, | |
| 51 | Lexical.TOKEN.LET, | |
| 52 | Lexical.TOKEN.AWAIT, | |
| 53 | Lexical.TOKEN.USE, | |
| 54 | Lexical.TOKEN.VAL, | |
| 55 | Lexical.TOKEN.DO, | |
| 56 | Lexical.TOKEN.FOR, | |
| 57 | Lexical.TOKEN.WHILE, | |
| 58 | Lexical.TOKEN.OPERATOR | |
| 59 | ] | |
| 60 | ||
| 61 | add_parser( | |
| 62 | (context) -> Trees.Statements.Statement is | |
| 63 | let start = context.location | |
| 64 | if context.next_token(Lexical.TOKEN.LET, syntax_error_message) then | |
| 65 | let want_dispose mut = false | |
| 66 | if context.current_token == Lexical.TOKEN.USE then | |
| 67 | context.next_token() | |
| 68 | ||
| 69 | want_dispose = true | |
| 70 | fi | |
| 71 | ||
| 72 | let variable_list = variable_list_parser.parse(context)! | |
| 73 | ||
| 74 | if context.current_token == Lexical.TOKEN.IN then | |
| 75 | context.next_token() | |
| 76 | ||
| 77 | let expression = expression_parser.parse(context)! | |
| 78 | let location = start::expression.location | |
| 79 | ||
| 80 | return Trees.Statements.EXPRESSION( | |
| 81 | location, | |
| 82 | Trees.Expressions.LET_IN( | |
| 83 | location, | |
| 84 | want_dispose, | |
| 85 | variable_list, | |
| 86 | expression | |
| 87 | ) | |
| 88 | ) | |
| 89 | fi | |
| 90 | ||
| 91 | return Trees.Statements.LET(start::variable_list.location, want_dispose, variable_list) | |
| 92 | fi | |
| 93 | si, | |
| 94 | Lexical.TOKEN.LET | |
| 95 | ) | |
| 96 | ||
| 97 | add_parser( | |
| 98 | (context) is | |
| 99 | let start = context.location | |
| 100 | let end mut = context.location | |
| 101 | ||
| 102 | if context.next_token(Lexical.TOKEN.RETURN, syntax_error_message) then | |
| 103 | let expression: Trees.Expressions.Expression? mut = null | |
| 104 | if | |
| 105 | context.current.token != Lexical.TOKEN.SEMICOLON /\ | |
| 106 | primary_expression_tokens |> any(t => t == context.current.token) | |
| 107 | then | |
| 108 | expression = expression_parser.parse(context)! | |
| 109 | end = expression.location | |
| 110 | fi | |
| 111 | ||
| 112 | return Trees.Statements.RETURN(start::end, expression) | |
| 113 | fi | |
| 114 | si, | |
| 115 | Lexical.TOKEN.RETURN | |
| 116 | ) | |
| 117 | ||
| 118 | add_parser( | |
| 119 | (context) is | |
| 120 | let start = context.location | |
| 121 | let end mut = context.location | |
| 122 | if context.next_token(Lexical.TOKEN.YIELD, syntax_error_message) then | |
| 123 | // `in` cannot begin an expression, so its presence | |
| 124 | // here is unambiguously the yield-every-element form. | |
| 125 | let all = context.current.token == Lexical.TOKEN.IN | |
| 126 | ||
| 127 | if all then | |
| 128 | context.next_token() | |
| 129 | fi | |
| 130 | ||
| 131 | let expression = expression_parser.parse(context)! | |
| 132 | end = expression.location | |
| 133 | ||
| 134 | if all then | |
| 135 | return Trees.Statements.YIELD_ALL(start::end, expression) | |
| 136 | fi | |
| 137 | ||
| 138 | return Trees.Statements.YIELD(start::end, expression) | |
| 139 | fi | |
| 140 | si, | |
| 141 | Lexical.TOKEN.YIELD | |
| 142 | ) | |
| 143 | ||
| 144 | add_parser( | |
| 145 | (context) is | |
| 146 | let start = context.location | |
| 147 | let end mut = context.location | |
| 148 | if context.next_token(Lexical.TOKEN.THROW, syntax_error_message) then | |
| 149 | let expression: Trees.Expressions.Expression? mut = null | |
| 150 | if context.current.token != Lexical.TOKEN.SEMICOLON then | |
| 151 | expression = expression_parser.parse(context)! | |
| 152 | end = expression.location | |
| 153 | fi | |
| 154 | ||
| 155 | return Trees.Statements.THROW(start::end, expression) | |
| 156 | fi | |
| 157 | si, | |
| 158 | Lexical.TOKEN.THROW | |
| 159 | ) | |
| 160 | ||
| 161 | add_parser( | |
| 162 | (context) is | |
| 163 | let start = context.location | |
| 164 | if context.next_token(Lexical.TOKEN.ASSERT, syntax_error_message) then | |
| 165 | let end mut = start | |
| 166 | ||
| 167 | let expression = expression_parser.parse(context)! | |
| 168 | end = expression.location | |
| 169 | ||
| 170 | let message: Trees.Expressions.Expression? mut = null | |
| 171 | ||
| 172 | if context.current_token == Lexical.TOKEN.ELSE /\ context.continues(start) then | |
| 173 | context.next_token() | |
| 174 | ||
| 175 | message = expression_parser.parse(context)! | |
| 176 | end = message.location | |
| 177 | fi | |
| 178 | ||
| 179 | if context.current_token == Lexical.TOKEN.IN then | |
| 180 | context.next_token() | |
| 181 | ||
| 182 | let inner = expression_parser.parse(context)! | |
| 183 | let location = start::inner.location | |
| 184 | ||
| 185 | return Trees.Statements.EXPRESSION( | |
| 186 | location, | |
| 187 | Trees.Expressions.ASSERT_IN( | |
| 188 | location, | |
| 189 | expression, | |
| 190 | message, | |
| 191 | inner | |
| 192 | ) | |
| 193 | ) | |
| 194 | fi | |
| 195 | ||
| 196 | let result = Trees.Statements.ASSERT( | |
| 197 | start::end, | |
| 198 | expression, | |
| 199 | message | |
| 200 | ) | |
| 201 | ||
| 202 | return result | |
| 203 | fi | |
| 204 | si, | |
| 205 | Lexical.TOKEN.ASSERT | |
| 206 | ) | |
| 207 | ||
| 208 | add_parser( | |
| 209 | (context) => parse_if(context), | |
| 210 | Lexical.TOKEN.IF | |
| 211 | ) | |
| 212 | ||
| 213 | add_parser( | |
| 214 | (context) is | |
| 215 | let start = context.location | |
| 216 | ||
| 217 | context.next_token(Lexical.TOKEN.CASE) | |
| 218 | ||
| 219 | // An arm's statements end at the next arm as well as at | |
| 220 | // the `esac`. | |
| 221 | let use open = | |
| 222 | context.open_construct( | |
| 223 | Collections.LIST[Lexical.TOKEN]([ | |
| 224 | Lexical.TOKEN.WHEN, | |
| 225 | Lexical.TOKEN.ELSE, | |
| 226 | Lexical.TOKEN.ESAC | |
| 227 | ]) | |
| 228 | ) | |
| 229 | let expression = expression_parser.parse(context)! | |
| 230 | let seen_default mut = false | |
| 231 | let saw_malformed_pattern mut = false | |
| 232 | let match_list = Collections.LIST[Trees.Statements.CASE_MATCH]() | |
| 233 | ||
| 234 | while | |
| 235 | context.current.token == Lexical.TOKEN.WHEN \/ | |
| 236 | context.current.token == Lexical.TOKEN.ELSE | |
| 237 | do | |
| 238 | let match_start = context.location | |
| 239 | let match_expressions: Trees.Expressions.LIST? mut = null | |
| 240 | let match_pattern: Trees.Variables.VARIABLE? mut = null | |
| 241 | let match_guard: Trees.Expressions.Expression? mut = null | |
| 242 | let is_malformed_pattern mut = false | |
| 243 | if context.current.token == Lexical.TOKEN.WHEN then | |
| 244 | context.next_token(Lexical.TOKEN.WHEN) | |
| 245 | ||
| 246 | let classification = classify_when_arm(context) | |
| 247 | ||
| 248 | if classification.is_pattern then | |
| 249 | let parsed = _parse_pattern(context) | |
| 250 | ||
| 251 | if parsed? then | |
| 252 | parsed.mark_refutable() | |
| 253 | match_pattern = parsed | |
| 254 | fi | |
| 255 | ||
| 256 | if is_guard_operator(context) then | |
| 257 | context.next_token() | |
| 258 | match_guard = expression_parser.parse(context) | |
| 259 | fi | |
| 260 | elif classification.error_message? then | |
| 261 | context.error(classification.error_location!, classification.error_message!) | |
| 262 | ||
| 263 | is_malformed_pattern = true | |
| 264 | saw_malformed_pattern = true | |
| 265 | ||
| 266 | // classify_when_arm only speculated, so parse | |
| 267 | // the pattern for real before recovering the | |
| 268 | // offending shape. | |
| 269 | _parse_pattern(context) | |
| 270 | ||
| 271 | if is_guard_operator(context) then | |
| 272 | context.next_token() | |
| 273 | expression_parser.parse(context) | |
| 274 | fi | |
| 275 | ||
| 276 | if context.current_token == Lexical.TOKEN.PAREN_OPEN then | |
| 277 | expression_parser.parse(context) | |
| 278 | else | |
| 279 | while context.current_token == Lexical.TOKEN.COMMA do | |
| 280 | context.next_token() | |
| 281 | _parse_pattern(context) | |
| 282 | ||
| 283 | if is_guard_operator(context) then | |
| 284 | context.next_token() | |
| 285 | expression_parser.parse(context) | |
| 286 | fi | |
| 287 | od | |
| 288 | fi | |
| 289 | else | |
| 290 | match_expressions = expression_list_parser.parse(context) | |
| 291 | fi | |
| 292 | ||
| 293 | context.next_token(Lexical.TOKEN.THEN) | |
| 294 | else | |
| 295 | if seen_default then | |
| 296 | context.error(context.location, "more than one else arm in case statement") | |
| 297 | else | |
| 298 | seen_default = true | |
| 299 | fi | |
| 300 | context.next_token(Lexical.TOKEN.ELSE) | |
| 301 | fi | |
| 302 | let match_statements = statement_list_parser.parse(context)! | |
| 303 | let match = Trees.Statements.CASE_MATCH(match_start::match_statements.location, match_expressions, match_pattern, match_guard, match_statements) | |
| 304 | ||
| 305 | if is_malformed_pattern then | |
| 306 | match.poison() | |
| 307 | fi | |
| 308 | ||
| 309 | match_list.add(match) | |
| 310 | od | |
| 311 | ||
| 312 | let result = Trees.Statements.CASE(start::context.location, expression, match_list) | |
| 313 | context.expect_closer(Lexical.TOKEN.ESAC) | |
| 314 | ||
| 315 | if saw_malformed_pattern then | |
| 316 | result.poison() | |
| 317 | fi | |
| 318 | ||
| 319 | return result | |
| 320 | si, | |
| 321 | Lexical.TOKEN.CASE | |
| 322 | ) | |
| 323 | ||
| 324 | add_parser( | |
| 325 | (context) is | |
| 326 | let start = context.location | |
| 327 | context.next_token(Lexical.TOKEN.TRY) | |
| 328 | ||
| 329 | let use open = | |
| 330 | context.open_construct( | |
| 331 | Collections.LIST[Lexical.TOKEN]([ | |
| 332 | Lexical.TOKEN.CATCH, | |
| 333 | Lexical.TOKEN.FINALLY, | |
| 334 | Lexical.TOKEN.YRT | |
| 335 | ]) | |
| 336 | ) | |
| 337 | ||
| 338 | let body = statement_list_parser.parse(context)! | |
| 339 | let catches = Collections.LIST[Trees.Statements.CATCH]() | |
| 340 | ||
| 341 | while context.current.token == Lexical.TOKEN.CATCH do | |
| 342 | let catch_start = context.location | |
| 343 | context.next_token() | |
| 344 | let catch_variable = variable_parser.parse(context) | |
| 345 | let catch_body = statement_list_parser.parse(context)! | |
| 346 | catches.add(Trees.Statements.CATCH(catch_start::catch_body.location, catch_variable, catch_body)) | |
| 347 | od | |
| 348 | ||
| 349 | let `finally: Trees.Statements.LIST? mut = _ | |
| 350 | ||
| 351 | if context.current.token == Lexical.TOKEN.FINALLY then | |
| 352 | context.next_token() | |
| 353 | `finally = statement_list_parser.parse(context) | |
| 354 | fi | |
| 355 | ||
| 356 | while context.current.token == Lexical.TOKEN.CATCH do | |
| 357 | context.error(context.location, "catches cannot appear after finally") | |
| 358 | let catch_start = context.location | |
| 359 | context.next_token() | |
| 360 | let catch_variable = variable_parser.parse(context) | |
| 361 | let catch_body = statement_list_parser.parse(context)! | |
| 362 | catches.add(Trees.Statements.CATCH(catch_start::catch_body.location, catch_variable, catch_body)) | |
| 363 | od | |
| 364 | ||
| 365 | let result = Trees.Statements.TRY(start::context.location, start, body, catches, `finally) | |
| 366 | context.expect_closer(Lexical.TOKEN.YRT) | |
| 367 | return result | |
| 368 | si, | |
| 369 | Lexical.TOKEN.TRY | |
| 370 | ) | |
| 371 | ||
| 372 | add_parser( | |
| 373 | (context) is | |
| 374 | let start = context.location | |
| 375 | let condition: Trees.Expressions.Expression? mut = _ | |
| 376 | let binding: Trees.Statements.REFUTABLE_BINDING? mut = _ | |
| 377 | ||
| 378 | if context.current.token == Lexical.TOKEN.WHILE then | |
| 379 | context.next_token() | |
| 380 | ||
| 381 | if context.current_token == Lexical.TOKEN.LET then | |
| 382 | binding = try_parse_let_binding(context, Lexical.TOKEN.DO, "while") | |
| 383 | fi | |
| 384 | ||
| 385 | if !binding? then | |
| 386 | condition = expression_parser.parse(context) | |
| 387 | fi | |
| 388 | fi | |
| 389 | ||
| 390 | if context.next_token(Lexical.TOKEN.DO, syntax_error_message) then | |
| 391 | let use open = context.open_construct(Lexical.TOKEN.OD) | |
| 392 | ||
| 393 | let body = statement_list_parser.parse(context)! | |
| 394 | ||
| 395 | let result = Trees.Statements.DO(start::context.location, condition, binding, body) | |
| 396 | ||
| 397 | context.expect_closer(Lexical.TOKEN.OD) | |
| 398 | ||
| 399 | return result | |
| 400 | else | |
| 401 | return | |
| 402 | Trees.Statements.DO( | |
| 403 | start::context.current.location, | |
| 404 | condition, | |
| 405 | binding, | |
| 406 | Trees.Statements.LIST(start::context.current.location, Collections.LIST[Trees.Statements.Statement](0))) | |
| 407 | fi | |
| 408 | si, | |
| 409 | [Lexical.TOKEN.WHILE, Lexical.TOKEN.DO] | |
| 410 | ) | |
| 411 | ||
| 412 | add_parser( | |
| 413 | (context) is | |
| 414 | let start = context.location | |
| 415 | context.next_token(Lexical.TOKEN.FOR) | |
| 416 | ||
| 417 | let variable = variable_parser.parse(context) | |
| 418 | let expression: Trees.Expressions.Expression? mut = _ | |
| 419 | let body: Trees.Statements.LIST? mut = _ | |
| 420 | ||
| 421 | if (variable? /\ !variable.is_poisoned) \/ context.current_token == Lexical.TOKEN.IN then | |
| 422 | if context.next_token(Lexical.TOKEN.IN, "in for statement") then | |
| 423 | expression = expression_parser.parse(context) | |
| 424 | fi | |
| 425 | fi | |
| 426 | ||
| 427 | if (expression? /\ !expression.is_poisoned) \/ context.current_token == Lexical.TOKEN.DO then | |
| 428 | if context.next_token(Lexical.TOKEN.DO, "in for statement") then | |
| 429 | let use open = context.open_construct(Lexical.TOKEN.OD) | |
| 430 | ||
| 431 | body = statement_list_parser.parse(context) | |
| 432 | fi | |
| 433 | fi | |
| 434 | ||
| 435 | let result = Trees.Statements.FOR(start::context.location, variable, expression, body) | |
| 436 | ||
| 437 | if body? \/ context.current_token == Lexical.TOKEN.OD then | |
| 438 | context.expect_closer(Lexical.TOKEN.OD, "in for statement") | |
| 439 | fi | |
| 440 | ||
| 441 | if variable? \/ expression? \/ body? then | |
| 442 | return result | |
| 443 | fi | |
| 444 | si, | |
| 445 | Lexical.TOKEN.FOR | |
| 446 | ) | |
| 447 | ||
| 448 | add_parser( | |
| 449 | (context) is | |
| 450 | let start = context.location | |
| 451 | let end mut = context.location | |
| 452 | context.next_token() | |
| 453 | let expression: Trees.Expressions.Expression? mut = _ | |
| 454 | ||
| 455 | if | |
| 456 | context.current.token != Lexical.TOKEN.SEMICOLON /\ | |
| 457 | primary_expression_tokens |> any(t => t == context.current.token) | |
| 458 | then | |
| 459 | expression = expression_parser.parse(context)! | |
| 460 | end = expression.location | |
| 461 | fi | |
| 462 | ||
| 463 | return Trees.Statements.BREAK(start::end, expression) | |
| 464 | si, | |
| 465 | Lexical.TOKEN.BREAK | |
| 466 | ) | |
| 467 | ||
| 468 | add_parser( | |
| 469 | (context) is | |
| 470 | let start = context.location | |
| 471 | let end mut = context.location | |
| 472 | context.next_token() | |
| 473 | let label: Trees.Identifiers.Identifier? mut = _ | |
| 474 | if context.current.token == Lexical.TOKEN.IDENTIFIER then | |
| 475 | let parsed_label = identifier_parser.parse(context)! | |
| 476 | label = parsed_label | |
| 477 | end = parsed_label.location | |
| 478 | fi | |
| 479 | ||
| 480 | let result = Trees.Statements.CONTINUE(start::end, label) | |
| 481 | ||
| 482 | return result | |
| 483 | si, | |
| 484 | Lexical.TOKEN.CONTINUE | |
| 485 | ) | |
| 486 | ||
| 487 | add_parser( | |
| 488 | (context) => pragma_parser.parse(context)!, | |
| 489 | Lexical.TOKEN.AT | |
| 490 | ) | |
| 491 | si | |
| 492 | ||
| 493 | parse_if(context: CONTEXT) -> Trees.Statements.IF is | |
| 494 | let start = context.location | |
| 495 | ||
| 496 | // A branch's statements end at the next branch as well as at | |
| 497 | // the `fi`. | |
| 498 | let use open = | |
| 499 | context.open_construct( | |
| 500 | Collections.LIST[Lexical.TOKEN]([ | |
| 501 | Lexical.TOKEN.ELIF, | |
| 502 | Lexical.TOKEN.ELSE, | |
| 503 | Lexical.TOKEN.FI | |
| 504 | ]) | |
| 505 | ) | |
| 506 | ||
| 507 | let branches = Collections.LIST[Trees.Statements.IF_BRANCH]() | |
| 508 | ||
| 509 | let should_poison mut = false | |
| 510 | ||
| 511 | // Every other block statement spans to its own closing keyword - | |
| 512 | // DO, FOR and CASE are all built before consuming it. The `fi` | |
| 513 | // is consumed inside the loop below, so its position has to be | |
| 514 | // carried out explicitly or the span runs to the token after it. | |
| 515 | let fi_location: Source.LOCATION? mut = _ | |
| 516 | ||
| 517 | let seen_else mut = false | |
| 518 | ||
| 519 | do | |
| 520 | let branch_start = context.location | |
| 521 | let condition: Trees.Expressions.Expression? mut = _ | |
| 522 | let binding: Trees.Statements.REFUTABLE_BINDING? mut = _ | |
| 523 | ||
| 524 | // An else is the last arm: an arm after it could never run. | |
| 525 | if seen_else then | |
| 526 | should_poison = true | |
| 527 | ||
| 528 | if context.current_token == Lexical.TOKEN.ELIF then | |
| 529 | context.error(context.location, "elif cannot appear after else") | |
| 530 | else | |
| 531 | context.error(context.location, "more than one else arm in if statement") | |
| 532 | fi | |
| 533 | fi | |
| 534 | ||
| 535 | if | |
| 536 | context.current.token == Lexical.TOKEN.ELIF \/ context.current.token == Lexical.TOKEN.IF | |
| 537 | then | |
| 538 | context.next_token() | |
| 539 | ||
| 540 | if context.current_token == Lexical.TOKEN.LET then | |
| 541 | binding = try_parse_let_binding(context, Lexical.TOKEN.THEN, "if") | |
| 542 | fi | |
| 543 | ||
| 544 | if binding? then | |
| 545 | if binding.is_poisoned then | |
| 546 | should_poison = true | |
| 547 | if context.current_token != Lexical.TOKEN.THEN then | |
| 548 | break | |
| 549 | fi | |
| 550 | fi | |
| 551 | else | |
| 552 | condition = expression_parser.parse(context)! | |
| 553 | ||
| 554 | if condition.is_poisoned then | |
| 555 | should_poison = true | |
| 556 | if context.current_token != Lexical.TOKEN.THEN then | |
| 557 | break | |
| 558 | fi | |
| 559 | fi | |
| 560 | fi | |
| 561 | ||
| 562 | if !context.next_token(Lexical.TOKEN.THEN) then | |
| 563 | should_poison = true | |
| 564 | branches.add( | |
| 565 | Trees.Statements.IF_BRANCH( | |
| 566 | branch_start::context.current.location, | |
| 567 | condition, | |
| 568 | binding, | |
| 569 | Trees.Statements.LIST( | |
| 570 | start::context.current.location, | |
| 571 | Collections.LIST[Trees.Statements.Statement](0) | |
| 572 | ) | |
| 573 | ) | |
| 574 | ) | |
| 575 | ||
| 576 | if | |
| 577 | context.current_token != Lexical.TOKEN.FI /\ | |
| 578 | context.current_token != Lexical.TOKEN.ELIF /\ | |
| 579 | context.current_token != Lexical.TOKEN.ELSE then | |
| 580 | break | |
| 581 | fi | |
| 582 | fi | |
| 583 | else | |
| 584 | condition = null | |
| 585 | if !context.next_token(Lexical.TOKEN.ELSE) then | |
| 586 | should_poison = true | |
| 587 | break | |
| 588 | fi | |
| 589 | ||
| 590 | seen_else = true | |
| 591 | fi | |
| 592 | ||
| 593 | let body = statement_list_parser.parse(context) | |
| 594 | ||
| 595 | if body? then | |
| 596 | if body.is_poisoned then | |
| 597 | should_poison = true | |
| 598 | fi | |
| 599 | ||
| 600 | branches.add(Trees.Statements.IF_BRANCH(branch_start::body.location, condition, binding, body)) | |
| 601 | fi | |
| 602 | ||
| 603 | if context.current.token == Lexical.TOKEN.FI then | |
| 604 | fi_location = context.current.location | |
| 605 | context.next_token() | |
| 606 | break | |
| 607 | fi | |
| 608 | od | |
| 609 | ||
| 610 | let span_end = if let end: Source.LOCATION = fi_location then end else context.location fi | |
| 611 | ||
| 612 | let result = Trees.Statements.IF(start::span_end, branches) | |
| 613 | ||
| 614 | result.poison(should_poison) | |
| 615 | ||
| 616 | return result | |
| 617 | si | |
| 618 | ||
| 619 | // Parse an `if let` / `while let` clause list, disambiguating a | |
| 620 | // conditional definition (`if let x = e then`, `if let x = e, y = | |
| 621 | // f then`) from a let-in expression used as the condition (`if let | |
| 622 | // x = e in body then`). The clauses are accepted only when the | |
| 623 | // expected terminator (`THEN` for `if let`, `DO` for `while let`) | |
| 624 | // follows: on a match the speculation is committed and the binding | |
| 625 | // returned; otherwise it is rolled back and null returned, leaving | |
| 626 | // the caller to parse the condition as an expression. Parsing once | |
| 627 | // and committing — rather than probing and then re-parsing — keeps | |
| 628 | // each clause off the tokenizer's re-scan path, so a speculation | |
| 629 | // inside a clause is not counted twice by the loop detector. The | |
| 630 | // `let` is current on entry and is consumed as part of the | |
| 631 | // speculation. | |
| 632 | try_parse_let_binding( | |
| 633 | context: CONTEXT, | |
| 634 | terminator: Lexical.TOKEN, | |
| 635 | kind: string | |
| 636 | ) -> Trees.Statements.REFUTABLE_BINDING? is | |
| 637 | let use diagnostics_snapshot = context.diagnostics_speculate_then_backtrack() | |
| 638 | let use snapshot = context.tokenizer_speculate_then_backtrack() | |
| 639 | ||
| 640 | context.next_token() | |
| 641 | ||
| 642 | let binding = parse_let_binding(context, kind) | |
| 643 | ||
| 644 | if binding? /\ context.current_token == terminator then | |
| 645 | snapshot.commit() | |
| 646 | diagnostics_snapshot.commit() | |
| 647 | ||
| 648 | return binding | |
| 649 | fi | |
| 650 | ||
| 651 | return null | |
| 652 | si | |
| 653 | ||
| 654 | // A `/\` after a `when` pattern's own shape (before `then`) is a | |
| 655 | // guard, not a continuation of the pattern — `variable_parser` | |
| 656 | // never consumes it (unlike a generic type-bound `[T: A /\ B]`, | |
| 657 | // `Shape.CIRCLE` is a qualified name so the intersection-bound | |
| 658 | // branch in the type-expression parser never fires here). | |
| 659 | is_guard_operator(context: CONTEXT) -> bool => | |
| 660 | context.current.token == Lexical.TOKEN.OPERATOR /\ | |
| 661 | context.current.value_string =~ "/\\" | |
| 662 | ||
| 663 | // Parse a pattern in a position where it can fail to match, so | |
| 664 | // a leaf may carry the `~` match marker. | |
| 665 | _parse_pattern(context: CONTEXT) -> Trees.Variables.VARIABLE? is | |
| 666 | let previous_in_refutable_pattern = context.in_refutable_pattern | |
| 667 | context.in_refutable_pattern = true | |
| 668 | ||
| 669 | try | |
| 670 | return variable_parser.parse(context) | |
| 671 | finally | |
| 672 | context.in_refutable_pattern = previous_in_refutable_pattern | |
| 673 | yrt | |
| 674 | si | |
| 675 | ||
| 676 | // Disambiguate a `when` arm's pattern from a value-equality | |
| 677 | // expression list. Speculatively parse a variable (and, if | |
| 678 | // present, its guard); treat it as a pattern only if it carries | |
| 679 | // actual pattern shape — an ascription (`v: T`, `_: T`, or | |
| 680 | // per-element-ascribed destructure) or a destructure (`(a, b)`). | |
| 681 | // A bare identifier without ascription is an expression | |
| 682 | // (equality test against the resolved constant), not a binding | |
| 683 | // — bindings always carry shape information. | |
| 684 | // | |
| 685 | // Also flags two pattern-shaped forms that parse just far enough | |
| 686 | // to look like a pattern before diverging from `then`: a | |
| 687 | // constructor-argument group after the ascribed type (`when _: | |
| 688 | // T(args) then`) and a comma-separated list of binding patterns | |
| 689 | // in one arm. Neither is grammatical, and both would otherwise | |
| 690 | // reach a later pass half-built, so each is reported here as the | |
| 691 | // syntax error it is and its arm skipped. | |
| 692 | classify_when_arm(context: CONTEXT) -> (is_pattern: bool, error_message: string?, error_location: Source.LOCATION?) is | |
| 693 | let use diagnostics_snapshot = context.diagnostics_speculate_then_backtrack() | |
| 694 | let use snapshot = context.tokenizer_speculate_then_backtrack() | |
| 695 | ||
| 696 | let probe = _parse_pattern(context) | |
| 697 | ||
| 698 | if !probe? then | |
| 699 | return (false, null, null) | |
| 700 | fi | |
| 701 | ||
| 702 | if is_guard_operator(context) then | |
| 703 | context.next_token() | |
| 704 | expression_parser.parse(context) | |
| 705 | fi | |
| 706 | ||
| 707 | let has_pattern_shape = probe.is_explicit_type \/ !probe.left.is_simple_name | |
| 708 | ||
| 709 | if context.current_token == Lexical.TOKEN.THEN then | |
| 710 | return (has_pattern_shape, null, null) | |
| 711 | fi | |
| 712 | ||
| 713 | if has_pattern_shape then | |
| 714 | // Captured before the speculation unwinds, so the caller | |
| 715 | // can report at the offending token rather than at the | |
| 716 | // arm's leading pattern-variable token. | |
| 717 | let offending_location = context.location | |
| 718 | ||
| 719 | if | |
| 720 | context.current_token == Lexical.TOKEN.PAREN_OPEN \/ | |
| 721 | context.current_token == Lexical.TOKEN.COMMA | |
| 722 | then | |
| 723 | return ( | |
| 724 | false, | |
| 725 | "syntax error: expected {Lexical.TOKEN_NAMES[Lexical.TOKEN.THEN]} but found {context.current_token_name}", | |
| 726 | offending_location | |
| 727 | ) | |
| 728 | fi | |
| 729 | fi | |
| 730 | ||
| 731 | return (false, null, null) | |
| 732 | si | |
| 733 | ||
| 734 | parse_let_binding(context: CONTEXT, kind: string) -> Trees.Statements.REFUTABLE_BINDING? is | |
| 735 | let start = context.location | |
| 736 | let clauses = Collections.LIST[Trees.Statements.REFUTABLE_BINDING_CLAUSE]() | |
| 737 | ||
| 738 | let previous_in_refutable_pattern = context.in_refutable_pattern | |
| 739 | context.in_refutable_pattern = true | |
| 740 | ||
| 741 | try | |
| 742 | return _parse_clauses(context, kind, start, clauses) | |
| 743 | finally | |
| 744 | context.in_refutable_pattern = previous_in_refutable_pattern | |
| 745 | yrt | |
| 746 | si | |
| 747 | ||
| 748 | _parse_clauses( | |
| 749 | context: CONTEXT, | |
| 750 | kind: string, | |
| 751 | start: Source.LOCATION, | |
| 752 | clauses: Collections.MutableList[Trees.Statements.REFUTABLE_BINDING_CLAUSE] | |
| 753 | ) -> Trees.Statements.REFUTABLE_BINDING? is | |
| 754 | let should_poison mut = false | |
| 755 | ||
| 756 | do | |
| 757 | let clause = _parse_clause(context, kind) | |
| 758 | ||
| 759 | if !clause? then | |
| 760 | return null | |
| 761 | fi | |
| 762 | ||
| 763 | if clause.is_poisoned then | |
| 764 | should_poison = true | |
| 765 | fi | |
| 766 | ||
| 767 | clauses.add(clause) | |
| 768 | ||
| 769 | if context.current_token != Lexical.TOKEN.COMMA then | |
| 770 | break | |
| 771 | fi | |
| 772 | ||
| 773 | context.next_token() | |
| 774 | od | |
| 775 | ||
| 776 | if clauses.count == 0 then | |
| 777 | return null | |
| 778 | fi | |
| 779 | ||
| 780 | let result = | |
| 781 | Trees.Statements.REFUTABLE_BINDING( | |
| 782 | start::context.location, | |
| 783 | clauses | |
| 784 | ) | |
| 785 | ||
| 786 | result.poison(should_poison) | |
| 787 | ||
| 788 | return result | |
| 789 | si | |
| 790 | ||
| 791 | // Parse one clause of an `if let` / `while let` clause list. | |
| 792 | // Two forms share the clause position: | |
| 793 | // | |
| 794 | // pattern [: T] = e [/\ guard] — the full form | |
| 795 | // path [/\ guard] — leaf-name shorthand | |
| 796 | // path? [/\ guard] — leaf-name shorthand, explicit test | |
| 797 | // path: T [/\ guard] — leaf-name shorthand, narrowed | |
| 798 | // | |
| 799 | // A destructure or wildcard pattern can't open a path, so a | |
| 800 | // clause not starting with an identifier or self parses as the | |
| 801 | // full form directly. For the ambiguous openings, parse the | |
| 802 | // left-hand side as an expression first — it covers both a | |
| 803 | // simple pattern name and any path shape (`a.b`, `a[i].b`, | |
| 804 | // `f().b`) — then discriminate on what follows: an `=` (after | |
| 805 | // an optional `: T`) means it was a full-form pattern, which | |
| 806 | // must then be a simple name; anything else is the shorthand. | |
| 807 | _parse_clause(context: CONTEXT, kind: string) -> Trees.Statements.REFUTABLE_BINDING_CLAUSE? is | |
| 808 | if | |
| 809 | context.current_token != Lexical.TOKEN.IDENTIFIER /\ | |
| 810 | context.current_token != Lexical.TOKEN.SELF | |
| 811 | then | |
| 812 | let variable = variable_parser.parse(context) | |
| 813 | ||
| 814 | if variable? then | |
| 815 | return _build_clause(context, variable, kind) | |
| 816 | fi | |
| 817 | ||
| 818 | return null | |
| 819 | fi | |
| 820 | ||
| 821 | let start = context.location | |
| 822 | ||
| 823 | let expression = expression_parser.parse(context)! | |
| 824 | ||
| 825 | let narrow_type_expression: Trees.TypeExpressions.TypeExpression? mut = null | |
| 826 | ||
| 827 | if context.current_token == Lexical.TOKEN.COLON then | |
| 828 | context.next_token() | |
| 829 | ||
| 830 | narrow_type_expression = IoC.CONTAINER.instance.type_parser.parse(context) | |
| 831 | ||
| 832 | // A constructor-argument group after the ascribed type | |
| 833 | // (`name: T(args) = e`) is not grammatical. Consuming it | |
| 834 | // here leaves the rest of the clause to parse normally, | |
| 835 | // so the enclosing `if let` is still recognised as one | |
| 836 | // and the error does not cascade into a reading of the | |
| 837 | // condition as a let-in expression. | |
| 838 | if context.current_token == Lexical.TOKEN.PAREN_OPEN then | |
| 839 | context.error( | |
| 840 | context.location, | |
| 841 | "syntax error: expected {Lexical.TOKEN_NAMES[Lexical.TOKEN.ASSIGN]} but found {context.current_token_name}" | |
| 842 | ) | |
| 843 | ||
| 844 | expression_parser.parse(context) | |
| 845 | fi | |
| 846 | fi | |
| 847 | ||
| 848 | if context.current_token == Lexical.TOKEN.ASSIGN then | |
| 849 | return _build_full_clause(context, start, expression, narrow_type_expression, kind) | |
| 850 | fi | |
| 851 | ||
| 852 | return _build_shorthand_clause(context, start, expression, narrow_type_expression, kind) | |
| 853 | si | |
| 854 | ||
| 855 | // Full-form clause whose left-hand side arrived as an already | |
| 856 | // parsed expression: `name [: T] = e [/\ guard]`. The `=` is | |
| 857 | // current; the pattern must be a simple name. | |
| 858 | _build_full_clause( | |
| 859 | context: CONTEXT, | |
| 860 | start: Source.LOCATION, | |
| 861 | expression: Trees.Expressions.Expression, | |
| 862 | narrow_type_expression: Trees.TypeExpressions.TypeExpression?, | |
| 863 | kind: string | |
| 864 | ) -> Trees.Statements.REFUTABLE_BINDING_CLAUSE? is | |
| 865 | context.next_token() | |
| 866 | ||
| 867 | let name = | |
| 868 | if expression.is_unqualified_identifier then | |
| 869 | expression.try_copy_as_identifer() | |
| 870 | else | |
| 871 | null | |
| 872 | fi | |
| 873 | ||
| 874 | let initializer = expression_parser.parse(context)! | |
| 875 | ||
| 876 | if !name? then | |
| 877 | context.error(expression.location, "expected a simple variable name before = in {kind} let") | |
| 878 | return null | |
| 879 | fi | |
| 880 | ||
| 881 | let pattern_left = Trees.Variables.SIMPLE_VARIABLE_LEFT(expression.location, name) | |
| 882 | pattern_left.mark_refutable_recursive() | |
| 883 | ||
| 884 | let split = _split_chain_initializer(initializer) | |
| 885 | ||
| 886 | let clause = | |
| 887 | Trees.Statements.REFUTABLE_BINDING_CLAUSE( | |
| 888 | start::initializer.location, | |
| 889 | split.initializer, | |
| 890 | narrow_type_expression, | |
| 891 | pattern_left, | |
| 892 | split.guard | |
| 893 | ) | |
| 894 | ||
| 895 | return clause | |
| 896 | si | |
| 897 | ||
| 898 | // Leaf-name shorthand clause: `path`, `path?` or `path: T` with | |
| 899 | // no pattern and no `=`. Declares an immutable local named after | |
| 900 | // the path's last member, holding the value (`path` / `path?`) or | |
| 901 | // its narrowing (`: T`) — `if let x.y.z then f(z)` is | |
| 902 | // `if let z = x.y.z` with the name inferred, refutable on the | |
| 903 | // path's own optionality. A trailing `/\ …` chain becomes the | |
| 904 | // clause guard, as in the full form. | |
| 905 | _build_shorthand_clause( | |
| 906 | context: CONTEXT, | |
| 907 | start: Source.LOCATION, | |
| 908 | expression: Trees.Expressions.Expression, | |
| 909 | narrow_type_expression: Trees.TypeExpressions.TypeExpression?, | |
| 910 | kind: string | |
| 911 | ) -> Trees.Statements.REFUTABLE_BINDING_CLAUSE? is | |
| 912 | let guard: Trees.Expressions.Expression? mut = null | |
| 913 | let scrutinee: Trees.Expressions.Expression mut = expression | |
| 914 | let end mut = expression.location | |
| 915 | ||
| 916 | if narrow_type_expression? then | |
| 917 | // `path: T` — the type parser stopped before any | |
| 918 | // `/\ guard` chain. | |
| 919 | end = narrow_type_expression.location | |
| 920 | ||
| 921 | if context.current_token == Lexical.TOKEN.OPERATOR /\ context.current_string =~ "/\\" then | |
| 922 | context.next_token() | |
| 923 | guard = expression_parser.parse(context)! | |
| 924 | ||
| 925 | end = guard.location | |
| 926 | fi | |
| 927 | else | |
| 928 | // `path? [/\ guard…]` or bare `path [/\ guard…]` — parsed | |
| 929 | // as one expression; the leftmost `/\` leaf is the | |
| 930 | // scrutinee, optionally carrying a trailing presence test, | |
| 931 | // and the rest is the guard. Refutability comes from the | |
| 932 | // scrutinee's own optionality, exactly as in the full form | |
| 933 | // `if let z = x.y.z` — the `?` is not required. | |
| 934 | let split = _split_chain_initializer(expression) | |
| 935 | ||
| 936 | if isa Trees.Expressions.HAS_VALUE(split.initializer) then | |
| 937 | let has_value = cast Trees.Expressions.HAS_VALUE?(split.initializer)! | |
| 938 | ||
| 939 | scrutinee = has_value.left | |
| 940 | elif _infer_leaf_name(split.initializer)? then | |
| 941 | scrutinee = split.initializer | |
| 942 | else | |
| 943 | return _build_missing_test_clause(context, start, expression, kind) | |
| 944 | fi | |
| 945 | ||
| 946 | guard = split.guard | |
| 947 | fi | |
| 948 | ||
| 949 | let leaf mut = _infer_leaf_name(scrutinee) | |
| 950 | let can_infer = leaf? | |
| 951 | ||
| 952 | if !can_infer then | |
| 953 | if narrow_type_expression? then | |
| 954 | // An ascribed name with no `= …`: the shorthand lifts | |
| 955 | // its variable out of a member path, and a bare name | |
| 956 | // has no path to lift from, so what is missing is the | |
| 957 | // initializer rather than the name. | |
| 958 | return _build_missing_test_clause(context, start, expression, kind) | |
| 959 | fi | |
| 960 | ||
| 961 | // Still yield a clause — poisoned, with a placeholder | |
| 962 | // variable — so the rest of the chain parses and the | |
| 963 | // whole statement is still recognised as {kind} let. | |
| 964 | context.error( | |
| 965 | start::end, | |
| 966 | "cannot infer a variable name for this expression", | |
| 967 | start::end, | |
| 968 | "help: use {kind} let name = ...") | |
| 969 | leaf = Trees.Identifiers.Identifier(scrutinee.location, "value") | |
| 970 | fi | |
| 971 | ||
| 972 | let pattern_left = Trees.Variables.SIMPLE_VARIABLE_LEFT(leaf!.location, leaf) | |
| 973 | pattern_left.mark_refutable_recursive() | |
| 974 | ||
| 975 | let clause = | |
| 976 | Trees.Statements.REFUTABLE_BINDING_CLAUSE( | |
| 977 | start::end, | |
| 978 | scrutinee, | |
| 979 | narrow_type_expression, | |
| 980 | pattern_left, | |
| 981 | guard | |
| 982 | ) | |
| 983 | ||
| 984 | clause.is_inferred_name = true | |
| 985 | clause.poison(!can_infer) | |
| 986 | ||
| 987 | return clause | |
| 988 | si | |
| 989 | ||
| 990 | // A clause whose scrutinee has no name to lift out: a bare local | |
| 991 | // (a full-form clause missing its `= …`, which is also how a | |
| 992 | // same-named shadow is steered away from) or an expression with | |
| 993 | // no path leaf to name a variable after (a call, an index). A | |
| 994 | // member path never reaches here — it takes the shorthand. Report | |
| 995 | // the most likely omission and yield a poisoned clause so the rest | |
| 996 | // of the chain still parses and diagnoses usefully. | |
| 997 | _build_missing_test_clause( | |
| 998 | context: CONTEXT, | |
| 999 | start: Source.LOCATION, | |
| 1000 | expression: Trees.Expressions.Expression, | |
| 1001 | kind: string | |
| 1002 | ) -> Trees.Statements.REFUTABLE_BINDING_CLAUSE? is | |
| 1003 | let leaf mut = | |
| 1004 | if expression.is_unqualified_identifier then | |
| 1005 | expression.try_copy_as_identifer() | |
| 1006 | else | |
| 1007 | _infer_leaf_name(expression) | |
| 1008 | fi | |
| 1009 | ||
| 1010 | if !leaf? then | |
| 1011 | // A call or index result: nameable only with the full form. | |
| 1012 | context.error( | |
| 1013 | start::expression.location, | |
| 1014 | "cannot infer a variable name for this expression", | |
| 1015 | start::expression.location, | |
| 1016 | "help: use {kind} let name = ...") | |
| 1017 | leaf = Trees.Identifiers.Identifier(expression.location, "value") | |
| 1018 | else | |
| 1019 | // A bare local: a leaf but no path, so no `= …`. | |
| 1020 | context.error(expression.location, "{kind} let requires an initializer") | |
| 1021 | fi | |
| 1022 | ||
| 1023 | let pattern_left = Trees.Variables.SIMPLE_VARIABLE_LEFT(leaf.location, leaf) | |
| 1024 | pattern_left.mark_refutable_recursive() | |
| 1025 | ||
| 1026 | let clause = | |
| 1027 | Trees.Statements.REFUTABLE_BINDING_CLAUSE( | |
| 1028 | start::expression.location, | |
| 1029 | Trees.Expressions.NULL(expression.location), | |
| 1030 | null, | |
| 1031 | pattern_left, | |
| 1032 | null | |
| 1033 | ) | |
| 1034 | ||
| 1035 | clause.poison(true) | |
| 1036 | ||
| 1037 | return clause | |
| 1038 | si | |
| 1039 | ||
| 1040 | // The shorthand's variable name: the last member of a path. A | |
| 1041 | // fresh identifier node — the scrutinee keeps its own. A bare | |
| 1042 | // name has no path to take a leaf from, and the new variable | |
| 1043 | // would shadow the scrutinee's own name inside the clause's | |
| 1044 | // scope; narrowing already covers `if x?` on a local. | |
| 1045 | _infer_leaf_name(scrutinee: Trees.Expressions.Expression) -> Trees.Identifiers.Identifier? is | |
| 1046 | if isa Trees.Expressions.MEMBER(scrutinee) then | |
| 1047 | let member = cast Trees.Expressions.MEMBER(scrutinee) | |
| 1048 | ||
| 1049 | return Trees.Identifiers.Identifier(member.identifier.location, member.identifier.name) | |
| 1050 | fi | |
| 1051 | ||
| 1052 | return null | |
| 1053 | si | |
| 1054 | ||
| 1055 | // Build one REFUTABLE_BINDING_CLAUSE from a parsed VARIABLE. | |
| 1056 | // Reports the missing-initializer error and poisons the clause | |
| 1057 | // when there is no `= …`; otherwise splits the initializer's | |
| 1058 | // top-level `/\` chain into (scrutinee, guard) per the rule | |
| 1059 | // described on `_split_chain_initializer`. | |
| 1060 | _build_clause( | |
| 1061 | context: CONTEXT, | |
| 1062 | variable: Trees.Variables.VARIABLE, | |
| 1063 | kind: string | |
| 1064 | ) -> Trees.Statements.REFUTABLE_BINDING_CLAUSE is | |
| 1065 | let pattern_left = variable.left | |
| 1066 | pattern_left.mark_refutable_recursive() | |
| 1067 | ||
| 1068 | let narrow_type_expression = | |
| 1069 | if variable.is_explicit_type then | |
| 1070 | variable.type_expression | |
| 1071 | else | |
| 1072 | null | |
| 1073 | fi | |
| 1074 | ||
| 1075 | let initializer = variable.initializer | |
| 1076 | ||
| 1077 | if !initializer? then | |
| 1078 | context.error(variable.location, "{kind} let requires an initializer") | |
| 1079 | ||
| 1080 | let clause = | |
| 1081 | Trees.Statements.REFUTABLE_BINDING_CLAUSE( | |
| 1082 | variable.location, | |
| 1083 | Trees.Expressions.NULL(variable.location), | |
| 1084 | narrow_type_expression, | |
| 1085 | pattern_left, | |
| 1086 | null | |
| 1087 | ) | |
| 1088 | ||
| 1089 | clause.poison(true) | |
| 1090 | return clause | |
| 1091 | fi | |
| 1092 | ||
| 1093 | // Split a top-level `/\` chain in the initializer: the | |
| 1094 | // leftmost leaf is the real scrutinee (the value to | |
| 1095 | // test for presence / narrow); the remaining right-hand | |
| 1096 | // operands form a guard expression evaluated after the | |
| 1097 | // clause's bindings come into scope, on the then-arm. | |
| 1098 | // `/\` returns bool, so a top-level `/\` initializer is | |
| 1099 | // never a sensible refutable value on its own — every | |
| 1100 | // such tree is a chain. | |
| 1101 | let split = _split_chain_initializer(initializer) | |
| 1102 | ||
| 1103 | let clause = | |
| 1104 | Trees.Statements.REFUTABLE_BINDING_CLAUSE( | |
| 1105 | variable.location, | |
| 1106 | split.initializer, | |
| 1107 | narrow_type_expression, | |
| 1108 | pattern_left, | |
| 1109 | split.guard | |
| 1110 | ) | |
| 1111 | ||
| 1112 | clause.poison(variable.is_poisoned) | |
| 1113 | ||
| 1114 | return clause | |
| 1115 | si | |
| 1116 | ||
| 1117 | // Walk down the left spine of a `/\` tree. The leftmost leaf | |
| 1118 | // becomes the binding's initializer; the right operands in | |
| 1119 | // source order are recombined left-assoc as the guard. Returns | |
| 1120 | // (initializer, null) when the input is not a `/\` BINARY. | |
| 1121 | _split_chain_initializer(expr: Trees.Expressions.Expression) -> (initializer: Trees.Expressions.Expression, guard: Trees.Expressions.Expression?) is | |
| 1122 | if !isa Trees.Expressions.BINARY(expr) then | |
| 1123 | return (expr, null) | |
| 1124 | fi | |
| 1125 | ||
| 1126 | let head = cast Trees.Expressions.BINARY(expr) | |
| 1127 | ||
| 1128 | if !head.actual_operation? \/ !(head.actual_operation =~ "/\\") then | |
| 1129 | return (expr, null) | |
| 1130 | fi | |
| 1131 | ||
| 1132 | let collected = Collections.LIST[Trees.Expressions.Expression]() | |
| 1133 | let leftmost: Trees.Expressions.Expression mut = expr | |
| 1134 | ||
| 1135 | do | |
| 1136 | if !isa Trees.Expressions.BINARY(leftmost) then | |
| 1137 | break | |
| 1138 | fi | |
| 1139 | ||
| 1140 | let cur = cast Trees.Expressions.BINARY(leftmost) | |
| 1141 | ||
| 1142 | if !cur.actual_operation? \/ !(cur.actual_operation =~ "/\\") then | |
| 1143 | break | |
| 1144 | fi | |
| 1145 | ||
| 1146 | collected.add(cur.right) | |
| 1147 | leftmost = cur.left | |
| 1148 | od | |
| 1149 | ||
| 1150 | // `collected` is in reverse source order (outermost-right | |
| 1151 | // first). Rebuild the guard left-assoc to match how the | |
| 1152 | // user wrote it. | |
| 1153 | let n = collected.count | |
| 1154 | let guard: Trees.Expressions.Expression mut = collected[n - 1] | |
| 1155 | let i mut = n - 2 | |
| 1156 | ||
| 1157 | while i >= 0 do | |
| 1158 | let right = collected[i] | |
| 1159 | guard = Trees.Expressions.BINARY( | |
| 1160 | guard.location::right.location, | |
| 1161 | Trees.Identifiers.Identifier(guard.location, "/\\"), | |
| 1162 | "/\\", | |
| 1163 | guard, | |
| 1164 | right | |
| 1165 | ) | |
| 1166 | i = i - 1 | |
| 1167 | od | |
| 1168 | ||
| 1169 | return (leftmost, guard) | |
| 1170 | si | |
| 1171 | ||
| 1172 | other_token(context: CONTEXT) -> Trees.Statements.Statement? is | |
| 1173 | if context.current.token == Lexical.TOKEN.IDENTIFIER then | |
| 1174 | if _starts_nested_function(context) then | |
| 1175 | return parse_nested_function(context) | |
| 1176 | fi | |
| 1177 | ||
| 1178 | let want_backtrack = true | |
| 1179 | ||
| 1180 | let use tokenizer_snapshot = context.tokenizer_speculate_then_backtrack() | |
| 1181 | ||
| 1182 | let label = identifier_parser.parse(context)! | |
| 1183 | ||
| 1184 | if context.current.token == Lexical.TOKEN.COLON then | |
| 1185 | context.next_token() | |
| 1186 | ||
| 1187 | if labellable_statement_tokens.contains(context.current.token) then | |
| 1188 | // TODO: maybe we don't actually want to commit yet - could this be a broken | |
| 1189 | // property definition? | |
| 1190 | ||
| 1191 | tokenizer_snapshot.commit() | |
| 1192 | ||
| 1193 | let statement = self.parse(context)! | |
| 1194 | return Trees.Statements.LABELLED(label.location::statement.location, label, statement) | |
| 1195 | fi | |
| 1196 | fi | |
| 1197 | fi | |
| 1198 | ||
| 1199 | let left = expression_parser.parse(context)! | |
| 1200 | ||
| 1201 | if isa Trees.Expressions.Literals.NONE(left) then | |
| 1202 | return null | |
| 1203 | elif context.current.token == Lexical.TOKEN.ASSIGN then | |
| 1204 | context.next_token() | |
| 1205 | ||
| 1206 | let right = parse_assigned_value(context, expression_parser) | |
| 1207 | ||
| 1208 | return | |
| 1209 | Trees.Statements.ASSIGNMENT( | |
| 1210 | left.location::right.location, | |
| 1211 | left.rewrite_as_assignment_left(), | |
| 1212 | right) | |
| 1213 | else | |
| 1214 | return Trees.Statements.EXPRESSION(left.location, left) | |
| 1215 | fi | |
| 1216 | si | |
| 1217 | ||
| 1218 | ||
| 1219 | // A named function written among a body's statements. Nothing in the | |
| 1220 | // expression grammar can follow a call's closing `)` with `is`, `=>` | |
| 1221 | // or `->`, so a bounded peek settles the shape outright and the parse | |
| 1222 | // that follows needs no speculation of its own. | |
| 1223 | // | |
| 1224 | // The scan gives up at the first token a formal parameter list cannot | |
| 1225 | // contain, which is what keeps it out of a call's arguments. That | |
| 1226 | // matters beyond saving work: reading into an interpolated string | |
| 1227 | // argument lexes it in the wrong mode, and the damage outlives the | |
| 1228 | // backtrack. | |
| 1229 | _starts_nested_function(context: CONTEXT) -> bool is | |
| 1230 | if context.in_top_level_statements then | |
| 1231 | return false | |
| 1232 | fi | |
| 1233 | ||
| 1234 | let use snapshot = context.tokenizer_speculate_then_backtrack_bounded() | |
| 1235 | ||
| 1236 | context.next_token() | |
| 1237 | ||
| 1238 | if context.current.token != Lexical.TOKEN.PAREN_OPEN then | |
| 1239 | return false | |
| 1240 | fi | |
| 1241 | ||
| 1242 | let depth mut = 0 | |
| 1243 | ||
| 1244 | while !context.is_end_of_file do | |
| 1245 | let token = context.current.token | |
| 1246 | ||
| 1247 | if token == Lexical.TOKEN.PAREN_OPEN then | |
| 1248 | depth = depth + 1 | |
| 1249 | elif token == Lexical.TOKEN.PAREN_CLOSE then | |
| 1250 | depth = depth - 1 | |
| 1251 | ||
| 1252 | if depth <= 0 then | |
| 1253 | context.next_token() | |
| 1254 | ||
| 1255 | return | |
| 1256 | context.current.token == Lexical.TOKEN.IS \/ | |
| 1257 | context.current.token == Lexical.TOKEN.ARROW_FAT \/ | |
| 1258 | context.current.token == Lexical.TOKEN.ARROW_THIN | |
| 1259 | fi | |
| 1260 | elif !_can_appear_in_formal_arguments(token) then | |
| 1261 | return false | |
| 1262 | fi | |
| 1263 | ||
| 1264 | context.next_token() | |
| 1265 | od | |
| 1266 | ||
| 1267 | return false | |
| 1268 | si | |
| 1269 | ||
| 1270 | // What a formal parameter list is made of: names, their type | |
| 1271 | // ascriptions and the types themselves, destructure groups, and the | |
| 1272 | // attribute pragmas a parameter can carry. Everything else - a | |
| 1273 | // literal, an operator, a keyword that opens an expression - says the | |
| 1274 | // parentheses hold call arguments instead. | |
| 1275 | _can_appear_in_formal_arguments(token: Lexical.TOKEN) -> bool => | |
| 1276 | token == Lexical.TOKEN.IDENTIFIER \/ | |
| 1277 | token == Lexical.TOKEN.COLON \/ | |
| 1278 | token == Lexical.TOKEN.COMMA \/ | |
| 1279 | token == Lexical.TOKEN.DOT \/ | |
| 1280 | token == Lexical.TOKEN.QUESTION \/ | |
| 1281 | token == Lexical.TOKEN.ARROW_THIN \/ | |
| 1282 | token == Lexical.TOKEN.ARRAY_DEF \/ | |
| 1283 | token == Lexical.TOKEN.SQUARE_OPEN \/ | |
| 1284 | token == Lexical.TOKEN.SQUARE_CLOSE \/ | |
| 1285 | token == Lexical.TOKEN.REF \/ | |
| 1286 | token == Lexical.TOKEN.PTR \/ | |
| 1287 | token == Lexical.TOKEN.AT | |
| 1288 | ||
| 1289 | // The expression parser builds the literal, taking its name and its | |
| 1290 | // formals from what would otherwise have been a call; the name is a | |
| 1291 | // local of the enclosing body, initialized with the literal, so the | |
| 1292 | // function is reached from the statement after this one onward. | |
| 1293 | parse_nested_function(context: CONTEXT) -> Trees.Statements.Statement? is | |
| 1294 | let start = context.location | |
| 1295 | ||
| 1296 | context.allow_nested_function = true | |
| 1297 | ||
| 1298 | let parsed = expression_parser.parse(context) | |
| 1299 | ||
| 1300 | context.allow_nested_function = false | |
| 1301 | ||
| 1302 | let literal = cast Trees.Expressions.FUNCTION?(parsed) | |
| 1303 | ||
| 1304 | if !literal? \/ !literal.nested_name? then | |
| 1305 | if parsed? then | |
| 1306 | return Trees.Statements.EXPRESSION(parsed.location, parsed) | |
| 1307 | fi | |
| 1308 | ||
| 1309 | return null | |
| 1310 | fi | |
| 1311 | ||
| 1312 | let name = literal.nested_name! | |
| 1313 | ||
| 1314 | return Trees.Statements.FUNCTION(start::literal.location, name, literal) | |
| 1315 | si | |
| 1316 | si | |
| 1317 | si |