Skip to content
← Back

src/syntax/parsers/definitions/trait.ghul

1
namespace Syntax.Parsers.Definitions is
2
use Source
3
4
class TRAIT(
5
identifier_parser: Parser[Trees.Identifiers.Identifier],
6
type_parser: Parser[Trees.TypeExpressions.TypeExpression],
7
type_list_parser: Parser[Trees.TypeExpressions.LIST],
8
modifier_list_parser: Parser[Trees.Modifiers.LIST],
9
definition_list_parser: Parser[Trees.Definitions.LIST]
10
): Base[Trees.Definitions.TRAIT] is
11
super()
12
13
parse(context: CONTEXT) -> Trees.Definitions.TRAIT? is
14
let start = context.location
15
context.in_classy = true
16
context.global_indent = start.start_column
17
18
try
19
context.next_token(Lexical.TOKEN.TRAIT)
20
let identifier = identifier_parser.parse(context)
21
22
let is_poisoned mut = false
23
24
if !identifier? then
25
return null
26
fi
27
28
let arguments: Trees.TypeExpressions.LIST? mut = null
29
let ancestors: Trees.TypeExpressions.LIST? mut = null
30
31
if context.current.token == Lexical.TOKEN.SQUARE_OPEN then
32
context.next_token()
33
34
context.in_type_parameters = true
35
context.in_generic_parameter_list = true
36
arguments = type_list_parser.parse(context)!
37
context.in_type_parameters = false
38
context.in_generic_parameter_list = false
39
arguments.check_no_reference_types(context.logger)
40
41
is_poisoned = arguments.is_poisoned
42
43
if
44
!is_poisoned \/
45
context.current.token == Lexical.TOKEN.SQUARE_CLOSE
46
then
47
is_poisoned = !context.next_token(Lexical.TOKEN.SQUARE_CLOSE) \/ is_poisoned
48
fi
49
fi
50
51
if context.current.token == Lexical.TOKEN.COLON then
52
context.next_token()
53
ancestors = type_list_parser.parse(context)!
54
ancestors.check_no_reference_types(context.logger)
55
56
is_poisoned = is_poisoned \/ ancestors.is_poisoned
57
fi
58
59
let modifiers = modifier_list_parser.parse(context)!
60
61
let expect_body = !is_poisoned \/ context.current.token == Lexical.TOKEN.IS
62
let have_body mut = false
63
64
let body: Trees.Definitions.LIST mut
65
66
if expect_body /\ context.next_token(Lexical.TOKEN.IS) then
67
let use open = context.open_construct(Lexical.TOKEN.SI)
68
69
let in_trait = context.in_trait
70
context.in_trait = true
71
72
body = definition_list_parser.parse(context)!
73
74
context.in_trait = in_trait
75
76
have_body = true
77
else
78
body = Trees.Definitions.LIST(LOCATION.internal, Collections.LIST[Trees.Definitions.Definition](0))
79
is_poisoned = true
80
fi
81
82
let result = Trees.Definitions.TRAIT(
83
start::context.location,
84
identifier,
85
arguments,
86
ancestors,
87
modifiers,
88
body
89
)
90
91
result.poison(is_poisoned)
92
93
if have_body then
94
context.expect_closer(Lexical.TOKEN.SI)
95
fi
96
97
return result
98
finally
99
context.in_classy = false
100
yrt
101
si
102
si
103
si