TextParser.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. class TextParser {
  21. public:
  22. TextParser(Vector<Token>& tokens_, XML::Node const* node_)
  23. : m_tokens(tokens_)
  24. , m_node(node_)
  25. {
  26. }
  27. ParseErrorOr<ClauseHeader> parse_clause_header();
  28. ParseErrorOr<Tree> parse_step_without_substeps();
  29. ParseErrorOr<Tree> parse_step_with_substeps(Tree substeps);
  30. private:
  31. struct IfConditionParseResult {
  32. bool is_if_branch;
  33. NullableTree condition;
  34. };
  35. void retreat();
  36. [[nodiscard]] auto rollback_point();
  37. ParseErrorOr<Token const*> peek_token();
  38. ParseErrorOr<Token const*> consume_token();
  39. ParseErrorOr<Token const*> consume_token_with_one_of_types(std::initializer_list<TokenType> types);
  40. ParseErrorOr<Token const*> consume_token_with_type(TokenType type);
  41. ParseErrorOr<void> consume_word(StringView word);
  42. ParseErrorOr<void> consume_words(std::initializer_list<StringView> words);
  43. bool is_eof() const;
  44. ParseErrorOr<void> expect_eof() const;
  45. ParseErrorOr<Tree> parse_record_direct_list_initialization();
  46. ParseErrorOr<Tree> parse_expression();
  47. ParseErrorOr<Tree> parse_condition();
  48. ParseErrorOr<Tree> parse_return_statement();
  49. ParseErrorOr<Tree> parse_assert();
  50. ParseErrorOr<Tree> parse_assignment();
  51. ParseErrorOr<Tree> parse_simple_step_or_inline_if_branch();
  52. ParseErrorOr<IfConditionParseResult> parse_if_beginning();
  53. ParseErrorOr<Tree> parse_inline_if_else();
  54. ParseErrorOr<Tree> parse_if(Tree then_branch);
  55. ParseErrorOr<Tree> parse_else(Tree else_branch);
  56. Vector<Token> const& m_tokens;
  57. size_t m_next_token_index = 0;
  58. XML::Node const* m_node;
  59. };
  60. }