IDLParser.cpp 34 KB

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