Skip to content

Commit 53408c9

Browse files
committed
Make structural_parser internal
1 parent a9a8af6 commit 53408c9

File tree

2 files changed

+57
-55
lines changed

2 files changed

+57
-55
lines changed
Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1 @@
1-
namespace stage2 {
21

3-
struct streaming_structural_parser: structural_parser {
4-
really_inline streaming_structural_parser(size_t len, parser &_doc_parser)
5-
: structural_parser(len, _doc_parser) {}
6-
7-
// override to add streaming
8-
WARN_UNUSED really_inline error_code start(UNUSED size_t len) {
9-
log_start();
10-
init(); // sets is_valid to false
11-
// Capacity ain't no thang for streaming, so we don't check it.
12-
// Advance to the first character as soon as possible
13-
advance_char();
14-
// Push the root scope (there is always at least one scope)
15-
if (start_document()) {
16-
return on_error(DEPTH_ERROR);
17-
}
18-
return SUCCESS;
19-
}
20-
21-
// override to add streaming
22-
WARN_UNUSED really_inline error_code finish() {
23-
if ( structurals.past_end(doc_parser().n_structural_indexes) ) {
24-
log_error("IMPOSSIBLE: past the end of the JSON!");
25-
return on_error(TAPE_ERROR);
26-
}
27-
end_document(0, 1);
28-
if (depth != 0) {
29-
log_error("Unclosed objects or arrays!");
30-
return on_error(TAPE_ERROR);
31-
}
32-
bool finished = structurals.at_end(doc_parser().n_structural_indexes);
33-
if (!finished) { log_value("(and has more)"); }
34-
return on_success(finished ? SUCCESS : SUCCESS_AND_HAS_MORE);
35-
}
36-
};
37-
38-
} // namespace stage2
39-
40-
/************
41-
* The JSON is parsed to a tape, see the accompanying tape.md file
42-
* for documentation.
43-
***********/
44-
WARN_UNUSED error_code implementation::stage2(const uint8_t *buf, size_t len, parser &doc_parser, size_t &next_json) const noexcept {
45-
doc_parser.parsing_buf = buf;
46-
doc_parser.next_structural = next_json;
47-
stage2::streaming_structural_parser parser(len, doc_parser);
48-
error_code result = parser.start(len);
49-
if (result) { return result; }
50-
51-
if (parser.parse_root_value()) {
52-
return parser.error();
53-
}
54-
next_json = doc_parser.next_structural;
55-
return parser.finish();
56-
}

src/generic/stage2/structural_parser.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
namespace stage2 {
77

8+
namespace {
9+
810
struct number_writer {
911
parser &doc_parser;
1012

@@ -439,6 +441,43 @@ struct structural_parser {
439441
}
440442
};
441443

444+
struct streaming_structural_parser: structural_parser {
445+
really_inline streaming_structural_parser(size_t len, parser &_doc_parser)
446+
: structural_parser(len, _doc_parser) {}
447+
448+
// override to add streaming
449+
WARN_UNUSED really_inline error_code start(UNUSED size_t len) {
450+
log_start();
451+
init(); // sets is_valid to false
452+
// Capacity ain't no thang for streaming, so we don't check it.
453+
// Advance to the first character as soon as possible
454+
advance_char();
455+
// Push the root scope (there is always at least one scope)
456+
if (start_document()) {
457+
return on_error(DEPTH_ERROR);
458+
}
459+
return SUCCESS;
460+
}
461+
462+
// override to add streaming
463+
WARN_UNUSED really_inline error_code finish() {
464+
if ( structurals.past_end(doc_parser().n_structural_indexes) ) {
465+
log_error("IMPOSSIBLE: past the end of the JSON!");
466+
return on_error(TAPE_ERROR);
467+
}
468+
end_document(0, 1);
469+
if (depth != 0) {
470+
log_error("Unclosed objects or arrays!");
471+
return on_error(TAPE_ERROR);
472+
}
473+
bool finished = structurals.at_end(doc_parser().n_structural_indexes);
474+
if (!finished) { log_value("(and has more)"); }
475+
return on_success(finished ? SUCCESS : SUCCESS_AND_HAS_MORE);
476+
}
477+
}; // struct streaming_structural_parser
478+
479+
} // namespace {}
480+
442481
} // namespace stage2
443482

444483
/************
@@ -458,6 +497,24 @@ WARN_UNUSED error_code implementation::stage2(const uint8_t *buf, size_t len, pa
458497
return parser.finish();
459498
}
460499

500+
/************
501+
* The JSON is parsed to a tape, see the accompanying tape.md file
502+
* for documentation.
503+
***********/
504+
WARN_UNUSED error_code implementation::stage2(const uint8_t *buf, size_t len, parser &doc_parser, size_t &next_json) const noexcept {
505+
doc_parser.parsing_buf = buf;
506+
doc_parser.next_structural = next_json;
507+
stage2::streaming_structural_parser parser(len, doc_parser);
508+
error_code result = parser.start(len);
509+
if (result) { return result; }
510+
511+
if (parser.parse_root_value()) {
512+
return parser.error();
513+
}
514+
next_json = doc_parser.next_structural;
515+
return parser.finish();
516+
}
517+
461518
WARN_UNUSED error_code implementation::parse(const uint8_t *buf, size_t len, parser &doc_parser) const noexcept {
462519
error_code code = stage1(buf, len, doc_parser, false);
463520
if (!code) {

0 commit comments

Comments
 (0)