-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.h
More file actions
54 lines (46 loc) · 1.24 KB
/
Parser.h
File metadata and controls
54 lines (46 loc) · 1.24 KB
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
//
// Created by ryousuke kaga on 2023/11/09.
//
#ifndef INTERPRETER_IN_CPP_PARSER_H
#define INTERPRETER_IN_CPP_PARSER_H
#include "Token.h"
#include "Expr.h"
#include "Interpreter.h"
#include "Stmt.h"
class ParserError : public std::runtime_error {
std::string what_message;
public:
ParserError(const std::string& msg) : runtime_error(msg) {}
const char* what() const noexcept override
{
return what_message.c_str();
}
};
class Parser {
public:
Parser(std::vector<Token>& tokens): tokens(tokens) {}
std::vector<std::unique_ptr<Stmt>> parse();
private:
Expr* expression();
Expr* equality();
Expr* comparison();
Expr* term();
Expr* factor();
Expr* unary();
Expr* primary();
Token* advance();
Token* peek();
Token* previous();
Token* consume(TokenType type, char* message);
bool match(std::initializer_list<TokenType> types);
bool check(TokenType type);
bool isAtEnd();
ParserError error(Token* token, const char* message);
void synchronize();
std::unique_ptr<Stmt> statement();
std::unique_ptr<Stmt> printStatement();
std::unique_ptr<Stmt> expressionStatement();
std::vector<Token> tokens;
int current = 0;
};
#endif //INTERPRETER_IN_CPP_PARSER_H