Skip to content
← Back

src/syntax/parsers/definitions/enum.ghul

1
namespace Syntax.Parsers.Definitions is
2
3
use Source
4
5
class ENUM(
6
identifier_parser: Parser[Trees.Identifiers.Identifier],
7
modifier_list_parser: Parser[Trees.Modifiers.LIST],
8
expression_parser: Parser[Trees.Expressions.Expression]
9
): Base[Trees.Definitions.ENUM] is
10
super()
11
12
parse(context: CONTEXT) -> Trees.Definitions.ENUM? is
13
context.next_token(Lexical.TOKEN.ENUM)
14
let start = context.location
15
let name = identifier_parser.parse(context)
16
17
let modifiers = modifier_list_parser.parse(context)!
18
19
let members = Collections.LIST[Trees.Definitions.ENUM_MEMBER]()
20
21
let expect_si mut = false
22
23
if (name? /\ !name.is_poisoned) \/ context.current.token == Lexical.TOKEN.IS then
24
if context.next_token(Lexical.TOKEN.IS) then
25
let use open = context.open_construct(Lexical.TOKEN.SI)
26
27
expect_si = true
28
29
while context.current.token != Lexical.TOKEN.SI do
30
if members.count > 0 then
31
if !context.next_token(Lexical.TOKEN.COMMA) then
32
expect_si = false
33
break
34
fi
35
36
// Trailing comma: stop when the comma is
37
// followed by the closing `si` rather than
38
// another member.
39
if context.current.token == Lexical.TOKEN.SI then
40
break
41
fi
42
fi
43
44
let member_name = identifier_parser.parse(context)
45
46
if member_name? then
47
let member_end mut = member_name.location
48
let member_initializer: Trees.Expressions.Expression? mut = null
49
if context.current.token == Lexical.TOKEN.ASSIGN then
50
context.next_token()
51
52
member_initializer = expression_parser.parse(context)!
53
54
if member_initializer.is_poisoned then
55
expect_si = false
56
fi
57
58
member_end = member_initializer.location
59
fi
60
61
members.add(Trees.Definitions.ENUM_MEMBER(member_name.location::member_end, member_name, member_initializer))
62
else
63
expect_si = false
64
break
65
fi
66
od
67
fi
68
fi
69
70
let end = context.location
71
72
if
73
expect_si
74
then
75
context.expect_closer(Lexical.TOKEN.SI)
76
fi
77
78
if name? then
79
return Trees.Definitions.ENUM(start::end, name, modifiers, members)
80
fi
81
82
return null
83
si
84
si
85
si