Skip to content
← Back

src/syntax/parsers/expressions/primary.ghul

1
namespace Syntax.Parsers.Expressions is
2
use IO.Std
3
use Ghul.Disposable
4
5
use Source
6
7
use Logging
8
9
class PRIMARY(
10
identifier_parser: Parser[Trees.Identifiers.Identifier],
11
type_parser: Parser[Trees.TypeExpressions.TypeExpression],
12
expression_parser: Parser[Trees.Expressions.Expression],
13
expression_list_parser: Parser[Trees.Expressions.LIST],
14
expression_tuple_parser: Parser[Trees.Expressions.Expression],
15
statement_parser: Parser[Trees.Statements.Statement],
16
statement_list_parser: Parser[Trees.Statements.LIST],
17
variable_list_parser: Parser[Trees.Variables.LIST],
18
pragma_parser: Parser[Trees.Pragmas.PRAGMA],
19
variable_parser: Parser[Trees.Variables.VARIABLE]
20
): Base[Trees.Expressions.Expression] is
21
description: string => "primary expression"
22
23
_list_parser: Parsers.Expressions.LIST
24
25
super()
26
27
init(..) is
28
_list_parser = Parsers.Expressions.LIST(expression_parser)
29
30
add_parsers()
31
si
32
33
// `@Foo() name: T` — an attribute pragma on a lambda-literal
34
// parameter. Recognised only where a tuple element (and so
35
// potentially a lambda parameter) can start — everywhere else
36
// `@` falls through to the base dispatch and its ordinary
37
// "unexpected token" error, so it never shows up as a spurious
38
// option in an unrelated "expected ..." diagnostic. Whether
39
// this position genuinely turns out to be a lambda parameter
40
// (as opposed to an element of a value tuple that was never
41
// rewritten into one) is checked once the enclosing construct
42
// is known — see COMPILE_TUPLES.visit_tuple and
43
// TUPLE_ELEMENT.rewrite_as_expression.
44
parse(context: CONTEXT) -> Trees.Expressions.Expression? is
45
if context.current.token == Lexical.TOKEN.AT /\ context.allow_tuple_element then
46
return _parse_pragma_variable(context)
47
fi
48
49
return super.parse(context)
50
si
51
52
// A compound statement in element position settles the element: it
53
// is never a lambda's formal parameter. The flag has to be cleared
54
// before the body is parsed, not merely when the element is
55
// complete - inside the body, `name = e` is an assignment
56
// statement, while the flag says to read it as a named element,
57
// which declares a local shadowing the one being assigned.
58
//
59
// A statement parser that reports an error and returns nothing is
60
// stood in for by a poisoned value, as for any other unexpected
61
// token.
62
_parse_statement(context: CONTEXT) -> Trees.Expressions.Expression is
63
context.allow_tuple_element = false
64
65
let start = context.location
66
let statement = statement_parser.parse(context)
67
68
if !statement? then
69
let result = Trees.Expressions.Literals.NONE(start)
70
71
result.poison()
72
73
return result
74
fi
75
76
return Trees.Expressions.STATEMENT(statement.location, statement)
77
si
78
79
_parse_pragma_variable(context: CONTEXT) -> Trees.Expressions.Expression is
80
let start = context.location
81
82
let pragmas = Collections.LIST[Trees.Pragmas.PRAGMA]()
83
84
while context.current.token == Lexical.TOKEN.AT do
85
let pragma = pragma_parser.parse(context)
86
87
if !pragma? then
88
break
89
fi
90
91
pragmas.add(pragma)
92
od
93
94
context.allow_tuple_element = false
95
96
let identifier = identifier_parser.parse(context)!
97
98
if identifier.is_qualified then
99
context.error(identifier.location, "expected a simple parameter name here")
100
fi
101
102
let end mut = identifier.location
103
let type_expression: Trees.TypeExpressions.TypeExpression mut = Trees.TypeExpressions.INFER(identifier.location)
104
let initializer: Trees.Expressions.Expression? mut = null
105
106
if context.current.token == Lexical.TOKEN.COLON then
107
context.next_token()
108
let parsed_type = type_parser.parse(context)
109
110
if parsed_type? then
111
type_expression = parsed_type
112
end = parsed_type.location
113
fi
114
fi
115
116
if context.current.token == Lexical.TOKEN.ASSIGN then
117
context.next_token()
118
initializer = expression_parser.parse(context)
119
120
if initializer? then
121
end = initializer.location
122
fi
123
fi
124
125
let result = Trees.Expressions.VARIABLE(start::end, identifier, type_expression, initializer)
126
result.set_pragmas(pragmas)
127
128
return result
129
si
130
131
// The target type of a `cast`, or null when it is elided and
132
// comes from the context instead. Tuple types are
133
// parenthesised, so `cast (int, string)(v)` opens with the same
134
// token as `cast(v)`: a type is only taken as the target when
135
// one parses cleanly *and* the argument list's `(` follows it.
136
// Cleanly is what separates the two - `cast(f(3))` reaches a
137
// `(` where the type expression wanted a `)`, and taking the
138
// half-parsed `f` as the target loses the call and leaves the
139
// type parse's own errors behind. `cast (f)(x)` still reads as
140
// a cast to the parenthesised type `(f)`, as it did before the
141
// elided form existed.
142
_try_parse_cast_type(context: CONTEXT) -> Trees.TypeExpressions.TypeExpression? is
143
if context.current.token != Lexical.TOKEN.PAREN_OPEN then
144
return type_parser.parse(context)!
145
fi
146
147
let use diagnostics_snapshot = context.diagnostics_speculate_then_backtrack()
148
let use snapshot = context.tokenizer_speculate_then_backtrack()
149
150
let errors_before = context.logger.error_count
151
152
let type_expression = type_parser.parse(context)
153
154
// The group that follows holds the cast's value only where it
155
// is written on the same line: a line opening with `(` starts
156
// something new, so the cast ends at the line break and the
157
// group it was read as the target of is its value.
158
if
159
type_expression? /\
160
!type_expression.is_poisoned /\
161
context.logger.error_count == errors_before /\
162
context.current.token == Lexical.TOKEN.PAREN_OPEN /\
163
!context.current.first_on_line
164
then
165
snapshot.commit()
166
diagnostics_snapshot.commit()
167
168
return type_expression
169
fi
170
171
return null
172
si
173
174
// A comprehension written without its element: `{for i in xs}`,
175
// `[for i in xs if p(i)]`. Entered with `for` current where the
176
// element belongs. A loop is a valid element, so the text is read
177
// speculatively as the comprehension's clauses alone, and that
178
// reading is kept only if it reaches the closing bracket without
179
// complaint; a loop element never does, since its `do` ends the
180
// clauses short of the bracket. Otherwise nothing is consumed and
181
// the element is parsed as it would be anyway.
182
_parse_comprehension_without_element(context: CONTEXT, start: LOCATION, is_lazy: bool) -> Trees.Expressions.Expression? is
183
let for_location = context.location
184
185
let use diagnostics_snapshot = context.diagnostics_speculate_then_backtrack()
186
let use snapshot = context.tokenizer_speculate_then_backtrack_bounded()
187
188
let errors_before = context.logger.error_count
189
190
let element = Trees.Expressions.Literals.NONE(for_location)
191
element.poison()
192
193
let result = _parse_comprehension(context, start, element, is_lazy)
194
195
if context.logger.error_count == errors_before then
196
snapshot.commit()
197
diagnostics_snapshot.commit()
198
199
context.error(for_location, "expected an element expression before for in comprehension")
200
201
return result
202
fi
203
204
snapshot.backtrack()
205
diagnostics_snapshot.backtrack()
206
207
return null
208
si
209
210
// `[element for p in source if condition ...]`, or the same in
211
// braces for a lazy comprehension, entered with the element parsed
212
// and `for` current. Clauses need no separator: each starts with
213
// its own keyword, and a source or condition ends at the next one
214
// or at the closing bracket or brace.
215
_parse_comprehension(context: CONTEXT, start: LOCATION, element: Trees.Expressions.Expression, is_lazy: bool) -> Trees.Expressions.Expression is
216
let closer = if is_lazy then Lexical.TOKEN.BRACE_CLOSE else Lexical.TOKEN.SQUARE_CLOSE fi
217
let closer_text = if is_lazy then "'}}'" else "']'" fi
218
let what = if is_lazy then "comprehension" else "list comprehension" fi
219
220
let use open =
221
context.open_construct(
222
Collections.LIST[Lexical.TOKEN]([
223
Lexical.TOKEN.FOR,
224
Lexical.TOKEN.IF,
225
closer
226
])
227
)
228
229
let clauses = Collections.LIST[Trees.Expressions.COMPREHENSION_CLAUSE]()
230
let first_for: Trees.Expressions.COMPREHENSION_FOR? mut = null
231
let should_poison mut = element.is_poisoned
232
let clause_failed mut = false
233
234
while
235
context.current.token == Lexical.TOKEN.FOR \/ context.current.token == Lexical.TOKEN.IF
236
do
237
let clause_start = context.location
238
239
if context.current.token == Lexical.TOKEN.FOR then
240
context.next_token()
241
242
let variable = variable_parser.parse(context)
243
244
if !variable? \/ variable.is_poisoned then
245
should_poison = true
246
clause_failed = true
247
break
248
fi
249
250
if !context.next_token(Lexical.TOKEN.IN, "in {what}") then
251
should_poison = true
252
clause_failed = true
253
break
254
fi
255
256
let source = expression_parser.parse(context)!
257
258
should_poison = should_poison \/ variable.is_poisoned \/ source.is_poisoned
259
260
let clause = Trees.Expressions.COMPREHENSION_FOR(clause_start::source.location, variable, source)
261
262
if clauses.count == 0 then
263
first_for = clause
264
fi
265
266
clauses.add(clause)
267
else
268
context.next_token()
269
270
let condition = expression_parser.parse(context)!
271
272
should_poison = should_poison \/ condition.is_poisoned
273
274
clauses.add(Trees.Expressions.COMPREHENSION_IF(clause_start::condition.location, condition))
275
fi
276
od
277
278
let end mut = context.location
279
280
// A clause that failed has already said what it expected;
281
// anything else short of the bracket is reported here. Either
282
// way the remains are skipped as far as the closing bracket.
283
// If the bracket never comes, the skip stops at whatever an
284
// enclosing construct is waiting for, or at the next line
285
// written level with the one the comprehension started on.
286
if context.current.token != closer then
287
if !clause_failed then
288
context.error(context.location, "expected for, if or {closer_text} in {what}")
289
should_poison = true
290
fi
291
292
while
293
!context.is_end_of_file /\
294
context.current.token != closer /\
295
!context.is_awaited_ender(context.current.token) /\
296
!context.is_level_with_open_construct
297
do
298
context.next_token()
299
od
300
301
end = context.location
302
fi
303
304
if context.current.token == closer then
305
context.next_token()
306
fi
307
308
// Only the array form takes a written type: a lazy one is a
309
// pipe of its element type.
310
let type_expression: Trees.TypeExpressions.TypeExpression =
311
if !is_lazy /\ context.current.token == Lexical.TOKEN.COLON then
312
context.next_token()
313
let parsed = type_parser.parse(context)!
314
end = parsed.location
315
parsed
316
else
317
Trees.TypeExpressions.INFER(start::end)
318
fi
319
320
// A clause that failed before it was built leaves nothing to
321
// hang the element on, and the element would name variables
322
// nothing declares, so the recovery is an empty list literal.
323
if !first_for? then
324
let recovered =
325
Trees.Expressions.SEQUENCE(
326
start::end,
327
Trees.Expressions.LIST(element.location, Collections.LIST[Trees.Expressions.Expression]()),
328
type_expression
329
)
330
331
recovered.poison()
332
333
return recovered
334
fi
335
336
for i in 1..clauses.count do
337
clauses[i - 1].inner = clauses[i]
338
od
339
340
let result =
341
Trees.Expressions.COMPREHENSION(
342
start::end,
343
element,
344
first_for,
345
type_expression,
346
is_lazy
347
)
348
349
result.poison(should_poison)
350
351
return result
352
si
353
354
add_parsers() is
355
add_parser(
356
(context) -> Trees.Expressions.Expression is
357
let start = context.location
358
let identifier = identifier_parser.parse(context)!
359
360
// A bare unqualified `_` in a value position is the
361
// default-value expression. The `: T` and `= init`
362
// guards keep the typed-discard formal `_: T` on the
363
// identifier path. Binding and pattern positions
364
// (`let _ =`, destructure leaves, `for _`, `if let`)
365
// never reach this parser — they parse identifiers
366
// directly — so the discard meanings don't collide
367
// with the value meaning. Inside a `(...)` group the
368
// speculative tuple/pattern rewrite converts a `_`
369
// element back to a discard leaf via DEFAULT's
370
// try_copy_as_*.
371
if
372
identifier.name =~ "_" /\
373
!identifier.is_qualified /\
374
context.current.token != Lexical.TOKEN.COLON /\
375
context.current.token != Lexical.TOKEN.ASSIGN
376
then
377
// `_(args)` constructs the type the context
378
// expects. `_` is never callable, so a `(` here
379
// opens a constructor argument list, by the same
380
// reasoning that makes `[` a type argument below.
381
if context.current_token == Lexical.TOKEN.PAREN_OPEN then
382
context.next_token()
383
384
let arguments: Trees.Expressions.LIST? mut = null
385
386
if context.current.token != Lexical.TOKEN.PAREN_CLOSE then
387
arguments = expression_list_parser.parse(context)
388
fi
389
390
context.next_token(Lexical.TOKEN.PAREN_CLOSE, syntax_error_message)
391
392
if !arguments? then
393
arguments =
394
Trees.Expressions.LIST(
395
context.location,
396
Collections.LIST[Trees.Expressions.Expression]()
397
)
398
fi
399
400
return Trees.Expressions.CONSTRUCT(start::arguments.location, arguments)
401
fi
402
403
// `_[T]` pins the type explicitly. `_` is
404
// never indexable, so a `[` here is the type
405
// argument, not an indexer.
406
let type_expression: Trees.TypeExpressions.TypeExpression? mut = null
407
408
if context.current_token == Lexical.TOKEN.SQUARE_OPEN then
409
context.next_token()
410
type_expression = type_parser.parse(context)
411
context.next_token(Lexical.TOKEN.SQUARE_CLOSE)
412
fi
413
414
return Trees.Expressions.DEFAULT(start::context.location, type_expression)
415
fi
416
417
if context.allow_tuple_element /\ !identifier.is_qualified then
418
context.allow_tuple_element = false
419
let end mut = identifier.location
420
let type_expression: Trees.TypeExpressions.TypeExpression? mut = null
421
let initializer: Trees.Expressions.Expression? mut = null
422
423
if context.current.token == Lexical.TOKEN.COLON then
424
context.next_token()
425
type_expression = type_parser.parse(context)
426
end = type_expression!.location
427
fi
428
429
if context.current.token == Lexical.TOKEN.ASSIGN then
430
context.next_token()
431
initializer = expression_parser.parse(context)
432
end = initializer!.location
433
fi
434
435
if initializer? then
436
if type_expression == null then
437
type_expression = Trees.TypeExpressions.INFER(context.location)
438
fi
439
fi
440
441
if type_expression? then
442
return Trees.Expressions.VARIABLE(start::end, identifier, type_expression, initializer)
443
fi
444
fi
445
446
return Trees.Expressions.IDENTIFIER(identifier.location, identifier)
447
si, Lexical.TOKEN.IDENTIFIER
448
)
449
450
add_parser(
451
(context) is
452
let start = context.location
453
context.next_token()
454
455
// `[ ]` with the brackets tokenized separately is the
456
// empty list literal, the same as the `[]` single token;
457
// its element type comes from the surrounding context.
458
// Otherwise the token after the first element decides:
459
// `for` makes it a comprehension, anything else a list.
460
if context.current.token == Lexical.TOKEN.FOR then
461
if let recovered = _parse_comprehension_without_element(context, start, false) then
462
return recovered
463
fi
464
fi
465
466
let elements_start = context.location
467
let first =
468
if context.current.token == Lexical.TOKEN.SQUARE_CLOSE then
469
null
470
else
471
_list_parser.parse_element(context)
472
fi
473
474
if first? /\ context.current.token == Lexical.TOKEN.FOR then
475
return _parse_comprehension(context, start, first, false)
476
fi
477
478
let elements =
479
if first? then
480
_list_parser.parse_after(context, elements_start, first)
481
else
482
Trees.Expressions.LIST(context.location, Collections.LIST[Trees.Expressions.Expression]())
483
fi
484
485
let end mut = context.location
486
context.next_token(Lexical.TOKEN.SQUARE_CLOSE)
487
let type_expression: Trees.TypeExpressions.TypeExpression =
488
if context.current.token == Lexical.TOKEN.COLON then
489
context.next_token()
490
let parsed = type_parser.parse(context)!
491
end = parsed.location
492
parsed
493
else
494
Trees.TypeExpressions.INFER(start::end)
495
fi
496
497
return Trees.Expressions.SEQUENCE(start::end, elements, type_expression)
498
si,
499
Lexical.TOKEN.SQUARE_OPEN
500
)
501
502
// `{element for p in source ...}`: a lazy comprehension. Braces
503
// mean nothing else in an expression, so what follows the
504
// element has to be a `for` clause.
505
add_parser(
506
(context) is
507
let start = context.location
508
context.next_token()
509
510
if context.current.token == Lexical.TOKEN.FOR then
511
if let recovered = _parse_comprehension_without_element(context, start, true) then
512
return recovered
513
fi
514
fi
515
516
let element = expression_parser.parse(context)!
517
518
if context.current.token != Lexical.TOKEN.FOR then
519
context.error(context.location, "expected for in comprehension")
520
fi
521
522
return _parse_comprehension(context, start, element, true)
523
si,
524
Lexical.TOKEN.BRACE_OPEN
525
)
526
527
add_parser(
528
(context) is
529
// `[]` is a single token. As a value it is the empty
530
// list literal: the element type can't come from the
531
// (absent) elements, so it relies on a constraint pushed
532
// down by the surrounding context — a typed initializer,
533
// a call argument with a known formal type, and so on.
534
// With no such constraint compile_tuples reports
535
// "cannot infer type of list literal with no elements".
536
let location = context.location
537
538
context.next_token()
539
540
return Trees.Expressions.SEQUENCE(
541
location,
542
Trees.Expressions.LIST(location, Collections.LIST[Trees.Expressions.Expression]()),
543
Trees.TypeExpressions.INFER(location)
544
)
545
si,
546
Lexical.TOKEN.ARRAY_DEF
547
)
548
549
add_parser(
550
(context) is
551
return expression_tuple_parser.parse(context)!
552
si,
553
Lexical.TOKEN.PAREN_OPEN
554
)
555
556
// `new` is no longer part of the language, but it stays a
557
// reserved token so that reaching one here can say so. Freed
558
// as an identifier it would instead report a symbol that does
559
// not exist, which says nothing about what to write instead.
560
// Consuming just the keyword and parsing on leaves
561
// `TYPE(args)`, which is what the construction should have
562
// been written as, so the rest of the expression analyses
563
// normally and the reader gets this error alone rather than a
564
// cascade behind it.
565
add_parser(
566
(context) is
567
let location = context.location
568
569
context.next_token(Lexical.TOKEN.NEW)
570
571
context.logger.error(
572
location,
573
"new is not supported",
574
location,
575
"help: construct by calling the type, as in TYPE(arguments)"
576
)
577
578
let expression = parse(context)
579
580
if !expression? then
581
return Trees.Expressions.Literals.STRING(location, "")
582
fi
583
584
return expression
585
si,
586
Lexical.TOKEN.NEW
587
)
588
589
add_parser(
590
(context) -> Trees.Expressions.Expression is
591
let start = context.location
592
593
context.next_token(Lexical.TOKEN.CAST)
594
595
let opens_with_group = context.current.token == Lexical.TOKEN.PAREN_OPEN
596
597
let type_expression = _try_parse_cast_type(context)
598
599
context.next_token(Lexical.TOKEN.PAREN_OPEN)
600
601
// A parenthesised target reads two ways whenever it
602
// is also a value: as the cast's target type with
603
// the parentheses that follow holding its value, or
604
// as the cast's value with those parentheses
605
// calling the result. Both readings are carried to
606
// resolve-type-expressions, which can ask the scope
607
// which of the two the name is. A target that only
608
// reads as a type - a tuple, a function type - is
609
// unambiguous: its value reading could not be
610
// called. A name is the only target shape whose
611
// value reading is callable, so it is the only one
612
// taken as ambiguous.
613
let called_value =
614
if
615
opens_with_group /\
616
isa Trees.TypeExpressions.NAMED(type_expression)
617
then
618
type_expression.try_copy_as_value_expression()
619
else
620
null
621
fi
622
623
if called_value? /\ type_expression? then
624
let arguments =
625
if context.current.token != Lexical.TOKEN.PAREN_CLOSE then
626
expression_list_parser.parse(context)!
627
else
628
Trees.Expressions.LIST(
629
context.location,
630
Collections.LIST[Trees.Expressions.Expression]()
631
)
632
fi
633
634
let end = context.location
635
636
context.next_token(Lexical.TOKEN.PAREN_CLOSE)
637
638
// A cast takes one value, so an argument list of
639
// any other length settles the reading here: only
640
// the call can be meant, and it is built as one.
641
if arguments.expressions.count != 1 then
642
return Trees.Expressions.CALL(
643
start::end,
644
Trees.Expressions.CAST(start::end, null, called_value, true),
645
arguments
646
)
647
fi
648
649
let result =
650
Trees.Expressions.CAST(
651
start::end,
652
type_expression,
653
arguments.expressions[0],
654
true
655
)
656
657
result.set_ambiguous_readings(
658
Trees.Expressions.CALL(
659
start::end,
660
Trees.Expressions.CAST(start::end, null, called_value, true),
661
arguments
662
),
663
arguments
664
)
665
666
return result
667
fi
668
669
let value = expression_parser.parse(context)!
670
let result = Trees.Expressions.CAST(start::context.location, type_expression, value, true)
671
672
context.next_token(Lexical.TOKEN.PAREN_CLOSE)
673
return result
674
si,
675
Lexical.TOKEN.CAST
676
)
677
678
add_parser(
679
(context) is
680
let start = context.location
681
context.next_token(Lexical.TOKEN.ISA)
682
let type_expression = type_parser.parse(context)!
683
context.next_token(Lexical.TOKEN.PAREN_OPEN)
684
let value = expression_parser.parse(context)!
685
let result = Trees.Expressions.ISA(start::context.location, type_expression, value)
686
context.next_token(Lexical.TOKEN.PAREN_CLOSE)
687
return result
688
si,
689
Lexical.TOKEN.ISA
690
)
691
692
add_parser(
693
(context) is
694
let start = context.location
695
context.next_token(Lexical.TOKEN.TYPEOF)
696
let type_expression = type_parser.parse(context)!
697
let result = Trees.Expressions.TYPEOF(start::context.location, type_expression)
698
return result
699
si,
700
Lexical.TOKEN.TYPEOF
701
)
702
703
add_parser(
704
(context) is
705
let location = context.location
706
let value_string = context.current.value_string
707
context.next_token()
708
return Trees.Expressions.Literals.INTEGER(location, value_string)
709
si,
710
Lexical.TOKEN.INT_LITERAL
711
)
712
713
add_parser(
714
(context) is
715
let location = context.location
716
let value_string = context.current.value_string
717
context.next_token()
718
return Trees.Expressions.Literals.FLOAT(location, value_string)
719
si,
720
Lexical.TOKEN.FLOAT_LITERAL
721
)
722
723
add_parser(
724
(context) is
725
let location = context.location
726
let value_string = context.current.value_string
727
context.next_token()
728
return Trees.Expressions.Literals.STRING(location, value_string)
729
si,
730
Lexical.TOKEN.STRING_LITERAL
731
)
732
733
add_parser(
734
(context) is
735
let location = context.location
736
let value_string = context.current.value_string
737
context.next_token()
738
return Trees.Expressions.Literals.CHARACTER(location, value_string)
739
si,
740
Lexical.TOKEN.CHAR_LITERAL
741
)
742
743
add_parser(
744
(context) => parse_string_with_interpolations(context),
745
Lexical.TOKEN.ENTER_STRING
746
)
747
748
add_parser(
749
(context) is
750
let location = context.location
751
let value_string = context.current.value_string
752
context.next_token()
753
return Trees.Expressions.Literals.BOOLEAN(location, value_string)
754
si,
755
[Lexical.TOKEN.TRUE, Lexical.TOKEN.FALSE]
756
)
757
758
add_parser(
759
(context) is
760
let location = context.location
761
context.next_token()
762
return Trees.Expressions.NULL(location)
763
si,
764
Lexical.TOKEN.NULL
765
)
766
767
add_parser(
768
(context) is
769
let location = context.location
770
context.next_token()
771
return Trees.Expressions.SELF(location)
772
si,
773
Lexical.TOKEN.SELF
774
)
775
776
add_parser(
777
(context) is
778
let location = context.location
779
context.next_token()
780
return Trees.Expressions.SUPER(location)
781
si, Lexical.TOKEN.SUPER
782
)
783
784
add_parser(
785
(context) is
786
let location = context.location
787
context.next_token()
788
return Trees.Expressions.RECURSE(location)
789
si, Lexical.TOKEN.REC
790
)
791
792
// Loops in expression position are delegated in the same way as
793
// `if` and `case`. The wrapped statement produces a value only
794
// when a valued `break` targets it; compile-expressions reports
795
// the value-less forms.
796
add_parser(
797
(context) => _parse_statement(context),
798
[
799
Lexical.TOKEN.IF,
800
Lexical.TOKEN.CASE,
801
Lexical.TOKEN.DO,
802
Lexical.TOKEN.FOR,
803
Lexical.TOKEN.WHILE
804
]
805
)
806
807
// `val ... lav` — a value-producing block. The block's
808
// value is the LUB of its tail expression (if it provides
809
// one) and every `return E` whose target is this block;
810
// `return E` inside a `val ... lav` exits the innermost
811
// enclosing val-block rather than the enclosing function.
812
add_parser(
813
(context) -> Trees.Expressions.Expression is
814
let start = context.location
815
context.next_token(Lexical.TOKEN.VAL)
816
817
let use open = context.open_construct(Lexical.TOKEN.LAV)
818
let statements = statement_list_parser.parse(context)!
819
let end = context.location
820
context.expect_closer(Lexical.TOKEN.LAV, syntax_error_message)
821
return Trees.Expressions.VAL_BLOCK(start::end, statements)
822
si, Lexical.TOKEN.VAL
823
)
824
825
add_parser(
826
(context) is
827
let start = context.location
828
context.next_token()
829
let want_dispose mut = false
830
831
if context.current_token == Lexical.TOKEN.USE then
832
context.next_token()
833
834
want_dispose = true
835
fi
836
837
let variable_list = variable_list_parser.parse(context)!
838
839
let expression =
840
if context.next_token(Lexical.TOKEN.IN) then
841
expression_parser.parse(context)!
842
else
843
Trees.Expressions.Literals.NONE(start::variable_list.location)
844
fi
845
846
let location = start::expression.location
847
848
return Trees.Expressions.LET_IN(
849
location,
850
want_dispose,
851
variable_list,
852
expression
853
)
854
si,
855
Lexical.TOKEN.LET
856
)
857
858
add_parser(
859
(context) is
860
let start = context.location
861
context.next_token()
862
863
let condition = expression_parser.parse(context)!
864
865
let message: Trees.Expressions.Expression? mut = null
866
867
if context.current_token == Lexical.TOKEN.ELSE /\ context.continues(start) then
868
context.next_token()
869
message = expression_parser.parse(context)
870
fi
871
872
let expression =
873
if context.next_token(Lexical.TOKEN.IN, syntax_error_message) then
874
expression_parser.parse(context)!
875
else
876
Trees.Expressions.Literals.NONE(start::condition.location)
877
fi
878
879
let location = start::expression.location
880
881
return Trees.Expressions.ASSERT_IN(
882
location,
883
condition,
884
message,
885
expression
886
)
887
si,
888
Lexical.TOKEN.ASSERT
889
)
890
si
891
892
parse_string_with_interpolations(context: CONTEXT) -> Trees.Expressions.Expression is
893
// ENTER_STRING expression (':' FORMAT_STRING)? (CONTINUE_STRING expression (':' FORMAT_STRING)? )* (EXIT_STRING | CANCEL_STRING)
894
// ^ we're here
895
896
let start = context.location
897
898
let fragments = Collections.LIST[Trees.Expressions.INTERPOLATION_FRAGMENT]()
899
900
let is_first mut = true
901
let success mut = false
902
let line mut = start.start_line
903
904
while
905
context.current_token != Lexical.TOKEN.EXIT_STRING
906
do
907
// a newline in the string part will result in a CANCEL_STRING token
908
// we should stop parsing the string and return immediately
909
// TODO: return the interpolation that we've assembled so far
910
911
if context.current_token == Lexical.TOKEN.CANCEL_STRING then
912
// TODO might want to skip to end of line, as the input could be garbled
913
context.next_token()
914
return Trees.Expressions.Literals.STRING(start::context.location, "")
915
fi
916
917
// we've either just entered a string interpolation, or we've just passed a closing curly bracket
918
// either way we're expecting a string fragment
919
920
line = context.location.start_line
921
922
let success_line = parse_single_interpolated_string_fragment(context, is_first, fragments)
923
success = success_line.success
924
line = success_line.line
925
926
if !success then
927
skip_to_end_of_string_with_interpolations(context, line)
928
929
return Trees.Expressions.Literals.STRING(start::context.location, "")
930
fi
931
932
if context.current_token == Lexical.TOKEN.CANCEL_STRING then
933
// TODO might want to skip to end of line, as the input could be garbled
934
context.next_token()
935
return Trees.Expressions.Literals.STRING(start::context.location, "")
936
fi
937
938
(success, line) = parse_single_interpolated_expression(context, fragments)
939
940
if !success then
941
skip_to_end_of_string_with_interpolations(context, line)
942
943
return Trees.Expressions.Literals.STRING(start::context.location, "")
944
fi
945
946
is_first = false
947
od
948
949
if context.current_token == Lexical.TOKEN.CANCEL_STRING then
950
context.next_token()
951
elif context.current_token != Lexical.TOKEN.EXIT_STRING then
952
context.error(context.location, "unexpected token in string interpolation")
953
954
if context.location.start_line == start.start_line then
955
skip_to_end_of_string_with_interpolations(context, line)
956
fi
957
else
958
parse_single_interpolated_string_fragment(context, is_first, fragments)
959
fi
960
961
let end = context.location
962
963
let expression_count mut = 0
964
let should_poison mut = false
965
let total_fragment_length mut = 0
966
967
for e in fragments do
968
if e.is_expression then
969
expression_count = expression_count + 1
970
if e.expression.is_poisoned then
971
should_poison = true
972
fi
973
else
974
let fragment = cast Trees.Expressions.Literals.STRING?(e.expression)!
975
let fragment_length = fragment.value_string.length
976
977
total_fragment_length = total_fragment_length + fragment_length
978
fi
979
od
980
981
let result = Trees.Expressions.STRING_INTERPOLATION(start::end, fragments, total_fragment_length, expression_count)
982
983
return result
984
si
985
986
skip_to_end_of_string_with_interpolations(context: CONTEXT, line: int) is
987
let retries mut = 50
988
989
while
990
context.current_token != Lexical.TOKEN.EXIT_STRING /\
991
context.current_token != Lexical.TOKEN.CANCEL_STRING /\
992
context.location.start_line == line /\
993
!context.is_end_of_file /\
994
retries > 0
995
do
996
context.next_token()
997
retries = retries - 1
998
od
999
si
1000
1001
parse_single_interpolated_string_fragment(
1002
context: CONTEXT,
1003
is_first: bool,
1004
into: Collections.MutableList[Trees.Expressions.INTERPOLATION_FRAGMENT]
1005
) ->
1006
(success: bool, line: int)
1007
is
1008
// ENTER_STRING expression (':' FORMAT_STRING)? (CONTINUE_STRING expression (':' FORMAT_STRING)? )* (EXIT_STRING | CANCEL_STRING)
1009
// ^ we're here ^ or here ^ or here
1010
1011
// we've either just entered a string interpolation, or we've just passed a closing curly bracket
1012
// either way we're expecting a string fragment
1013
1014
if is_first then
1015
if !context.expect_token(Lexical.TOKEN.ENTER_STRING) then
1016
return (false, 0)
1017
fi
1018
else
1019
if
1020
context.current_token != Lexical.TOKEN.CONTINUE_STRING /\
1021
context.current_token != Lexical.TOKEN.EXIT_STRING
1022
then
1023
context.logger.error(context.location, "syntax error: expected }} but found {context.current_token_name}")
1024
1025
return (false, 0)
1026
fi
1027
fi
1028
1029
into.add(
1030
Trees.Expressions.INTERPOLATION_FRAGMENT(
1031
false,
1032
Trees.Expressions.Literals.STRING(context.location, context.current_string!),
1033
null,
1034
null
1035
)
1036
)
1037
1038
let line = context.location.start_line
1039
1040
context.next_token()
1041
1042
return (true, line)
1043
si
1044
1045
parse_single_interpolated_expression(context: CONTEXT, into: Collections.MutableList[Trees.Expressions.INTERPOLATION_FRAGMENT]) -> (success: bool, line: int) is
1046
// ENTER_STRING expression (':' FORMAT_STRING)? (CONTINUE_STRING expression (':' FORMAT_STRING)? )* (EXIT_STRING | CANCEL_STRING)
1047
// ^ we're here ^ or here
1048
1049
let start = context.location
1050
1051
let expression: Trees.Expressions.Expression mut
1052
1053
// Neither an interpolated expression nor the alignment
1054
// after it is ever a formal-argument list, and the `:` that
1055
// can follow either opens a format specifier rather than a
1056
// type. Clear the flag the enclosing expression list may
1057
// have left set for the whole fragment, so nothing inside
1058
// it consumes that `:`.
1059
let previous_allow_tuple_element = context.allow_tuple_element
1060
context.allow_tuple_element = false
1061
1062
if context.current_token != Lexical.TOKEN.CONTINUE_STRING then
1063
expression = expression_parser.parse(context)!
1064
else
1065
expression = Trees.Expressions.Literals.STRING(context.location, "")
1066
context.logger.error(context.location, "expected an expression")
1067
context.next_token()
1068
fi
1069
1070
if expression.is_poisoned then
1071
if
1072
context.current_token != Lexical.TOKEN.COMMA /\
1073
context.current_token != Lexical.TOKEN.COLON /\
1074
context.current_token != Lexical.TOKEN.CONTINUE_STRING /\
1075
context.current_token != Lexical.TOKEN.EXIT_STRING
1076
then
1077
context.allow_tuple_element = previous_allow_tuple_element
1078
return (false, 0)
1079
fi
1080
fi
1081
1082
let alignment: Trees.Expressions.Expression? mut = null
1083
let format: string? mut = null
1084
1085
let line mut = 0
1086
1087
if context.current_token == Lexical.TOKEN.COMMA then
1088
context.next_token()
1089
1090
alignment = expression_parser.parse(context)!
1091
1092
if alignment.is_poisoned then
1093
if
1094
context.current_token != Lexical.TOKEN.COLON /\
1095
context.current_token != Lexical.TOKEN.CONTINUE_STRING /\
1096
context.current_token != Lexical.TOKEN.EXIT_STRING
1097
then
1098
context.allow_tuple_element = previous_allow_tuple_element
1099
return (false, 0)
1100
fi
1101
fi
1102
1103
line = alignment.location.end_line
1104
fi
1105
1106
if context.current_token == Lexical.TOKEN.COLON then
1107
context.expect_format_specifier()
1108
1109
context.next_token()
1110
1111
if context.expect_token(Lexical.TOKEN.FORMAT_STRING) then
1112
format = context.current_string
1113
line = context.location.end_line
1114
1115
context.next_token()
1116
fi
1117
fi
1118
1119
context.allow_tuple_element = previous_allow_tuple_element
1120
1121
into.add(Trees.Expressions.INTERPOLATION_FRAGMENT(true, expression, alignment, format))
1122
1123
return (true, line)
1124
si
1125
1126
other_token(context: CONTEXT) -> Trees.Expressions.Expression is
1127
super.other_token(context)
1128
1129
let result = Trees.Expressions.Literals.NONE(context.location)
1130
1131
result.poison()
1132
1133
return result
1134
si
1135
1136
si
1137
si