Skip to content
← Back

src/syntax/parsers/definitions/variant_list.ghul

1
namespace Syntax.Parsers.Definitions is
2
use Source;
3
use Logging;
4
5
class VARIANT_LIST(variant_parser: Parser[Trees.Definitions.VARIANT]): Base[Trees.Definitions.LIST] is
6
super();
7
8
parse(context: CONTEXT) -> Trees.Definitions.LIST is
9
let start = context.location;
10
let should_poison mut = false;
11
12
/*
13
parse a list of typed-union variants using the supplied variant parser
14
15
union_definition ::= "union" identifier type_parameters? modifiers? "is" variant_definition+ "si"
16
^ we're here
17
*/
18
19
try
20
let variants = Collections.LIST[Trees.Definitions.Definition]();
21
22
let start = context.location;
23
let end mut = context.location;
24
25
while context.current_token != Lexical.TOKEN.SI /\ context.current_token != Lexical.TOKEN.END_OF_INPUT do
26
let variant_start = context.location;
27
28
let variant = variant_parser.parse(context);
29
30
if variant? then
31
variants.add(variant);
32
end = variant.location;
33
else
34
end = context.location;
35
fi
36
37
if !variant? \/ variant.is_poisoned then
38
should_poison = true;
39
40
if variant_start.start_column <= context.member_indent \/ context.current_token != Lexical.TOKEN.IDENTIFIER then
41
// either indent is less than the member indent or we're not looking at an idenfier
42
// either way we've likely fallen off the end of the union definition:
43
44
if context.current_token == Lexical.TOKEN.SI then
45
context.next_token();
46
else
47
context.next_token(Lexical.TOKEN.SI);
48
fi
49
50
break;
51
fi
52
fi
53
od
54
55
let result = Trees.Definitions.LIST(
56
start::end,
57
variants
58
);
59
60
61
result.poison(should_poison);
62
63
return result;
64
finally
65
context.in_classy = false;
66
yrt
67
si
68
69
lookahead_for_body(context: CONTEXT) -> bool is
70
let want_backtrack = true;
71
72
let line = context.current.location.start_line;
73
74
let use diagnostics_snapshot = context.diagnostics_speculate_then_backtrack();
75
76
// search on the current line for an 'is'
77
while context.current.location.start_line == line /\ context.current.location.start_column >= context.global_indent do
78
if context.current.token == Lexical.TOKEN.IS then
79
diagnostics_snapshot.commit();
80
81
context.next_token();
82
83
// is on the same line as the start of the class definition means it's likely the block is associated with the
84
// class, and we should parse it as such, even if the first part of the class definition is invalid
85
return true;
86
fi
87
88
context.next_token();
89
od
90
91
if context.current_token == Lexical.TOKEN.IS /\ context.current.location.start_column >= context.global_indent then
92
diagnostics_snapshot.commit();
93
context.next_token();
94
95
// again, if the `is` is on the line immediately following the class definition, and it's properly indented,
96
// it's likely the block is associated with the class, and we should parse it as such, even if the first part of
97
// the class definition is invalid
98
return true;
99
fi
100
101
return false;
102
si
103
si
104
si