1
- #include " generic/stage2/structural_parser .h"
1
+ #include " generic/stage2/json_iterator .h"
2
2
#include " generic/stage2/tape_writer.h"
3
3
#include " generic/stage2/atomparsing.h"
4
4
@@ -12,12 +12,12 @@ struct tape_builder {
12
12
dom_parser_implementation &dom_parser,
13
13
dom::document &doc) noexcept {
14
14
dom_parser.doc = &doc;
15
- structural_parser iter (dom_parser, STREAMING ? dom_parser.next_structural_index : 0 );
15
+ json_iterator iter (dom_parser, STREAMING ? dom_parser.next_structural_index : 0 );
16
16
tape_builder builder (doc);
17
17
return iter.walk_document <STREAMING>(builder);
18
18
}
19
19
20
- simdjson_really_inline error_code root_primitive (structural_parser &iter, const uint8_t *value) {
20
+ simdjson_really_inline error_code root_primitive (json_iterator &iter, const uint8_t *value) {
21
21
switch (*value) {
22
22
case ' "' : return parse_string (iter, value);
23
23
case ' t' : return parse_root_true_atom (iter, value);
@@ -32,7 +32,7 @@ struct tape_builder {
32
32
return TAPE_ERROR;
33
33
}
34
34
}
35
- simdjson_really_inline error_code primitive (structural_parser &iter, const uint8_t *value) {
35
+ simdjson_really_inline error_code primitive (json_iterator &iter, const uint8_t *value) {
36
36
switch (*value) {
37
37
case ' "' : return parse_string (iter, value);
38
38
case ' t' : return parse_true_atom (iter, value);
@@ -47,54 +47,64 @@ struct tape_builder {
47
47
return TAPE_ERROR;
48
48
}
49
49
}
50
- simdjson_really_inline void empty_object (structural_parser &iter) {
50
+ simdjson_really_inline void empty_object (json_iterator &iter) {
51
51
iter.log_value (" empty object" );
52
52
empty_container (iter, internal::tape_type::START_OBJECT, internal::tape_type::END_OBJECT);
53
53
}
54
- simdjson_really_inline void empty_array (structural_parser &iter) {
54
+ simdjson_really_inline void empty_array (json_iterator &iter) {
55
55
iter.log_value (" empty array" );
56
56
empty_container (iter, internal::tape_type::START_ARRAY, internal::tape_type::END_ARRAY);
57
57
}
58
58
59
- simdjson_really_inline void start_document (structural_parser &iter) {
59
+ simdjson_really_inline void start_document (json_iterator &iter) {
60
60
iter.log_start_value (" document" );
61
61
start_container (iter);
62
62
iter.dom_parser .is_array [iter.depth ] = false ;
63
63
}
64
- simdjson_really_inline void start_object (structural_parser &iter) {
64
+ simdjson_really_inline void start_object (json_iterator &iter) {
65
65
iter.log_start_value (" object" );
66
66
start_container (iter);
67
67
iter.dom_parser .is_array [iter.depth ] = false ;
68
68
}
69
- simdjson_really_inline void start_array (structural_parser &iter) {
69
+ simdjson_really_inline void start_array (json_iterator &iter) {
70
70
iter.log_start_value (" array" );
71
71
start_container (iter);
72
72
iter.dom_parser .is_array [iter.depth ] = true ;
73
73
}
74
74
75
- simdjson_really_inline void end_object (structural_parser &iter) {
75
+ simdjson_really_inline void end_object (json_iterator &iter) {
76
76
iter.log_end_value (" object" );
77
77
end_container (iter, internal::tape_type::START_OBJECT, internal::tape_type::END_OBJECT);
78
78
}
79
- simdjson_really_inline void end_array (structural_parser &iter) {
79
+ simdjson_really_inline void end_array (json_iterator &iter) {
80
80
iter.log_end_value (" array" );
81
81
end_container (iter, internal::tape_type::START_ARRAY, internal::tape_type::END_ARRAY);
82
82
}
83
- simdjson_really_inline void end_document (structural_parser &iter) {
83
+ simdjson_really_inline void end_document (json_iterator &iter) {
84
84
iter.log_end_value (" document" );
85
85
constexpr uint32_t start_tape_index = 0 ;
86
86
tape.append (start_tape_index, internal::tape_type::ROOT);
87
87
tape_writer::write (iter.dom_parser .doc ->tape [start_tape_index], next_tape_index (iter), internal::tape_type::ROOT);
88
88
}
89
- SIMDJSON_WARN_UNUSED simdjson_really_inline error_code key (structural_parser &iter, const uint8_t *key) {
89
+ SIMDJSON_WARN_UNUSED simdjson_really_inline error_code key (json_iterator &iter, const uint8_t *key) {
90
90
return parse_string (iter, key, true );
91
91
}
92
92
93
+ // Called after end_object/end_array. Not called after empty_object/empty_array,
94
+ // as the parent is already known in those cases.
95
+ //
96
+ // The object returned from end_container() should support the in_container(),
97
+ // in_array() and in_object() methods, allowing the iterator to branch to the
98
+ // correct place.
99
+ simdjson_really_inline tape_builder &end_container (json_iterator &iter) {
100
+ iter.depth --;
101
+ return *this ;
102
+ }
93
103
// increment_count increments the count of keys in an object or values in an array.
94
- simdjson_really_inline void increment_count (structural_parser &iter) {
104
+ simdjson_really_inline void increment_count (json_iterator &iter) {
95
105
iter.dom_parser .open_containers [iter.depth ].count ++; // we have a key value pair in the object at parser.dom_parser.depth - 1
96
106
}
97
- simdjson_really_inline bool in_array (structural_parser &iter) noexcept {
107
+ simdjson_really_inline bool in_array (json_iterator &iter) noexcept {
98
108
return iter.dom_parser .is_array [iter.depth ];
99
109
}
100
110
@@ -106,7 +116,7 @@ struct tape_builder {
106
116
107
117
simdjson_really_inline tape_builder (dom::document &doc) noexcept : tape{doc.tape .get ()}, current_string_buf_loc{doc.string_buf .get ()} {}
108
118
109
- SIMDJSON_WARN_UNUSED simdjson_really_inline error_code parse_string (structural_parser &iter, const uint8_t *value, bool key = false ) {
119
+ SIMDJSON_WARN_UNUSED simdjson_really_inline error_code parse_string (json_iterator &iter, const uint8_t *value, bool key = false ) {
110
120
iter.log_value (key ? " key" : " string" );
111
121
uint8_t *dst = on_start_string (iter);
112
122
dst = stringparsing::parse_string (value, dst);
@@ -118,13 +128,13 @@ struct tape_builder {
118
128
return SUCCESS;
119
129
}
120
130
121
- SIMDJSON_WARN_UNUSED simdjson_really_inline error_code parse_number (structural_parser &iter, const uint8_t *value) {
131
+ SIMDJSON_WARN_UNUSED simdjson_really_inline error_code parse_number (json_iterator &iter, const uint8_t *value) {
122
132
iter.log_value (" number" );
123
133
if (!numberparsing::parse_number (value, tape)) { iter.log_error (" Invalid number" ); return NUMBER_ERROR; }
124
134
return SUCCESS;
125
135
}
126
136
127
- simdjson_really_inline error_code parse_root_number (structural_parser &iter, const uint8_t *value) {
137
+ simdjson_really_inline error_code parse_root_number (json_iterator &iter, const uint8_t *value) {
128
138
//
129
139
// We need to make a copy to make sure that the string is space terminated.
130
140
// This is not about padding the input, which should already padded up
@@ -149,42 +159,42 @@ struct tape_builder {
149
159
return error;
150
160
}
151
161
152
- SIMDJSON_WARN_UNUSED simdjson_really_inline error_code parse_true_atom (structural_parser &iter, const uint8_t *value) {
162
+ SIMDJSON_WARN_UNUSED simdjson_really_inline error_code parse_true_atom (json_iterator &iter, const uint8_t *value) {
153
163
iter.log_value (" true" );
154
164
if (!atomparsing::is_valid_true_atom (value)) { return T_ATOM_ERROR; }
155
165
tape.append (0 , internal::tape_type::TRUE_VALUE);
156
166
return SUCCESS;
157
167
}
158
168
159
- SIMDJSON_WARN_UNUSED simdjson_really_inline error_code parse_root_true_atom (structural_parser &iter, const uint8_t *value) {
169
+ SIMDJSON_WARN_UNUSED simdjson_really_inline error_code parse_root_true_atom (json_iterator &iter, const uint8_t *value) {
160
170
iter.log_value (" true" );
161
171
if (!atomparsing::is_valid_true_atom (value, iter.remaining_len ())) { return T_ATOM_ERROR; }
162
172
tape.append (0 , internal::tape_type::TRUE_VALUE);
163
173
return SUCCESS;
164
174
}
165
175
166
- SIMDJSON_WARN_UNUSED simdjson_really_inline error_code parse_false_atom (structural_parser &iter, const uint8_t *value) {
176
+ SIMDJSON_WARN_UNUSED simdjson_really_inline error_code parse_false_atom (json_iterator &iter, const uint8_t *value) {
167
177
iter.log_value (" false" );
168
178
if (!atomparsing::is_valid_false_atom (value)) { return F_ATOM_ERROR; }
169
179
tape.append (0 , internal::tape_type::FALSE_VALUE);
170
180
return SUCCESS;
171
181
}
172
182
173
- SIMDJSON_WARN_UNUSED simdjson_really_inline error_code parse_root_false_atom (structural_parser &iter, const uint8_t *value) {
183
+ SIMDJSON_WARN_UNUSED simdjson_really_inline error_code parse_root_false_atom (json_iterator &iter, const uint8_t *value) {
174
184
iter.log_value (" false" );
175
185
if (!atomparsing::is_valid_false_atom (value, iter.remaining_len ())) { return F_ATOM_ERROR; }
176
186
tape.append (0 , internal::tape_type::FALSE_VALUE);
177
187
return SUCCESS;
178
188
}
179
189
180
- SIMDJSON_WARN_UNUSED simdjson_really_inline error_code parse_null_atom (structural_parser &iter, const uint8_t *value) {
190
+ SIMDJSON_WARN_UNUSED simdjson_really_inline error_code parse_null_atom (json_iterator &iter, const uint8_t *value) {
181
191
iter.log_value (" null" );
182
192
if (!atomparsing::is_valid_null_atom (value)) { return N_ATOM_ERROR; }
183
193
tape.append (0 , internal::tape_type::NULL_VALUE);
184
194
return SUCCESS;
185
195
}
186
196
187
- SIMDJSON_WARN_UNUSED simdjson_really_inline error_code parse_root_null_atom (structural_parser &iter, const uint8_t *value) {
197
+ SIMDJSON_WARN_UNUSED simdjson_really_inline error_code parse_root_null_atom (json_iterator &iter, const uint8_t *value) {
188
198
iter.log_value (" null" );
189
199
if (!atomparsing::is_valid_null_atom (value, iter.remaining_len ())) { return N_ATOM_ERROR; }
190
200
tape.append (0 , internal::tape_type::NULL_VALUE);
@@ -193,23 +203,23 @@ struct tape_builder {
193
203
194
204
// private:
195
205
196
- simdjson_really_inline uint32_t next_tape_index (structural_parser &iter) {
206
+ simdjson_really_inline uint32_t next_tape_index (json_iterator &iter) {
197
207
return uint32_t (tape.next_tape_loc - iter.dom_parser .doc ->tape .get ());
198
208
}
199
209
200
- simdjson_really_inline void empty_container (structural_parser &iter, internal::tape_type start, internal::tape_type end) {
210
+ simdjson_really_inline void empty_container (json_iterator &iter, internal::tape_type start, internal::tape_type end) {
201
211
auto start_index = next_tape_index (iter);
202
212
tape.append (start_index+2 , start);
203
213
tape.append (start_index, end);
204
214
}
205
215
206
- simdjson_really_inline void start_container (structural_parser &iter) {
216
+ simdjson_really_inline void start_container (json_iterator &iter) {
207
217
iter.dom_parser .open_containers [iter.depth ].tape_index = next_tape_index (iter);
208
218
iter.dom_parser .open_containers [iter.depth ].count = 0 ;
209
219
tape.skip (); // We don't actually *write* the start element until the end.
210
220
}
211
221
212
- simdjson_really_inline void end_container (structural_parser &iter, internal::tape_type start, internal::tape_type end) noexcept {
222
+ simdjson_really_inline void end_container (json_iterator &iter, internal::tape_type start, internal::tape_type end) noexcept {
213
223
// Write the ending tape element, pointing at the start location
214
224
const uint32_t start_tape_index = iter.dom_parser .open_containers [iter.depth ].tape_index ;
215
225
tape.append (start_tape_index, end);
@@ -221,7 +231,7 @@ struct tape_builder {
221
231
tape_writer::write (iter.dom_parser .doc ->tape [start_tape_index], next_tape_index (iter) | (uint64_t (cntsat) << 32 ), start);
222
232
}
223
233
224
- simdjson_really_inline uint8_t *on_start_string (structural_parser &iter) noexcept {
234
+ simdjson_really_inline uint8_t *on_start_string (json_iterator &iter) noexcept {
225
235
// we advance the point, accounting for the fact that we have a NULL termination
226
236
tape.append (current_string_buf_loc - iter.dom_parser .doc ->string_buf .get (), internal::tape_type::STRING);
227
237
return current_string_buf_loc + sizeof (uint32_t );
0 commit comments