Skip to content
← Back

src/syntax/trees/expressions/list.ghul

1
namespace Syntax.Trees.Expressions is
2
use Source;
3
4
class LIST: Expression, Collections.Iterable[Expression] is
5
expressions: Collections.LIST[Expression];
6
count: int => expressions.count;
7
8
// True when the source ended the list with a comma: `f(a, b,)`,
9
// `[a, b,]`, `(a, b,)`. The comma contributes no element, so the
10
// span is the only record that the source opened one more slot
11
// than it filled — the parser ends the list at the closing
12
// bracket in that case, and at the last element otherwise.
13
has_trailing_comma: bool =>
14
expressions.count > 0 /\
15
(
16
location.end_line != expressions[expressions.count - 1].location.end_line \/
17
location.end_column != expressions[expressions.count - 1].location.end_column
18
);
19
20
init(location: LOCATION, expressions: Collections.Iterable[Expression]) is
21
super.init(location);
22
23
self.expressions = Collections.LIST[Expression](expressions);
24
si
25
26
iterator: Collections.Iterator[Expression] => expressions.iterator;
27
28
try_get_string_literal_at(index: int) -> string? =>
29
if index < expressions.count then
30
expressions[index].try_get_string_literal()
31
else
32
null
33
fi;
34
35
replace_element(index: int, value: Expression) is
36
expressions[index] = value;
37
si
38
39
rewrite_as_variables() is
40
for index in 0..expressions.count do
41
let replacement = expressions[index].try_copy_as_variable();
42
43
if replacement? then
44
expressions[index] = replacement;
45
fi
46
od
47
si
48
49
rewrite_as_tuple_elements() is
50
for index in 0..expressions.count do
51
let replacement = expressions[index].try_copy_as_tuple_element();
52
53
if replacement? then
54
expressions[index] = replacement;
55
fi
56
od
57
si
58
59
accept(visitor: Visitor) is
60
visitor.visit(self);
61
si
62
63
walk(visitor: Visitor) is
64
if !visitor.pre(self) then
65
for e in expressions do
66
e.walk(visitor);
67
od
68
fi
69
accept(visitor);
70
si
71
si
72
si