TextParser.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. struct AbstractOperation {
  13. StringView name;
  14. Vector<FunctionArgument> arguments;
  15. };
  16. struct Accessor {
  17. Vector<StringView> qualified_name;
  18. };
  19. StringView section_number;
  20. Variant<AK::Empty, AbstractOperation, Accessor> header;
  21. };
  22. struct TextParseError { };
  23. struct FailedTextParseDiagnostic {
  24. Location location;
  25. String message;
  26. };
  27. template<typename T>
  28. using TextParseErrorOr = ErrorOr<T, TextParseError>;
  29. class TextParser {
  30. public:
  31. TextParser(SpecificationParsingContext& ctx, Vector<Token> const& tokens, XML::Node const* node)
  32. : m_ctx(ctx)
  33. , m_tokens(tokens)
  34. , m_node(node)
  35. {
  36. }
  37. TextParseErrorOr<ClauseHeader> parse_clause_header();
  38. TextParseErrorOr<Tree> parse_step_without_substeps();
  39. TextParseErrorOr<Tree> parse_step_with_substeps(Tree substeps);
  40. FailedTextParseDiagnostic get_diagnostic() const;
  41. private:
  42. struct IfConditionParseResult {
  43. bool is_if_branch;
  44. NullableTree condition;
  45. };
  46. struct CustomMessage {
  47. StringView message;
  48. };
  49. void save_error(Variant<TokenType, StringView, CustomMessage>&& expected);
  50. void retreat();
  51. [[nodiscard]] auto rollback_point();
  52. Optional<Token> peek_token();
  53. Optional<Token> consume_token();
  54. TextParseErrorOr<Token> consume_token_with_one_of_types(std::initializer_list<TokenType> types);
  55. TextParseErrorOr<Token> consume_token_with_type(TokenType type);
  56. TextParseErrorOr<void> consume_token(TokenType type, StringView data);
  57. TextParseErrorOr<void> consume_word(StringView word);
  58. TextParseErrorOr<void> consume_words(std::initializer_list<StringView> words);
  59. bool is_eof() const;
  60. TextParseErrorOr<void> expect_eof();
  61. TextParseErrorOr<Tree> parse_record_direct_list_initialization();
  62. TextParseErrorOr<Vector<Tree>> parse_function_arguments();
  63. TextParseErrorOr<Tree> parse_list_initialization();
  64. TextParseErrorOr<Tree> parse_the_this_value();
  65. TextParseErrorOr<Tree> parse_value();
  66. TextParseErrorOr<Tree> parse_expression();
  67. TextParseErrorOr<Tree> parse_condition();
  68. TextParseErrorOr<Tree> parse_return_statement();
  69. TextParseErrorOr<Tree> parse_assert();
  70. TextParseErrorOr<Tree> parse_assignment();
  71. TextParseErrorOr<Tree> parse_perform();
  72. TextParseErrorOr<Tree> parse_simple_step_or_inline_if_branch();
  73. TextParseErrorOr<IfConditionParseResult> parse_if_beginning();
  74. TextParseErrorOr<Tree> parse_inline_if_else();
  75. TextParseErrorOr<Tree> parse_if(Tree then_branch);
  76. TextParseErrorOr<Tree> parse_else(Tree else_branch);
  77. TextParseErrorOr<Vector<StringView>> parse_qualified_name();
  78. TextParseErrorOr<Vector<FunctionArgument>> parse_function_arguments_in_declaration();
  79. TextParseErrorOr<ClauseHeader::AbstractOperation> parse_abstract_operation_declaration();
  80. TextParseErrorOr<ClauseHeader::Accessor> parse_accessor_declaration();
  81. SpecificationParsingContext& m_ctx;
  82. Vector<Token> const& m_tokens;
  83. size_t m_next_token_index = 0;
  84. XML::Node const* m_node;
  85. size_t m_max_parsed_tokens = 0;
  86. Vector<Variant<TokenType, StringView, CustomMessage>, 8> m_suitable_continuations;
  87. };
  88. }