TextParser.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 "Parser/ParseError.h"
  9. #include "Parser/Token.h"
  10. namespace JSSpecCompiler {
  11. class TextParser {
  12. public:
  13. struct DefinitionParseResult {
  14. StringView section_number;
  15. StringView function_name;
  16. Vector<StringView> arguments;
  17. };
  18. TextParser(Vector<Token>& tokens_, XML::Node const* node_)
  19. : m_tokens(tokens_)
  20. , m_node(node_)
  21. {
  22. }
  23. ParseErrorOr<DefinitionParseResult> parse_definition();
  24. ParseErrorOr<Tree> parse_step_without_substeps();
  25. ParseErrorOr<Tree> parse_step_with_substeps(Tree substeps);
  26. private:
  27. struct IfConditionParseResult {
  28. bool is_if_branch;
  29. NullableTree condition;
  30. };
  31. void retreat();
  32. [[nodiscard]] auto rollback_point();
  33. ParseErrorOr<Token const*> peek_token();
  34. ParseErrorOr<Token const*> consume_token();
  35. ParseErrorOr<Token const*> consume_token_with_one_of_types(std::initializer_list<TokenType> types);
  36. ParseErrorOr<Token const*> consume_token_with_type(TokenType type);
  37. ParseErrorOr<void> consume_word(StringView word);
  38. ParseErrorOr<void> consume_words(std::initializer_list<StringView> words);
  39. bool is_eof() const;
  40. ParseErrorOr<void> expect_eof() const;
  41. ParseErrorOr<Tree> parse_record_direct_list_initialization();
  42. ParseErrorOr<Tree> parse_expression();
  43. ParseErrorOr<Tree> parse_condition();
  44. ParseErrorOr<Tree> parse_return_statement();
  45. ParseErrorOr<Tree> parse_assert();
  46. ParseErrorOr<Tree> parse_assignment();
  47. ParseErrorOr<Tree> parse_simple_step_or_inline_if_branch();
  48. ParseErrorOr<IfConditionParseResult> parse_if_beginning();
  49. ParseErrorOr<Tree> parse_inline_if_else();
  50. ParseErrorOr<Tree> parse_if(Tree then_branch);
  51. ParseErrorOr<Tree> parse_else(Tree else_branch);
  52. Vector<Token> const& m_tokens;
  53. size_t m_next_token_index = 0;
  54. XML::Node const* m_node;
  55. };
  56. }