-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.hpp
93 lines (70 loc) · 2.21 KB
/
parser.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#ifndef _PARSER_HPP_
#define _PARSER_HPP_
#include "ast.hpp"
#include "token.hpp"
#include "errors_holder_trait.hpp"
#include <vector>
#include <format>
#include <utility>
#include <optional>
#include <string_view>
#include <initializer_list>
namespace pl0::parser {
using namespace error;
using namespace token;
using namespace ast;
class Parser final : public ErrorsHolderTrait {
public:
explicit Parser(std::vector<Token>& tokens)
: m_tokens(std::move(tokens)),
m_curr(0),
m_panicMode(false) {}
auto parseProgram() -> StatementPtr;
private:
auto block() -> StatementPtr;
auto constDeclarations() -> StatementPtr;
auto variableDeclarations() -> StatementPtr;
auto procedureDeclaration() -> StatementPtr;
auto statement() -> StatementPtr;
auto assignStatement() -> StatementPtr;
auto callStatement() -> StatementPtr;
auto inputStatement() -> StatementPtr;
auto printStatement() -> StatementPtr;
auto beginStatement() -> StatementPtr;
auto ifStatement() -> StatementPtr;
auto whileStatement() -> StatementPtr;
auto condition() -> ExpressionPtr;
auto expression() -> ExpressionPtr;
auto termExpression() -> ExpressionPtr;
auto factorExpression() -> ExpressionPtr;
auto convertToInteger(Token name) -> std::optional<int>;
[[nodiscard]]
inline auto previous() const -> const Token& {
return m_tokens[m_curr - 1];
}
[[nodiscard]]
inline auto current() const -> const Token& {
return m_tokens[m_curr];
}
constexpr auto isAtEnd() const -> bool {
return m_curr >= m_tokens.size();
}
inline auto advance() -> void {
if(isAtEnd()) return;
m_curr++;
}
auto match(const std::initializer_list<TokenType>& types) -> bool;
auto consume(TokenType type, const char* message) -> std::optional<Token>;
auto synchronize() -> void;
template<typename... Args>
inline auto error(std::string_view fmt, Args&&... args) -> void {
pushError(std::vformat(fmt, std::make_format_args(args...)));
m_panicMode = true;
}
private:
std::vector<Token> m_tokens;
std::uint32_t m_curr;
bool m_panicMode;
};
}
#endif