-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJsonDbParser.cpp
executable file
·332 lines (277 loc) · 9.51 KB
/
JsonDbParser.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
Copyright (C) 2010 Wouter van Kleunen
This file is part of JsonDb.
Foobar is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Foobar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with JsonDb. If not, see <http://www.gnu.org/licenses/>.
*/
#include "JsonDb.h"
#include "JsonDbValues.h"
#include "JsonDbParser.h"
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/spirit/include/classic_core.hpp>
#include <boost/spirit/include/classic_confix.hpp>
#include <boost/spirit/include/classic_escape_char.hpp>
#include <boost/spirit/include/classic_lists.hpp>
using namespace boost;
using namespace boost::spirit;
using namespace boost::spirit::classic;
std::string remove_trailing_quote(std::string const &s)
{
assert((s[s.length() - 1] == '"') || (s[s.length() - 1] == '\''));
return s.substr(0, s.length() - 1);
}
// this class's methods get called by the spirit parse resulting
// in the creation of a JSON object or array
//
class Semantic_actions
{
public:
Semantic_actions(JsonDb::TransactionHandle &_transaction, ValuePointer root);
void begin_obj ( char c );
void end_obj ( char c );
void begin_array ( char c );
void end_array ( char c );
void new_c_esc_ch( char c );
void new_name ( const char* str, const char* end );
void new_str ( const char* str, const char* end );
void new_true ( const char* str, const char* end );
void new_false( const char* str, const char* end );
void new_null ( const char* str, const char* end );
void new_int ( int i );
void new_real( double d );
private:
void add_to_current(ValuePointer value);
std::string get_current_str();
JsonDb::TransactionHandle transaction;
std::vector<ValuePointer> stack; // previous child objects and arrays
ValuePointer current_value; // the child object or array that is currently being constructed
ValuePointer root;
std::string name; // of current name/value pair
std::string current_str; // current name or string value
};
Semantic_actions::Semantic_actions(JsonDb::TransactionHandle &_transaction, ValuePointer _root)
: transaction(_transaction)
{
root = _root;
current_value = _root;
}
void Semantic_actions::begin_obj(char c)
{
assert(c == '{');
// Store the current value in the stack
stack.push_back(current_value);
// Add a new value
ValuePointer new_value(new ValueObject(null_key));
add_to_current(new_value);
current_value = new_value;
}
void Semantic_actions::end_obj(char c)
{
assert(c == '}');
transaction->Store(current_value->GetKey(), current_value);
current_value = stack.back();
stack.pop_back();
}
void Semantic_actions::begin_array(char c)
{
assert(c == '[');
// Store the current value in the stack
stack.push_back(current_value);
// Add a new value
ValuePointer new_value(new ValueArray(null_key));
add_to_current(new_value);
current_value = new_value;
}
void Semantic_actions::end_array(char c)
{
assert(c == ']');
transaction->Store(current_value->GetKey(), current_value);
current_value = stack.back();
stack.pop_back();
}
void Semantic_actions::new_c_esc_ch(char c) // work around for c_escape_ch_p parser bug where
// esc chars are not converted unless the semantic action
// is attached directlry to the parser
{
current_str += c;
}
void Semantic_actions::new_name(const char *str, const char *end)
{
assert(current_value != NULL && current_value->GetType() == Value::VALUE_OBJECT);
name = get_current_str();
}
void Semantic_actions::new_str(const char *str, const char *end)
{
ValuePointer value(new ValueString(null_key, get_current_str()));
add_to_current(value);
}
void Semantic_actions::new_true(const char *str, const char *end)
{
assert(std::string(str, end) == "true");
ValuePointer value(new ValueNumberBoolean(null_key, true));
add_to_current(value);
}
void Semantic_actions::new_false(const char *str, const char *end)
{
assert(std::string(str, end) == "false");
ValuePointer value(new ValueNumberBoolean(null_key, false));
add_to_current(value);
}
void Semantic_actions::new_null(const char *str, const char *end)
{
assert(std::string(str, end) == "null");
ValuePointer value(new ValueNull(null_key));
add_to_current(value);
}
void Semantic_actions::new_int(int i)
{
ValuePointer value(new ValueNumberInteger(null_key, i));
add_to_current(value);
}
void Semantic_actions::new_real(double d)
{
ValuePointer value(new ValueNumberReal(null_key, d));
add_to_current(value);
}
void Semantic_actions::add_to_current(ValuePointer value)
{
if(current_value == root)
{
// Replace the root with this value
ValueKey key = current_value->GetKey();
current_value->Delete(transaction);
value->SetKey(key);
transaction->Store(key, value);
} else if(current_value->GetType() == Value::VALUE_OBJECT)
{
// Set the field
ValuePointer old_value = current_value->Get(transaction, name, create);
ValueKey key = old_value->GetKey();
old_value->Delete(transaction);
value->SetKey(key);
transaction->Store(key, value);
} else if(current_value->GetType() == Value::VALUE_ARRAY)
{
// Append to the list
ValueKey key = transaction->GenerateKey();
value->SetKey(key);
current_value->Append(transaction, key);
transaction->Store(key, value);
}
}
std::string Semantic_actions::get_current_str()
{
std::string result = remove_trailing_quote(current_str);
current_str = "";
return result;
}
// the spirit grammer
//
class Json_grammer
: public grammar<Json_grammer>
{
public:
Json_grammer(Semantic_actions& semantic_actions)
: actions(semantic_actions)
{ }
template< typename ScannerT >
struct definition
{
definition(Json_grammer const &self)
{
// need to convert the semantic action class methods to functors with the
// parameter signature expected by spirit
typedef function< void( char ) > Char_action;
typedef function< void( const char*, const char* ) > Str_action;
typedef function< void( double ) > Real_action;
typedef function< void( int ) > Int_action;
Char_action begin_obj ( bind( &Semantic_actions::begin_obj, &self.actions, _1 ) );
Char_action end_obj ( bind( &Semantic_actions::end_obj, &self.actions, _1 ) );
Char_action begin_array ( bind( &Semantic_actions::begin_array, &self.actions, _1 ) );
Char_action end_array ( bind( &Semantic_actions::end_array, &self.actions, _1 ) );
Char_action new_c_esc_ch ( bind( &Semantic_actions::new_c_esc_ch, &self.actions, _1 ) );
Str_action new_name ( bind( &Semantic_actions::new_name, &self.actions, _1, _2 ) );
Str_action new_str ( bind( &Semantic_actions::new_str, &self.actions, _1, _2 ) );
Str_action new_true ( bind( &Semantic_actions::new_true, &self.actions, _1, _2 ) );
Str_action new_false ( bind( &Semantic_actions::new_false, &self.actions, _1, _2 ) );
Str_action new_null ( bind( &Semantic_actions::new_null, &self.actions, _1, _2 ) );
Real_action new_real ( bind( &Semantic_actions::new_real, &self.actions, _1 ) );
Int_action new_int ( bind( &Semantic_actions::new_int, &self.actions, _1 ) );
json = value >> end_p
;
object
= confix_p
(
ch_p('{')[ begin_obj ],
*members,
ch_p('}')[ end_obj ]
)
;
members
= pair >> *( ',' >> pair )
;
pair
= string[ new_name ]
>> ':'
>> value
;
value
= string[ new_str ]
| number
| object
| array
| str_p( "true" ) [ new_true ]
| str_p( "false" )[ new_false ]
| str_p( "null" ) [ new_null ]
;
array
= confix_p
(
ch_p('[')[ begin_array ],
*list_p( elements, ',' ),
ch_p(']')[ end_array ]
)
;
elements
= value >> *( ',' >> value )
;
string
= lexeme_d // this causes white space inside a string to be retained
[
confix_p
(
'"', *c_escape_ch_p[ new_c_esc_ch ], '"'
) |
confix_p
(
'\'', *c_escape_ch_p[ new_c_esc_ch ], '\''
)
]
;
number
= strict_real_p[ new_real ]
| int_p [ new_int ]
;
}
rule< ScannerT > json, object, members, pair, array, elements, value, string, number;
const rule< ScannerT >& start() const { return json; }
};
Semantic_actions& actions;
};
bool JsonDb_ParseJsonExpression(JsonDb::TransactionHandle &transaction, std::string const &expression, ValuePointer root)
{
Semantic_actions semantic_actions(transaction, root);
parse_info<> info = parse(expression.c_str(), Json_grammer(semantic_actions), space_p);
if(!info.full)
throw std::runtime_error((boost::format("Failed to parse json expression: '%s'") % expression).str().c_str());
return true;
}