IDLParser.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, 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. #include "IDLParser.h"
  10. #include <AK/LexicalPath.h>
  11. #include <AK/QuickSort.h>
  12. #include <LibCore/File.h>
  13. [[noreturn]] static void report_parsing_error(StringView message, StringView filename, StringView input, size_t offset)
  14. {
  15. // FIXME: Spaghetti code ahead.
  16. size_t lineno = 1;
  17. size_t colno = 1;
  18. size_t start_line = 0;
  19. size_t line_length = 0;
  20. for (size_t index = 0; index < input.length(); ++index) {
  21. if (offset == index)
  22. colno = index - start_line + 1;
  23. if (input[index] == '\n') {
  24. if (index >= offset)
  25. break;
  26. start_line = index + 1;
  27. line_length = 0;
  28. ++lineno;
  29. } else {
  30. ++line_length;
  31. }
  32. }
  33. StringBuilder error_message;
  34. error_message.appendff("{}\n", input.substring_view(start_line, line_length));
  35. for (size_t i = 0; i < colno - 1; ++i)
  36. error_message.append(' ');
  37. error_message.append("\033[1;31m^\n"sv);
  38. error_message.appendff("{}:{}: error: {}\033[0m\n", filename, lineno, message);
  39. warnln("{}", error_message.string_view());
  40. exit(EXIT_FAILURE);
  41. }
  42. static String convert_enumeration_value_to_cpp_enum_member(String const& value, HashTable<String>& names_already_seen)
  43. {
  44. StringBuilder builder;
  45. GenericLexer lexer { value };
  46. while (!lexer.is_eof()) {
  47. lexer.ignore_while([](auto c) { return is_ascii_space(c) || c == '-' || c == '_'; });
  48. auto word = lexer.consume_while([](auto c) { return is_ascii_alphanumeric(c); });
  49. if (!word.is_empty()) {
  50. builder.append(word.to_titlecase_string());
  51. } else {
  52. auto non_alnum_string = lexer.consume_while([](auto c) { return !is_ascii_alphanumeric(c); });
  53. if (!non_alnum_string.is_empty())
  54. builder.append('_');
  55. }
  56. }
  57. if (builder.is_empty())
  58. builder.append("Empty"sv);
  59. while (names_already_seen.contains(builder.string_view()))
  60. builder.append('_');
  61. names_already_seen.set(builder.string_view());
  62. return builder.build();
  63. }
  64. namespace IDL {
  65. HashTable<NonnullOwnPtr<Interface>> Parser::s_interfaces {};
  66. HashMap<String, Interface*> Parser::s_resolved_imports {};
  67. void Parser::assert_specific(char ch)
  68. {
  69. if (!lexer.consume_specific(ch))
  70. report_parsing_error(String::formatted("expected '{}'", ch), filename, input, lexer.tell());
  71. }
  72. void Parser::consume_whitespace()
  73. {
  74. bool consumed = true;
  75. while (consumed) {
  76. consumed = lexer.consume_while(is_ascii_space).length() > 0;
  77. if (lexer.consume_specific("//")) {
  78. lexer.consume_until('\n');
  79. lexer.ignore();
  80. consumed = true;
  81. }
  82. }
  83. }
  84. void Parser::assert_string(StringView expected)
  85. {
  86. if (!lexer.consume_specific(expected))
  87. report_parsing_error(String::formatted("expected '{}'", expected), filename, input, lexer.tell());
  88. }
  89. HashMap<String, String> Parser::parse_extended_attributes()
  90. {
  91. HashMap<String, String> extended_attributes;
  92. for (;;) {
  93. consume_whitespace();
  94. if (lexer.consume_specific(']'))
  95. break;
  96. auto name = lexer.consume_until([](auto ch) { return ch == ']' || ch == '=' || ch == ','; });
  97. if (lexer.consume_specific('=')) {
  98. auto value = lexer.consume_until([](auto ch) { return ch == ']' || ch == ','; });
  99. extended_attributes.set(name, value);
  100. } else {
  101. extended_attributes.set(name, {});
  102. }
  103. lexer.consume_specific(',');
  104. }
  105. consume_whitespace();
  106. return extended_attributes;
  107. }
  108. static HashTable<String> import_stack;
  109. Optional<Interface&> Parser::resolve_import(auto path)
  110. {
  111. auto include_path = LexicalPath::join(import_base_path, path).string();
  112. if (!Core::File::exists(include_path))
  113. report_parsing_error(String::formatted("{}: No such file or directory", include_path), filename, input, lexer.tell());
  114. auto real_path = Core::File::real_path_for(include_path);
  115. if (s_resolved_imports.contains(real_path))
  116. return *s_resolved_imports.find(real_path)->value;
  117. if (import_stack.contains(real_path))
  118. report_parsing_error(String::formatted("Circular import detected: {}", include_path), filename, input, lexer.tell());
  119. import_stack.set(real_path);
  120. auto file_or_error = Core::File::open(real_path, Core::OpenMode::ReadOnly);
  121. if (file_or_error.is_error())
  122. report_parsing_error(String::formatted("Failed to open {}: {}", real_path, file_or_error.error()), filename, input, lexer.tell());
  123. auto data = file_or_error.value()->read_all();
  124. auto& result = Parser(real_path, data, import_base_path).parse();
  125. import_stack.remove(real_path);
  126. s_resolved_imports.set(real_path, &result);
  127. return result;
  128. }
  129. NonnullRefPtr<Type> Parser::parse_type()
  130. {
  131. if (lexer.consume_specific('(')) {
  132. NonnullRefPtrVector<Type> union_member_types;
  133. union_member_types.append(parse_type());
  134. consume_whitespace();
  135. assert_string("or"sv);
  136. consume_whitespace();
  137. union_member_types.append(parse_type());
  138. consume_whitespace();
  139. while (lexer.consume_specific("or")) {
  140. consume_whitespace();
  141. union_member_types.append(parse_type());
  142. consume_whitespace();
  143. }
  144. assert_specific(')');
  145. bool nullable = lexer.consume_specific('?');
  146. return adopt_ref(*new UnionType("", nullable, move(union_member_types)));
  147. }
  148. bool unsigned_ = lexer.consume_specific("unsigned");
  149. if (unsigned_)
  150. consume_whitespace();
  151. // FIXME: Actually treat "unrestricted" and normal floats/doubles differently.
  152. if (lexer.consume_specific("unrestricted"))
  153. consume_whitespace();
  154. auto name = lexer.consume_until([](auto ch) { return !is_ascii_alphanumeric(ch) && ch != '_'; });
  155. if (name.equals_ignoring_case("long"sv)) {
  156. consume_whitespace();
  157. if (lexer.consume_specific("long"sv))
  158. name = "long long"sv;
  159. }
  160. NonnullRefPtrVector<Type> parameters;
  161. bool is_parameterized_type = false;
  162. if (lexer.consume_specific('<')) {
  163. is_parameterized_type = true;
  164. parameters.append(parse_type());
  165. while (lexer.consume_specific(',')) {
  166. consume_whitespace();
  167. parameters.append(parse_type());
  168. }
  169. lexer.consume_specific('>');
  170. }
  171. auto nullable = lexer.consume_specific('?');
  172. StringBuilder builder;
  173. if (unsigned_)
  174. builder.append("unsigned "sv);
  175. builder.append(name);
  176. if (is_parameterized_type)
  177. return adopt_ref(*new ParameterizedType(builder.to_string(), nullable, move(parameters)));
  178. return adopt_ref(*new Type(builder.to_string(), nullable));
  179. }
  180. void Parser::parse_attribute(HashMap<String, String>& extended_attributes, Interface& interface)
  181. {
  182. bool readonly = lexer.consume_specific("readonly");
  183. if (readonly)
  184. consume_whitespace();
  185. if (lexer.consume_specific("attribute"))
  186. consume_whitespace();
  187. auto type = parse_type();
  188. consume_whitespace();
  189. auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == ';'; });
  190. consume_whitespace();
  191. assert_specific(';');
  192. auto name_as_string = name.to_string();
  193. auto getter_callback_name = String::formatted("{}_getter", name_as_string.to_snakecase());
  194. auto setter_callback_name = String::formatted("{}_setter", name_as_string.to_snakecase());
  195. Attribute attribute {
  196. readonly,
  197. move(type),
  198. move(name_as_string),
  199. move(extended_attributes),
  200. move(getter_callback_name),
  201. move(setter_callback_name),
  202. };
  203. interface.attributes.append(move(attribute));
  204. }
  205. void Parser::parse_constant(Interface& interface)
  206. {
  207. lexer.consume_specific("const");
  208. consume_whitespace();
  209. auto type = parse_type();
  210. consume_whitespace();
  211. auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == '='; });
  212. consume_whitespace();
  213. lexer.consume_specific('=');
  214. consume_whitespace();
  215. auto value = lexer.consume_while([](auto ch) { return !is_ascii_space(ch) && ch != ';'; });
  216. consume_whitespace();
  217. assert_specific(';');
  218. Constant constant {
  219. move(type),
  220. move(name),
  221. move(value),
  222. };
  223. interface.constants.append(move(constant));
  224. }
  225. Vector<Parameter> Parser::parse_parameters()
  226. {
  227. consume_whitespace();
  228. Vector<Parameter> parameters;
  229. for (;;) {
  230. if (lexer.next_is(')'))
  231. break;
  232. HashMap<String, String> extended_attributes;
  233. if (lexer.consume_specific('['))
  234. extended_attributes = parse_extended_attributes();
  235. bool optional = lexer.consume_specific("optional");
  236. if (optional)
  237. consume_whitespace();
  238. auto type = parse_type();
  239. bool variadic = lexer.consume_specific("..."sv);
  240. consume_whitespace();
  241. auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == ',' || ch == ')' || ch == '='; });
  242. Parameter parameter = { move(type), move(name), optional, {}, extended_attributes, variadic };
  243. consume_whitespace();
  244. if (variadic) {
  245. // Variadic parameters must be last and do not have default values.
  246. parameters.append(move(parameter));
  247. break;
  248. }
  249. if (lexer.next_is(')')) {
  250. parameters.append(move(parameter));
  251. break;
  252. }
  253. if (lexer.next_is('=') && optional) {
  254. assert_specific('=');
  255. consume_whitespace();
  256. auto default_value = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == ',' || ch == ')'; });
  257. parameter.optional_default_value = default_value;
  258. }
  259. parameters.append(move(parameter));
  260. if (lexer.next_is(')'))
  261. break;
  262. assert_specific(',');
  263. consume_whitespace();
  264. }
  265. return parameters;
  266. }
  267. Function Parser::parse_function(HashMap<String, String>& extended_attributes, Interface& interface, IsSpecialOperation is_special_operation)
  268. {
  269. bool static_ = false;
  270. if (lexer.consume_specific("static")) {
  271. static_ = true;
  272. consume_whitespace();
  273. }
  274. auto return_type = parse_type();
  275. consume_whitespace();
  276. auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == '('; });
  277. consume_whitespace();
  278. assert_specific('(');
  279. auto parameters = parse_parameters();
  280. assert_specific(')');
  281. consume_whitespace();
  282. assert_specific(';');
  283. Function function { move(return_type), name, move(parameters), move(extended_attributes), {}, false };
  284. // "Defining a special operation with an identifier is equivalent to separating the special operation out into its own declaration without an identifier."
  285. if (is_special_operation == IsSpecialOperation::No || (is_special_operation == IsSpecialOperation::Yes && !name.is_empty())) {
  286. if (!static_)
  287. interface.functions.append(function);
  288. else
  289. interface.static_functions.append(function);
  290. }
  291. return function;
  292. }
  293. void Parser::parse_constructor(Interface& interface)
  294. {
  295. assert_string("constructor"sv);
  296. consume_whitespace();
  297. assert_specific('(');
  298. auto parameters = parse_parameters();
  299. assert_specific(')');
  300. consume_whitespace();
  301. assert_specific(';');
  302. interface.constructors.append(Constructor { interface.name, move(parameters) });
  303. }
  304. void Parser::parse_stringifier(HashMap<String, String>& extended_attributes, Interface& interface)
  305. {
  306. assert_string("stringifier"sv);
  307. consume_whitespace();
  308. interface.has_stringifier = true;
  309. if (lexer.next_is("readonly"sv) || lexer.next_is("attribute"sv)) {
  310. parse_attribute(extended_attributes, interface);
  311. interface.stringifier_attribute = interface.attributes.last().name;
  312. } else {
  313. assert_specific(';');
  314. }
  315. }
  316. void Parser::parse_iterable(Interface& interface)
  317. {
  318. assert_string("iterable"sv);
  319. assert_specific('<');
  320. auto first_type = parse_type();
  321. if (lexer.next_is(',')) {
  322. if (interface.supports_indexed_properties())
  323. report_parsing_error("Interfaces with a pair iterator must not supported indexed properties."sv, filename, input, lexer.tell());
  324. assert_specific(',');
  325. consume_whitespace();
  326. auto second_type = parse_type();
  327. interface.pair_iterator_types = Tuple { move(first_type), move(second_type) };
  328. } else {
  329. if (!interface.supports_indexed_properties())
  330. report_parsing_error("Interfaces with a value iterator must supported indexed properties."sv, filename, input, lexer.tell());
  331. interface.value_iterator_type = move(first_type);
  332. }
  333. assert_specific('>');
  334. assert_specific(';');
  335. }
  336. void Parser::parse_getter(HashMap<String, String>& extended_attributes, Interface& interface)
  337. {
  338. assert_string("getter"sv);
  339. consume_whitespace();
  340. auto function = parse_function(extended_attributes, interface, IsSpecialOperation::Yes);
  341. if (function.parameters.size() != 1)
  342. report_parsing_error(String::formatted("Named/indexed property getters must have only 1 parameter, got {} parameters.", function.parameters.size()), filename, input, lexer.tell());
  343. auto& identifier = function.parameters.first();
  344. if (identifier.type->nullable)
  345. report_parsing_error("identifier's type must not be nullable."sv, filename, input, lexer.tell());
  346. if (identifier.optional)
  347. report_parsing_error("identifier must not be optional."sv, filename, input, lexer.tell());
  348. // FIXME: Disallow variadic functions once they're supported.
  349. if (identifier.type->name == "DOMString") {
  350. if (interface.named_property_getter.has_value())
  351. report_parsing_error("An interface can only have one named property getter."sv, filename, input, lexer.tell());
  352. interface.named_property_getter = move(function);
  353. } else if (identifier.type->name == "unsigned long") {
  354. if (interface.indexed_property_getter.has_value())
  355. report_parsing_error("An interface can only have one indexed property getter."sv, filename, input, lexer.tell());
  356. interface.indexed_property_getter = move(function);
  357. } else {
  358. report_parsing_error(String::formatted("Named/indexed property getter's identifier's type must be either 'DOMString' or 'unsigned long', got '{}'.", identifier.type->name), filename, input, lexer.tell());
  359. }
  360. }
  361. void Parser::parse_setter(HashMap<String, String>& extended_attributes, Interface& interface)
  362. {
  363. assert_string("setter"sv);
  364. consume_whitespace();
  365. auto function = parse_function(extended_attributes, interface, IsSpecialOperation::Yes);
  366. if (function.parameters.size() != 2)
  367. report_parsing_error(String::formatted("Named/indexed property setters must have only 2 parameters, got {} parameter(s).", function.parameters.size()), filename, input, lexer.tell());
  368. auto& identifier = function.parameters.first();
  369. if (identifier.type->nullable)
  370. report_parsing_error("identifier's type must not be nullable."sv, filename, input, lexer.tell());
  371. if (identifier.optional)
  372. report_parsing_error("identifier must not be optional."sv, filename, input, lexer.tell());
  373. // FIXME: Disallow variadic functions once they're supported.
  374. if (identifier.type->name == "DOMString") {
  375. if (interface.named_property_setter.has_value())
  376. report_parsing_error("An interface can only have one named property setter."sv, filename, input, lexer.tell());
  377. if (!interface.named_property_getter.has_value())
  378. report_parsing_error("A named property setter must be accompanied by a named property getter."sv, filename, input, lexer.tell());
  379. interface.named_property_setter = move(function);
  380. } else if (identifier.type->name == "unsigned long") {
  381. if (interface.indexed_property_setter.has_value())
  382. report_parsing_error("An interface can only have one indexed property setter."sv, filename, input, lexer.tell());
  383. if (!interface.indexed_property_getter.has_value())
  384. report_parsing_error("An indexed property setter must be accompanied by an indexed property getter."sv, filename, input, lexer.tell());
  385. interface.indexed_property_setter = move(function);
  386. } else {
  387. report_parsing_error(String::formatted("Named/indexed property setter's identifier's type must be either 'DOMString' or 'unsigned long', got '{}'.", identifier.type->name), filename, input, lexer.tell());
  388. }
  389. }
  390. void Parser::parse_deleter(HashMap<String, String>& extended_attributes, Interface& interface)
  391. {
  392. assert_string("deleter"sv);
  393. consume_whitespace();
  394. auto function = parse_function(extended_attributes, interface, IsSpecialOperation::Yes);
  395. if (function.parameters.size() != 1)
  396. report_parsing_error(String::formatted("Named property deleter must have only 1 parameter, got {} parameters.", function.parameters.size()), filename, input, lexer.tell());
  397. auto& identifier = function.parameters.first();
  398. if (identifier.type->nullable)
  399. report_parsing_error("identifier's type must not be nullable."sv, filename, input, lexer.tell());
  400. if (identifier.optional)
  401. report_parsing_error("identifier must not be optional."sv, filename, input, lexer.tell());
  402. // FIXME: Disallow variadic functions once they're supported.
  403. if (identifier.type->name == "DOMString") {
  404. if (interface.named_property_deleter.has_value())
  405. report_parsing_error("An interface can only have one named property deleter."sv, filename, input, lexer.tell());
  406. if (!interface.named_property_getter.has_value())
  407. report_parsing_error("A named property deleter must be accompanied by a named property getter."sv, filename, input, lexer.tell());
  408. interface.named_property_deleter = move(function);
  409. } else {
  410. report_parsing_error(String::formatted("Named property deleter's identifier's type must be 'DOMString', got '{}'.", identifier.type->name), filename, input, lexer.tell());
  411. }
  412. }
  413. void Parser::parse_interface(Interface& interface)
  414. {
  415. consume_whitespace();
  416. interface.name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
  417. consume_whitespace();
  418. if (lexer.consume_specific(':')) {
  419. consume_whitespace();
  420. interface.parent_name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
  421. consume_whitespace();
  422. }
  423. assert_specific('{');
  424. for (;;) {
  425. HashMap<String, String> extended_attributes;
  426. consume_whitespace();
  427. if (lexer.consume_specific('}')) {
  428. consume_whitespace();
  429. assert_specific(';');
  430. break;
  431. }
  432. if (lexer.consume_specific('[')) {
  433. extended_attributes = parse_extended_attributes();
  434. if (!interface.has_unscopable_member && extended_attributes.contains("Unscopable"))
  435. interface.has_unscopable_member = true;
  436. }
  437. if (lexer.next_is("constructor")) {
  438. parse_constructor(interface);
  439. continue;
  440. }
  441. if (lexer.next_is("const")) {
  442. parse_constant(interface);
  443. continue;
  444. }
  445. if (lexer.next_is("stringifier")) {
  446. parse_stringifier(extended_attributes, interface);
  447. continue;
  448. }
  449. if (lexer.next_is("iterable")) {
  450. parse_iterable(interface);
  451. continue;
  452. }
  453. if (lexer.next_is("readonly") || lexer.next_is("attribute")) {
  454. parse_attribute(extended_attributes, interface);
  455. continue;
  456. }
  457. if (lexer.next_is("getter")) {
  458. parse_getter(extended_attributes, interface);
  459. continue;
  460. }
  461. if (lexer.next_is("setter")) {
  462. parse_setter(extended_attributes, interface);
  463. continue;
  464. }
  465. if (lexer.next_is("deleter")) {
  466. parse_deleter(extended_attributes, interface);
  467. continue;
  468. }
  469. parse_function(extended_attributes, interface);
  470. }
  471. interface.wrapper_class = String::formatted("{}Wrapper", interface.name);
  472. interface.wrapper_base_class = String::formatted("{}Wrapper", interface.parent_name.is_empty() ? String::empty() : interface.parent_name);
  473. interface.constructor_class = String::formatted("{}Constructor", interface.name);
  474. interface.prototype_class = String::formatted("{}Prototype", interface.name);
  475. interface.prototype_base_class = String::formatted("{}Prototype", interface.parent_name.is_empty() ? "Object" : interface.parent_name);
  476. consume_whitespace();
  477. }
  478. void Parser::parse_enumeration(Interface& interface)
  479. {
  480. assert_string("enum"sv);
  481. consume_whitespace();
  482. Enumeration enumeration {};
  483. auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
  484. consume_whitespace();
  485. assert_specific('{');
  486. bool first = true;
  487. for (; !lexer.is_eof();) {
  488. consume_whitespace();
  489. if (lexer.next_is('}'))
  490. break;
  491. if (!first) {
  492. assert_specific(',');
  493. consume_whitespace();
  494. }
  495. assert_specific('"');
  496. auto string = lexer.consume_until('"');
  497. assert_specific('"');
  498. consume_whitespace();
  499. if (enumeration.values.contains(string))
  500. report_parsing_error(String::formatted("Enumeration {} contains duplicate member '{}'", name, string), filename, input, lexer.tell());
  501. else
  502. enumeration.values.set(string);
  503. if (first)
  504. enumeration.first_member = move(string);
  505. first = false;
  506. }
  507. consume_whitespace();
  508. assert_specific('}');
  509. assert_specific(';');
  510. HashTable<String> names_already_seen;
  511. for (auto& entry : enumeration.values)
  512. enumeration.translated_cpp_names.set(entry, convert_enumeration_value_to_cpp_enum_member(entry, names_already_seen));
  513. interface.enumerations.set(name, move(enumeration));
  514. consume_whitespace();
  515. }
  516. void Parser::parse_typedef(Interface& interface)
  517. {
  518. assert_string("typedef"sv);
  519. consume_whitespace();
  520. HashMap<String, String> extended_attributes;
  521. if (lexer.consume_specific('['))
  522. extended_attributes = parse_extended_attributes();
  523. auto type = parse_type();
  524. consume_whitespace();
  525. auto name = lexer.consume_until(';');
  526. assert_specific(';');
  527. interface.typedefs.set(name, Typedef { move(extended_attributes), move(type) });
  528. consume_whitespace();
  529. }
  530. void Parser::parse_dictionary(Interface& interface)
  531. {
  532. assert_string("dictionary"sv);
  533. consume_whitespace();
  534. Dictionary dictionary {};
  535. auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
  536. consume_whitespace();
  537. if (lexer.consume_specific(':')) {
  538. consume_whitespace();
  539. dictionary.parent_name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
  540. consume_whitespace();
  541. }
  542. assert_specific('{');
  543. for (;;) {
  544. consume_whitespace();
  545. if (lexer.consume_specific('}')) {
  546. consume_whitespace();
  547. assert_specific(';');
  548. break;
  549. }
  550. bool required = false;
  551. HashMap<String, String> extended_attributes;
  552. if (lexer.consume_specific("required")) {
  553. required = true;
  554. consume_whitespace();
  555. if (lexer.consume_specific('['))
  556. extended_attributes = parse_extended_attributes();
  557. }
  558. auto type = parse_type();
  559. consume_whitespace();
  560. auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == ';'; });
  561. consume_whitespace();
  562. Optional<StringView> default_value;
  563. if (lexer.consume_specific('=')) {
  564. VERIFY(!required);
  565. consume_whitespace();
  566. default_value = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == ';'; });
  567. consume_whitespace();
  568. }
  569. assert_specific(';');
  570. DictionaryMember member {
  571. required,
  572. move(type),
  573. name,
  574. move(extended_attributes),
  575. Optional<String>(move(default_value)),
  576. };
  577. dictionary.members.append(move(member));
  578. }
  579. // dictionary members need to be evaluated in lexicographical order
  580. quick_sort(dictionary.members, [&](auto& one, auto& two) {
  581. return one.name < two.name;
  582. });
  583. interface.dictionaries.set(name, move(dictionary));
  584. consume_whitespace();
  585. }
  586. void Parser::parse_interface_mixin(Interface& interface)
  587. {
  588. auto mixin_interface_ptr = make<Interface>();
  589. auto& mixin_interface = *mixin_interface_ptr;
  590. VERIFY(s_interfaces.set(move(mixin_interface_ptr)) == AK::HashSetResult::InsertedNewEntry);
  591. mixin_interface.module_own_path = interface.module_own_path;
  592. mixin_interface.is_mixin = true;
  593. assert_string("interface"sv);
  594. consume_whitespace();
  595. assert_string("mixin"sv);
  596. auto offset = lexer.tell();
  597. parse_interface(mixin_interface);
  598. if (!mixin_interface.parent_name.is_empty())
  599. report_parsing_error("Mixin interfaces are not allowed to have inherited parents"sv, filename, input, offset);
  600. auto name = mixin_interface.name;
  601. interface.mixins.set(move(name), &mixin_interface);
  602. }
  603. void Parser::parse_callback_function(HashMap<String, String>& extended_attributes, Interface& interface)
  604. {
  605. assert_string("callback"sv);
  606. consume_whitespace();
  607. auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
  608. consume_whitespace();
  609. assert_specific('=');
  610. consume_whitespace();
  611. auto return_type = parse_type();
  612. consume_whitespace();
  613. assert_specific('(');
  614. auto parameters = parse_parameters();
  615. assert_specific(')');
  616. consume_whitespace();
  617. assert_specific(';');
  618. interface.callback_functions.set(name, CallbackFunction { move(return_type), move(parameters), extended_attributes.contains("LegacyTreatNonObjectAsNull") });
  619. consume_whitespace();
  620. }
  621. void Parser::parse_non_interface_entities(bool allow_interface, Interface& interface)
  622. {
  623. consume_whitespace();
  624. while (!lexer.is_eof()) {
  625. HashMap<String, String> extended_attributes;
  626. if (lexer.consume_specific('['))
  627. extended_attributes = parse_extended_attributes();
  628. if (lexer.next_is("dictionary")) {
  629. parse_dictionary(interface);
  630. } else if (lexer.next_is("enum")) {
  631. parse_enumeration(interface);
  632. } else if (lexer.next_is("typedef")) {
  633. parse_typedef(interface);
  634. } else if (lexer.next_is("interface mixin")) {
  635. parse_interface_mixin(interface);
  636. } else if (lexer.next_is("callback")) {
  637. parse_callback_function(extended_attributes, interface);
  638. } else if ((allow_interface && !lexer.next_is("interface")) || !allow_interface) {
  639. auto current_offset = lexer.tell();
  640. auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
  641. consume_whitespace();
  642. if (lexer.consume_specific("includes")) {
  643. consume_whitespace();
  644. auto mixin_name = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == ';'; });
  645. interface.included_mixins.ensure(name).set(mixin_name);
  646. consume_whitespace();
  647. assert_specific(';');
  648. consume_whitespace();
  649. } else {
  650. report_parsing_error("expected 'enum' or 'dictionary'"sv, filename, input, current_offset);
  651. }
  652. } else {
  653. interface.extended_attributes = move(extended_attributes);
  654. break;
  655. }
  656. }
  657. consume_whitespace();
  658. }
  659. void resolve_typedef(Interface& interface, NonnullRefPtr<Type>& type, HashMap<String, String>* extended_attributes = {})
  660. {
  661. if (is<ParameterizedType>(*type)) {
  662. auto parameterized_type = static_ptr_cast<ParameterizedType>(type);
  663. auto& parameters = static_cast<Vector<NonnullRefPtr<Type>>&>(parameterized_type->parameters);
  664. for (auto& parameter : parameters)
  665. resolve_typedef(interface, parameter);
  666. return;
  667. }
  668. auto it = interface.typedefs.find(type->name);
  669. if (it == interface.typedefs.end())
  670. return;
  671. bool is_nullable = type->nullable;
  672. type = it->value.type;
  673. type->nullable = is_nullable;
  674. if (!extended_attributes)
  675. return;
  676. for (auto& attribute : it->value.extended_attributes)
  677. extended_attributes->set(attribute.key, attribute.value);
  678. }
  679. void resolve_parameters_typedefs(Interface& interface, Vector<Parameter>& parameters)
  680. {
  681. for (auto& parameter : parameters)
  682. resolve_typedef(interface, parameter.type, &parameter.extended_attributes);
  683. }
  684. template<typename FunctionType>
  685. void resolve_function_typedefs(Interface& interface, FunctionType& function)
  686. {
  687. resolve_typedef(interface, function.return_type);
  688. resolve_parameters_typedefs(interface, function.parameters);
  689. }
  690. Interface& Parser::parse()
  691. {
  692. auto this_module = Core::File::real_path_for(filename);
  693. auto interface_ptr = make<Interface>();
  694. auto& interface = *interface_ptr;
  695. VERIFY(s_interfaces.set(move(interface_ptr)) == AK::HashSetResult::InsertedNewEntry);
  696. interface.module_own_path = this_module;
  697. s_resolved_imports.set(this_module, &interface);
  698. Vector<Interface&> imports;
  699. HashTable<String> required_imported_paths;
  700. while (lexer.consume_specific("#import")) {
  701. consume_whitespace();
  702. assert_specific('<');
  703. auto path = lexer.consume_until('>');
  704. lexer.ignore();
  705. auto maybe_interface = resolve_import(path);
  706. if (maybe_interface.has_value()) {
  707. for (auto& entry : maybe_interface.value().required_imported_paths)
  708. required_imported_paths.set(entry);
  709. imports.append(maybe_interface.release_value());
  710. }
  711. consume_whitespace();
  712. }
  713. interface.required_imported_paths = required_imported_paths;
  714. parse_non_interface_entities(true, interface);
  715. if (lexer.consume_specific("interface"))
  716. parse_interface(interface);
  717. parse_non_interface_entities(false, interface);
  718. for (auto& import : imports) {
  719. // FIXME: Instead of copying every imported entity into the current interface, query imports directly
  720. for (auto& dictionary : import.dictionaries)
  721. interface.dictionaries.set(dictionary.key, dictionary.value);
  722. for (auto& enumeration : import.enumerations) {
  723. auto enumeration_copy = enumeration.value;
  724. enumeration_copy.is_original_definition = false;
  725. interface.enumerations.set(enumeration.key, move(enumeration_copy));
  726. }
  727. for (auto& typedef_ : import.typedefs)
  728. interface.typedefs.set(typedef_.key, typedef_.value);
  729. for (auto& mixin : import.mixins) {
  730. if (auto it = interface.mixins.find(mixin.key); it != interface.mixins.end() && it->value != mixin.value)
  731. report_parsing_error(String::formatted("Mixin '{}' was already defined in {}", mixin.key, mixin.value->module_own_path), filename, input, lexer.tell());
  732. interface.mixins.set(mixin.key, mixin.value);
  733. }
  734. for (auto& callback_function : import.callback_functions)
  735. interface.callback_functions.set(callback_function.key, callback_function.value);
  736. }
  737. // Resolve mixins
  738. if (auto it = interface.included_mixins.find(interface.name); it != interface.included_mixins.end()) {
  739. for (auto& entry : it->value) {
  740. auto mixin_it = interface.mixins.find(entry);
  741. if (mixin_it == interface.mixins.end())
  742. report_parsing_error(String::formatted("Mixin '{}' was never defined", entry), filename, input, lexer.tell());
  743. auto& mixin = mixin_it->value;
  744. interface.attributes.extend(mixin->attributes);
  745. interface.constants.extend(mixin->constants);
  746. interface.functions.extend(mixin->functions);
  747. interface.static_functions.extend(mixin->static_functions);
  748. if (interface.has_stringifier && mixin->has_stringifier)
  749. report_parsing_error(String::formatted("Both interface '{}' and mixin '{}' have defined stringifier attributes", interface.name, mixin->name), filename, input, lexer.tell());
  750. if (mixin->has_stringifier) {
  751. interface.stringifier_attribute = mixin->stringifier_attribute;
  752. interface.has_stringifier = true;
  753. }
  754. if (mixin->has_unscopable_member)
  755. interface.has_unscopable_member = true;
  756. }
  757. }
  758. // Resolve typedefs
  759. for (auto& attribute : interface.attributes)
  760. resolve_typedef(interface, attribute.type, &attribute.extended_attributes);
  761. for (auto& constant : interface.constants)
  762. resolve_typedef(interface, constant.type);
  763. for (auto& constructor : interface.constructors)
  764. resolve_parameters_typedefs(interface, constructor.parameters);
  765. for (auto& function : interface.functions)
  766. resolve_function_typedefs(interface, function);
  767. for (auto& static_function : interface.static_functions)
  768. resolve_function_typedefs(interface, static_function);
  769. if (interface.value_iterator_type.has_value())
  770. resolve_typedef(interface, *interface.value_iterator_type);
  771. if (interface.pair_iterator_types.has_value()) {
  772. resolve_typedef(interface, interface.pair_iterator_types->get<0>());
  773. resolve_typedef(interface, interface.pair_iterator_types->get<1>());
  774. }
  775. if (interface.named_property_getter.has_value())
  776. resolve_function_typedefs(interface, *interface.named_property_getter);
  777. if (interface.named_property_setter.has_value())
  778. resolve_function_typedefs(interface, *interface.named_property_setter);
  779. if (interface.indexed_property_getter.has_value())
  780. resolve_function_typedefs(interface, *interface.indexed_property_getter);
  781. if (interface.indexed_property_setter.has_value())
  782. resolve_function_typedefs(interface, *interface.indexed_property_setter);
  783. if (interface.named_property_deleter.has_value())
  784. resolve_function_typedefs(interface, *interface.named_property_deleter);
  785. if (interface.named_property_getter.has_value())
  786. resolve_function_typedefs(interface, *interface.named_property_getter);
  787. for (auto& dictionary : interface.dictionaries) {
  788. for (auto& dictionary_member : dictionary.value.members)
  789. resolve_typedef(interface, dictionary_member.type, &dictionary_member.extended_attributes);
  790. }
  791. for (auto& callback_function : interface.callback_functions)
  792. resolve_function_typedefs(interface, callback_function.value);
  793. // Create overload sets
  794. for (auto& function : interface.functions) {
  795. auto& overload_set = interface.overload_sets.ensure(function.name);
  796. function.overload_index = overload_set.size();
  797. overload_set.append(function);
  798. }
  799. for (auto& overload_set : interface.overload_sets) {
  800. if (overload_set.value.size() == 1)
  801. continue;
  802. for (auto& overloaded_function : overload_set.value)
  803. overloaded_function.is_overloaded = true;
  804. }
  805. for (auto& function : interface.static_functions) {
  806. auto& overload_set = interface.static_overload_sets.ensure(function.name);
  807. function.overload_index = overload_set.size();
  808. overload_set.append(function);
  809. }
  810. for (auto& overload_set : interface.static_overload_sets) {
  811. if (overload_set.value.size() == 1)
  812. continue;
  813. for (auto& overloaded_function : overload_set.value)
  814. overloaded_function.is_overloaded = true;
  815. }
  816. // FIXME: Add support for overloading constructors
  817. if (interface.will_generate_code())
  818. interface.required_imported_paths.set(this_module);
  819. interface.imported_modules = move(imports);
  820. return interface;
  821. }
  822. Parser::Parser(String filename, StringView contents, String import_base_path)
  823. : import_base_path(move(import_base_path))
  824. , filename(move(filename))
  825. , input(contents)
  826. , lexer(input)
  827. {
  828. }
  829. }