-
Notifications
You must be signed in to change notification settings - Fork 5
/
parser_helper.cpp
99 lines (85 loc) · 2.44 KB
/
parser_helper.cpp
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
94
95
96
97
98
99
#include "parser_helper.h"
using namespace std;
const char* ParserException::what() const noexcept {
cout << msg << " at " << loc << endl;
return msg.c_str();
}
void SymbolTable::enterScope() {
scope++;
if (scope >= symbols.capacity()) {
symbols.push_back({});
}
}
void SymbolTable::leaveScope() {
if (scope >= 0) {
symbols[scope].clear();
scope--;
}
}
void SymbolTable::newSymbol(string name, shared_ptr<DeclAST> ast) {
if (scope >= 0) {
symbols.at((unsigned) scope).push_back(make_pair(name, ast));
}
}
shared_ptr<DeclAST> SymbolTable::findSymbol(string name) {
for (int i = scope; i > 0; i--) {
auto sv = symbols[i];
for (pair<string, shared_ptr<DeclAST>> p : sv) {
if (p.first == name) {
return p.second;
}
}
};
return nullptr;
}
Tokenizer::Tokenizer(shared_ptr<Lexer> _lexer) : lexer(nullptr), currentToken(nullptr) {
lexer = _lexer;
pop(); // Prime the pump
};
void Tokenizer::push(shared_ptr<Token> token) {
tokens.push(currentToken);
currentToken = token;
}
shared_ptr<Token> Tokenizer::pop() {
shared_ptr<Token> old = currentToken;
if (tokens.empty()) {
currentToken = lexer->nextToken();
} else {
currentToken = tokens.top();
tokens.pop();
}
return old;
}
shared_ptr<Token> Tokenizer::peek() {
return currentToken;
}
shared_ptr<Token> Tokenizer::popAny(vector<Token::Kind> kinds) {
string str_kinds;
for (Token::Kind kind : kinds) {
if (currentToken->getKind() == kind) {
return pop();
}
str_kinds += kind_to_string(kind) + ", ";
}
throw ParserException("Expected one of " + str_kinds + "got " + kind_to_string(currentToken->getKind()), currentToken->getLocation());
}
shared_ptr<Token> Tokenizer::pop(Token::Kind kind) {
if (currentToken->getKind() == kind) {
return pop();
}
throw ParserException("Expected " + kind_to_string(kind) + ", got " + kind_to_string(currentToken->getKind()), currentToken->getLocation());
}
shared_ptr<Token> Tokenizer::peekAny(vector<Token::Kind> kinds) {
for (Token::Kind kind : kinds) {
if (currentToken->getKind() == kind) {
return currentToken;
}
}
return nullptr;
}
shared_ptr<Token> Tokenizer::peek(Token::Kind kind) {
if (currentToken->getKind() == kind) {
return currentToken;
}
return nullptr;
}