Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3a56e13

Browse files
committed
Make parse() a method
1 parent ec28acb commit 3a56e13

File tree

1 file changed

+67
-63
lines changed

1 file changed

+67
-63
lines changed

‎src/generic/stage2/structural_parser.h‎

Lines changed: 67 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ struct structural_parser : structural_iterator {
1717
uint32_t depth{0};
1818

1919
template<bool STREAMING, typename T>
20-
WARN_UNUSED static really_inline error_code parse(dom_parser_implementation &dom_parser, T &builder) noexcept;
20+
WARN_UNUSED really_inline error_code parse(T &builder) noexcept;
21+
template<bool STREAMING, typename T>
22+
WARN_UNUSED static really_inline error_code parse(dom_parser_implementation &dom_parser, T &builder) noexcept {
23+
structural_parser parser(dom_parser, STREAMING ? dom_parser.next_structural_index : 0);
24+
return parser.parse<STREAMING>(builder);
25+
}
2126

2227
// For non-streaming, to pass an explicit 0 as next_structural, which enables optimizations
2328
really_inline structural_parser(dom_parser_implementation &_parser, uint32_t start_structural_index)
@@ -102,50 +107,49 @@ struct structural_parser : structural_iterator {
102107
}; // struct structural_parser
103108

104109
template<bool STREAMING, typename T>
105-
WARN_UNUSED really_inline error_code structural_parser::parse(dom_parser_implementation &dom_parser, T &builder) noexcept {
106-
stage2::structural_parser parser(dom_parser, STREAMING ? dom_parser.next_structural_index : 0);
110+
WARN_UNUSED really_inline error_code structural_parser::parse(T &builder) noexcept {
107111
logger::log_start();
108112

109113
//
110114
// Start the document
111115
//
112-
if (parser.at_end()) { return EMPTY; }
113-
SIMDJSON_TRY( parser.start_document() );
114-
builder.start_document(parser);
116+
if (at_end()) { return EMPTY; }
117+
SIMDJSON_TRY( start_document() );
118+
builder.start_document(*this);
115119

116120
//
117121
// Read first value
118122
//
119123
{
120-
const uint8_t *value = parser.advance();
124+
const uint8_t *value = advance();
121125
switch (*value) {
122126
case '{': {
123-
if (parser.empty_object(builder)) { goto document_end; }
124-
SIMDJSON_TRY( parser.start_object(builder) );
127+
if (empty_object(builder)) { goto document_end; }
128+
SIMDJSON_TRY( start_object(builder) );
125129
goto object_begin;
126130
}
127131
case '[': {
128-
if (parser.empty_array(builder)) { goto document_end; }
129-
SIMDJSON_TRY( parser.start_array(builder) );
132+
if (empty_array(builder)) { goto document_end; }
133+
SIMDJSON_TRY( start_array(builder) );
130134
// Make sure the outer array is closed before continuing; otherwise, there are ways we could get
131135
// into memory corruption. See https://github.com/simdjson/simdjson/issues/906
132136
if (!STREAMING) {
133-
if (parser.buf[dom_parser.structural_indexes[dom_parser.n_structural_indexes - 1]] != ']') {
137+
if (buf[parser.structural_indexes[parser.n_structural_indexes - 1]] != ']') {
134138
return TAPE_ERROR;
135139
}
136140
}
137141
goto array_begin;
138142
}
139-
case '"': SIMDJSON_TRY( builder.parse_string(parser, value) ); goto document_end;
140-
case 't': SIMDJSON_TRY( builder.parse_root_true_atom(parser, value) ); goto document_end;
141-
case 'f': SIMDJSON_TRY( builder.parse_root_false_atom(parser, value) ); goto document_end;
142-
case 'n': SIMDJSON_TRY( builder.parse_root_null_atom(parser, value) ); goto document_end;
143+
case '"': SIMDJSON_TRY( builder.parse_string(*this, value) ); goto document_end;
144+
case 't': SIMDJSON_TRY( builder.parse_root_true_atom(*this, value) ); goto document_end;
145+
case 'f': SIMDJSON_TRY( builder.parse_root_false_atom(*this, value) ); goto document_end;
146+
case 'n': SIMDJSON_TRY( builder.parse_root_null_atom(*this, value) ); goto document_end;
143147
case '-':
144148
case '0': case '1': case '2': case '3': case '4':
145149
case '5': case '6': case '7': case '8': case '9':
146-
SIMDJSON_TRY( builder.parse_root_number(parser, value) ); goto document_end;
150+
SIMDJSON_TRY( builder.parse_root_number(*this, value) ); goto document_end;
147151
default:
148-
parser.log_error("Document starts with a non-value character");
152+
log_error("Document starts with a non-value character");
149153
return TAPE_ERROR;
150154
}
151155
}
@@ -154,121 +158,121 @@ WARN_UNUSED really_inline error_code structural_parser::parse(dom_parser_impleme
154158
// Object parser states
155159
//
156160
object_begin: {
157-
const uint8_t *key = parser.advance();
161+
const uint8_t *key = advance();
158162
if (*key != '"') {
159-
parser.log_error("Object does not start with a key");
163+
log_error("Object does not start with a key");
160164
return TAPE_ERROR;
161165
}
162-
builder.increment_count(parser);
163-
SIMDJSON_TRY( builder.parse_key(parser, key) );
166+
builder.increment_count(*this);
167+
SIMDJSON_TRY( builder.parse_key(*this, key) );
164168
goto object_field;
165169
} // object_begin:
166170

167171
object_field: {
168-
if (unlikely( parser.advance_char() != ':' )) { parser.log_error("Missing colon after key in object"); return TAPE_ERROR; }
169-
const uint8_t *value = parser.advance();
172+
if (unlikely( advance_char() != ':' )) { log_error("Missing colon after key in object"); return TAPE_ERROR; }
173+
const uint8_t *value = advance();
170174
switch (*value) {
171175
case '{': {
172-
if (parser.empty_object(builder)) { break; };
173-
SIMDJSON_TRY( parser.start_object(builder) );
176+
if (empty_object(builder)) { break; };
177+
SIMDJSON_TRY( start_object(builder) );
174178
goto object_begin;
175179
}
176180
case '[': {
177-
if (parser.empty_array(builder)) { break; };
178-
SIMDJSON_TRY( parser.start_array(builder) );
181+
if (empty_array(builder)) { break; };
182+
SIMDJSON_TRY( start_array(builder) );
179183
goto array_begin;
180184
}
181-
case '"': SIMDJSON_TRY( builder.parse_string(parser, value) ); break;
182-
case 't': SIMDJSON_TRY( builder.parse_true_atom(parser, value) ); break;
183-
case 'f': SIMDJSON_TRY( builder.parse_false_atom(parser, value) ); break;
184-
case 'n': SIMDJSON_TRY( builder.parse_null_atom(parser, value) ); break;
185+
case '"': SIMDJSON_TRY( builder.parse_string(*this, value) ); break;
186+
case 't': SIMDJSON_TRY( builder.parse_true_atom(*this, value) ); break;
187+
case 'f': SIMDJSON_TRY( builder.parse_false_atom(*this, value) ); break;
188+
case 'n': SIMDJSON_TRY( builder.parse_null_atom(*this, value) ); break;
185189
case '-':
186190
case '0': case '1': case '2': case '3': case '4':
187191
case '5': case '6': case '7': case '8': case '9':
188-
SIMDJSON_TRY( builder.parse_number(parser, value) ); break;
192+
SIMDJSON_TRY( builder.parse_number(*this, value) ); break;
189193
default:
190-
parser.log_error("Non-value found when value was expected!");
194+
log_error("Non-value found when value was expected!");
191195
return TAPE_ERROR;
192196
}
193197
} // object_field:
194198

195199
object_continue: {
196-
switch (parser.advance_char()) {
200+
switch (advance_char()) {
197201
case ',': {
198-
builder.increment_count(parser);
199-
const uint8_t *key = parser.advance();
200-
if (unlikely( *key != '"' )) { parser.log_error("Key string missing at beginning of field in object"); return TAPE_ERROR; }
201-
SIMDJSON_TRY( builder.parse_key(parser, key) );
202+
builder.increment_count(*this);
203+
const uint8_t *key = advance();
204+
if (unlikely( *key != '"' )) { log_error("Key string missing at beginning of field in object"); return TAPE_ERROR; }
205+
SIMDJSON_TRY( builder.parse_key(*this, key) );
202206
goto object_field;
203207
}
204208
case '}':
205-
builder.end_object(parser);
206-
parser.depth--;
209+
builder.end_object(*this);
210+
depth--;
207211
goto scope_end;
208212
default:
209-
parser.log_error("No comma between object fields");
213+
log_error("No comma between object fields");
210214
return TAPE_ERROR;
211215
}
212216
} // object_continue:
213217

214218
scope_end: {
215-
if (parser.depth == 0) { goto document_end; }
216-
if (parser.parser.is_array[parser.depth]) { goto array_continue; }
219+
if (depth == 0) { goto document_end; }
220+
if (parser.is_array[depth]) { goto array_continue; }
217221
goto object_continue;
218222
} // scope_end:
219223

220224
//
221225
// Array parser states
222226
//
223227
array_begin: {
224-
builder.increment_count(parser);
228+
builder.increment_count(*this);
225229
} // array_begin:
226230

227231
array_value: {
228-
const uint8_t *value = parser.advance();
232+
const uint8_t *value = advance();
229233
switch (*value) {
230234
case '{': {
231-
if (parser.empty_object(builder)) { break; };
232-
SIMDJSON_TRY( parser.start_object(builder) );
235+
if (empty_object(builder)) { break; };
236+
SIMDJSON_TRY( start_object(builder) );
233237
goto object_begin;
234238
}
235239
case '[': {
236-
if (parser.empty_array(builder)) { break; };
237-
SIMDJSON_TRY( parser.start_array(builder) );
240+
if (empty_array(builder)) { break; };
241+
SIMDJSON_TRY( start_array(builder) );
238242
goto array_begin;
239243
}
240-
case '"': SIMDJSON_TRY( builder.parse_string(parser, value) ); break;
241-
case 't': SIMDJSON_TRY( builder.parse_true_atom(parser, value) ); break;
242-
case 'f': SIMDJSON_TRY( builder.parse_false_atom(parser, value) ); break;
243-
case 'n': SIMDJSON_TRY( builder.parse_null_atom(parser, value) ); break;
244+
case '"': SIMDJSON_TRY( builder.parse_string(*this, value) ); break;
245+
case 't': SIMDJSON_TRY( builder.parse_true_atom(*this, value) ); break;
246+
case 'f': SIMDJSON_TRY( builder.parse_false_atom(*this, value) ); break;
247+
case 'n': SIMDJSON_TRY( builder.parse_null_atom(*this, value) ); break;
244248
case '-':
245249
case '0': case '1': case '2': case '3': case '4':
246250
case '5': case '6': case '7': case '8': case '9':
247-
SIMDJSON_TRY( builder.parse_number(parser, value) ); break;
251+
SIMDJSON_TRY( builder.parse_number(*this, value) ); break;
248252
default:
249-
parser.log_error("Non-value found when value was expected!");
253+
log_error("Non-value found when value was expected!");
250254
return TAPE_ERROR;
251255
}
252256
} // array_value:
253257

254258
array_continue: {
255-
switch (parser.advance_char()) {
259+
switch (advance_char()) {
256260
case ',':
257-
builder.increment_count(parser);
261+
builder.increment_count(*this);
258262
goto array_value;
259263
case ']':
260-
builder.end_array(parser);
261-
parser.depth--;
264+
builder.end_array(*this);
265+
depth--;
262266
goto scope_end;
263267
default:
264-
parser.log_error("Missing comma between array values");
268+
log_error("Missing comma between array values");
265269
return TAPE_ERROR;
266270
}
267271
} // array_continue:
268272

269273
document_end: {
270-
builder.end_document(parser);
271-
return parser.finish<STREAMING>();
274+
builder.end_document(*this);
275+
return finish<STREAMING>();
272276
} // document_end:
273277

274278
} // parse_structurals()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /