Appearance
| 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 | // A named-argument call is walked with its arguments put into the | |
| 40 | // order of the parameters they name, and any omitted ones filled in. | |
| 41 | // The order they were written in is kept here the first time, so | |
| 42 | // that a later walk of the same call starts from what was written | |
| 43 | // rather than permuting the permuted list again. | |
| 44 | _written: Collections.List[Expression]? | |
| 45 | ||
| 46 | record_written_order() is | |
| 47 | if !_written? then | |
| 48 | _written = Collections.LIST[Expression](expressions) | |
| 49 | fi | |
| 50 | si | |
| 51 | ||
| 52 | clear() is | |
| 53 | super.clear() | |
| 54 | _restore_written_order() | |
| 55 | si | |
| 56 | ||
| 57 | clear_outputs() is | |
| 58 | super.clear_outputs() | |
| 59 | _restore_written_order() | |
| 60 | si | |
| 61 | ||
| 62 | _restore_written_order() is | |
| 63 | if let written = _written then | |
| 64 | expressions.clear() | |
| 65 | expressions.add_range(written) | |
| 66 | fi | |
| 67 | si | |
| 68 | ||
| 69 | rewrite_as_variables() is | |
| 70 | for index in 0..expressions.count do | |
| 71 | let replacement = expressions[index].try_copy_as_variable() | |
| 72 | ||
| 73 | if replacement? then | |
| 74 | expressions[index] = replacement | |
| 75 | fi | |
| 76 | od | |
| 77 | si | |
| 78 | ||
| 79 | rewrite_as_tuple_elements() is | |
| 80 | for index in 0..expressions.count do | |
| 81 | let replacement = expressions[index].try_copy_as_tuple_element() | |
| 82 | ||
| 83 | if replacement? then | |
| 84 | expressions[index] = replacement | |
| 85 | fi | |
| 86 | od | |
| 87 | si | |
| 88 | ||
| 89 | accept(visitor: Visitor) is | |
| 90 | visitor.visit(self) | |
| 91 | si | |
| 92 | ||
| 93 | walk(visitor: Visitor) is | |
| 94 | if !visitor.pre(self) then | |
| 95 | for e in expressions do | |
| 96 | e.walk(visitor) | |
| 97 | od | |
| 98 | fi | |
| 99 | accept(visitor) | |
| 100 | si | |
| 101 | si | |
| 102 | si |