54 lines
1.2 KiB
C++
54 lines
1.2 KiB
C++
#ifndef PARSER_HH
|
|
#define PARSER_HH
|
|
|
|
#include "expr.hh"
|
|
#include "stmt.hh"
|
|
#include "../tokenizer.hh"
|
|
#include <cstddef>
|
|
#include <exception>
|
|
#include <vector>
|
|
|
|
struct ParseException : public std::exception {
|
|
ParseException() : message{""} {}
|
|
ParseException(const std::string& message) : message{message} {}
|
|
const char* what() const noexcept override {
|
|
return message.c_str();
|
|
}
|
|
|
|
private:
|
|
std::string message;
|
|
};
|
|
|
|
struct Parser {
|
|
Parser(std::vector<Token> tokens) : tokens{tokens} {}
|
|
std::vector<Stmt> parse();
|
|
Stmt declaration();
|
|
Stmt var_declaration();
|
|
Stmt statement();
|
|
Stmt print_statement();
|
|
Stmt expression_statement();
|
|
Expr expression();
|
|
Expr assignment();
|
|
Expr equality();
|
|
Expr comparison();
|
|
Expr term();
|
|
Expr factor();
|
|
Expr unary();
|
|
Expr primary();
|
|
Token advance();
|
|
Token peek();
|
|
Token previous();
|
|
Token consume(TokenType type, std::string &&message);
|
|
bool match(std::vector<TokenType> types);
|
|
bool check(TokenType type);
|
|
bool is_at_end();
|
|
ParseException error(Token token, std::string &message);
|
|
ParseException error(Token token, std::string &&message);
|
|
void synchronize();
|
|
|
|
std::vector<Token> tokens;
|
|
size_t current = 0;
|
|
};
|
|
|
|
#endif
|