SpecParser.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/OwnPtr.h>
  8. #include "AST/AST.h"
  9. #include "CompilationPipeline.h"
  10. #include "Forward.h"
  11. #include "Parser/ParseError.h"
  12. #include "Parser/Token.h"
  13. namespace JSSpecCompiler {
  14. class SpecificationParsingContext {
  15. AK_MAKE_NONCOPYABLE(SpecificationParsingContext);
  16. AK_MAKE_NONMOVABLE(SpecificationParsingContext);
  17. public:
  18. SpecificationParsingContext(TranslationUnitRef translation_unit)
  19. : m_translation_unit(translation_unit)
  20. {
  21. }
  22. DiagnosticEngine& diag();
  23. template<typename Func>
  24. auto with_new_logical_scope(Func&& func);
  25. LogicalLocation& current_logical_scope();
  26. Location file_scope() const;
  27. Location location_from_xml_offset(XML::Offset offset) const;
  28. private:
  29. TranslationUnitRef m_translation_unit;
  30. RefPtr<LogicalLocation> m_current_logical_scope;
  31. };
  32. class AlgorithmStepList {
  33. public:
  34. static ParseErrorOr<AlgorithmStepList> create(XML::Node::Element const& element);
  35. Vector<AlgorithmStep> m_steps;
  36. Tree m_expression = error_tree;
  37. };
  38. class AlgorithmStep {
  39. public:
  40. static ParseErrorOr<AlgorithmStep> create(XML::Node const* node);
  41. ParseErrorOr<Tree> parse();
  42. Tree m_expression = error_tree;
  43. Vector<Token> m_tokens;
  44. NullableTree m_substeps;
  45. XML::Node const* m_node;
  46. };
  47. class Algorithm {
  48. public:
  49. static ParseErrorOr<Algorithm> create(XML::Node const* node);
  50. AlgorithmStepList m_steps;
  51. Tree m_tree = error_tree;
  52. };
  53. class SpecFunction {
  54. public:
  55. struct Argument {
  56. StringView name;
  57. };
  58. static ParseErrorOr<SpecFunction> create(XML::Node const* element);
  59. ParseErrorOr<void> parse_definition(XML::Node const* element);
  60. StringView m_section_number;
  61. StringView m_id;
  62. StringView m_name;
  63. Vector<Argument> m_arguments;
  64. Algorithm m_algorithm;
  65. };
  66. class SpecParsingStep : public CompilationStep {
  67. public:
  68. SpecParsingStep();
  69. ~SpecParsingStep();
  70. void run(TranslationUnitRef translation_unit) override;
  71. private:
  72. OwnPtr<XML::Document> m_document;
  73. ByteBuffer m_input;
  74. };
  75. }