TextParser.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 FunctionDefinition {
  13. StringView name;
  14. Vector<FunctionArgument> arguments;
  15. };
  16. StringView section_number;
  17. Variant<AK::Empty, FunctionDefinition> header;
  18. };
  19. struct TextParseError { };
  20. struct FailedTextParseDiagnostic {
  21. Location location;
  22. String message;
  23. };
  24. template<typename T>
  25. using TextParseErrorOr = ErrorOr<T, TextParseError>;
  26. class TextParser {
  27. public:
  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();
  35. TextParseErrorOr<Tree> 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<Tree> parse_expression();
  60. TextParseErrorOr<Tree> parse_condition();
  61. TextParseErrorOr<Tree> parse_return_statement();
  62. TextParseErrorOr<Tree> parse_assert();
  63. TextParseErrorOr<Tree> parse_assignment();
  64. TextParseErrorOr<Tree> parse_simple_step_or_inline_if_branch();
  65. TextParseErrorOr<IfConditionParseResult> parse_if_beginning();
  66. TextParseErrorOr<Tree> parse_inline_if_else();
  67. TextParseErrorOr<Tree> parse_if(Tree then_branch);
  68. TextParseErrorOr<Tree> parse_else(Tree else_branch);
  69. SpecificationParsingContext& m_ctx;
  70. Vector<Token> const& m_tokens;
  71. size_t m_next_token_index = 0;
  72. XML::Node const* m_node;
  73. size_t m_max_parsed_tokens = 0;
  74. Vector<Variant<TokenType, StringView, CustomMessage>, 8> m_suitable_continuations;
  75. };
  76. }