JSSpecCompiler: Issue meaningful errors in TextParser

This commit is contained in:
Dan Klishch 2024-01-17 01:36:39 -05:00 committed by Andrew Kaster
parent dee4978d67
commit 6b30c4d2f0
Notes: sideshowbarker 2024-07-17 00:49:59 +09:00
5 changed files with 272 additions and 147 deletions

View file

@ -23,17 +23,30 @@ struct ClauseHeader {
Variant<AK::Empty, FunctionDefinition> header;
};
struct TextParseError { };
struct FailedTextParseDiagnostic {
Location location;
String message;
};
template<typename T>
using TextParseErrorOr = ErrorOr<T, TextParseError>;
class TextParser {
public:
TextParser(Vector<Token>& tokens_, XML::Node const* node_)
: m_tokens(tokens_)
, m_node(node_)
TextParser(SpecificationParsingContext& ctx, Vector<Token> const& tokens, XML::Node const* node)
: m_ctx(ctx)
, m_tokens(tokens)
, m_node(node)
{
}
ParseErrorOr<ClauseHeader> parse_clause_header();
ParseErrorOr<Tree> parse_step_without_substeps();
ParseErrorOr<Tree> parse_step_with_substeps(Tree substeps);
TextParseErrorOr<ClauseHeader> parse_clause_header();
TextParseErrorOr<Tree> parse_step_without_substeps();
TextParseErrorOr<Tree> parse_step_with_substeps(Tree substeps);
FailedTextParseDiagnostic get_diagnostic() const;
private:
struct IfConditionParseResult {
@ -41,32 +54,43 @@ private:
NullableTree condition;
};
struct CustomMessage {
StringView message;
};
void save_error(Variant<TokenType, StringView, CustomMessage>&& expected);
void retreat();
[[nodiscard]] auto rollback_point();
ParseErrorOr<Token const*> peek_token();
ParseErrorOr<Token const*> consume_token();
ParseErrorOr<Token const*> consume_token_with_one_of_types(std::initializer_list<TokenType> types);
ParseErrorOr<Token const*> consume_token_with_type(TokenType type);
ParseErrorOr<void> consume_word(StringView word);
ParseErrorOr<void> consume_words(std::initializer_list<StringView> words);
Optional<Token> peek_token();
Optional<Token> consume_token();
TextParseErrorOr<Token> consume_token_with_one_of_types(std::initializer_list<TokenType> types);
TextParseErrorOr<Token> consume_token_with_type(TokenType type);
TextParseErrorOr<void> consume_token(TokenType type, StringView data);
TextParseErrorOr<void> consume_word(StringView word);
TextParseErrorOr<void> consume_words(std::initializer_list<StringView> words);
bool is_eof() const;
ParseErrorOr<void> expect_eof() const;
TextParseErrorOr<void> expect_eof();
ParseErrorOr<Tree> parse_record_direct_list_initialization();
ParseErrorOr<Tree> parse_expression();
ParseErrorOr<Tree> parse_condition();
ParseErrorOr<Tree> parse_return_statement();
ParseErrorOr<Tree> parse_assert();
ParseErrorOr<Tree> parse_assignment();
ParseErrorOr<Tree> parse_simple_step_or_inline_if_branch();
ParseErrorOr<IfConditionParseResult> parse_if_beginning();
ParseErrorOr<Tree> parse_inline_if_else();
ParseErrorOr<Tree> parse_if(Tree then_branch);
ParseErrorOr<Tree> parse_else(Tree else_branch);
TextParseErrorOr<Tree> parse_record_direct_list_initialization();
TextParseErrorOr<Tree> parse_expression();
TextParseErrorOr<Tree> parse_condition();
TextParseErrorOr<Tree> parse_return_statement();
TextParseErrorOr<Tree> parse_assert();
TextParseErrorOr<Tree> parse_assignment();
TextParseErrorOr<Tree> parse_simple_step_or_inline_if_branch();
TextParseErrorOr<IfConditionParseResult> parse_if_beginning();
TextParseErrorOr<Tree> parse_inline_if_else();
TextParseErrorOr<Tree> parse_if(Tree then_branch);
TextParseErrorOr<Tree> parse_else(Tree else_branch);
SpecificationParsingContext& m_ctx;
Vector<Token> const& m_tokens;
size_t m_next_token_index = 0;
XML::Node const* m_node;
size_t m_max_parsed_tokens = 0;
Vector<Variant<TokenType, StringView, CustomMessage>, 8> m_suitable_continuations;
};
}