TextParser.h 2.8 KB

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