Skip to content
← Back

src/syntax/parsers/definitions/property.ghul

1
namespace Syntax.Parsers.Definitions is
2
use IO.Std
3
4
use Source
5
6
use Logging
7
8
class PROPERTY(
9
identifier_parser: Parser[Trees.Identifiers.Identifier],
10
type_parser: Parser[Trees.TypeExpressions.TypeExpression],
11
modifier_list_parser: Parser[Trees.Modifiers.LIST],
12
body_parser: Parser[Trees.Bodies.Body]
13
): Base[Trees.Definitions.PROPERTY] is
14
super()
15
16
parse(context: CONTEXT) -> Trees.Definitions.PROPERTY is
17
let fail mut = false
18
let progress mut = false
19
20
try
21
if context.in_classy then
22
context.in_member = true
23
context.member_indent = context.location.start_column
24
else
25
context.in_global_function = true
26
context.global_indent = context.location.start_column
27
fi
28
29
let start = context.location
30
let name = identifier_parser.parse(context)
31
let type_expression: Trees.TypeExpressions.TypeExpression mut
32
33
if context.current.token == Lexical.TOKEN.COLON then
34
context.next_token()
35
progress = true
36
37
if let parsed = type_parser.parse(context) then
38
type_expression = parsed
39
40
if parsed.is_poisoned then
41
fail = true
42
fi
43
else
44
type_expression = Trees.TypeExpressions.INFER(context.location)
45
fail = true
46
fi
47
else
48
type_expression = Trees.TypeExpressions.INFER(context.location)
49
fi
50
51
let modifiers = modifier_list_parser.parse(context)!
52
let read_body: Trees.Bodies.Body? mut = null
53
let assign_body: Trees.Bodies.Body? mut = null
54
let setter_argument_name: Trees.Identifiers.Identifier? mut = null
55
let expect_semicolon mut = true
56
let setter_argument_missing mut = false
57
58
do
59
if context.current.token == Lexical.TOKEN.ASSIGN then
60
if setter_argument_name? then
61
context.error(context.location, "replacing assign")
62
fi
63
64
context.next_token()
65
progress = true
66
67
setter_argument_name = identifier_parser.parse(context)
68
69
if !setter_argument_name? then
70
setter_argument_missing = true
71
fi
72
expect_semicolon = context.current.token == Lexical.TOKEN.ARROW_FAT \/ context.current.token == Lexical.TOKEN.INNATE
73
74
try
75
assign_body = body_parser.parse(context)
76
catch ue: UNWIND_TO_MEMBER_EXCEPTION
77
assign_body = Trees.Bodies.NULL(context.location)
78
fail = true
79
yrt
80
81
if context.current.token == Lexical.TOKEN.COMMA then
82
context.next_token()
83
progress = true
84
else
85
if isa Trees.Bodies.NULL(assign_body) then
86
expect_semicolon = true
87
fi
88
89
break
90
fi
91
elif
92
context.current.token == Lexical.TOKEN.IS \/
93
context.current.token == Lexical.TOKEN.ARROW_FAT \/
94
context.current.token == Lexical.TOKEN.INNATE
95
then
96
expect_semicolon = context.current.token == Lexical.TOKEN.ARROW_FAT \/ context.current.token == Lexical.TOKEN.INNATE
97
98
if read_body? then
99
context.error(context.location, "replacing read")
100
fi
101
102
try
103
read_body = body_parser.parse(context)
104
catch ue: UNWIND_TO_MEMBER_EXCEPTION
105
read_body = Trees.Bodies.NULL(context.location)
106
fail = true
107
yrt
108
109
if context.current.token == Lexical.TOKEN.COMMA then
110
context.next_token()
111
progress = true
112
else
113
if isa Trees.Bodies.NULL(read_body) then
114
expect_semicolon = true
115
else
116
progress = true
117
fi
118
119
break
120
fi
121
elif context.current_token == Lexical.TOKEN.SEMICOLON then
122
break
123
elif context.at_inferred_terminator then
124
// a line break ends a bare declaration the same way
125
// a `;` does; the terminator check below reports it
126
break
127
elif context.current_token == Lexical.TOKEN.COMMA then
128
if read_body? then
129
context.error(context.location, "replacing read")
130
elif assign_body? then
131
context.error(context.location, "empty read body must precede write body")
132
else
133
progress = true
134
context.next_token()
135
136
read_body = Syntax.Trees.Bodies.NULL(context.current.location)
137
fi
138
else
139
if !fail then
140
context.error(context.location, "syntax error in property: unexpected token {context.current_token_name}")
141
fi
142
143
fail = true
144
145
// A function header damaged so that it reads as a
146
// property, `f n: int) -> int is`, still has its body
147
// after it. Read to a body opener on this line, so the
148
// body is kept rather than its statements being read
149
// one at a time as members.
150
// A `-> T` on the way is the header's return type, and
151
// stands in for a type the property did not declare,
152
// so a `return` in the body is judged against it.
153
while
154
!context.is_end_of_file /\
155
!context.current.first_on_line /\
156
context.current.token != Lexical.TOKEN.IS /\
157
context.current.token != Lexical.TOKEN.ARROW_FAT
158
do
159
if
160
context.current.token == Lexical.TOKEN.ARROW_THIN /\
161
isa Trees.TypeExpressions.INFER(type_expression)
162
then
163
context.next_token()
164
progress = true
165
166
if let return_type = type_parser.parse(context) then
167
if !return_type.is_poisoned then
168
type_expression = return_type
169
fi
170
fi
171
else
172
context.next_token()
173
progress = true
174
fi
175
od
176
177
if
178
!context.current.first_on_line /\
179
(context.current.token == Lexical.TOKEN.IS \/ context.current.token == Lexical.TOKEN.ARROW_FAT) /\
180
!read_body?
181
then
182
continue
183
fi
184
185
break
186
fi
187
188
expect_semicolon = true
189
od
190
191
if context.in_trait /\ read_body == null then
192
read_body = Trees.Bodies.NULL(context.location)
193
fi
194
195
let result =
196
Trees.Definitions.PROPERTY(
197
start::context.location,
198
type_expression,
199
name,
200
modifiers,
201
read_body,
202
setter_argument_name,
203
assign_body
204
)
205
206
if expect_semicolon then
207
if !fail \/ context.current.token == Lexical.TOKEN.SEMICOLON then
208
context.accept_terminator()
209
fi
210
fi
211
212
if fail /\ !progress then
213
debug_always("no progress parsing property at {context.location}")
214
@IF.dotnet()
215
IoC.CONTAINER.instance.watchdog.request_restart()
216
217
context.error(context.location, "syntax error in property: unexpected token {context.current_token_name}")
218
context.next_token()
219
220
throw UNWIND_BAD_PROPERTY_EXCEPTION(null)
221
fi
222
223
result.poison(fail \/ setter_argument_missing)
224
225
return result
226
finally
227
context.in_member = false
228
yrt
229
si
230
si
231
si