-
Notifications
You must be signed in to change notification settings - Fork 5
/
parser_helper.h
51 lines (44 loc) · 1.18 KB
/
parser_helper.h
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
#include <string>
#include <vector>
#include <stack>
#include <memory>
#include <exception>
#include "lexer.h"
#include "ast.h"
using std::shared_ptr;
using std::vector;
using std::pair;
using std::string;
class ParserException : public std::exception {
public:
ParserException(string msg, Location loc) : msg(msg), loc(loc) {}
virtual const char* what() const noexcept;
private:
string msg;
Location loc;
};
class SymbolTable {
public:
void enterScope();
void leaveScope();
void newSymbol(string name, shared_ptr<DeclAST> ast);
shared_ptr<DeclAST> findSymbol(string name);
private:
vector<vector<pair<string, shared_ptr<DeclAST>>>> symbols;
int scope = -1;
};
class Tokenizer {
public:
Tokenizer(shared_ptr<Lexer> _lexer);
void push(shared_ptr<Token> tok);
shared_ptr<Token> pop();
shared_ptr<Token> popAny(std::vector<Token::Kind> kinds);
shared_ptr<Token> pop(Token::Kind kind);
shared_ptr<Token> peek();
shared_ptr<Token> peekAny(std::vector<Token::Kind> kinds);
shared_ptr<Token> peek(Token::Kind kind);
private:
shared_ptr<Lexer> lexer;
shared_ptr<Token> currentToken;
std::stack<shared_ptr<Token>> tokens;
};