Webserv
Loading...
Searching...
No Matches
Parser.cpp
Go to the documentation of this file.
1#include "webserv.hpp"
2
3JSONParser::JSONParser(const string filename) : tokenizer(filename) {}
4
6 string key = "";
7 JSONNode *parsed;
8 while (tokenizer.hasMoreTokens()) {
9 Token token;
10 try {
11 token = tokenizer.getToken();
12 switch (token.type) {
13 case CURLY_OPEN: {
14 parsed = parseObject();
15 break;
16 }
17 case ARRAY_OPEN: {
18 parsed = parseList();
19 break;
20 }
21 case STRING: {
23 parsed = parseString();
24 break;
25 }
26 case NUMBER: {
28 parsed = parseNumber();
29 break;
30 }
31 case BOOLEAN: {
33 parsed = parseBoolean();
34 break;
35 }
36 default:
37 break;
38 }
39 } catch (std::logic_error &e) {
40 break;
41 }
42 }
43 return parsed;
44}
45
47 JSONNode *node = new JSONNode;
48 JSONObject *keyObjectMap = new JSONObject;
49 while (1) {
51 Token token = tokenizer.getToken();
52 string key = token.value;
54 token = tokenizer.getToken();
55 switch (token.type) {
56 case CURLY_OPEN: {
57 if (DEBUG)
58 cout << "=object=|" << key << "|===>>\n";
59 (*keyObjectMap)[key] = parseObject();
60 if (DEBUG)
61 cout << "<<===end object\n";
62 break;
63 }
64 case ARRAY_OPEN: {
65 if (DEBUG)
66 cout << "-list-|" << key << "|--->>\n";
67 (*keyObjectMap)[key] = parseList();
68 if (DEBUG)
69 cout << "<<---end list\n";
70 break;
71 }
72 case STRING: {
74 (*keyObjectMap)[key] = parseString();
75 if (DEBUG)
76 cout << key << "='" << (*keyObjectMap)[key]->str() << "'\n";
77 break;
78 }
79 case NUMBER: {
81 (*keyObjectMap)[key] = parseNumber();
82 if (DEBUG)
83 cout << key << "=" << (*keyObjectMap)[key]->nbr() << "\n";
84 break;
85 }
86 case BOOLEAN: {
88 (*keyObjectMap)[key] = parseBoolean();
89 if (DEBUG)
90 cout << key << "(BOOL)\n";
91 break;
92 }
93 case NULL_TYPE: {
94 (*keyObjectMap)[key] = parseNull();
95 break;
96 }
97 default:
98 break;
99 }
100 token = tokenizer.getToken();
101 if (token.type == CURLY_CLOSE)
102 break;
103
104 } else {
105 throw std::logic_error("No more tokens");
106 }
107 }
108 node->setObject(keyObjectMap);
109 return node;
110}
112 JSONNode *node = new JSONNode();
113 JSONList *list = new JSONList();
114 bool hasCompleted = false;
115 while (!hasCompleted) {
116 if (!tokenizer.hasMoreTokens()) {
117 throw std::logic_error("No more tokens");
118 } else {
119 Token token = tokenizer.getToken();
120 JSONNode *subNode;
121 switch (token.type) {
122 case CURLY_OPEN: {
123 if (DEBUG)
124 cout << "=object===>>\n";
125 subNode = parseObject();
126 if (DEBUG)
127 cout << "<<===end object\n";
128 break;
129 }
130 case ARRAY_OPEN: {
131 if (DEBUG)
132 cout << "-list--->>\n";
133 subNode = parseList();
134 if (DEBUG)
135 cout << "<<---end list\n";
136 break;
137 }
138 case STRING: {
140 subNode = parseString();
141 if (DEBUG)
142 cout << "|'" << subNode->str() << "'";
143 break;
144 }
145 case NUMBER: {
147 subNode = parseNumber();
148 if (DEBUG)
149 cout << "|" << subNode->nbr();
150 break;
151 }
152 case BOOLEAN: {
154 subNode = parseBoolean();
155 break;
156 }
157 case NULL_TYPE: {
158 subNode = parseNull();
159 break;
160 }
161 default:
162 break;
163 }
164 list->push_back(subNode);
165 token = tokenizer.getToken();
166 if (token.type == ARRAY_CLOSE) {
167 hasCompleted = true;
168 }
169 }
170 }
171 node->setList(list);
172 return node;
173}
175 JSONNode *node = new JSONNode();
176 Token token = tokenizer.getToken();
177 string *sValue = new string(token.value);
178 node->setString(sValue);
179 return node;
180}
182 JSONNode *node = new JSONNode();
183 Token token = tokenizer.getToken();
184 string value = token.value;
185 int nbr = std::atoi(value.c_str());
186 node->setNumber(nbr);
187 return node;
188}
190 JSONNode *node = new JSONNode();
191 Token token = tokenizer.getToken();
192 node->setBoolean(token.value == "True" ? true : false);
193 return node;
194}
196 JSONNode *node = new JSONNode();
197 node->setNull();
198 return node;
199}
@ NUMBER
Definition: Token.hpp:9
@ CURLY_OPEN
Definition: Token.hpp:5
@ ARRAY_OPEN
Definition: Token.hpp:10
@ NULL_TYPE
Definition: Token.hpp:14
@ BOOLEAN
Definition: Token.hpp:13
@ ARRAY_CLOSE
Definition: Token.hpp:11
@ CURLY_CLOSE
Definition: Token.hpp:6
@ STRING
Definition: Token.hpp:8
void setObject(JSONObject *object)
Definition: Nodes.cpp:29
int nbr()
Definition: Nodes.cpp:18
void setNumber(int nbr)
Definition: Nodes.cpp:41
void setNull()
Definition: Nodes.cpp:49
void setBoolean(bool v)
Definition: Nodes.cpp:45
string str()
Definition: Nodes.cpp:13
void setString(string *str)
Definition: Nodes.cpp:37
void setList(JSONList *list)
Definition: Nodes.cpp:33
Tokenizer tokenizer
Definition: Parser.hpp:6
JSONNode * parseString()
Definition: Parser.cpp:174
JSONNode * parseNumber()
Definition: Parser.cpp:181
JSONParser(const string filename)
Definition: Parser.cpp:3
JSONNode * parseList()
Definition: Parser.cpp:111
JSONNode * parseNull()
Definition: Parser.cpp:195
JSONNode * parse()
Definition: Parser.cpp:5
JSONNode * parseObject()
Definition: Parser.cpp:46
JSONNode * parseBoolean()
Definition: Parser.cpp:189
void rollBackToken()
Definition: Token.cpp:26
bool hasMoreTokens()
Definition: Token.cpp:9
Token getToken()
Definition: Token.cpp:31
string value
Definition: Token.hpp:18
TOKEN type
Definition: Token.hpp:19
std::vector< JSONNode * > JSONList
Definition: webserv.hpp:49
#define DEBUG
Definition: webserv.hpp:2
std::map< string, JSONNode * > JSONObject
Definition: webserv.hpp:48