-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
117 lines (104 loc) · 3.38 KB
/
parser.cpp
File metadata and controls
117 lines (104 loc) · 3.38 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "parser.h"
#include "types.h"
#include "AST.h"
#include <iostream>
#include <memory>
Parser::Parser(Lexer lexer) {
this->lexer = lexer;
this->currentToken = this->lexer.getNextToken();
}
void Parser::error(std::string msg) {
std::cout << msg << std::endl;
exit(-1);
}
void Parser::eat(std::string type) {
if(this->currentToken.type == type)
this->currentToken = this->lexer.getNextToken();
else
this->error("Expected type: \"" + type + "\" got type: \"" + this->currentToken.type + '"');
}
std::shared_ptr<Value> Parser::value() {
Token tok = this->currentToken;
if(tok.type == STRING) {
this->eat(STRING);
auto stringPtr = std::make_shared<String>(String(tok.value));
return stringPtr;
}
if(tok.type == NUMBER) {
this->eat(NUMBER);
auto numPtr = std::make_shared<Number>(Number(std::stof(tok.value)));
return numPtr;
}
// first token type for object
if(tok.type == LCURL) {
std::shared_ptr<Value> objectPtr = this->object();
return objectPtr;
}
// first token type for array
if(tok.type == LSQUARE) {
std::shared_ptr<Value> arrayPtr = this->array();
return arrayPtr;
}
if(tok.type == BOOLEAN) {
this->eat(BOOLEAN);
std::shared_ptr<Boolean> stringPtr = std::make_shared<Boolean>(Boolean(tok.value == "true" ? true : false));
return stringPtr;
}
if(tok.type == _NULL) {
this->eat(_NULL);
std::shared_ptr<Null> nullPtr = std::make_shared<Null>();
return nullPtr;
}
throw std::runtime_error("Something went wrong in Parser::value()");
}
std::vector<std::shared_ptr<Value>> Parser::arrayList() {
std::vector<std::shared_ptr<Value>> values;
std::string tokType = this->currentToken.type;
if(tokType == STRING || tokType == NUMBER ||
tokType == LCURL || tokType == LSQUARE ||
tokType == BOOLEAN || tokType == _NULL) {
values.push_back(this->value());
while(this->currentToken.type == COMMA) {
this->eat(COMMA);
for(auto elem : this->arrayList())
values.push_back(elem);
}
}
return values;
}
std::shared_ptr<Value> Parser::array() {
this->eat(LSQUARE);
auto values = this->arrayList();
this->eat(RSQUARE);
auto arrayPtr = std::make_shared<Array>(Array(values));
// arrayPtr->values = values;
return std::dynamic_pointer_cast<Value>(arrayPtr);
}
std::vector<std::shared_ptr<Assign>> Parser::objectList() {
auto left = std::make_shared<String>(String(this->currentToken.value));
this->eat(STRING);
this->eat(COLON);
std::vector<std::shared_ptr<Assign>> values = { std::make_shared<Assign>(Assign(left, this->value())) };
while(this->currentToken.type == COMMA) {
this->eat(COMMA);
for(auto elem : this->objectList())
values.push_back(elem);
}
return values;
}
std::shared_ptr<Value> Parser::object() {
this->eat(LCURL);
auto values = this->objectList();
this->eat(RCURL);
auto objPtr = std::make_shared<Object>();
objPtr->values = values;
return objPtr;
}
std::shared_ptr<AST> Parser::ast() {
std::string tokType = this->currentToken.type;
if(tokType == LCURL)
return this->object();
if(tokType == LSQUARE)
return this->array();
throw std::runtime_error("Invalid JSON");
}