/* * Copyright (c) 2023, Dan Klishch * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include "AST/AST.h" #include "Function.h" #include "Parser/ParseError.h" #include "Parser/Token.h" namespace JSSpecCompiler { struct ClauseHeader { struct FunctionDefinition { StringView name; Vector arguments; }; StringView section_number; Variant header; }; class TextParser { public: TextParser(Vector& tokens_, XML::Node const* node_) : m_tokens(tokens_) , m_node(node_) { } ParseErrorOr parse_clause_header(); ParseErrorOr parse_step_without_substeps(); ParseErrorOr parse_step_with_substeps(Tree substeps); private: struct IfConditionParseResult { bool is_if_branch; NullableTree condition; }; void retreat(); [[nodiscard]] auto rollback_point(); ParseErrorOr peek_token(); ParseErrorOr consume_token(); ParseErrorOr consume_token_with_one_of_types(std::initializer_list types); ParseErrorOr consume_token_with_type(TokenType type); ParseErrorOr consume_word(StringView word); ParseErrorOr consume_words(std::initializer_list words); bool is_eof() const; ParseErrorOr expect_eof() const; ParseErrorOr parse_record_direct_list_initialization(); ParseErrorOr parse_expression(); ParseErrorOr parse_condition(); ParseErrorOr parse_return_statement(); ParseErrorOr parse_assert(); ParseErrorOr parse_assignment(); ParseErrorOr parse_simple_step_or_inline_if_branch(); ParseErrorOr parse_if_beginning(); ParseErrorOr parse_inline_if_else(); ParseErrorOr parse_if(Tree then_branch); ParseErrorOr parse_else(Tree else_branch); Vector const& m_tokens; size_t m_next_token_index = 0; XML::Node const* m_node; }; }