SpecificationParsingStep.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/SpecificationParsing.h"
  12. #include "Parser/TextParser.h"
  13. #include "Parser/XMLUtils.h"
  14. namespace JSSpecCompiler {
  15. SpecificationParsingStep::SpecificationParsingStep()
  16. : CompilationStep("parser"sv)
  17. {
  18. }
  19. SpecificationParsingStep::~SpecificationParsingStep() = default;
  20. void SpecificationParsingStep::run(TranslationUnitRef translation_unit)
  21. {
  22. SpecificationParsingContext ctx(translation_unit);
  23. auto filename = translation_unit->filename();
  24. auto file_or_error = Core::File::open_file_or_standard_stream(filename, Core::File::OpenMode::Read);
  25. if (file_or_error.is_error()) {
  26. ctx.diag().fatal_error(Location::global_scope(),
  27. "unable to open '{}': {}", filename, file_or_error.error());
  28. return;
  29. }
  30. auto input_or_error = file_or_error.value()->read_until_eof();
  31. if (input_or_error.is_error()) {
  32. ctx.diag().fatal_error(Location::global_scope(),
  33. "unable to read '{}': {}", filename, input_or_error.error());
  34. return;
  35. }
  36. m_input = input_or_error.release_value();
  37. XML::Parser parser { m_input };
  38. auto document_or_error = parser.parse();
  39. if (document_or_error.is_error()) {
  40. ctx.diag().fatal_error(ctx.file_scope(),
  41. "XML::Parser failed to parse input: {}", document_or_error.error());
  42. ctx.diag().note(ctx.file_scope(),
  43. "since XML::Parser backtracks on error, the message above is likely to point to the "
  44. "first tag in the input - use external XML verifier to find out the exact cause of error");
  45. return;
  46. }
  47. m_document = make<XML::Document>(document_or_error.release_value());
  48. auto const& root = m_document->root();
  49. if (!root.is_element() || root.as_element().name != tag_specification) {
  50. ctx.diag().fatal_error(ctx.location_from_xml_offset(root.offset),
  51. "document root must be <specification> tag");
  52. return;
  53. }
  54. m_specification = Specification::create(ctx, &root);
  55. m_specification->collect_into(translation_unit);
  56. }
  57. }