IDLParser.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. return Parser(real_path, data, import_base_path).parse();
  121. }
  122. NonnullRefPtr<Type> Parser::parse_type()
  123. {
  124. if (lexer.consume_specific('(')) {
  125. NonnullRefPtrVector<Type> union_member_types;
  126. union_member_types.append(parse_type());
  127. consume_whitespace();
  128. assert_string("or");
  129. consume_whitespace();
  130. union_member_types.append(parse_type());
  131. consume_whitespace();
  132. while (lexer.consume_specific("or")) {
  133. consume_whitespace();
  134. union_member_types.append(parse_type());
  135. consume_whitespace();
  136. }
  137. assert_specific(')');
  138. bool nullable = lexer.consume_specific('?');
  139. return adopt_ref(*new UnionType("", nullable, move(union_member_types)));
  140. }
  141. bool unsigned_ = lexer.consume_specific("unsigned");
  142. if (unsigned_)
  143. consume_whitespace();
  144. auto name = lexer.consume_until([](auto ch) { return !is_ascii_alphanumeric(ch) && ch != '_'; });
  145. NonnullRefPtrVector<Type> parameters;
  146. bool is_parameterized_type = false;
  147. if (lexer.consume_specific('<')) {
  148. is_parameterized_type = true;
  149. parameters.append(parse_type());
  150. while (lexer.consume_specific(',')) {
  151. consume_whitespace();
  152. parameters.append(parse_type());
  153. }
  154. lexer.consume_specific('>');
  155. }
  156. auto nullable = lexer.consume_specific('?');
  157. StringBuilder builder;
  158. if (unsigned_)
  159. builder.append("unsigned ");
  160. builder.append(name);
  161. if (is_parameterized_type)
  162. return adopt_ref(*new ParameterizedType(builder.to_string(), nullable, move(parameters)));
  163. return adopt_ref(*new Type(builder.to_string(), nullable));
  164. }
  165. void Parser::parse_attribute(HashMap<String, String>& extended_attributes, Interface& interface)
  166. {
  167. bool readonly = lexer.consume_specific("readonly");
  168. if (readonly)
  169. consume_whitespace();
  170. if (lexer.consume_specific("attribute"))
  171. consume_whitespace();
  172. auto type = parse_type();
  173. consume_whitespace();
  174. auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == ';'; });
  175. consume_whitespace();
  176. assert_specific(';');
  177. auto name_as_string = name.to_string();
  178. auto getter_callback_name = String::formatted("{}_getter", name_as_string.to_snakecase());
  179. auto setter_callback_name = String::formatted("{}_setter", name_as_string.to_snakecase());
  180. Attribute attribute {
  181. readonly,
  182. move(type),
  183. move(name_as_string),
  184. move(extended_attributes),
  185. move(getter_callback_name),
  186. move(setter_callback_name),
  187. };
  188. interface.attributes.append(move(attribute));
  189. }
  190. void Parser::parse_constant(Interface& interface)
  191. {
  192. lexer.consume_specific("const");
  193. consume_whitespace();
  194. auto type = parse_type();
  195. consume_whitespace();
  196. auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == '='; });
  197. consume_whitespace();
  198. lexer.consume_specific('=');
  199. consume_whitespace();
  200. auto value = lexer.consume_while([](auto ch) { return !is_ascii_space(ch) && ch != ';'; });
  201. consume_whitespace();
  202. assert_specific(';');
  203. Constant constant {
  204. move(type),
  205. move(name),
  206. move(value),
  207. };
  208. interface.constants.append(move(constant));
  209. }
  210. Vector<Parameter> Parser::parse_parameters()
  211. {
  212. consume_whitespace();
  213. Vector<Parameter> parameters;
  214. for (;;) {
  215. if (lexer.next_is(')'))
  216. break;
  217. HashMap<String, String> extended_attributes;
  218. if (lexer.consume_specific('['))
  219. extended_attributes = parse_extended_attributes();
  220. bool optional = lexer.consume_specific("optional");
  221. if (optional)
  222. consume_whitespace();
  223. auto type = parse_type();
  224. bool variadic = lexer.consume_specific("..."sv);
  225. consume_whitespace();
  226. auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == ',' || ch == ')' || ch == '='; });
  227. Parameter parameter = { move(type), move(name), optional, {}, extended_attributes, variadic };
  228. consume_whitespace();
  229. if (variadic) {
  230. // Variadic parameters must be last and do not have default values.
  231. parameters.append(move(parameter));
  232. break;
  233. }
  234. if (lexer.next_is(')')) {
  235. parameters.append(move(parameter));
  236. break;
  237. }
  238. if (lexer.next_is('=') && optional) {
  239. assert_specific('=');
  240. consume_whitespace();
  241. auto default_value = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == ',' || ch == ')'; });
  242. parameter.optional_default_value = default_value;
  243. }
  244. parameters.append(move(parameter));
  245. if (lexer.next_is(')'))
  246. break;
  247. assert_specific(',');
  248. consume_whitespace();
  249. }
  250. return parameters;
  251. }
  252. Function Parser::parse_function(HashMap<String, String>& extended_attributes, Interface& interface, IsSpecialOperation is_special_operation)
  253. {
  254. bool static_ = false;
  255. if (lexer.consume_specific("static")) {
  256. static_ = true;
  257. consume_whitespace();
  258. }
  259. auto return_type = parse_type();
  260. consume_whitespace();
  261. auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == '('; });
  262. consume_whitespace();
  263. assert_specific('(');
  264. auto parameters = parse_parameters();
  265. assert_specific(')');
  266. consume_whitespace();
  267. assert_specific(';');
  268. Function function { move(return_type), name, move(parameters), move(extended_attributes) };
  269. // "Defining a special operation with an identifier is equivalent to separating the special operation out into its own declaration without an identifier."
  270. if (is_special_operation == IsSpecialOperation::No || (is_special_operation == IsSpecialOperation::Yes && !name.is_empty())) {
  271. if (!static_)
  272. interface.functions.append(function);
  273. else
  274. interface.static_functions.append(function);
  275. }
  276. return function;
  277. }
  278. void Parser::parse_constructor(Interface& interface)
  279. {
  280. assert_string("constructor");
  281. consume_whitespace();
  282. assert_specific('(');
  283. auto parameters = parse_parameters();
  284. assert_specific(')');
  285. consume_whitespace();
  286. assert_specific(';');
  287. interface.constructors.append(Constructor { interface.name, move(parameters) });
  288. }
  289. void Parser::parse_stringifier(HashMap<String, String>& extended_attributes, Interface& interface)
  290. {
  291. assert_string("stringifier");
  292. consume_whitespace();
  293. interface.has_stringifier = true;
  294. if (lexer.next_is("readonly") || lexer.next_is("attribute")) {
  295. parse_attribute(extended_attributes, interface);
  296. interface.stringifier_attribute = interface.attributes.last().name;
  297. } else {
  298. assert_specific(';');
  299. }
  300. }
  301. void Parser::parse_iterable(Interface& interface)
  302. {
  303. assert_string("iterable");
  304. assert_specific('<');
  305. auto first_type = parse_type();
  306. if (lexer.next_is(',')) {
  307. if (interface.supports_indexed_properties())
  308. report_parsing_error("Interfaces with a pair iterator must not supported indexed properties.", filename, input, lexer.tell());
  309. assert_specific(',');
  310. consume_whitespace();
  311. auto second_type = parse_type();
  312. interface.pair_iterator_types = Tuple { move(first_type), move(second_type) };
  313. } else {
  314. if (!interface.supports_indexed_properties())
  315. report_parsing_error("Interfaces with a value iterator must supported indexed properties.", filename, input, lexer.tell());
  316. interface.value_iterator_type = move(first_type);
  317. }
  318. assert_specific('>');
  319. assert_specific(';');
  320. }
  321. void Parser::parse_getter(HashMap<String, String>& extended_attributes, Interface& interface)
  322. {
  323. assert_string("getter");
  324. consume_whitespace();
  325. auto function = parse_function(extended_attributes, interface, IsSpecialOperation::Yes);
  326. if (function.parameters.size() != 1)
  327. report_parsing_error(String::formatted("Named/indexed property getters must have only 1 parameter, got {} parameters.", function.parameters.size()), filename, input, lexer.tell());
  328. auto& identifier = function.parameters.first();
  329. if (identifier.type->nullable)
  330. report_parsing_error("identifier's type must not be nullable.", filename, input, lexer.tell());
  331. if (identifier.optional)
  332. report_parsing_error("identifier must not be optional.", filename, input, lexer.tell());
  333. // FIXME: Disallow variadic functions once they're supported.
  334. if (identifier.type->name == "DOMString") {
  335. if (interface.named_property_getter.has_value())
  336. report_parsing_error("An interface can only have one named property getter.", filename, input, lexer.tell());
  337. interface.named_property_getter = move(function);
  338. } else if (identifier.type->name == "unsigned long") {
  339. if (interface.indexed_property_getter.has_value())
  340. report_parsing_error("An interface can only have one indexed property getter.", filename, input, lexer.tell());
  341. interface.indexed_property_getter = move(function);
  342. } else {
  343. 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());
  344. }
  345. }
  346. void Parser::parse_setter(HashMap<String, String>& extended_attributes, Interface& interface)
  347. {
  348. assert_string("setter");
  349. consume_whitespace();
  350. auto function = parse_function(extended_attributes, interface, IsSpecialOperation::Yes);
  351. if (function.parameters.size() != 2)
  352. report_parsing_error(String::formatted("Named/indexed property setters must have only 2 parameters, got {} parameter(s).", function.parameters.size()), filename, input, lexer.tell());
  353. auto& identifier = function.parameters.first();
  354. if (identifier.type->nullable)
  355. report_parsing_error("identifier's type must not be nullable.", filename, input, lexer.tell());
  356. if (identifier.optional)
  357. report_parsing_error("identifier must not be optional.", filename, input, lexer.tell());
  358. // FIXME: Disallow variadic functions once they're supported.
  359. if (identifier.type->name == "DOMString") {
  360. if (interface.named_property_setter.has_value())
  361. report_parsing_error("An interface can only have one named property setter.", filename, input, lexer.tell());
  362. if (!interface.named_property_getter.has_value())
  363. report_parsing_error("A named property setter must be accompanied by a named property getter.", filename, input, lexer.tell());
  364. interface.named_property_setter = move(function);
  365. } else if (identifier.type->name == "unsigned long") {
  366. if (interface.indexed_property_setter.has_value())
  367. report_parsing_error("An interface can only have one indexed property setter.", filename, input, lexer.tell());
  368. if (!interface.indexed_property_getter.has_value())
  369. report_parsing_error("An indexed property setter must be accompanied by an indexed property getter.", filename, input, lexer.tell());
  370. interface.indexed_property_setter = move(function);
  371. } else {
  372. 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());
  373. }
  374. }
  375. void Parser::parse_deleter(HashMap<String, String>& extended_attributes, Interface& interface)
  376. {
  377. assert_string("deleter");
  378. consume_whitespace();
  379. auto function = parse_function(extended_attributes, interface, IsSpecialOperation::Yes);
  380. if (function.parameters.size() != 1)
  381. report_parsing_error(String::formatted("Named property deleter must have only 1 parameter, got {} parameters.", function.parameters.size()), filename, input, lexer.tell());
  382. auto& identifier = function.parameters.first();
  383. if (identifier.type->nullable)
  384. report_parsing_error("identifier's type must not be nullable.", filename, input, lexer.tell());
  385. if (identifier.optional)
  386. report_parsing_error("identifier must not be optional.", filename, input, lexer.tell());
  387. // FIXME: Disallow variadic functions once they're supported.
  388. if (identifier.type->name == "DOMString") {
  389. if (interface.named_property_deleter.has_value())
  390. report_parsing_error("An interface can only have one named property deleter.", filename, input, lexer.tell());
  391. if (!interface.named_property_getter.has_value())
  392. report_parsing_error("A named property deleter must be accompanied by a named property getter.", filename, input, lexer.tell());
  393. interface.named_property_deleter = move(function);
  394. } else {
  395. report_parsing_error(String::formatted("Named property deleter's identifier's type must be 'DOMString', got '{}'.", identifier.type->name), filename, input, lexer.tell());
  396. }
  397. }
  398. void Parser::parse_interface(Interface& interface)
  399. {
  400. consume_whitespace();
  401. interface.name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
  402. consume_whitespace();
  403. if (lexer.consume_specific(':')) {
  404. consume_whitespace();
  405. interface.parent_name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
  406. consume_whitespace();
  407. }
  408. assert_specific('{');
  409. for (;;) {
  410. HashMap<String, String> extended_attributes;
  411. consume_whitespace();
  412. if (lexer.consume_specific('}')) {
  413. consume_whitespace();
  414. assert_specific(';');
  415. break;
  416. }
  417. if (lexer.consume_specific('[')) {
  418. extended_attributes = parse_extended_attributes();
  419. if (!interface.has_unscopable_member && extended_attributes.contains("Unscopable"))
  420. interface.has_unscopable_member = true;
  421. }
  422. if (lexer.next_is("constructor")) {
  423. parse_constructor(interface);
  424. continue;
  425. }
  426. if (lexer.next_is("const")) {
  427. parse_constant(interface);
  428. continue;
  429. }
  430. if (lexer.next_is("stringifier")) {
  431. parse_stringifier(extended_attributes, interface);
  432. continue;
  433. }
  434. if (lexer.next_is("iterable")) {
  435. parse_iterable(interface);
  436. continue;
  437. }
  438. if (lexer.next_is("readonly") || lexer.next_is("attribute")) {
  439. parse_attribute(extended_attributes, interface);
  440. continue;
  441. }
  442. if (lexer.next_is("getter")) {
  443. parse_getter(extended_attributes, interface);
  444. continue;
  445. }
  446. if (lexer.next_is("setter")) {
  447. parse_setter(extended_attributes, interface);
  448. continue;
  449. }
  450. if (lexer.next_is("deleter")) {
  451. parse_deleter(extended_attributes, interface);
  452. continue;
  453. }
  454. parse_function(extended_attributes, interface);
  455. }
  456. interface.wrapper_class = String::formatted("{}Wrapper", interface.name);
  457. interface.wrapper_base_class = String::formatted("{}Wrapper", interface.parent_name.is_empty() ? String::empty() : interface.parent_name);
  458. interface.constructor_class = String::formatted("{}Constructor", interface.name);
  459. interface.prototype_class = String::formatted("{}Prototype", interface.name);
  460. interface.prototype_base_class = String::formatted("{}Prototype", interface.parent_name.is_empty() ? "Object" : interface.parent_name);
  461. consume_whitespace();
  462. }
  463. void Parser::parse_enumeration(Interface& interface)
  464. {
  465. assert_string("enum");
  466. consume_whitespace();
  467. Enumeration enumeration {};
  468. auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
  469. consume_whitespace();
  470. assert_specific('{');
  471. bool first = true;
  472. for (; !lexer.is_eof();) {
  473. consume_whitespace();
  474. if (lexer.next_is('}'))
  475. break;
  476. if (!first) {
  477. assert_specific(',');
  478. consume_whitespace();
  479. }
  480. assert_specific('"');
  481. auto string = lexer.consume_until('"');
  482. assert_specific('"');
  483. consume_whitespace();
  484. if (enumeration.values.contains(string))
  485. report_parsing_error(String::formatted("Enumeration {} contains duplicate member '{}'", name, string), filename, input, lexer.tell());
  486. else
  487. enumeration.values.set(string);
  488. if (first)
  489. enumeration.first_member = move(string);
  490. first = false;
  491. }
  492. consume_whitespace();
  493. assert_specific('}');
  494. assert_specific(';');
  495. HashTable<String> names_already_seen;
  496. for (auto& entry : enumeration.values)
  497. enumeration.translated_cpp_names.set(entry, convert_enumeration_value_to_cpp_enum_member(entry, names_already_seen));
  498. interface.enumerations.set(name, move(enumeration));
  499. consume_whitespace();
  500. }
  501. void Parser::parse_dictionary(Interface& interface)
  502. {
  503. assert_string("dictionary");
  504. consume_whitespace();
  505. Dictionary dictionary {};
  506. auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
  507. consume_whitespace();
  508. if (lexer.consume_specific(':')) {
  509. consume_whitespace();
  510. dictionary.parent_name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
  511. consume_whitespace();
  512. }
  513. assert_specific('{');
  514. for (;;) {
  515. consume_whitespace();
  516. if (lexer.consume_specific('}')) {
  517. consume_whitespace();
  518. assert_specific(';');
  519. break;
  520. }
  521. bool required = false;
  522. HashMap<String, String> extended_attributes;
  523. if (lexer.consume_specific("required")) {
  524. required = true;
  525. consume_whitespace();
  526. if (lexer.consume_specific('['))
  527. extended_attributes = parse_extended_attributes();
  528. }
  529. auto type = parse_type();
  530. consume_whitespace();
  531. auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == ';'; });
  532. consume_whitespace();
  533. Optional<StringView> default_value;
  534. if (lexer.consume_specific('=')) {
  535. VERIFY(!required);
  536. consume_whitespace();
  537. default_value = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == ';'; });
  538. consume_whitespace();
  539. }
  540. assert_specific(';');
  541. DictionaryMember member {
  542. required,
  543. move(type),
  544. name,
  545. move(extended_attributes),
  546. Optional<String>(move(default_value)),
  547. };
  548. dictionary.members.append(move(member));
  549. }
  550. // dictionary members need to be evaluated in lexicographical order
  551. quick_sort(dictionary.members, [&](auto& one, auto& two) {
  552. return one.name < two.name;
  553. });
  554. interface.dictionaries.set(name, move(dictionary));
  555. consume_whitespace();
  556. }
  557. void Parser::parse_interface_mixin(Interface& interface)
  558. {
  559. auto mixin_interface = make<Interface>();
  560. mixin_interface->module_own_path = interface.module_own_path;
  561. mixin_interface->is_mixin = true;
  562. assert_string("interface");
  563. consume_whitespace();
  564. assert_string("mixin");
  565. auto offset = lexer.tell();
  566. parse_interface(*mixin_interface);
  567. if (!mixin_interface->parent_name.is_empty())
  568. report_parsing_error("Mixin interfaces are not allowed to have inherited parents", filename, input, offset);
  569. auto name = mixin_interface->name;
  570. interface.mixins.set(move(name), move(mixin_interface));
  571. }
  572. void Parser::parse_non_interface_entities(bool allow_interface, Interface& interface)
  573. {
  574. while (!lexer.is_eof()) {
  575. if (lexer.next_is("dictionary")) {
  576. parse_dictionary(interface);
  577. } else if (lexer.next_is("enum")) {
  578. parse_enumeration(interface);
  579. } else if (lexer.next_is("interface mixin")) {
  580. parse_interface_mixin(interface);
  581. } else if ((allow_interface && !lexer.next_is("interface")) || !allow_interface) {
  582. auto current_offset = lexer.tell();
  583. auto name = lexer.consume_until([](auto ch) { return is_ascii_space(ch); });
  584. consume_whitespace();
  585. if (lexer.consume_specific("includes")) {
  586. consume_whitespace();
  587. auto mixin_name = lexer.consume_until([](auto ch) { return is_ascii_space(ch) || ch == ';'; });
  588. interface.included_mixins.ensure(name).set(mixin_name);
  589. consume_whitespace();
  590. assert_specific(';');
  591. consume_whitespace();
  592. } else {
  593. report_parsing_error("expected 'enum' or 'dictionary'", filename, input, current_offset);
  594. }
  595. } else {
  596. break;
  597. }
  598. }
  599. }
  600. NonnullOwnPtr<Interface> Parser::parse()
  601. {
  602. auto this_module = Core::File::real_path_for(filename);
  603. s_all_imported_paths.set(this_module);
  604. auto interface = make<Interface>();
  605. interface->module_own_path = this_module;
  606. NonnullOwnPtrVector<Interface> imports;
  607. while (lexer.consume_specific("#import")) {
  608. consume_whitespace();
  609. assert_specific('<');
  610. auto path = lexer.consume_until('>');
  611. lexer.ignore();
  612. auto maybe_interface = resolve_import(path);
  613. if (maybe_interface.has_value()) {
  614. for (auto& entry : maybe_interface.value()->imported_paths)
  615. s_all_imported_paths.set(entry);
  616. imports.append(maybe_interface.release_value());
  617. }
  618. consume_whitespace();
  619. }
  620. interface->imported_paths = s_all_imported_paths;
  621. if (lexer.consume_specific('['))
  622. interface->extended_attributes = parse_extended_attributes();
  623. parse_non_interface_entities(true, *interface);
  624. if (lexer.consume_specific("interface"))
  625. parse_interface(*interface);
  626. parse_non_interface_entities(false, *interface);
  627. for (auto& import : imports) {
  628. // FIXME: Instead of copying every imported entity into the current interface, query imports directly
  629. for (auto& dictionary : import.dictionaries)
  630. interface->dictionaries.set(dictionary.key, dictionary.value);
  631. for (auto& enumeration : import.enumerations) {
  632. auto enumeration_copy = enumeration.value;
  633. enumeration_copy.is_original_definition = false;
  634. interface->enumerations.set(enumeration.key, move(enumeration_copy));
  635. }
  636. for (auto& mixin : import.mixins) {
  637. if (interface->mixins.contains(mixin.key))
  638. report_parsing_error(String::formatted("Mixin '{}' was already defined in {}", mixin.key, mixin.value->module_own_path), filename, input, lexer.tell());
  639. interface->mixins.set(mixin.key, move(mixin.value));
  640. }
  641. }
  642. // Resolve mixins
  643. if (auto it = interface->included_mixins.find(interface->name); it != interface->included_mixins.end()) {
  644. for (auto& entry : it->value) {
  645. auto mixin_it = interface->mixins.find(entry);
  646. if (mixin_it == interface->mixins.end())
  647. report_parsing_error(String::formatted("Mixin '{}' was never defined", entry), filename, input, lexer.tell());
  648. auto& mixin = mixin_it->value;
  649. interface->attributes.extend(mixin->attributes);
  650. interface->constants.extend(mixin->constants);
  651. interface->functions.extend(mixin->functions);
  652. interface->static_functions.extend(mixin->static_functions);
  653. if (interface->has_stringifier && mixin->has_stringifier)
  654. report_parsing_error(String::formatted("Both interface '{}' and mixin '{}' have defined stringifier attributes", interface->name, mixin->name), filename, input, lexer.tell());
  655. if (mixin->has_stringifier) {
  656. interface->stringifier_attribute = mixin->stringifier_attribute;
  657. interface->has_stringifier = true;
  658. }
  659. }
  660. }
  661. interface->imported_modules = move(imports);
  662. return interface;
  663. }
  664. Parser::Parser(String filename, StringView contents, String import_base_path)
  665. : import_base_path(move(import_base_path))
  666. , filename(move(filename))
  667. , input(contents)
  668. , lexer(input)
  669. {
  670. }
  671. }