Skip to content

Commit e18be13

Browse files
feat(parser): add error handling and diagnostics support
1 parent fb85656 commit e18be13

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,68 @@
1-
#include "wheel_lexer/kind.hxx"
21
#if !defined(PARSER_HXX)
32
#define PARSER_HXX
43

4+
#if !defined (WHEEL_EXPERIMENT) && !defined (WHEEL_SMALL_VEC)
5+
#include <vector>
6+
#endif
7+
58
#include <string_view>
69
#include "config.hxx"
710
#include <wheel_lexer/lexer.hxx>
811
#include <wheel_lexer/token.hxx>
912
#include <wheel_memory/allocator.hxx>
13+
#include <wheel_memory/vec.hxx>
14+
#include "wheel_parser/error.hxx"
1015
#include "wheel_parser/ast/nodes.hxx"
1116
#include "wheel_parser/ast/symbol.hxx"
17+
#include "wheel_parser/ast/keywords.hxx"
1218

1319
using wheel_lexer::Token;
1420
using wheel_lexer::TokenKind;
1521
using wheel_lexer::Lexer;
1622
using wheel_memory::Arena;
17-
using wheel_parser::ast::VariableDeclaration;
23+
using wheel_parser::ast::StatementNode;
1824
using wheel_parser::ast::StringInterner;
25+
using wheel_parser::ast::Keyword;
1926
using std::string_view;
2027

2128
WHEEL_PARSER_NAMESPACE
29+
#if defined(WHEEL_EXPERIMENT) && defined(WHEEL_SMALL_VEC)
30+
using ParserErrorList = wheel_memory::SmallVec<ParseError>;
31+
#else
32+
using ParserErrorList = std::vector<ParseError>;
33+
#endif
34+
2235
class WheelParser {
2336
private:
2437
Lexer &m_lexer;
2538
Arena &m_arena;
2639
Token m_current_token;
2740
StringInterner &m_interner;
41+
ParserErrorList m_errors;
2842

2943
private:
3044
void next_token() noexcept {
3145
m_current_token = m_lexer.next_token();
3246
}
3347

34-
bool str_matches(string_view str) noexcept;
35-
bool token_matches(TokenKind token_kind) noexcept;
48+
bool token_matches(TokenKind token_kind) const noexcept;
49+
bool keyword_matches(Keyword keyword) const noexcept;
50+
void skip_spaces() noexcept;
51+
void synchronize_statement() noexcept;
52+
StatementNode *emit_error(ParseErrorCode code) noexcept;
3653

3754
const Token &consume() noexcept;
3855
const Token *copy_token() const noexcept;
3956

4057
public:
4158
explicit WheelParser(Lexer &lexer, Arena &arena, StringInterner &interner) noexcept;
42-
VariableDeclaration *parse_variable_declaration() noexcept;
59+
StatementNode *parse_variable_declaration() noexcept;
4360
void parse() noexcept;
61+
size_t error_count() const noexcept;
62+
const ParseError *errors_data() const noexcept;
63+
void clear_errors() noexcept;
64+
ParseDiagnosticList collect_diagnostics(std::string_view file_name = {}) const;
4465
};
4566

4667
WHEEL_PARSER_END_NAMESPACE
47-
#endif // PARSER_HXX
68+
#endif // PARSER_HXX

0 commit comments

Comments
 (0)