Skip to content
← Back

src/syntax/parsers/expressions/secondary.ghul

1
namespace Syntax.Parsers.Expressions is
2
use IO.Std
3
use Ghul.Disposable
4
5
use Source
6
use Logging
7
8
class SECONDARY(
9
identifier_parser: Parser[Trees.Identifiers.Identifier],
10
type_parser: Parser[Trees.TypeExpressions.TypeExpression],
11
type_list_parser: Parser[Trees.TypeExpressions.LIST],
12
expression_parser: Parser[Trees.Expressions.Expression],
13
expression_primary_parser: Parser[Trees.Expressions.Expression],
14
expression_list_parser: Parser[Trees.Expressions.LIST],
15
body_parser: Parser[Trees.Bodies.Body]
16
): Base[Trees.Expressions.Expression] is
17
description: string => "secondary expression"
18
19
super()
20
21
// A token sits in identifier position after a `.` if it
22
// looks like a word (its `value_string` opens with a letter
23
// or underscore) and it shares a line with the dot. The
24
// line guard matters: a `.` at the end of a line followed
25
// by a structural keyword on the next (`namespace Foo.\n is`)
26
// must not eat that keyword.
27
looks_like_partial_member_keyword(context: CONTEXT, dot_location: LOCATION) -> bool is
28
if context.current.token == Lexical.TOKEN.IDENTIFIER then
29
return false
30
fi
31
32
let s = context.current.value_string
33
34
// value_string is null for a value-less token
35
@suppress("presence-test-non-optional")
36
if !s? \/ s.length == 0 then
37
return false
38
fi
39
40
let first = s[0]
41
42
if !((first >= 'a' /\ first <= 'z') \/ (first >= 'A' /\ first <= 'Z') \/ first == '_') then
43
return false
44
fi
45
46
return context.current.location.start_line == dot_location.start_line
47
si
48
49
// An operator token sits in member-name position after a `.`
50
// when whitespace separates the two: the tokenizer scans a run
51
// of operator characters greedily and `.` is one of them, so
52
// `a.=~(b)` lexes as the single operator `.=~` and never
53
// reaches here — only `a. =~(b)` does. The backtick escape
54
// (`a.`=~(b)`) arrives as an ordinary IDENTIFIER and takes the
55
// branch above.
56
//
57
// The same line guard as the keyword case, for the same
58
// reason: a `.` ending a line must not swallow an operator
59
// leading the next one.
60
looks_like_operator_member_name(context: CONTEXT, dot_location: LOCATION) -> bool is
61
if context.current.token != Lexical.TOKEN.OPERATOR then
62
return false
63
fi
64
65
let s = context.current.value_string
66
67
// value_string is null for a value-less token
68
@suppress("presence-test-non-optional")
69
if !s? \/ s.length == 0 then
70
return false
71
fi
72
73
return context.current.location.start_line == dot_location.start_line
74
si
75
76
// What may continue a `|>` onto the next line: a plain name, or a
77
// function named with operator characters.
78
_can_lead_thread_first_right_side(context: CONTEXT) -> bool static =>
79
context.current.token == Lexical.TOKEN.IDENTIFIER \/
80
context.current.token == Lexical.TOKEN.OPERATOR
81
82
// The stand-in for a `|>` right side that has not been typed
83
// yet. An empty, poisoned name resolves to nothing and reports
84
// nothing, the same recovery a bare `.` at the end of a line
85
// gets.
86
_missing_thread_first_function(location: LOCATION) -> Trees.Expressions.Expression static is
87
let name = Trees.Expressions.IDENTIFIER(location, Trees.Identifiers.Identifier(location, ""))
88
89
name.poison()
90
91
return name
92
si
93
94
// Build the thread-first call for a right side that is not a call:
95
// either nothing at all, or a name with no argument list. The
96
// subject is threaded in as it is for a complete call, so the
97
// node the completer meets has the same shape whether or not the
98
// user has finished typing.
99
_incomplete_thread_first(
100
start: LOCATION,
101
subject: Trees.Expressions.Expression,
102
completion_target: LOCATION,
103
function: Trees.Expressions.Expression
104
) -> Trees.Expressions.CALL static is
105
let call =
106
Trees.Expressions.CALL(
107
start::function.location,
108
function,
109
Trees.Expressions.LIST(subject.location, [subject])
110
)
111
112
call.is_thread_first = true
113
call.completion_target = completion_target
114
call.poison()
115
116
return call
117
si
118
119
parse(context: CONTEXT) -> Trees.Expressions.Expression is
120
let use tokenizer_state = context.tokenizer_speculate_then_commit()
121
122
let start = context.location
123
let result = expression_primary_parser.parse(context)!
124
125
return _parse_trailers(context, start, result, tokenizer_state)
126
si
127
128
// The right side of `|>` or `~>`, with `subject` threaded in as
129
// the first argument of its call. Reached from the expression
130
// parser, where both operators are a binary precedence level: the
131
// subject is whatever that level gathered on the left, and the
132
// call is a closed form, so an operator after it applies to the
133
// whole chain.
134
parse_thread_first(
135
context: CONTEXT,
136
subject: Trees.Expressions.Expression,
137
propagating: bool,
138
spelling: string
139
) -> Trees.Expressions.Expression is
140
let use tokenizer_state = context.tokenizer_speculate_then_commit()
141
142
return _parse_thread_first(context, subject.location, subject, tokenizer_state, propagating, spelling)
143
si
144
145
// Parse the right side as a function call with the subject
146
// threaded in as its first argument, so the call resolves and
147
// emits like any other, then apply any trailers that follow the
148
// call to its result. `propagating` marks a `~>` call, whose
149
// evaluation the expression passes lower inside a presence test
150
// on the subject.
151
_parse_thread_first(
152
context: CONTEXT,
153
start: Source.LOCATION,
154
subject: Trees.Expressions.Expression,
155
tokenizer_state: Syntax.Parsers.TOKEN_LOOKAHEAD_SPECULATE_THEN_COMMIT,
156
propagating: bool,
157
spelling: string
158
) -> Trees.Expressions.Expression is
159
let operator_location = context.location
160
161
context.next_token()
162
163
let pipe_start = context.location
164
165
// A thread-first operator at the end of a line usually
166
// carries its call over to the next one, which is how a
167
// wrapped chain is written. What it must not do is reach
168
// across the boundary for something that was never a call:
169
// an unfinished `xs |>` is followed by the next statement,
170
// and reading that as the right side loses it and reports
171
// the loss somewhere else entirely. Only a name continues
172
// the chain, so anything else on a fresh line means the
173
// call has not been typed yet - hand back the placeholder
174
// the completer anchors on.
175
if context.current.first_on_line /\ !_can_lead_thread_first_right_side(context) then
176
context.error(operator_location, "expected a function call after {spelling}")
177
178
return _incomplete_thread_first(
179
start,
180
subject,
181
operator_location::pipe_start,
182
_missing_thread_first_function(operator_location)
183
)
184
fi
185
186
// A function named with an operator is not a primary
187
// expression, so `xs |> $` needs its own step here.
188
// With no argument list after the name the threaded
189
// value is the only argument, which is the point of
190
// the spelling; an argument list is still accepted
191
// and threads the value in ahead of it as usual.
192
let rhs: Trees.Expressions.Expression mut
193
194
if context.current.token == Lexical.TOKEN.OPERATOR then
195
let name_location = context.location
196
197
let name =
198
Trees.Expressions.IDENTIFIER(
199
name_location,
200
Trees.Identifiers.Identifier(name_location, context.current.value_string)
201
)
202
203
context.next_token()
204
205
if context.current.token == Lexical.TOKEN.PAREN_OPEN then
206
rhs = _parse_trailers(context, pipe_start, name, tokenizer_state)
207
else
208
rhs =
209
_parse_trailers(
210
context,
211
pipe_start,
212
Trees.Expressions.CALL(
213
name_location,
214
name,
215
Trees.Expressions.LIST(
216
name_location,
217
Collections.LIST[Trees.Expressions.Expression]()
218
)
219
),
220
tokenizer_state
221
)
222
fi
223
else
224
rhs =
225
_parse_trailers(context, pipe_start, expression_primary_parser.parse(context)!, tokenizer_state)
226
fi
227
228
let rhs_call = _call_to_thread_into(rhs)
229
230
if rhs_call? then
231
// Thread the subject in as the first actual argument
232
// and flag the call, so every later pass sees an
233
// ordinary call.
234
rhs_call.arguments.expressions.insert(0, subject)
235
rhs_call.is_thread_first = true
236
rhs_call.propagates_absence = propagating
237
rhs_call.completion_target = operator_location::rhs_call.function.location
238
239
return rhs
240
fi
241
242
// A name with no argument list is what half-typed code
243
// looks like, so keep the thread-first shape for the
244
// completer instead of handing back the bare right side.
245
// Poisoning the name is what stops the placeholder call
246
// resolving and cascading.
247
context.error(rhs.location, "the right side of {spelling} must be a function call")
248
rhs.poison()
249
250
return _incomplete_thread_first(start, subject, operator_location::rhs.location, rhs)
251
si
252
253
// The call a `|>` right side threads its subject into: the
254
// outermost one, reached through any trailers written after it.
255
// `xs |> f().count` threads into `f()` and reads `count` from the
256
// result, while `xs |> BOX(10).combine(3)` threads into `combine`
257
// with `BOX(10)` as its receiver. Absent when there is no call.
258
_call_to_thread_into(rhs: Trees.Expressions.Expression) -> Trees.Expressions.CALL? static is
259
let current: Trees.Expressions.Expression mut = rhs
260
261
do
262
if let call: Trees.Expressions.CALL = current then
263
return call
264
elif let member: Trees.Expressions.MEMBER = current then
265
current = member.left
266
elif let index: Trees.Expressions.INDEX = current then
267
current = index.left
268
elif let unwrap: Trees.Expressions.UNWRAP = current then
269
current = unwrap.left
270
elif let has_value: Trees.Expressions.HAS_VALUE = current then
271
current = has_value.left
272
else
273
return null
274
fi
275
od
276
si
277
278
// Consume the postfix trailers (`.member`, `(args)`, `[index]`, …)
279
// that follow a primary.
280
_parse_trailers(
281
context: CONTEXT,
282
start: LOCATION,
283
result_in: Trees.Expressions.Expression,
284
tokenizer_state: Syntax.Parsers.TOKEN_LOOKAHEAD_SPECULATE_THEN_COMMIT
285
) -> Trees.Expressions.Expression =>
286
_parse_trailers(context, start, result_in, tokenizer_state, null, null)
287
288
// A member name written on the line after its `.`. Wrapping a chain
289
// that way is legal, but so is a `.` left dangling at the end of a
290
// half-written line, and then the next line is some other statement
291
// or definition whose first name the `.` would take. The member and
292
// everything after it are read speculatively, and kept only if they
293
// parse without complaint; otherwise the name is reported missing
294
// after the `.` and the next line is left to be read as what it is.
295
// A program that parses keeps its reading whatever its layout.
296
_parse_member_on_next_line(
297
context: CONTEXT,
298
start: LOCATION,
299
left: Trees.Expressions.Expression,
300
completion_target_start: LOCATION,
301
tokenizer_state: Syntax.Parsers.TOKEN_LOOKAHEAD_SPECULATE_THEN_COMMIT
302
) -> Trees.Expressions.Expression is
303
let missing_at = context.previous_end
304
305
let use diagnostics_snapshot = context.diagnostics_speculate_then_backtrack()
306
let use snapshot = context.tokenizer_speculate_then_backtrack_bounded()
307
308
let errors_before = context.logger.error_count
309
310
let member = identifier_parser.parse(context)!
311
312
let result =
313
_parse_trailers(
314
context,
315
start,
316
Trees.Expressions.MEMBER(start::member.location, left, member, completion_target_start::member.location),
317
tokenizer_state,
318
left,
319
member
320
)
321
322
if !result.is_poisoned /\ context.logger.error_count == errors_before then
323
snapshot.commit()
324
diagnostics_snapshot.commit()
325
326
return result
327
fi
328
329
snapshot.backtrack()
330
diagnostics_snapshot.backtrack()
331
332
context.error(missing_at, "expected a member name here")
333
334
return Trees.Expressions.MEMBER(
335
start..context.location,
336
left,
337
Trees.Identifiers.Identifier(completion_target_start, ""),
338
completion_target_start::context.location
339
)
340
si
341
342
_parse_trailers(
343
context: CONTEXT,
344
start: LOCATION,
345
result_in: Trees.Expressions.Expression,
346
tokenizer_state: Syntax.Parsers.TOKEN_LOOKAHEAD_SPECULATE_THEN_COMMIT,
347
previous_left_in: Trees.Expressions.Expression?,
348
previous_identifier_in: Trees.Identifiers.Identifier?
349
) -> Trees.Expressions.Expression is
350
let result: Trees.Expressions.Expression mut = result_in
351
let previous_left: Trees.Expressions.Expression? mut = previous_left_in
352
let previous_identifier: Trees.Identifiers.Identifier? mut = previous_identifier_in
353
354
do
355
// A trailer that could equally open a new member or
356
// statement never glues across a line break: a line-start
357
// `(`, `[`, backtick generic-argument list or operator ends
358
// the expression. A line-start `!` is a prefix `not`
359
// opening the next statement, a line-start `=~` the next
360
// operator member, a line-start `rec` the self-reference
361
// call of an enclosing anonymous function rather than a
362
// rec-lambda marker for this expression. Trailers that lead
363
// continuation lines by convention (`.`, `?`, `ref`) still
364
// continue.
365
if
366
context.current.first_on_line /\
367
(
368
context.current.token == Lexical.TOKEN.PAREN_OPEN \/
369
context.current.token == Lexical.TOKEN.SQUARE_OPEN \/
370
context.current.token == Lexical.TOKEN.SQUARE_OPEN_TICK \/
371
context.current.token == Lexical.TOKEN.REC \/
372
context.current.token == Lexical.TOKEN.OPERATOR
373
)
374
then
375
return result
376
fi
377
378
case context.current.token
379
when Lexical.TOKEN.PAREN_OPEN then
380
context.next_token()
381
let arguments: Trees.Expressions.LIST mut
382
if context.current.token != Lexical.TOKEN.PAREN_CLOSE then
383
arguments = expression_list_parser.parse(context)!
384
else
385
arguments = Trees.Expressions.LIST(context.location, Collections.LIST[Trees.Expressions.Expression]())
386
fi
387
let call_expression = Trees.Expressions.CALL(start::context.location, result, arguments)
388
call_expression.rewrite_named_arguments(context.logger)
389
result = call_expression
390
context.next_token(Lexical.TOKEN.PAREN_CLOSE, syntax_error_message)
391
392
when Lexical.TOKEN.SQUARE_OPEN then
393
context.next_token()
394
395
let done mut = false
396
397
let left: Trees.Expressions.Expression? mut = _
398
let identifier: Trees.Identifiers.Identifier? mut = _
399
400
if result.is_member then
401
left = previous_left
402
identifier = previous_identifier
403
404
elif result.is_identifier then
405
left = null
406
identifier = result.try_copy_as_identifer()
407
fi
408
409
if identifier? then
410
let use tokenizer_state = context.tokenizer_speculate_then_commit()
411
let use logger_state = context.logger_speculate_then_commit()
412
413
let type_arguments = type_list_parser.parse(context)
414
415
if type_arguments? /\ !type_arguments.is_poisoned /\ context.next_token(Lexical.TOKEN.SQUARE_CLOSE) then
416
let index_expression: Trees.Expressions.Expression? mut = _
417
418
if type_arguments.count == 1 then
419
let index = type_arguments.elements[0].try_copy_as_value_expression()
420
if index? then
421
index_expression = Trees.Expressions.INDEX(start::context.location, result, index)
422
fi
423
fi
424
425
done = true
426
427
if index_expression? then
428
result = Trees.Expressions.AMBIGUOUS_EXPRESSION(
429
start::context.location,
430
index_expression,
431
left,
432
identifier,
433
type_arguments
434
)
435
else
436
result = Trees.Expressions.GENERIC_APPLICATION(
437
start::context.location,
438
left,
439
identifier,
440
type_arguments
441
)
442
fi
443
444
tokenizer_state.commit()
445
logger_state.commit()
446
447
// FIXME: continue generates incorrect code here
448
// continue;
449
fi
450
451
tokenizer_state.backtrack_if_speculating()
452
logger_state.backtrack_if_speculating()
453
fi
454
455
if !done then
456
let index = expression_parser.parse(context)!
457
458
result = Trees.Expressions.INDEX(start::context.location, result, index)
459
460
context.next_token(Lexical.TOKEN.SQUARE_CLOSE, syntax_error_message)
461
fi
462
463
when Lexical.TOKEN.DOT then
464
let completion_target_start = context.location
465
466
context.next_token()
467
468
if
469
context.current.token == Lexical.TOKEN.IDENTIFIER /\
470
context.current.first_on_line
471
then
472
return _parse_member_on_next_line(context, start, result, completion_target_start, tokenizer_state)
473
elif
474
context.current.token == Lexical.TOKEN.IDENTIFIER
475
then
476
let member = identifier_parser.parse(context)!
477
478
previous_left = result
479
previous_identifier = member
480
481
result = Trees.Expressions.MEMBER(start::member.location, result, member, completion_target_start::member.location)
482
elif looks_like_partial_member_keyword(context, completion_target_start) then
483
// Mid-typing case like `value.is` (en route to
484
// `value.is_empty`). The token is a reserved
485
// word the tokenizer has already classified,
486
// but it sits in identifier position after a
487
// `.`; treat it as a partial member name so
488
// the completer has a MEMBER node with a
489
// populated `right` to anchor suggestions on.
490
// The same-line guard prevents this consuming
491
// a structural keyword on the next line
492
// (`namespace Test.\n is`).
493
let name = context.current.value_string
494
let member = Trees.Identifiers.Identifier(context.location, name)
495
context.next_token()
496
497
previous_left = result
498
previous_identifier = member
499
500
result = Trees.Expressions.MEMBER(start::member.location, result, member, completion_target_start::member.location)
501
elif looks_like_operator_member_name(context, completion_target_start) then
502
// `a. =~(b)` — an operator member named after a
503
// dot. Same node as any other member access;
504
// the operator's name is just spelled in
505
// operator characters.
506
let name = context.current.value_string
507
let member = Trees.Identifiers.Identifier(context.location, name)
508
context.next_token()
509
510
previous_left = result
511
previous_identifier = member
512
513
result = Trees.Expressions.MEMBER(start::member.location, result, member, completion_target_start::member.location)
514
elif
515
context.current.token == Lexical.TOKEN.INT_LITERAL \/
516
context.current.token == Lexical.TOKEN.FLOAT_LITERAL \/
517
context.current.token == Lexical.TOKEN.DOUBLE_LITERAL
518
then
519
// `.` followed by a numeric literal, e.g. `p.0`.
520
// Positional tuple members are named by their
521
// index, but a bare number is not an identifier, so
522
// the index must be backtick-escaped (`p.` followed
523
// by a backtick and the number). Point at that, then
524
// consume the number so it doesn't linger and
525
// cascade into a spurious "expected ;" on the rest of
526
// the statement. Recover with a poisoned placeholder
527
// identifier so later passes that assume
528
// MEMBER.identifier is non-null don't crash.
529
context.error(context.location, "a numeric member name must be escaped with a leading backtick")
530
let member_location = context.location
531
context.next_token()
532
result = Trees.Expressions.MEMBER(start::member_location, result, Trees.Identifiers.Identifier(completion_target_start, ""), completion_target_start::member_location)
533
else
534
// `.` not followed by a member name: expect_token
535
// reports it. Recover with a poisoned placeholder
536
// identifier rather than null — later passes assume
537
// MEMBER.identifier is non-null and would otherwise
538
// crash dereferencing it. The offending token is left
539
// for the enclosing context to resynchronise on.
540
context.expect_token(Lexical.TOKEN.IDENTIFIER)
541
result = Trees.Expressions.MEMBER(start..context.location, result, Trees.Identifiers.Identifier(completion_target_start, ""), completion_target_start::context.location)
542
fi
543
544
when Lexical.TOKEN.ARROW_THIN, Lexical.TOKEN.ARROW_FAT, Lexical.TOKEN.IS, Lexical.TOKEN.REC then
545
// we could be in a global function or in a method. If the left part looks like it could be a function or method definition
546
// then it's an error and we need to recover.
547
548
// we'll need a heuristic to try to decide if the user is trying to define a nested function or if they've missed off a closing `si`
549
// and actually this is a method definition (if we're in a classy context) or a global function definition (if we're not in a classy context)
550
551
let is_nested_function_definition mut = false
552
553
if result.could_be_nested_function_definition /\ tokenizer_state.is_speculating then
554
// TODO need same logic but for nested within a global function
555
if context.in_member then
556
if result.location.start_column > context.member_indent then
557
// probably an attempt at nesting a named function definition
558
is_nested_function_definition = true
559
elif
560
result.location.start_column <= context.member_indent /\
561
result.location.start_column > context.global_indent
562
then
563
// probably a missing `si` in a preceding member definition
564
context.error(result.location, "expected 'si' after member definition")
565
566
tokenizer_state.backtrack()
567
568
throw UNWIND_TO_MEMBER_EXCEPTION(null)
569
fi
570
elif context.in_global_function then
571
if result.location.start_column > context.global_indent then
572
// probably an attempt at nesting a named function definition
573
is_nested_function_definition = true
574
else
575
// probably a missing `si` in a preceding member definition
576
context.error(result.location, "expected 'si' after member definition")
577
578
tokenizer_state.backtrack()
579
580
throw UNWIND_TO_GLOBAL_EXCEPTION(null)
581
fi
582
elif context.in_classy then
583
// not clear how we might have ended up here
584
tokenizer_state.backtrack()
585
586
throw UNWIND_TO_GLOBAL_EXCEPTION(null)
587
fi
588
fi
589
590
// it's unlikely to be member or global function definition, so we should
591
// parse it as a function literal, even if it has a name or is otherwise garbled.
592
// In particular, if has a block body delimited with with is / si, then we want to
593
// consume that block, otherwise our view of the block structure will get out of sync
594
// and we'll think we've let the enclosing function or method when we reach the closing
595
// `si` of the function literal
596
597
// A named function among the statements of a body. The
598
// statement parser has already established that the shape
599
// can only be one, so the name and the parenthesised
600
// formals are taken from the call and the literal is built
601
// from them. Everywhere else the same shape is still the
602
// mistake it always was.
603
let nested_name: Trees.Identifiers.Identifier? mut = _
604
let nested_call: Trees.Expressions.CALL? mut = _
605
let should_poison mut = false
606
607
if result.could_be_nested_function_definition then
608
let claimed mut = false
609
610
if context.allow_nested_function then
611
if let call: Trees.Expressions.CALL = result then
612
if let callee: Trees.Expressions.IDENTIFIER = call.function then
613
context.allow_nested_function = false
614
615
nested_call = call
616
nested_name = callee.identifier
617
claimed = true
618
fi
619
fi
620
fi
621
622
if !claimed then
623
should_poison = true
624
context.error(result.location, "nested function definition")
625
fi
626
elif !result.could_be_formal_argument then
627
should_poison = true
628
context.error(result.location, "expected function literal formal arguments")
629
fi
630
631
let type_expression: Trees.TypeExpressions.TypeExpression mut
632
let arguments: Trees.Expressions.LIST mut
633
634
if context.current.token == Lexical.TOKEN.ARROW_THIN then
635
context.next_token()
636
type_expression = type_parser.parse(context)!
637
type_expression.check_is_not_reference(context.logger, "function cannot return a ref type")
638
else
639
type_expression = Trees.TypeExpressions.INFER(start::result.location)
640
fi
641
642
if context.expect_token([Lexical.TOKEN.ARROW_FAT, Lexical.TOKEN.IS, Lexical.TOKEN.REC]) then
643
if should_poison then
644
// error already reported
645
arguments = Trees.Expressions.LIST(start::result.location, Collections.LIST[Trees.Expressions.Expression]())
646
elif nested_name? then
647
arguments = nested_call!.arguments
648
arguments.rewrite_as_variables()
649
elif isa Trees.Expressions.TUPLE(result) then
650
arguments = result.elements
651
arguments.rewrite_as_variables()
652
else
653
let elements = Collections.LIST[Trees.Expressions.Expression]()
654
elements.add(result)
655
arguments = Trees.Expressions.LIST(start::result.location, elements)
656
fi
657
658
let is_recursive =
659
if context.current.token == Lexical.TOKEN.REC then
660
context.next_token()
661
true
662
else
663
false
664
fi
665
666
// A lambda body is a fresh statement context. The
667
// enclosing argument list leaves allow_tuple_element
668
// set while an argument expression parses, so without
669
// this reset the body's leading `x = e` statement is
670
// consumed as a formal-argument-style variable
671
// declaration rather than an assignment. A body
672
// written inside a top-level statement is an ordinary
673
// body too, and takes named functions like any other.
674
context.allow_tuple_element = false
675
676
let was_top_level = context.in_top_level_statements
677
678
context.in_top_level_statements = false
679
680
let body: Trees.Bodies.Body mut = _
681
682
try
683
body = body_parser.parse(context)!
684
finally
685
context.in_top_level_statements = was_top_level
686
yrt
687
688
let literal =
689
Trees.Expressions.FUNCTION(
690
start::body.location,
691
arguments,
692
type_expression,
693
body,
694
is_recursive
695
)
696
697
if let name = nested_name then
698
literal.set_nested_name(name)
699
fi
700
701
result = literal
702
703
if should_poison then
704
result.poison()
705
fi
706
707
return result
708
else
709
// TODO: is this possible?
710
return Trees.Expressions.Literals.NONE(start::context.location)
711
fi
712
713
when Lexical.TOKEN.QUESTION then
714
let question_location = context.location
715
tokenizer_state.commit_if_speculating()
716
717
context.next_token()
718
719
if context.current.token == Lexical.TOKEN.DOT then
720
// `?.` coalescing member access. Tokenizer
721
// breaks `?.` into two adjacent tokens (per
722
// the `?`/`!` followed-by-`.` rule in
723
// read_operator), so we recognise the
724
// sequence here.
725
let completion_target_start = question_location
726
context.next_token()
727
728
if context.current.token == Lexical.TOKEN.IDENTIFIER then
729
let member = identifier_parser.parse(context)!
730
731
previous_left = result
732
previous_identifier = member
733
734
let member_node =
735
Trees.Expressions.MEMBER(
736
start::member.location,
737
result,
738
member,
739
completion_target_start::member.location
740
)
741
742
member_node.is_coalesce = true
743
result = member_node
744
else
745
// `?.` not followed by a member name: recover
746
// with a poisoned placeholder identifier, not
747
// null, so passes assuming MEMBER.identifier is
748
// non-null don't crash.
749
context.expect_token(Lexical.TOKEN.IDENTIFIER)
750
751
let member_node =
752
Trees.Expressions.MEMBER(
753
start..context.location,
754
result,
755
Trees.Identifiers.Identifier(completion_target_start, ""),
756
completion_target_start::context.location
757
)
758
759
member_node.is_coalesce = true
760
result = member_node
761
fi
762
else
763
result = Trees.Expressions.HAS_VALUE(start::question_location, result)
764
fi
765
766
when Lexical.TOKEN.REF then
767
result = Trees.Expressions.REFERENCE(start::context.location, result)
768
tokenizer_state.commit_if_speculating()
769
770
context.next_token()
771
772
when Lexical.TOKEN.OPERATOR then
773
tokenizer_state.commit_if_speculating()
774
775
if context.current_string =~ "!" then
776
result = Trees.Expressions.UNWRAP(start::context.location, result)
777
778
context.next_token()
779
else
780
return result
781
fi
782
783
when Lexical.TOKEN.SQUARE_OPEN_TICK then
784
tokenizer_state.commit_if_speculating()
785
786
context.next_token()
787
788
let type_arguments = type_list_parser.parse(context)!
789
790
let left: Trees.Expressions.Expression? mut = _
791
let identifier: Trees.Identifiers.Identifier? mut = _
792
793
if result.is_member then
794
left = previous_left
795
identifier = previous_identifier
796
797
elif result.is_identifier then
798
left = null
799
identifier = result.try_copy_as_identifer()
800
fi
801
802
if identifier? then
803
result = Trees.Expressions.GENERIC_APPLICATION(
804
start::context.location,
805
left,
806
identifier,
807
type_arguments
808
)
809
else
810
result.poison()
811
context.error(result.location, "cannot apply type arguments to this")
812
fi
813
814
context.next_token(Lexical.TOKEN.SQUARE_CLOSE)
815
816
else
817
return result
818
esac
819
od
820
si
821
si
822
si