Serenity Operating System
1/*
2 * Copyright (c) 2020-2023, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
4 * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
5 * Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
6 *
7 * SPDX-License-Identifier: BSD-2-Clause
8 */
9
10#include "IDLParser.h"
11#include <AK/Assertions.h>
12#include <AK/LexicalPath.h>
13#include <AK/QuickSort.h>
14#include <LibCore/DeprecatedFile.h>
15#include <LibCore/File.h>
16
17[[noreturn]] static void report_parsing_error(StringView message, StringView filename, StringView input, size_t offset)
18{
19 // FIXME: Spaghetti code ahead.
20
21 size_t lineno = 1;
22 size_t colno = 1;
23 size_t start_line = 0;
24 size_t line_length = 0;
25 for (size_t index = 0; index < input.length(); ++index) {
26 if (offset == index)
27 colno = index - start_line + 1;
28
29 if (input[index] == '\n') {
30 if (index >= offset)
31 break;
32
33 start_line = index + 1;
34 line_length = 0;
35 ++lineno;
36 } else {
37 ++line_length;
38 }
39 }
40
41 StringBuilder error_message;
42 error_message.appendff("{}\n", input.substring_view(start_line, line_length));
43 for (size_t i = 0; i < colno - 1; ++i)
44 error_message.append(' ');
45 error_message.append("\033[1;31m^\n"sv);
46 error_message.appendff("{}:{}: error: {}\033[0m\n", filename, lineno, message);
47
48 warnln("{}", error_message.string_view());
49 exit(EXIT_FAILURE);
50}
51
52static DeprecatedString convert_enumeration_value_to_cpp_enum_member(DeprecatedString const& value, HashTable<DeprecatedString>& names_already_seen)
53{
54 StringBuilder builder;
55 GenericLexer lexer { value };
56
57 while (!lexer.is_eof()) {
58 lexer.ignore_while([](auto c) { return is_ascii_space(c) || c == '-' || c == '_'; });
59 auto word = lexer.consume_while([](auto c) { return is_ascii_alphanumeric(c); });
60 if (!word.is_empty()) {
61 builder.append(word.to_titlecase_string());
62 } else {
63 auto non_alnum_string = lexer.consume_while([](auto c) { return !is_ascii_alphanumeric(c); });
64 if (!non_alnum_string.is_empty())
65 builder.append('_');
66 }
67 }
68
69 if (builder.is_empty())
70 builder.append("Empty"sv);
71
72 while (names_already_seen.contains(builder.string_view()))
73 builder.append('_');
74
75 names_already_seen.set(builder.string_view());
76 return builder.to_deprecated_string();
77}
78
79namespace IDL {
80
81void Parser::assert_specific(char ch)
82{
83 if (!lexer.consume_specific(ch))
84 report_parsing_error(DeprecatedString::formatted("expected '{}'", ch), filename, input, lexer.tell());
85}
86
87void Parser::consume_whitespace()
88{
89 bool consumed = true;
90 while (consumed) {
91 consumed = lexer.consume_while(is_ascii_space).length() > 0;
92
93 if (lexer.consume_specific("//")) {
94 lexer.consume_until('\n');
95 lexer.ignore();
96 consumed = true;
97 }
98 }
99}
100
101void Parser::assert_string(StringView expected)
102{
103 if (!lexer.consume_specific(expected))
104 report_parsing_error(DeprecatedString::formatted("expected '{}'", expected), filename, input, lexer.tell());
105}
106
107HashMap<DeprecatedString, DeprecatedString> Parser::parse_extended_attributes()
108{
109 HashMap<DeprecatedString, DeprecatedString> extended_attributes;
110 for (;;) {
111 consume_whitespace();
112 if (lexer.consume_specific(']'))
113 break;
114 auto name = lexer.consume_until([](auto ch) { return ch == ']' || ch == '=' || ch == ','; });
115 if (lexer.consume_specific('=')) {
116 bool did_open_paren = false;
117 auto value = lexer.consume_until(
118 [&did_open_paren](auto ch) {
119 if (ch == '(') {
120 did_open_paren = true;
121 return false;
122 }
123 if (did_open_paren)
124 return ch == ')';
125 return ch == ']' || ch == ',';
126 });
127 extended_attributes.set(name, value);
128 } else {
129 extended_attributes.set(name, {});
130 }
131 lexer.consume_specific(',');
132 }
133 consume_whitespace();
134 return extended_attributes;
135}
136
137static HashTable<DeprecatedString> import_stack;
138Optional<Interface&> Parser::resolve_import(auto path)
139{
140 auto include_path = LexicalPath::join(import_base_path, path).string();
141 if (!Core::DeprecatedFile::exists(include_path))
142 report_parsing_error(DeprecatedString::formatted("{}: No such file or directory", include_path), filename, input, lexer.tell());
143
144 auto real_path = Core::DeprecatedFile::real_path_for(include_path);
145 if (top_level_resolved_imports().contains(real_path))
146 return *top_level_resolved_imports().find(real_path)->value;
147
148 if (import_stack.contains(real_path))
149 report_parsing_error(DeprecatedString::formatted("Circular import detected: {}", include_path), filename, input, lexer.tell());
150 import_stack.set(real_path);
151
152 auto file_or_error = Core::File::open(real_path, Core::File::OpenMode::Read);
153 if (file_or_error.is_error())
154 report_parsing_error(DeprecatedString::formatted("Failed to open {}: {}", real_path, file_or_error.error()), filename, input, lexer.tell());
155
156 auto data_or_error = file_or_error.value()->read_until_eof();
157 if (data_or_error.is_error())
158 report_parsing_error(DeprecatedString::formatted("Failed to read {}: {}", real_path, data_or_error.error()), filename, input, lexer.tell());
159 auto& result = Parser(this, real_path, data_or_error.value(), import_base_path).parse();
160 import_stack.remove(real_path);
161
162 top_level_resolved_imports().set(real_path, &result);
163 return result;
164}
165
166NonnullRefPtr<Type const> Parser::parse_type()
167{
168 if (lexer.consume_specific('(')) {
169 Vector<NonnullRefPtr<Type const>> union_member_types;
170 union_member_types.append(parse_type());
171 consume_whitespace();
172 assert_string("or"sv);
173 consume_whitespace();
174 union_member_types.append(parse_type());
175 consume_whitespace();
176
177 while (lexer.consume_specific("or")) {
178 consume_whitespace();
179 union_member_types.append(parse_type());
180 consume_whitespace();
181 }
182
183 assert_specific(')');
184
185 bool nullable = lexer.consume_specific('?');
186
187 return adopt_ref(*new UnionType("", nullable, move(union_member_types)));
188 }
189
190 bool unsigned_ = lexer.consume_specific("unsigned");
191 if (unsigned_)
192 consume_whitespace();
193
194 // FIXME: Actually treat "unrestricted" and normal floats/doubles differently.
195 if (lexer.consume_specific("unrestricted"))
196 consume_whitespace();
197
198 auto name = lexer.consume_until([](auto ch) { return !is_ascii_alphanumeric(ch) && ch != '_'; });
199
200 if (name.equals_ignoring_ascii_case("long"sv)) {
201 consume_whitespace();
202 if (lexer.consume_specific("long"sv))
203 name = "long long"sv;
204 }
205
206 Vector<NonnullRefPtr<Type const>> parameters;
207 bool is_parameterized_type = false;
208 if (lexer.consume_specific('<')) {
209 is_parameterized_type = true;
210 parameters.append(parse_type());
211 while (lexer.consume_specific(',')) {
212 consume_whitespace();
213 parameters.append(parse_type());
214 }
215 lexer.consume_specific('>');
216 }
217 auto nullable = lexer.consume_specific('?');
218 StringBuilder builder;
219 if (unsigned_)
220 builder.append("unsigned "sv);
221 builder.append(name);
222
223 if (is_parameterized_type)
224 return adopt_ref(*new ParameterizedType(builder.to_deprecated_string(), nullable, move(parameters)));
225
226 return adopt_ref(*new Type(builder.to_deprecated_string(), nullable));
227}
228
229void Parser::parse_attribute(HashMap<DeprecatedString, DeprecatedString>& extended_attributes, Interface& interface)
230{
231 bool inherit = lexer.consume_specific("inherit");
232 if (inherit)
233 consume_whitespace();
234
235 bool readonly = lexer.consume_specific("readonly");
236 if (readonly)
237 consume_whitespace();
238
239 if (lexer.consume_specific("attribute"))
240 consume_whitespace();
241
242 auto type = parse_type();
243 consume_whitespace();
244 auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == ';'; });
245 consume_whitespace();
246
247 assert_specific(';');
248
249 auto name_as_string = name.to_deprecated_string();
250 auto getter_callback_name = DeprecatedString::formatted("{}_getter", name_as_string.to_snakecase());
251 auto setter_callback_name = DeprecatedString::formatted("{}_setter", name_as_string.to_snakecase());
252
253 Attribute attribute {
254 inherit,
255 readonly,
256 move(type),
257 move(name_as_string),
258 move(extended_attributes),
259 move(getter_callback_name),
260 move(setter_callback_name),
261 };
262 interface.attributes.append(move(attribute));
263}
264
265void Parser::parse_constant(Interface& interface)
266{
267 lexer.consume_specific("const");
268 consume_whitespace();
269
270 auto type = parse_type();
271 consume_whitespace();
272 auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == '='; });
273 consume_whitespace();
274 lexer.consume_specific('=');
275 consume_whitespace();
276 auto value = lexer.consume_while([](auto ch) { return !is_ascii_space(ch) && ch != ';'; });
277 consume_whitespace();
278 assert_specific(';');
279
280 Constant constant {
281 move(type),
282 move(name),
283 move(value),
284 };
285 interface.constants.append(move(constant));
286}
287
288Vector<Parameter> Parser::parse_parameters()
289{
290 consume_whitespace();
291 Vector<Parameter> parameters;
292 for (;;) {
293 if (lexer.next_is(')'))
294 break;
295 HashMap<DeprecatedString, DeprecatedString> extended_attributes;
296 if (lexer.consume_specific('['))
297 extended_attributes = parse_extended_attributes();
298 bool optional = lexer.consume_specific("optional");
299 if (optional)
300 consume_whitespace();
301 if (lexer.consume_specific('[')) {
302 // Not explicitly forbidden by the grammar but unlikely to happen in practice - if it does,
303 // we'll have to teach the parser how to merge two sets of extended attributes.
304 VERIFY(extended_attributes.is_empty());
305 extended_attributes = parse_extended_attributes();
306 }
307 auto type = parse_type();
308 bool variadic = lexer.consume_specific("..."sv);
309 consume_whitespace();
310 auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == ',' || ch == ')' || ch == '='; });
311 Parameter parameter = { move(type), move(name), optional, {}, extended_attributes, variadic };
312 consume_whitespace();
313 if (variadic) {
314 // Variadic parameters must be last and do not have default values.
315 parameters.append(move(parameter));
316 break;
317 }
318 if (lexer.next_is(')')) {
319 parameters.append(move(parameter));
320 break;
321 }
322 if (lexer.next_is('=') && optional) {
323 assert_specific('=');
324 consume_whitespace();
325 auto default_value = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == ',' || ch == ')'; });
326 parameter.optional_default_value = default_value;
327 }
328 parameters.append(move(parameter));
329 if (lexer.next_is(')'))
330 break;
331 assert_specific(',');
332 consume_whitespace();
333 }
334 return parameters;
335}
336
337Function Parser::parse_function(HashMap<DeprecatedString, DeprecatedString>& extended_attributes, Interface& interface, IsSpecialOperation is_special_operation)
338{
339 bool static_ = false;
340 if (lexer.consume_specific("static")) {
341 static_ = true;
342 consume_whitespace();
343 }
344
345 auto return_type = parse_type();
346 consume_whitespace();
347 auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == '('; });
348 consume_whitespace();
349 assert_specific('(');
350 auto parameters = parse_parameters();
351 assert_specific(')');
352 consume_whitespace();
353 assert_specific(';');
354
355 Function function { move(return_type), name, move(parameters), move(extended_attributes), {}, false };
356
357 // "Defining a special operation with an identifier is equivalent to separating the special operation out into its own declaration without an identifier."
358 if (is_special_operation == IsSpecialOperation::No || (is_special_operation == IsSpecialOperation::Yes && !name.is_empty())) {
359 if (!static_)
360 interface.functions.append(function);
361 else
362 interface.static_functions.append(function);
363 }
364
365 return function;
366}
367
368void Parser::parse_constructor(Interface& interface)
369{
370 assert_string("constructor"sv);
371 consume_whitespace();
372 assert_specific('(');
373 auto parameters = parse_parameters();
374 assert_specific(')');
375 consume_whitespace();
376 assert_specific(';');
377
378 interface.constructors.append(Constructor { interface.name, move(parameters) });
379}
380
381void Parser::parse_stringifier(HashMap<DeprecatedString, DeprecatedString>& extended_attributes, Interface& interface)
382{
383 assert_string("stringifier"sv);
384 consume_whitespace();
385 interface.has_stringifier = true;
386 if (lexer.next_is("attribute"sv) || lexer.next_is("inherit"sv) || lexer.next_is("readonly"sv)) {
387 parse_attribute(extended_attributes, interface);
388 interface.stringifier_attribute = interface.attributes.last().name;
389 } else {
390 assert_specific(';');
391 }
392}
393
394void Parser::parse_iterable(Interface& interface)
395{
396 assert_string("iterable"sv);
397 assert_specific('<');
398 auto first_type = parse_type();
399 if (lexer.next_is(',')) {
400 if (interface.supports_indexed_properties())
401 report_parsing_error("Interfaces with a pair iterator must not supported indexed properties."sv, filename, input, lexer.tell());
402
403 assert_specific(',');
404 consume_whitespace();
405 auto second_type = parse_type();
406 interface.pair_iterator_types = Tuple { move(first_type), move(second_type) };
407 } else {
408 if (!interface.supports_indexed_properties())
409 report_parsing_error("Interfaces with a value iterator must supported indexed properties."sv, filename, input, lexer.tell());
410
411 interface.value_iterator_type = move(first_type);
412 }
413 assert_specific('>');
414 assert_specific(';');
415}
416
417void Parser::parse_getter(HashMap<DeprecatedString, DeprecatedString>& extended_attributes, Interface& interface)
418{
419 assert_string("getter"sv);
420 consume_whitespace();
421 auto function = parse_function(extended_attributes, interface, IsSpecialOperation::Yes);
422
423 if (function.parameters.size() != 1)
424 report_parsing_error(DeprecatedString::formatted("Named/indexed property getters must have only 1 parameter, got {} parameters.", function.parameters.size()), filename, input, lexer.tell());
425
426 auto& identifier = function.parameters.first();
427
428 if (identifier.type->is_nullable())
429 report_parsing_error("identifier's type must not be nullable."sv, filename, input, lexer.tell());
430
431 if (identifier.optional)
432 report_parsing_error("identifier must not be optional."sv, filename, input, lexer.tell());
433
434 // FIXME: Disallow variadic functions once they're supported.
435
436 if (identifier.type->name() == "DOMString") {
437 if (interface.named_property_getter.has_value())
438 report_parsing_error("An interface can only have one named property getter."sv, filename, input, lexer.tell());
439
440 interface.named_property_getter = move(function);
441 } else if (identifier.type->name() == "unsigned long") {
442 if (interface.indexed_property_getter.has_value())
443 report_parsing_error("An interface can only have one indexed property getter."sv, filename, input, lexer.tell());
444
445 interface.indexed_property_getter = move(function);
446 } else {
447 report_parsing_error(DeprecatedString::formatted("Named/indexed property getter's identifier's type must be either 'DOMString' or 'unsigned long', got '{}'.", identifier.type->name()), filename, input, lexer.tell());
448 }
449}
450
451void Parser::parse_setter(HashMap<DeprecatedString, DeprecatedString>& extended_attributes, Interface& interface)
452{
453 assert_string("setter"sv);
454 consume_whitespace();
455 auto function = parse_function(extended_attributes, interface, IsSpecialOperation::Yes);
456
457 if (function.parameters.size() != 2)
458 report_parsing_error(DeprecatedString::formatted("Named/indexed property setters must have only 2 parameters, got {} parameter(s).", function.parameters.size()), filename, input, lexer.tell());
459
460 auto& identifier = function.parameters.first();
461
462 if (identifier.type->is_nullable())
463 report_parsing_error("identifier's type must not be nullable."sv, filename, input, lexer.tell());
464
465 if (identifier.optional)
466 report_parsing_error("identifier must not be optional."sv, filename, input, lexer.tell());
467
468 // FIXME: Disallow variadic functions once they're supported.
469
470 if (identifier.type->name() == "DOMString") {
471 if (interface.named_property_setter.has_value())
472 report_parsing_error("An interface can only have one named property setter."sv, filename, input, lexer.tell());
473
474 if (!interface.named_property_getter.has_value())
475 report_parsing_error("A named property setter must be accompanied by a named property getter."sv, filename, input, lexer.tell());
476
477 interface.named_property_setter = move(function);
478 } else if (identifier.type->name() == "unsigned long") {
479 if (interface.indexed_property_setter.has_value())
480 report_parsing_error("An interface can only have one indexed property setter."sv, filename, input, lexer.tell());
481
482 if (!interface.indexed_property_getter.has_value())
483 report_parsing_error("An indexed property setter must be accompanied by an indexed property getter."sv, filename, input, lexer.tell());
484
485 interface.indexed_property_setter = move(function);
486 } else {
487 report_parsing_error(DeprecatedString::formatted("Named/indexed property setter's identifier's type must be either 'DOMString' or 'unsigned long', got '{}'.", identifier.type->name()), filename, input, lexer.tell());
488 }
489}
490
491void Parser::parse_deleter(HashMap<DeprecatedString, DeprecatedString>& extended_attributes, Interface& interface)
492{
493 assert_string("deleter"sv);
494 consume_whitespace();
495 auto function = parse_function(extended_attributes, interface, IsSpecialOperation::Yes);
496
497 if (function.parameters.size() != 1)
498 report_parsing_error(DeprecatedString::formatted("Named property deleter must have only 1 parameter, got {} parameters.", function.parameters.size()), filename, input, lexer.tell());
499
500 auto& identifier = function.parameters.first();
501
502 if (identifier.type->is_nullable())
503 report_parsing_error("identifier's type must not be nullable."sv, filename, input, lexer.tell());
504
505 if (identifier.optional)
506 report_parsing_error("identifier must not be optional."sv, filename, input, lexer.tell());
507
508 // FIXME: Disallow variadic functions once they're supported.
509
510 if (identifier.type->name() == "DOMString") {
511 if (interface.named_property_deleter.has_value())
512 report_parsing_error("An interface can only have one named property deleter."sv, filename, input, lexer.tell());
513
514 if (!interface.named_property_getter.has_value())
515 report_parsing_error("A named property deleter must be accompanied by a named property getter."sv, filename, input, lexer.tell());
516
517 interface.named_property_deleter = move(function);
518 } else {
519 report_parsing_error(DeprecatedString::formatted("Named property deleter's identifier's type must be 'DOMString', got '{}'.", identifier.type->name()), filename, input, lexer.tell());
520 }
521}
522
523void Parser::parse_interface(Interface& interface)
524{
525 consume_whitespace();
526 interface.name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
527 consume_whitespace();
528 if (lexer.consume_specific(':')) {
529 consume_whitespace();
530 interface.parent_name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
531 consume_whitespace();
532 }
533 assert_specific('{');
534
535 for (;;) {
536 HashMap<DeprecatedString, DeprecatedString> extended_attributes;
537
538 consume_whitespace();
539
540 if (lexer.consume_specific('}')) {
541 consume_whitespace();
542 assert_specific(';');
543 break;
544 }
545
546 if (lexer.consume_specific('[')) {
547 extended_attributes = parse_extended_attributes();
548 if (!interface.has_unscopable_member && extended_attributes.contains("Unscopable"))
549 interface.has_unscopable_member = true;
550 }
551
552 if (lexer.next_is("constructor")) {
553 parse_constructor(interface);
554 continue;
555 }
556
557 if (lexer.next_is("const")) {
558 parse_constant(interface);
559 continue;
560 }
561
562 if (lexer.next_is("stringifier")) {
563 parse_stringifier(extended_attributes, interface);
564 continue;
565 }
566
567 if (lexer.next_is("iterable")) {
568 parse_iterable(interface);
569 continue;
570 }
571
572 if (lexer.next_is("inherit") || lexer.next_is("readonly") || lexer.next_is("attribute")) {
573 parse_attribute(extended_attributes, interface);
574 continue;
575 }
576
577 if (lexer.next_is("getter")) {
578 parse_getter(extended_attributes, interface);
579 continue;
580 }
581
582 if (lexer.next_is("setter")) {
583 parse_setter(extended_attributes, interface);
584 continue;
585 }
586
587 if (lexer.next_is("deleter")) {
588 parse_deleter(extended_attributes, interface);
589 continue;
590 }
591
592 parse_function(extended_attributes, interface);
593 }
594
595 interface.constructor_class = DeprecatedString::formatted("{}Constructor", interface.name);
596 interface.prototype_class = DeprecatedString::formatted("{}Prototype", interface.name);
597 interface.prototype_base_class = DeprecatedString::formatted("{}Prototype", interface.parent_name.is_empty() ? "Object" : interface.parent_name);
598 interface.global_mixin_class = DeprecatedString::formatted("{}GlobalMixin", interface.name);
599 consume_whitespace();
600}
601
602void Parser::parse_enumeration(Interface& interface)
603{
604 assert_string("enum"sv);
605 consume_whitespace();
606
607 Enumeration enumeration {};
608
609 auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
610 consume_whitespace();
611
612 assert_specific('{');
613
614 bool first = true;
615 for (; !lexer.is_eof();) {
616 consume_whitespace();
617 if (lexer.next_is('}'))
618 break;
619 if (!first) {
620 assert_specific(',');
621 consume_whitespace();
622 }
623
624 assert_specific('"');
625 auto string = lexer.consume_until('"');
626 assert_specific('"');
627 consume_whitespace();
628
629 if (enumeration.values.contains(string))
630 report_parsing_error(DeprecatedString::formatted("Enumeration {} contains duplicate member '{}'", name, string), filename, input, lexer.tell());
631 else
632 enumeration.values.set(string);
633
634 if (first)
635 enumeration.first_member = move(string);
636
637 first = false;
638 }
639
640 consume_whitespace();
641 assert_specific('}');
642 assert_specific(';');
643
644 HashTable<DeprecatedString> names_already_seen;
645 for (auto& entry : enumeration.values)
646 enumeration.translated_cpp_names.set(entry, convert_enumeration_value_to_cpp_enum_member(entry, names_already_seen));
647
648 interface.enumerations.set(name, move(enumeration));
649 consume_whitespace();
650}
651
652void Parser::parse_typedef(Interface& interface)
653{
654 assert_string("typedef"sv);
655 consume_whitespace();
656
657 HashMap<DeprecatedString, DeprecatedString> extended_attributes;
658 if (lexer.consume_specific('['))
659 extended_attributes = parse_extended_attributes();
660
661 auto type = parse_type();
662 consume_whitespace();
663
664 auto name = lexer.consume_until(';');
665 assert_specific(';');
666
667 interface.typedefs.set(name, Typedef { move(extended_attributes), move(type) });
668 consume_whitespace();
669}
670
671void Parser::parse_dictionary(Interface& interface)
672{
673 assert_string("dictionary"sv);
674 consume_whitespace();
675
676 Dictionary dictionary {};
677
678 auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
679 consume_whitespace();
680
681 if (lexer.consume_specific(':')) {
682 consume_whitespace();
683 dictionary.parent_name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
684 consume_whitespace();
685 }
686 assert_specific('{');
687
688 for (;;) {
689 consume_whitespace();
690
691 if (lexer.consume_specific('}')) {
692 consume_whitespace();
693 assert_specific(';');
694 break;
695 }
696
697 bool required = false;
698 HashMap<DeprecatedString, DeprecatedString> extended_attributes;
699
700 if (lexer.consume_specific("required")) {
701 required = true;
702 consume_whitespace();
703 if (lexer.consume_specific('['))
704 extended_attributes = parse_extended_attributes();
705 }
706
707 auto type = parse_type();
708 consume_whitespace();
709
710 auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == ';'; });
711 consume_whitespace();
712
713 Optional<StringView> default_value;
714
715 if (lexer.consume_specific('=')) {
716 VERIFY(!required);
717 consume_whitespace();
718 default_value = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == ';'; });
719 consume_whitespace();
720 }
721
722 assert_specific(';');
723
724 DictionaryMember member {
725 required,
726 move(type),
727 name,
728 move(extended_attributes),
729 Optional<DeprecatedString>(move(default_value)),
730 };
731 dictionary.members.append(move(member));
732 }
733
734 // dictionary members need to be evaluated in lexicographical order
735 quick_sort(dictionary.members, [&](auto& one, auto& two) {
736 return one.name < two.name;
737 });
738
739 interface.dictionaries.set(name, move(dictionary));
740 consume_whitespace();
741}
742
743void Parser::parse_interface_mixin(Interface& interface)
744{
745 auto mixin_interface_ptr = make<Interface>();
746 auto& mixin_interface = *mixin_interface_ptr;
747 VERIFY(top_level_interfaces().set(move(mixin_interface_ptr)) == AK::HashSetResult::InsertedNewEntry);
748 mixin_interface.module_own_path = interface.module_own_path;
749 mixin_interface.is_mixin = true;
750
751 assert_string("interface"sv);
752 consume_whitespace();
753 assert_string("mixin"sv);
754 auto offset = lexer.tell();
755
756 parse_interface(mixin_interface);
757 if (!mixin_interface.parent_name.is_empty())
758 report_parsing_error("Mixin interfaces are not allowed to have inherited parents"sv, filename, input, offset);
759
760 auto name = mixin_interface.name;
761 interface.mixins.set(move(name), &mixin_interface);
762}
763
764void Parser::parse_callback_function(HashMap<DeprecatedString, DeprecatedString>& extended_attributes, Interface& interface)
765{
766 assert_string("callback"sv);
767 consume_whitespace();
768
769 auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
770 consume_whitespace();
771
772 assert_specific('=');
773 consume_whitespace();
774
775 auto return_type = parse_type();
776 consume_whitespace();
777 assert_specific('(');
778 auto parameters = parse_parameters();
779 assert_specific(')');
780 consume_whitespace();
781 assert_specific(';');
782
783 interface.callback_functions.set(name, CallbackFunction { move(return_type), move(parameters), extended_attributes.contains("LegacyTreatNonObjectAsNull") });
784 consume_whitespace();
785}
786
787void Parser::parse_non_interface_entities(bool allow_interface, Interface& interface)
788{
789 consume_whitespace();
790
791 while (!lexer.is_eof()) {
792 HashMap<DeprecatedString, DeprecatedString> extended_attributes;
793 if (lexer.consume_specific('['))
794 extended_attributes = parse_extended_attributes();
795 if (lexer.next_is("dictionary")) {
796 parse_dictionary(interface);
797 } else if (lexer.next_is("enum")) {
798 parse_enumeration(interface);
799 } else if (lexer.next_is("typedef")) {
800 parse_typedef(interface);
801 } else if (lexer.next_is("interface mixin")) {
802 parse_interface_mixin(interface);
803 } else if (lexer.next_is("callback")) {
804 parse_callback_function(extended_attributes, interface);
805 } else if ((allow_interface && !lexer.next_is("interface")) || !allow_interface) {
806 auto current_offset = lexer.tell();
807 auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
808 consume_whitespace();
809 if (lexer.consume_specific("includes")) {
810 consume_whitespace();
811 auto mixin_name = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == ';'; });
812 interface.included_mixins.ensure(name).set(mixin_name);
813 consume_whitespace();
814 assert_specific(';');
815 consume_whitespace();
816 } else {
817 report_parsing_error("expected 'enum' or 'dictionary'"sv, filename, input, current_offset);
818 }
819 } else {
820 interface.extended_attributes = move(extended_attributes);
821 break;
822 }
823 }
824
825 consume_whitespace();
826}
827
828static void resolve_union_typedefs(Interface& interface, UnionType& union_);
829
830static void resolve_typedef(Interface& interface, NonnullRefPtr<Type const>& type, HashMap<DeprecatedString, DeprecatedString>* extended_attributes = {})
831{
832 if (is<ParameterizedType>(*type)) {
833 auto& parameterized_type = const_cast<Type&>(*type).as_parameterized();
834 auto& parameters = static_cast<Vector<NonnullRefPtr<Type const>>&>(parameterized_type.parameters());
835 for (auto& parameter : parameters)
836 resolve_typedef(interface, parameter);
837 return;
838 }
839
840 // Resolve anonymous union types until we get named types that can be resolved in the next step.
841 if (is<UnionType>(*type) && type->name().is_empty()) {
842 resolve_union_typedefs(interface, const_cast<Type&>(*type).as_union());
843 return;
844 }
845
846 auto it = interface.typedefs.find(type->name());
847 if (it == interface.typedefs.end())
848 return;
849 bool nullable = type->is_nullable();
850 type = it->value.type;
851 const_cast<Type&>(*type).set_nullable(nullable);
852 if (extended_attributes) {
853 for (auto& attribute : it->value.extended_attributes)
854 extended_attributes->set(attribute.key, attribute.value);
855 }
856
857 // Recursively resolve typedefs in unions after we resolved the type itself - e.g. for this:
858 // typedef (A or B) Union1;
859 // typedef (C or D) Union2;
860 // typedef (Union1 or Union2) NestedUnion;
861 // We run:
862 // - resolve_typedef(NestedUnion) -> NestedUnion gets replaced by UnionType(Union1, Union2)
863 // - resolve_typedef(Union1) -> Union1 gets replaced by UnionType(A, B)
864 // - resolve_typedef(Union2) -> Union2 gets replaced by UnionType(C, D)
865 // So whatever referenced NestedUnion ends up with the following resolved union:
866 // UnionType(UnionType(A, B), UnionType(C, D))
867 // Note that flattening unions is handled separately as per the spec.
868 if (is<UnionType>(*type))
869 resolve_union_typedefs(interface, const_cast<Type&>(*type).as_union());
870}
871
872static void resolve_union_typedefs(Interface& interface, UnionType& union_)
873{
874 auto& member_types = static_cast<Vector<NonnullRefPtr<Type const>>&>(union_.member_types());
875 for (auto& member_type : member_types)
876 resolve_typedef(interface, member_type);
877}
878
879static void resolve_parameters_typedefs(Interface& interface, Vector<Parameter>& parameters)
880{
881 for (auto& parameter : parameters)
882 resolve_typedef(interface, parameter.type, ¶meter.extended_attributes);
883}
884
885template<typename FunctionType>
886void resolve_function_typedefs(Interface& interface, FunctionType& function)
887{
888 resolve_typedef(interface, function.return_type);
889 resolve_parameters_typedefs(interface, function.parameters);
890}
891
892Interface& Parser::parse()
893{
894 auto this_module = Core::DeprecatedFile::real_path_for(filename);
895
896 auto interface_ptr = make<Interface>();
897 auto& interface = *interface_ptr;
898 VERIFY(top_level_interfaces().set(move(interface_ptr)) == AK::HashSetResult::InsertedNewEntry);
899 interface.module_own_path = this_module;
900 top_level_resolved_imports().set(this_module, &interface);
901
902 Vector<Interface&> imports;
903 HashTable<DeprecatedString> required_imported_paths;
904 while (lexer.consume_specific("#import")) {
905 consume_whitespace();
906 assert_specific('<');
907 auto path = lexer.consume_until('>');
908 lexer.ignore();
909 auto maybe_interface = resolve_import(path);
910 if (maybe_interface.has_value()) {
911 for (auto& entry : maybe_interface.value().required_imported_paths)
912 required_imported_paths.set(entry);
913 imports.append(maybe_interface.release_value());
914 }
915 consume_whitespace();
916 }
917 interface.required_imported_paths = required_imported_paths;
918
919 parse_non_interface_entities(true, interface);
920
921 if (lexer.consume_specific("interface"))
922 parse_interface(interface);
923
924 parse_non_interface_entities(false, interface);
925
926 for (auto& import : imports) {
927 // FIXME: Instead of copying every imported entity into the current interface, query imports directly
928 for (auto& dictionary : import.dictionaries)
929 interface.dictionaries.set(dictionary.key, dictionary.value);
930
931 for (auto& enumeration : import.enumerations) {
932 auto enumeration_copy = enumeration.value;
933 enumeration_copy.is_original_definition = false;
934 interface.enumerations.set(enumeration.key, move(enumeration_copy));
935 }
936
937 for (auto& typedef_ : import.typedefs)
938 interface.typedefs.set(typedef_.key, typedef_.value);
939
940 for (auto& mixin : import.mixins) {
941 if (auto it = interface.mixins.find(mixin.key); it != interface.mixins.end() && it->value != mixin.value)
942 report_parsing_error(DeprecatedString::formatted("Mixin '{}' was already defined in {}", mixin.key, mixin.value->module_own_path), filename, input, lexer.tell());
943 interface.mixins.set(mixin.key, mixin.value);
944 }
945
946 for (auto& callback_function : import.callback_functions)
947 interface.callback_functions.set(callback_function.key, callback_function.value);
948 }
949
950 // Resolve mixins
951 if (auto it = interface.included_mixins.find(interface.name); it != interface.included_mixins.end()) {
952 for (auto& entry : it->value) {
953 auto mixin_it = interface.mixins.find(entry);
954 if (mixin_it == interface.mixins.end())
955 report_parsing_error(DeprecatedString::formatted("Mixin '{}' was never defined", entry), filename, input, lexer.tell());
956
957 auto& mixin = mixin_it->value;
958 interface.attributes.extend(mixin->attributes);
959 interface.constants.extend(mixin->constants);
960 interface.functions.extend(mixin->functions);
961 interface.static_functions.extend(mixin->static_functions);
962 if (interface.has_stringifier && mixin->has_stringifier)
963 report_parsing_error(DeprecatedString::formatted("Both interface '{}' and mixin '{}' have defined stringifier attributes", interface.name, mixin->name), filename, input, lexer.tell());
964
965 if (mixin->has_stringifier) {
966 interface.stringifier_attribute = mixin->stringifier_attribute;
967 interface.has_stringifier = true;
968 }
969
970 if (mixin->has_unscopable_member)
971 interface.has_unscopable_member = true;
972 }
973 }
974
975 // Resolve typedefs
976 for (auto& attribute : interface.attributes)
977 resolve_typedef(interface, attribute.type, &attribute.extended_attributes);
978 for (auto& constant : interface.constants)
979 resolve_typedef(interface, constant.type);
980 for (auto& constructor : interface.constructors)
981 resolve_parameters_typedefs(interface, constructor.parameters);
982 for (auto& function : interface.functions)
983 resolve_function_typedefs(interface, function);
984 for (auto& static_function : interface.static_functions)
985 resolve_function_typedefs(interface, static_function);
986 if (interface.value_iterator_type.has_value())
987 resolve_typedef(interface, *interface.value_iterator_type);
988 if (interface.pair_iterator_types.has_value()) {
989 resolve_typedef(interface, interface.pair_iterator_types->get<0>());
990 resolve_typedef(interface, interface.pair_iterator_types->get<1>());
991 }
992 if (interface.named_property_getter.has_value())
993 resolve_function_typedefs(interface, *interface.named_property_getter);
994 if (interface.named_property_setter.has_value())
995 resolve_function_typedefs(interface, *interface.named_property_setter);
996 if (interface.indexed_property_getter.has_value())
997 resolve_function_typedefs(interface, *interface.indexed_property_getter);
998 if (interface.indexed_property_setter.has_value())
999 resolve_function_typedefs(interface, *interface.indexed_property_setter);
1000 if (interface.named_property_deleter.has_value())
1001 resolve_function_typedefs(interface, *interface.named_property_deleter);
1002 if (interface.named_property_getter.has_value())
1003 resolve_function_typedefs(interface, *interface.named_property_getter);
1004 for (auto& dictionary : interface.dictionaries) {
1005 for (auto& dictionary_member : dictionary.value.members)
1006 resolve_typedef(interface, dictionary_member.type, &dictionary_member.extended_attributes);
1007 }
1008 for (auto& callback_function : interface.callback_functions)
1009 resolve_function_typedefs(interface, callback_function.value);
1010
1011 // Create overload sets
1012 for (auto& function : interface.functions) {
1013 auto& overload_set = interface.overload_sets.ensure(function.name);
1014 function.overload_index = overload_set.size();
1015 overload_set.append(function);
1016 }
1017 for (auto& overload_set : interface.overload_sets) {
1018 if (overload_set.value.size() == 1)
1019 continue;
1020 for (auto& overloaded_function : overload_set.value)
1021 overloaded_function.is_overloaded = true;
1022 }
1023 for (auto& function : interface.static_functions) {
1024 auto& overload_set = interface.static_overload_sets.ensure(function.name);
1025 function.overload_index = overload_set.size();
1026 overload_set.append(function);
1027 }
1028 for (auto& overload_set : interface.static_overload_sets) {
1029 if (overload_set.value.size() == 1)
1030 continue;
1031 for (auto& overloaded_function : overload_set.value)
1032 overloaded_function.is_overloaded = true;
1033 }
1034 // FIXME: Add support for overloading constructors
1035
1036 if (interface.will_generate_code())
1037 interface.required_imported_paths.set(this_module);
1038 interface.imported_modules = move(imports);
1039
1040 if (top_level_parser() == this)
1041 VERIFY(import_stack.is_empty());
1042
1043 return interface;
1044}
1045
1046Parser::Parser(DeprecatedString filename, StringView contents, DeprecatedString import_base_path)
1047 : import_base_path(move(import_base_path))
1048 , filename(move(filename))
1049 , input(contents)
1050 , lexer(input)
1051{
1052}
1053
1054Parser::Parser(Parser* parent, DeprecatedString filename, StringView contents, DeprecatedString import_base_path)
1055 : import_base_path(move(import_base_path))
1056 , filename(move(filename))
1057 , input(contents)
1058 , lexer(input)
1059 , parent(parent)
1060{
1061}
1062
1063Parser* Parser::top_level_parser()
1064{
1065 Parser* current = this;
1066 for (Parser* next = this; next; next = next->parent)
1067 current = next;
1068 return current;
1069}
1070
1071HashMap<DeprecatedString, Interface*>& Parser::top_level_resolved_imports()
1072{
1073 return top_level_parser()->resolved_imports;
1074}
1075
1076HashTable<NonnullOwnPtr<Interface>>& Parser::top_level_interfaces()
1077{
1078 return top_level_parser()->interfaces;
1079}
1080
1081Vector<DeprecatedString> Parser::imported_files() const
1082{
1083 return const_cast<Parser*>(this)->top_level_resolved_imports().keys();
1084}
1085}