Skip to content

Commit 36f9d91

Browse files
authored
Merge pull request #14 from PANFACTORY/feature/token
Feature/token
2 parents a8e3e8c + 1798214 commit 36f9d91

3 files changed

Lines changed: 225 additions & 196 deletions

File tree

src/node.h

Lines changed: 99 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616
#include <iostream>
1717

1818
namespace JsonParser {
19+
enum class ValueType {
20+
Default,
21+
String,
22+
Number,
23+
BoolNull,
24+
Object,
25+
Array,
26+
};
27+
1928
/**
2029
* @brief Node of AST
2130
*
@@ -28,108 +37,116 @@ namespace JsonParser {
2837
*
2938
* @param[in] std::string _key JSON key this node contains
3039
*/
31-
Node(std::string _key) : children(0), children_array(0) {
40+
Node(std::string _key) : object(0), array(0) {
3241
this->key = _key;
3342
this->value = "";
34-
this->isvalueset = false;
35-
this->ischildrenset = false;
36-
this->isarrayset = false;
43+
this->valuetype = ValueType::Default;
3744
}
38-
Node(const Node& node) : children(0), children_array(0) {
45+
Node(const Node& node) : object(0), array(0) {
3946
this->key = node.key;
4047
this->value = node.value;
41-
this->isvalueset = node.isvalueset;
42-
this->ischildrenset = node.ischildrenset;
43-
this->isarrayset = node.isarrayset;
44-
for (auto node_child : node.children) {
45-
this->children.push_back(new Node(*node_child));
48+
this->valuetype = node.valuetype;
49+
for (auto child : node.object) {
50+
this->object.push_back(new Node(*child));
4651
}
47-
for (auto node_child : node.children_array) {
48-
this->children_array.push_back(new Node(*node_child));
52+
for (auto child : node.array) {
53+
this->array.push_back(new Node(*child));
4954
}
5055
}
5156
~Node() {
52-
for (auto child : this->children) {
57+
for (auto child : this->object) {
5358
delete child;
5459
}
55-
for (auto child : this->children_array) {
60+
for (auto child : this->array) {
5661
delete child;
5762
}
5863
}
5964
Node& operator=(const Node& node) {
60-
for (auto child : this->children) {
65+
for (auto child : this->object) {
6166
delete child;
6267
}
63-
for (auto child : this->children_array) {
68+
for (auto child : this->array) {
6469
delete child;
6570
}
6671
this->key = node.key;
6772
this->value = node.value;
68-
this->isvalueset = node.isvalueset;
69-
this->ischildrenset = node.ischildrenset;
70-
this->isarrayset = node.isarrayset;
71-
for (auto node_child : node.children) {
72-
this->children.push_back(new Node(*node_child));
73+
this->valuetype = node.valuetype;
74+
for (auto child : node.object) {
75+
this->object.push_back(new Node(*child));
7376
}
74-
for (auto node_child : node.children_array) {
75-
this->children_array.push_back(new Node(*node_child));
77+
for (auto child : node.array) {
78+
this->array.push_back(new Node(*child));
7679
}
7780
return *this;
7881
}
7982

8083
/**
81-
* @brief Set value to this node
84+
* @brief Set value to this node as String
8285
*
8386
* @param[in] std::string _value JSON value this node contains
8487
*/
85-
void SetValue(std::string _value) {
88+
void SetString(std::string _value) {
8689
this->value = _value;
87-
if (this->isvalueset) {
88-
throw std::runtime_error("At SetValue() in Node class: Value is set to node set value.");
89-
}
90-
this->isvalueset = true;
91-
if (this->ischildrenset) {
92-
throw std::runtime_error("At SetValue() in Node class: Value is set to node set object.");
90+
if (this->valuetype != ValueType::Default) {
91+
throw std::runtime_error("At SetString() in Node class: Some value is already set.");
9392
}
94-
if (this->isarrayset) {
95-
throw std::runtime_error("At SetValue() in Node class: Value is set to node set array.");
93+
this->valuetype = ValueType::String;
94+
}
95+
96+
/**
97+
* @brief Set value to this node as Number
98+
*
99+
* @param[in] std::string _value JSON value this node contains
100+
*/
101+
void SetNumber(std::string _value) {
102+
this->value = _value;
103+
if (this->valuetype != ValueType::Default) {
104+
throw std::runtime_error("At SetNumber() in Node class: Some value is already set.");
96105
}
106+
this->valuetype = ValueType::Number;
97107
}
98108

99109
/**
100-
* @brief Append a child node
110+
* @brief Set value to this node as Boolean or Null
101111
*
102-
* @param[in] JsonParser::Node* _node Pointer indicating a child node appended in this node
112+
* @param[in] std::string _value JSON value this node contains
103113
*/
104-
void AppendChild(Node* _node = nullptr) {
105-
if (_node != nullptr) {
106-
this->children.push_back(_node);
114+
void SetBoolNull(std::string _value) {
115+
this->value = _value;
116+
if (this->valuetype != ValueType::Default) {
117+
throw std::runtime_error("At SetBoolNull() in Node class: Some value is already set.");
107118
}
108-
this->ischildrenset = true;
109-
if (this->isvalueset) {
110-
throw std::runtime_error("At AppendChild() in Node class: Object is set to node set value.");
119+
this->valuetype = ValueType::BoolNull;
120+
}
121+
122+
/**
123+
* @brief Append a child node as object
124+
*
125+
* @param[in] JsonParser::Node* child Pointer indicating a child node appended as object in this node
126+
*/
127+
void AppendObject(Node* child = nullptr) {
128+
if (child != nullptr) {
129+
this->object.push_back(child);
111130
}
112-
if (this->isarrayset) {
113-
throw std::runtime_error("At AppendChild() in Node class: Object is set to node set array.");
131+
if (this->valuetype != ValueType::Default && this->valuetype != ValueType::Object) {
132+
throw std::runtime_error("At AppendObject() in Node class: Some value not Object is alreadey set.");
114133
}
134+
this->valuetype = ValueType::Object;
115135
}
116136

117137
/**
118138
* @brief Append a child node as array
119139
*
120-
* @param[in] JsonParser::Node* _node Pointer indicating a child node appended as array in this node
140+
* @param[in] JsonParser::Node* child Pointer indicating a child node appended as array in this node
121141
*/
122-
void AppendArray(Node* _node = nullptr) {
123-
if (_node != nullptr) {
124-
this->children_array.push_back(_node);
142+
void AppendArray(Node* child = nullptr) {
143+
if (child != nullptr) {
144+
this->array.push_back(child);
125145
}
126-
this->isarrayset = true;
127-
if (this->isvalueset) {
128-
throw std::runtime_error("At AppendArray() in Node class: Array is set to node set value.");
129-
}
130-
if (this->ischildrenset) {
131-
throw std::runtime_error("At AppendArray() in Node class: Array is set to node set object.");
146+
if (this->valuetype != ValueType::Default && this->valuetype != ValueType::Array) {
147+
throw std::runtime_error("At AppendArray() in Node class: Some value not Array is alreadey set.");
132148
}
149+
this->valuetype = ValueType::Array;
133150
}
134151

135152
/**
@@ -139,7 +156,7 @@ namespace JsonParser {
139156
* @return Json::Parser Node* Reference indicating a child node corresponding to the key
140157
*/
141158
Node& operator[](std::string _key) {
142-
for (auto child : this->children) {
159+
for (auto child : this->object) {
143160
if (child->key == _key) {
144161
return *child;
145162
}
@@ -154,8 +171,8 @@ namespace JsonParser {
154171
* @return JsonParser::Node& Reference indicating a node corresponding to the index of array
155172
*/
156173
Node& operator[](unsigned int _index) {
157-
if (_index < this->children_array.size()) {
158-
return *this->children_array[_index];
174+
if (_index < this->array.size()) {
175+
return *this->array[_index];
159176
}
160177
throw std::range_error("\"index\" is over length of Array.");
161178
}
@@ -166,7 +183,7 @@ namespace JsonParser {
166183
* @return std::string& Reference indicating the value of node
167184
*/
168185
std::string& Value() {
169-
if (this-isvalueset) {
186+
if (this->valuetype == ValueType::String || this->valuetype == ValueType::Number || this->valuetype == ValueType::BoolNull) {
170187
return this->value;
171188
}
172189
throw std::runtime_error("Value doesn't exist.");
@@ -180,36 +197,44 @@ namespace JsonParser {
180197
std::string Str() {
181198
std::string str("");
182199
if (this->key != "") {
183-
str += this->key + ":";
184-
}
185-
if (this->isvalueset) {
200+
str += "\"" + this->key + "\":";
201+
}
202+
switch (this->valuetype) {
203+
case ValueType::String:
204+
str += "\"" + this->value + "\"";
205+
break;
206+
case ValueType::Number:
207+
case ValueType::BoolNull:
186208
str += this->value;
187-
} else if (this->ischildrenset) {
209+
break;
210+
case ValueType::Object:
188211
str += "{";
189-
if (this->children.size() > 0) {
190-
str += this->children[0]->Str();
212+
if (this->object.size() > 0) {
213+
str += this->object[0]->Str();
191214
}
192-
for (int i = 1; i < this->children.size(); ++i) {
193-
str += "," + this->children[i]->Str();
215+
for (int i = 1; i < this->object.size(); ++i) {
216+
str += "," + this->object[i]->Str();
194217
}
195218
str += "}";
196-
} else if (this->isarrayset) {
219+
break;
220+
case ValueType::Array:
197221
str += "[";
198-
if (this->children_array.size() > 0) {
199-
str += this->children_array[0]->Str();
222+
if (this->array.size() > 0) {
223+
str += this->array[0]->Str();
200224
}
201-
for (int i = 1; i < this->children_array.size(); ++i) {
202-
str += "," + this->children_array[i]->Str();
225+
for (int i = 1; i < this->array.size(); ++i) {
226+
str += "," + this->array[i]->Str();
203227
}
204228
str += "]";
205-
} else {
206-
throw std::runtime_error("At operator<< of Node class: \"node\" is set neither value, object nor array.");
229+
break;
230+
default:
231+
throw std::runtime_error("At Str() in Node class: This node has no content.");
207232
}
208233
return str;
209234
}
210235
private:
211236
std::string key, value;
212-
bool isvalueset, ischildrenset, isarrayset;
213-
std::vector<Node*> children, children_array;
237+
ValueType valuetype;
238+
std::vector<Node*> object, array;
214239
};
215240
}

src/parser.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ namespace JsonParser {
9292
// std::cout << "Object() is called and index is " << index << std::endl;
9393
switch (chs[index][0]) {
9494
case '}':
95-
parent->AppendChild();
95+
parent->AppendObject();
9696
return index + 1;
9797
case '"':
9898
{
99-
Node* child = new Node(chs[index]);
99+
Node* child = new Node(chs[index].substr(1, chs[index].size() - 2));
100100
int index_current = Grammar::Value(index + 2, chs, child);
101-
parent->AppendChild(child);
101+
parent->AppendObject(child);
102102

103103
while (index_current < chs.size() && chs[index_current][0] == ',') {
104104
// Check exists "key" of next object.
@@ -119,7 +119,7 @@ namespace JsonParser {
119119
}
120120

121121
index_current = Grammar::Value(index_current + 3, chs, child);
122-
parent->AppendChild(child);
122+
parent->AppendObject(child);
123123
}
124124

125125
return index_current + 1;
@@ -145,6 +145,8 @@ namespace JsonParser {
145145
// std::cout << "Value() is called and index is " << index << std::endl;
146146
switch (chs[index][0]) {
147147
case '"':
148+
node->SetString(chs[index].substr(1, chs[index].size() - 2));
149+
return index + 1;
148150
case '-':
149151
case '0':
150152
case '1':
@@ -156,10 +158,12 @@ namespace JsonParser {
156158
case '7':
157159
case '8':
158160
case '9':
161+
node->SetNumber(chs[index]);
162+
return index + 1;
159163
case 'n':
160164
case 't':
161165
case 'f':
162-
node->SetValue(chs[index]);
166+
node->SetBoolNull(chs[index]);
163167
return index + 1;
164168
case '{':
165169
return Grammar::Object(index + 1, chs, node);

0 commit comments

Comments
 (0)