Algorithm.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (c) 2023-2024, Dan Klishch <danilklishch@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Parser/Lexer.h"
  7. #include "Parser/SpecificationParsing.h"
  8. #include "Parser/XMLUtils.h"
  9. namespace JSSpecCompiler {
  10. Optional<Algorithm> Algorithm::create(SpecificationParsingContext& ctx, XML::Node const* element)
  11. {
  12. VERIFY(element->as_element().name == tag_emu_alg);
  13. Vector<XML::Node const*> steps_list;
  14. for (auto const& child : element->as_element().children) {
  15. child->content.visit(
  16. [&](XML::Node::Element const& element) {
  17. if (element.name == tag_ol) {
  18. steps_list.append(child);
  19. return;
  20. }
  21. ctx.diag().error(ctx.location_from_xml_offset(child->offset),
  22. "<{}> should not be a child of <emu-alg>"sv, element.name);
  23. },
  24. [&](XML::Node::Text const&) {
  25. if (!contains_empty_text(child)) {
  26. ctx.diag().error(ctx.location_from_xml_offset(child->offset),
  27. "non-empty text node should not be a child of <emu-alg>");
  28. }
  29. },
  30. [&](auto const&) {});
  31. }
  32. if (steps_list.size() != 1) {
  33. ctx.diag().error(ctx.location_from_xml_offset(element->offset),
  34. "<emu-alg> should have exactly one <ol> child"sv);
  35. return {};
  36. }
  37. auto steps_creation_result = AlgorithmStepList::create(ctx, steps_list[0]);
  38. if (steps_creation_result.has_value()) {
  39. Algorithm algorithm;
  40. algorithm.m_tree = steps_creation_result.release_value().tree();
  41. return algorithm;
  42. }
  43. return {};
  44. }
  45. }