SpecParser.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/NonnullOwnPtr.h>
  7. #include <LibCore/File.h>
  8. #include <LibXML/Parser/Parser.h>
  9. #include "Function.h"
  10. #include "Parser/Lexer.h"
  11. #include "Parser/SpecParser.h"
  12. #include "Parser/TextParser.h"
  13. #include "Parser/XMLUtils.h"
  14. namespace JSSpecCompiler {
  15. DiagnosticEngine& SpecificationParsingContext::diag()
  16. {
  17. return m_translation_unit->diag();
  18. }
  19. template<typename Func>
  20. auto SpecificationParsingContext::with_new_logical_scope(Func&& func)
  21. {
  22. TemporaryChange<RefPtr<LogicalLocation>> change(m_current_logical_scope, make_ref_counted<LogicalLocation>());
  23. return func();
  24. }
  25. LogicalLocation& SpecificationParsingContext::current_logical_scope()
  26. {
  27. return *m_current_logical_scope;
  28. }
  29. Location SpecificationParsingContext::file_scope() const
  30. {
  31. return { .filename = m_translation_unit->filename() };
  32. }
  33. Location SpecificationParsingContext::location_from_xml_offset(XML::Offset offset) const
  34. {
  35. return {
  36. .filename = m_translation_unit->filename(),
  37. .line = offset.line,
  38. .column = offset.column,
  39. .logical_location = m_current_logical_scope,
  40. };
  41. }
  42. ParseErrorOr<AlgorithmStep> AlgorithmStep::create(XML::Node const* node)
  43. {
  44. VERIFY(node->as_element().name == tag_li);
  45. auto [tokens, substeps] = TRY(tokenize_tree(node, true));
  46. AlgorithmStep result { .m_tokens = move(tokens), .m_node = node };
  47. if (substeps)
  48. result.m_substeps = TRY(AlgorithmStepList::create(substeps->as_element())).m_expression;
  49. result.m_expression = TRY(result.parse());
  50. return result;
  51. }
  52. ParseErrorOr<Tree> AlgorithmStep::parse()
  53. {
  54. TextParser parser(m_tokens, m_node);
  55. if (m_substeps)
  56. return parser.parse_step_with_substeps(RefPtr(m_substeps).release_nonnull());
  57. else
  58. return parser.parse_step_without_substeps();
  59. }
  60. ParseErrorOr<AlgorithmStepList> AlgorithmStepList::create(XML::Node::Element const& element)
  61. {
  62. VERIFY(element.name == tag_ol);
  63. AlgorithmStepList result;
  64. auto& steps = result.m_steps;
  65. Vector<Tree> step_expressions;
  66. for (auto const& child : element.children) {
  67. TRY(child->content.visit(
  68. [&](XML::Node::Element const& element) -> ParseErrorOr<void> {
  69. if (element.name != tag_li)
  70. return ParseError::create("<emu-alg> <ol> > :not(<li>) should not match any elements"sv, child);
  71. steps.append(TRY(AlgorithmStep::create(child)));
  72. step_expressions.append(steps.last().m_expression);
  73. return {};
  74. },
  75. [&](XML::Node::Text const&) -> ParseErrorOr<void> {
  76. if (!contains_empty_text(child))
  77. return ParseError::create("<emu-alg> <ol> should not have non-empty child text nodes"sv, child);
  78. return {};
  79. },
  80. move(ignore_comments)));
  81. }
  82. result.m_expression = make_ref_counted<TreeList>(move(step_expressions));
  83. return result;
  84. }
  85. ParseErrorOr<Algorithm> Algorithm::create(XML::Node const* node)
  86. {
  87. VERIFY(node->as_element().name == tag_emu_alg);
  88. XML::Node::Element const* steps_list = nullptr;
  89. for (auto const& child : node->as_element().children) {
  90. TRY(child->content.visit(
  91. [&](XML::Node::Element const& element) -> ParseErrorOr<void> {
  92. if (element.name == tag_ol) {
  93. if (steps_list != nullptr)
  94. return ParseError::create("<emu-alg> should have exactly one <ol> child"sv, child);
  95. steps_list = &element;
  96. return {};
  97. } else {
  98. return ParseError::create("<emu-alg> should not have children other than <ol>"sv, child);
  99. }
  100. },
  101. [&](XML::Node::Text const&) -> ParseErrorOr<void> {
  102. if (!contains_empty_text(child))
  103. return ParseError::create("<emu-alg> should not have non-empty child text nodes"sv, child);
  104. return {};
  105. },
  106. move(ignore_comments)));
  107. }
  108. if (steps_list == nullptr)
  109. return ParseError::create("<emu-alg> should have exactly one <ol> child"sv, node);
  110. Algorithm algorithm;
  111. algorithm.m_steps = TRY(AlgorithmStepList::create(*steps_list));
  112. algorithm.m_tree = algorithm.m_steps.m_expression;
  113. return algorithm;
  114. }
  115. ParseErrorOr<SpecFunction> SpecFunction::create(XML::Node const* element)
  116. {
  117. VERIFY(element->as_element().name == tag_emu_clause);
  118. SpecFunction result;
  119. result.m_id = TRY(get_attribute_by_name(element, attribute_id));
  120. result.m_name = TRY(get_attribute_by_name(element, attribute_aoid));
  121. u32 children_count = 0;
  122. bool has_definition = false;
  123. XML::Node const* algorithm_node = nullptr;
  124. XML::Node const* prose_node = nullptr;
  125. for (auto const& child : element->as_element().children) {
  126. TRY(child->content.visit(
  127. [&](XML::Node::Element const& element) -> ParseErrorOr<void> {
  128. ++children_count;
  129. if (element.name == tag_h1) {
  130. if (children_count != 1)
  131. return ParseError::create("<h1> should be the first child of a <emu-clause>"sv, child);
  132. TRY(result.parse_definition(child));
  133. has_definition = true;
  134. } else if (element.name == tag_p) {
  135. if (prose_node == nullptr)
  136. prose_node = child;
  137. } else if (element.name == tag_emu_alg) {
  138. algorithm_node = child;
  139. } else {
  140. return ParseError::create("Unknown child of <emu-clause>"sv, child);
  141. }
  142. return {};
  143. },
  144. [&](XML::Node::Text const&) -> ParseErrorOr<void> {
  145. if (!contains_empty_text(child)) {
  146. return ParseError::create("<emu-clause> should not have non-empty child text nodes"sv, child);
  147. }
  148. return {};
  149. },
  150. move(ignore_comments)));
  151. }
  152. if (algorithm_node == nullptr)
  153. return ParseError::create("No <emu-alg>"sv, element);
  154. if (prose_node == nullptr)
  155. return ParseError::create("No prose element"sv, element);
  156. if (!has_definition)
  157. return ParseError::create("Definition was not found"sv, element);
  158. result.m_algorithm = TRY(Algorithm::create(algorithm_node));
  159. return result;
  160. }
  161. ParseErrorOr<void> SpecFunction::parse_definition(XML::Node const* element)
  162. {
  163. auto tokens = TRY(tokenize_tree(element));
  164. TextParser parser(tokens.tokens, element);
  165. auto [section_number, function_name, arguments] = TRY(parser.parse_definition());
  166. if (function_name != m_name)
  167. return ParseError::create("Function name in definition differs from <emu-clause>[aoid]"sv, element);
  168. m_section_number = section_number;
  169. for (auto const& argument : arguments)
  170. m_arguments.append({ argument });
  171. return {};
  172. }
  173. SpecParsingStep::SpecParsingStep()
  174. : CompilationStep("parser"sv)
  175. {
  176. }
  177. SpecParsingStep::~SpecParsingStep() = default;
  178. void SpecParsingStep::run(TranslationUnitRef translation_unit)
  179. {
  180. SpecificationParsingContext ctx(translation_unit);
  181. auto filename = translation_unit->filename();
  182. auto file_or_error = Core::File::open_file_or_standard_stream(filename, Core::File::OpenMode::Read);
  183. if (file_or_error.is_error()) {
  184. ctx.diag().fatal_error(Location::global_scope(),
  185. "unable to open '{}': {}", filename, file_or_error.error());
  186. return;
  187. }
  188. auto input_or_error = file_or_error.value()->read_until_eof();
  189. if (input_or_error.is_error()) {
  190. ctx.diag().fatal_error(Location::global_scope(),
  191. "unable to read '{}': {}", filename, input_or_error.error());
  192. return;
  193. }
  194. m_input = input_or_error.release_value();
  195. XML::Parser parser { m_input };
  196. auto document_or_error = parser.parse();
  197. if (document_or_error.is_error()) {
  198. ctx.diag().fatal_error(ctx.file_scope(),
  199. "XML::Parser failed to parse input: {}", document_or_error.error());
  200. ctx.diag().note(ctx.file_scope(),
  201. "since XML::Parser backtracks on error, the message above is likely to point to the "
  202. "first tag in the input - use external XML verifier to find out the exact cause of error");
  203. return;
  204. }
  205. m_document = make<XML::Document>(document_or_error.release_value());
  206. auto spec_function = SpecFunction::create(&m_document->root()).release_value_but_fixme_should_propagate_errors();
  207. Vector<FunctionArgument> arguments;
  208. for (auto const& argument : spec_function.m_arguments)
  209. arguments.append({ argument.name });
  210. translation_unit->adopt_function(
  211. make_ref_counted<FunctionDefinition>(spec_function.m_name, spec_function.m_algorithm.m_tree, move(arguments)));
  212. }
  213. }