IDLParser.cpp 35 KB

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