Skip to content

Commit 1b56211

Browse files
committed
Give start_*/end_* error codes
1 parent d8974d5 commit 1b56211

File tree

2 files changed

+20
-25
lines changed

2 files changed

+20
-25
lines changed

src/generic/stage2/json_iterator.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ SIMDJSON_WARN_UNUSED simdjson_really_inline error_code json_iterator::walk_docum
9191
// Start the document
9292
//
9393
if (at_end()) { return EMPTY; }
94-
visitor.start_document(*this);
94+
SIMDJSON_TRY( visitor.start_document(*this) );
9595

9696
//
9797
// Read first value
@@ -127,7 +127,7 @@ SIMDJSON_WARN_UNUSED simdjson_really_inline error_code json_iterator::walk_docum
127127
object_begin:
128128
depth++;
129129
if (depth >= dom_parser.max_depth()) { log_error("Exceeded max depth!"); return DEPTH_ERROR; }
130-
visitor.start_object(*this);
130+
SIMDJSON_TRY( visitor.start_object(*this) );
131131

132132
if (advance() != '"') { log_error("Object does not start with a key"); return TAPE_ERROR; }
133133
visitor.increment_count(*this);
@@ -149,7 +149,7 @@ SIMDJSON_WARN_UNUSED simdjson_really_inline error_code json_iterator::walk_docum
149149
if (simdjson_unlikely( advance() != '"' )) { log_error("Key string missing at beginning of field in object"); return TAPE_ERROR; }
150150
SIMDJSON_TRY( visitor.key(*this, value) );
151151
goto object_field;
152-
case '}': visitor.end_object(*this); goto scope_end;
152+
case '}': SIMDJSON_TRY( visitor.end_object(*this) ); goto scope_end;
153153
default: log_error("No comma between object fields"); return TAPE_ERROR;
154154
}
155155

@@ -165,7 +165,7 @@ SIMDJSON_WARN_UNUSED simdjson_really_inline error_code json_iterator::walk_docum
165165
array_begin:
166166
depth++;
167167
if (depth >= dom_parser.max_depth()) { log_error("Exceeded max depth!"); return DEPTH_ERROR; }
168-
visitor.start_array(*this);
168+
SIMDJSON_TRY( visitor.start_array(*this) );
169169
visitor.increment_count(*this);
170170

171171
array_value:
@@ -178,12 +178,12 @@ SIMDJSON_WARN_UNUSED simdjson_really_inline error_code json_iterator::walk_docum
178178
array_continue:
179179
switch (advance()) {
180180
case ',': visitor.increment_count(*this); goto array_value;
181-
case ']': visitor.end_array(*this); goto scope_end;
181+
case ']': SIMDJSON_TRY( visitor.end_array(*this) ); goto scope_end;
182182
default: log_error("Missing comma between array values"); return TAPE_ERROR;
183183
}
184184

185185
document_end:
186-
visitor.end_document(*this);
186+
SIMDJSON_TRY( visitor.end_document(*this) );
187187

188188
dom_parser.next_structural_index = uint32_t(next_structural - &dom_parser.structural_indexes[0]);
189189

src/generic/stage2/tape_builder.h

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,50 +56,44 @@ struct tape_builder {
5656
empty_container(iter, internal::tape_type::START_ARRAY, internal::tape_type::END_ARRAY);
5757
}
5858

59-
simdjson_really_inline void start_document(json_iterator &iter) {
59+
SIMDJSON_WARN_UNUSED simdjson_really_inline error_code start_document(json_iterator &iter) {
6060
iter.log_start_value("document");
6161
start_container(iter);
6262
iter.dom_parser.is_array[iter.depth] = false;
63+
return SUCCESS;
6364
}
64-
simdjson_really_inline void start_object(json_iterator &iter) {
65+
SIMDJSON_WARN_UNUSED simdjson_really_inline error_code start_object(json_iterator &iter) {
6566
iter.log_start_value("object");
6667
start_container(iter);
6768
iter.dom_parser.is_array[iter.depth] = false;
69+
return SUCCESS;
6870
}
69-
simdjson_really_inline void start_array(json_iterator &iter) {
71+
SIMDJSON_WARN_UNUSED simdjson_really_inline error_code start_array(json_iterator &iter) {
7072
iter.log_start_value("array");
7173
start_container(iter);
7274
iter.dom_parser.is_array[iter.depth] = true;
75+
return SUCCESS;
7376
}
7477

75-
simdjson_really_inline void end_object(json_iterator &iter) {
78+
SIMDJSON_WARN_UNUSED simdjson_really_inline error_code end_object(json_iterator &iter) {
7679
iter.log_end_value("object");
77-
end_container(iter, internal::tape_type::START_OBJECT, internal::tape_type::END_OBJECT);
80+
return end_container(iter, internal::tape_type::START_OBJECT, internal::tape_type::END_OBJECT);
7881
}
79-
simdjson_really_inline void end_array(json_iterator &iter) {
82+
SIMDJSON_WARN_UNUSED simdjson_really_inline error_code end_array(json_iterator &iter) {
8083
iter.log_end_value("array");
81-
end_container(iter, internal::tape_type::START_ARRAY, internal::tape_type::END_ARRAY);
84+
return end_container(iter, internal::tape_type::START_ARRAY, internal::tape_type::END_ARRAY);
8285
}
83-
simdjson_really_inline void end_document(json_iterator &iter) {
86+
SIMDJSON_WARN_UNUSED simdjson_really_inline error_code end_document(json_iterator &iter) {
8487
iter.log_end_value("document");
8588
constexpr uint32_t start_tape_index = 0;
8689
tape.append(start_tape_index, internal::tape_type::ROOT);
8790
tape_writer::write(iter.dom_parser.doc->tape[start_tape_index], next_tape_index(iter), internal::tape_type::ROOT);
91+
return SUCCESS;
8892
}
8993
SIMDJSON_WARN_UNUSED simdjson_really_inline error_code key(json_iterator &iter, const uint8_t *key) {
9094
return parse_string(iter, key, true);
9195
}
9296

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-
}
10397
// increment_count increments the count of keys in an object or values in an array.
10498
simdjson_really_inline void increment_count(json_iterator &iter) {
10599
iter.dom_parser.open_containers[iter.depth].count++; // we have a key value pair in the object at parser.dom_parser.depth - 1
@@ -219,7 +213,7 @@ struct tape_builder {
219213
tape.skip(); // We don't actually *write* the start element until the end.
220214
}
221215

222-
simdjson_really_inline void end_container(json_iterator &iter, internal::tape_type start, internal::tape_type end) noexcept {
216+
SIMDJSON_WARN_UNUSED simdjson_really_inline error_code end_container(json_iterator &iter, internal::tape_type start, internal::tape_type end) noexcept {
223217
// Write the ending tape element, pointing at the start location
224218
const uint32_t start_tape_index = iter.dom_parser.open_containers[iter.depth].tape_index;
225219
tape.append(start_tape_index, end);
@@ -229,6 +223,7 @@ struct tape_builder {
229223
const uint32_t count = iter.dom_parser.open_containers[iter.depth].count;
230224
const uint32_t cntsat = count > 0xFFFFFF ? 0xFFFFFF : count;
231225
tape_writer::write(iter.dom_parser.doc->tape[start_tape_index], next_tape_index(iter) | (uint64_t(cntsat) << 32), start);
226+
return SUCCESS;
232227
}
233228

234229
simdjson_really_inline uint8_t *on_start_string(json_iterator &iter) noexcept {

0 commit comments

Comments
 (0)