TextParser.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "AST/AST.h"
  8. #include "Function.h"
  9. #include "Parser/Token.h"
  10. namespace JSSpecCompiler {
  11. struct ClauseHeader {
  12. StringView section_number;
  13. Variant<AK::Empty, AbstractOperationDeclaration, AccessorDeclaration, MethodDeclaration> header;
  14. };
  15. struct TextParseError { };
  16. struct FailedTextParseDiagnostic {
  17. Location location;
  18. String message;
  19. };
  20. template<typename T>
  21. using TextParseErrorOr = ErrorOr<T, TextParseError>;
  22. class TextParser {
  23. public:
  24. enum class ClauseHasAoidAttribute {
  25. No,
  26. Yes,
  27. };
  28. TextParser(SpecificationParsingContext& ctx, Vector<Token> const& tokens, XML::Node const* node)
  29. : m_ctx(ctx)
  30. , m_tokens(tokens)
  31. , m_node(node)
  32. {
  33. }
  34. TextParseErrorOr<ClauseHeader> parse_clause_header(ClauseHasAoidAttribute clause_has_aoid_attribute);
  35. TextParseErrorOr<NullableTree> parse_step_without_substeps();
  36. TextParseErrorOr<Tree> parse_step_with_substeps(Tree substeps);
  37. FailedTextParseDiagnostic get_diagnostic() const;
  38. private:
  39. struct IfConditionParseResult {
  40. bool is_if_branch;
  41. NullableTree condition;
  42. };
  43. struct CustomMessage {
  44. StringView message;
  45. };
  46. void save_error(Variant<TokenType, StringView, CustomMessage>&& expected);
  47. void retreat();
  48. [[nodiscard]] auto rollback_point();
  49. Optional<Token> peek_token();
  50. Optional<Token> consume_token();
  51. TextParseErrorOr<Token> consume_token_with_one_of_types(std::initializer_list<TokenType> types);
  52. TextParseErrorOr<Token> consume_token_with_type(TokenType type);
  53. TextParseErrorOr<void> consume_token(TokenType type, StringView data);
  54. TextParseErrorOr<void> consume_word(StringView word);
  55. TextParseErrorOr<void> consume_words(std::initializer_list<StringView> words);
  56. bool is_eof() const;
  57. TextParseErrorOr<void> expect_eof();
  58. TextParseErrorOr<Tree> parse_record_direct_list_initialization();
  59. TextParseErrorOr<Vector<Tree>> parse_function_arguments();
  60. TextParseErrorOr<Tree> parse_list_initialization();
  61. TextParseErrorOr<Tree> parse_the_this_value();
  62. TextParseErrorOr<Tree> parse_value();
  63. TextParseErrorOr<Tree> parse_expression();
  64. TextParseErrorOr<Tree> parse_condition();
  65. TextParseErrorOr<Tree> parse_return_statement();
  66. TextParseErrorOr<Tree> parse_assert();
  67. TextParseErrorOr<Tree> parse_assignment();
  68. TextParseErrorOr<Tree> parse_perform();
  69. TextParseErrorOr<Tree> parse_simple_step_or_inline_if_branch();
  70. TextParseErrorOr<IfConditionParseResult> parse_if_beginning();
  71. TextParseErrorOr<Tree> parse_inline_if_else();
  72. TextParseErrorOr<Tree> parse_if(Tree then_branch);
  73. TextParseErrorOr<Tree> parse_else(Tree else_branch);
  74. TextParseErrorOr<QualifiedName> parse_qualified_name();
  75. TextParseErrorOr<Vector<FunctionArgument>> parse_function_arguments_in_declaration();
  76. TextParseErrorOr<AbstractOperationDeclaration> parse_abstract_operation_declaration();
  77. TextParseErrorOr<MethodDeclaration> parse_method_declaration();
  78. TextParseErrorOr<AccessorDeclaration> parse_accessor_declaration();
  79. SpecificationParsingContext& m_ctx;
  80. Vector<Token> const& m_tokens;
  81. size_t m_next_token_index = 0;
  82. XML::Node const* m_node;
  83. size_t m_max_parsed_tokens = 0;
  84. Vector<Variant<TokenType, StringView, CustomMessage>, 8> m_suitable_continuations;
  85. };
  86. }