IDLParser.cpp 37 KB

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