Rename doc_parser -> parser

This commit is contained in:
John Keiser 2020-06-01 12:14:09 -07:00
parent a3a9bde83e
commit b86fb95306
7 changed files with 59 additions and 59 deletions

View File

@ -30,10 +30,10 @@ WARN_UNUSED error_code dom_parser_implementation::set_max_depth(size_t) noexcept
return SUCCESS; return SUCCESS;
} }
WARN_UNUSED error_code dom_parser_implementation::parse(const uint8_t *buf, size_t len, dom::parser &doc_parser) noexcept { WARN_UNUSED error_code dom_parser_implementation::parse(const uint8_t *buf, size_t len, dom::parser &parser) noexcept {
error_code code = stage1(buf, len, doc_parser, false); error_code code = stage1(buf, len, parser, false);
if (!code) { if (!code) {
code = stage2(buf, len, doc_parser); code = stage2(buf, len, parser);
} }
return code; return code;
} }

View File

@ -12,8 +12,8 @@ namespace stage1 {
class structural_scanner { class structural_scanner {
public: public:
really_inline structural_scanner(const uint8_t *_buf, uint32_t _len, dom::parser &_doc_parser, bool _streaming) really_inline structural_scanner(const uint8_t *_buf, uint32_t _len, dom::parser &_parser, bool _streaming)
: buf{_buf}, next_structural_index{_doc_parser.structural_indexes.get()}, doc_parser{_doc_parser}, idx{0}, len{_len}, error{SUCCESS}, streaming{_streaming} {} : buf{_buf}, next_structural_index{_parser.structural_indexes.get()}, parser{_parser}, idx{0}, len{_len}, error{SUCCESS}, streaming{_streaming} {}
really_inline void add_structural() { really_inline void add_structural() {
*next_structural_index = idx; *next_structural_index = idx;
@ -123,19 +123,19 @@ really_inline error_code scan() {
break; break;
} }
} }
if (unlikely(next_structural_index == doc_parser.structural_indexes.get())) { if (unlikely(next_structural_index == parser.structural_indexes.get())) {
return EMPTY; return EMPTY;
} }
*next_structural_index = len; *next_structural_index = len;
next_structural_index++; next_structural_index++;
doc_parser.n_structural_indexes = uint32_t(next_structural_index - doc_parser.structural_indexes.get()); parser.n_structural_indexes = uint32_t(next_structural_index - parser.structural_indexes.get());
return error; return error;
} }
private: private:
const uint8_t *buf; const uint8_t *buf;
uint32_t *next_structural_index; uint32_t *next_structural_index;
dom::parser &doc_parser; dom::parser &parser;
uint32_t idx; uint32_t idx;
uint32_t len; uint32_t len;
error_code error; error_code error;

View File

@ -30,10 +30,10 @@ WARN_UNUSED error_code dom_parser_implementation::set_max_depth(size_t) noexcept
return SUCCESS; return SUCCESS;
} }
WARN_UNUSED error_code dom_parser_implementation::parse(const uint8_t *buf, size_t len, dom::parser &doc_parser) noexcept { WARN_UNUSED error_code dom_parser_implementation::parse(const uint8_t *buf, size_t len, dom::parser &parser) noexcept {
error_code code = stage1(buf, len, doc_parser, false); error_code code = stage1(buf, len, parser, false);
if (!code) { if (!code) {
code = stage2(buf, len, doc_parser); code = stage2(buf, len, parser);
} }
return code; return code;
} }

View File

@ -19,7 +19,7 @@ struct streaming_structural_parser: structural_parser {
// override to add streaming // override to add streaming
WARN_UNUSED really_inline error_code finish() { WARN_UNUSED really_inline error_code finish() {
if ( structurals.past_end(doc_parser.n_structural_indexes) ) { if ( structurals.past_end(parser.n_structural_indexes) ) {
log_error("IMPOSSIBLE: past the end of the JSON!"); log_error("IMPOSSIBLE: past the end of the JSON!");
return on_error(TAPE_ERROR); return on_error(TAPE_ERROR);
} }
@ -28,11 +28,11 @@ struct streaming_structural_parser: structural_parser {
log_error("Unclosed objects or arrays!"); log_error("Unclosed objects or arrays!");
return on_error(TAPE_ERROR); return on_error(TAPE_ERROR);
} }
if (doc_parser.containing_scope[depth].tape_index != 0) { if (parser.containing_scope[depth].tape_index != 0) {
log_error("IMPOSSIBLE: root scope tape index did not start at 0!"); log_error("IMPOSSIBLE: root scope tape index did not start at 0!");
return on_error(TAPE_ERROR); return on_error(TAPE_ERROR);
} }
bool finished = structurals.at_end(doc_parser.n_structural_indexes); bool finished = structurals.at_end(parser.n_structural_indexes);
if (!finished) { log_value("(and has more)"); } if (!finished) { log_value("(and has more)"); }
return on_success(finished ? SUCCESS : SUCCESS_AND_HAS_MORE); return on_success(finished ? SUCCESS : SUCCESS_AND_HAS_MORE);
} }
@ -123,7 +123,7 @@ object_continue:
} }
scope_end: scope_end:
CONTINUE( parser.doc_parser.ret_address[parser.depth] ); CONTINUE( parser.parser.ret_address[parser.depth] );
// //
// Array parser parsers // Array parser parsers

View File

@ -48,31 +48,31 @@ struct unified_machine_addresses {
#define FAIL_IF(EXPR) { if (EXPR) { return addresses.error; } } #define FAIL_IF(EXPR) { if (EXPR) { return addresses.error; } }
struct number_writer { struct number_writer {
dom::parser &doc_parser; dom::parser &parser;
really_inline void write_s64(int64_t value) noexcept { really_inline void write_s64(int64_t value) noexcept {
append_tape(0, internal::tape_type::INT64); append_tape(0, internal::tape_type::INT64);
std::memcpy(&doc_parser.doc.tape[doc_parser.current_loc], &value, sizeof(value)); std::memcpy(&parser.doc.tape[parser.current_loc], &value, sizeof(value));
++doc_parser.current_loc; ++parser.current_loc;
} }
really_inline void write_u64(uint64_t value) noexcept { really_inline void write_u64(uint64_t value) noexcept {
append_tape(0, internal::tape_type::UINT64); append_tape(0, internal::tape_type::UINT64);
doc_parser.doc.tape[doc_parser.current_loc++] = value; parser.doc.tape[parser.current_loc++] = value;
} }
really_inline void write_double(double value) noexcept { really_inline void write_double(double value) noexcept {
append_tape(0, internal::tape_type::DOUBLE); append_tape(0, internal::tape_type::DOUBLE);
static_assert(sizeof(value) == sizeof(doc_parser.doc.tape[doc_parser.current_loc]), "mismatch size"); static_assert(sizeof(value) == sizeof(parser.doc.tape[parser.current_loc]), "mismatch size");
memcpy(&doc_parser.doc.tape[doc_parser.current_loc++], &value, sizeof(double)); memcpy(&parser.doc.tape[parser.current_loc++], &value, sizeof(double));
// doc.tape[doc.current_loc++] = *((uint64_t *)&d); // doc.tape[doc.current_loc++] = *((uint64_t *)&d);
} }
really_inline void append_tape(uint64_t val, internal::tape_type t) noexcept { really_inline void append_tape(uint64_t val, internal::tape_type t) noexcept {
doc_parser.doc.tape[doc_parser.current_loc++] = val | ((uint64_t(char(t))) << 56); parser.doc.tape[parser.current_loc++] = val | ((uint64_t(char(t))) << 56);
} }
}; // struct number_writer }; // struct number_writer
struct structural_parser { struct structural_parser {
structural_iterator structurals; structural_iterator structurals;
dom::parser &doc_parser; dom::parser &parser;
/** Next write location in the string buf for stage 2 parsing */ /** Next write location in the string buf for stage 2 parsing */
uint8_t *current_string_buf_loc{}; uint8_t *current_string_buf_loc{};
uint32_t depth; uint32_t depth;
@ -80,17 +80,17 @@ struct structural_parser {
really_inline structural_parser( really_inline structural_parser(
const uint8_t *buf, const uint8_t *buf,
size_t len, size_t len,
dom::parser &_doc_parser, dom::parser &_parser,
uint32_t next_structural = 0 uint32_t next_structural = 0
) : structurals(buf, len, _doc_parser.structural_indexes.get(), next_structural), doc_parser{_doc_parser}, depth{0} {} ) : structurals(buf, len, _parser.structural_indexes.get(), next_structural), parser{_parser}, depth{0} {}
WARN_UNUSED really_inline bool start_scope(ret_address continue_state) { WARN_UNUSED really_inline bool start_scope(ret_address continue_state) {
doc_parser.containing_scope[depth].tape_index = doc_parser.current_loc; parser.containing_scope[depth].tape_index = parser.current_loc;
doc_parser.containing_scope[depth].count = 0; parser.containing_scope[depth].count = 0;
doc_parser.current_loc++; // We don't actually *write* the start element until the end. parser.current_loc++; // We don't actually *write* the start element until the end.
doc_parser.ret_address[depth] = continue_state; parser.ret_address[depth] = continue_state;
depth++; depth++;
bool exceeded_max_depth = depth >= doc_parser.max_depth(); bool exceeded_max_depth = depth >= parser.max_depth();
if (exceeded_max_depth) { log_error("Exceeded max depth!"); } if (exceeded_max_depth) { log_error("Exceeded max depth!"); }
return exceeded_max_depth; return exceeded_max_depth;
} }
@ -115,14 +115,14 @@ struct structural_parser {
depth--; depth--;
// write our doc.tape location to the header scope // write our doc.tape location to the header scope
// The root scope gets written *at* the previous location. // The root scope gets written *at* the previous location.
append_tape(doc_parser.containing_scope[depth].tape_index, end); append_tape(parser.containing_scope[depth].tape_index, end);
// count can overflow if it exceeds 24 bits... so we saturate // count can overflow if it exceeds 24 bits... so we saturate
// the convention being that a cnt of 0xffffff or more is undetermined in value (>= 0xffffff). // the convention being that a cnt of 0xffffff or more is undetermined in value (>= 0xffffff).
const uint32_t start_tape_index = doc_parser.containing_scope[depth].tape_index; const uint32_t start_tape_index = parser.containing_scope[depth].tape_index;
const uint32_t count = doc_parser.containing_scope[depth].count; const uint32_t count = parser.containing_scope[depth].count;
const uint32_t cntsat = count > 0xFFFFFF ? 0xFFFFFF : count; const uint32_t cntsat = count > 0xFFFFFF ? 0xFFFFFF : count;
// This is a load and an OR. It would be possible to just write once at doc.tape[d.tape_index] // This is a load and an OR. It would be possible to just write once at doc.tape[d.tape_index]
write_tape(start_tape_index, doc_parser.current_loc | (uint64_t(cntsat) << 32), start); write_tape(start_tape_index, parser.current_loc | (uint64_t(cntsat) << 32), start);
} }
really_inline void end_object() { really_inline void end_object() {
@ -139,11 +139,11 @@ struct structural_parser {
} }
really_inline void append_tape(uint64_t val, internal::tape_type t) noexcept { really_inline void append_tape(uint64_t val, internal::tape_type t) noexcept {
doc_parser.doc.tape[doc_parser.current_loc++] = val | ((uint64_t(char(t))) << 56); parser.doc.tape[parser.current_loc++] = val | ((uint64_t(char(t))) << 56);
} }
really_inline void write_tape(uint32_t loc, uint64_t val, internal::tape_type t) noexcept { really_inline void write_tape(uint32_t loc, uint64_t val, internal::tape_type t) noexcept {
doc_parser.doc.tape[loc] = val | ((uint64_t(char(t))) << 56); parser.doc.tape[loc] = val | ((uint64_t(char(t))) << 56);
} }
// increment_count increments the count of keys in an object or values in an array. // increment_count increments the count of keys in an object or values in an array.
@ -151,12 +151,12 @@ struct structural_parser {
// must be increment in the preceding depth (depth-1) where the array or // must be increment in the preceding depth (depth-1) where the array or
// the object resides. // the object resides.
really_inline void increment_count() { really_inline void increment_count() {
doc_parser.containing_scope[depth - 1].count++; // we have a key value pair in the object at parser.depth - 1 parser.containing_scope[depth - 1].count++; // we have a key value pair in the object at parser.depth - 1
} }
really_inline uint8_t *on_start_string() noexcept { really_inline uint8_t *on_start_string() noexcept {
// we advance the point, accounting for the fact that we have a NULL termination // we advance the point, accounting for the fact that we have a NULL termination
append_tape(current_string_buf_loc - doc_parser.doc.string_buf.get(), internal::tape_type::STRING); append_tape(current_string_buf_loc - parser.doc.string_buf.get(), internal::tape_type::STRING);
return current_string_buf_loc + sizeof(uint32_t); return current_string_buf_loc + sizeof(uint32_t);
} }
@ -186,7 +186,7 @@ struct structural_parser {
WARN_UNUSED really_inline bool parse_number(const uint8_t *src, bool found_minus) { WARN_UNUSED really_inline bool parse_number(const uint8_t *src, bool found_minus) {
log_value("number"); log_value("number");
number_writer writer{doc_parser}; number_writer writer{parser};
bool succeeded = numberparsing::parse_number(src, found_minus, writer); bool succeeded = numberparsing::parse_number(src, found_minus, writer);
if (!succeeded) { log_error("Invalid number"); } if (!succeeded) { log_error("Invalid number"); }
return !succeeded; return !succeeded;
@ -272,7 +272,7 @@ struct structural_parser {
WARN_UNUSED really_inline error_code finish() { WARN_UNUSED really_inline error_code finish() {
// the string might not be NULL terminated. // the string might not be NULL terminated.
if ( !structurals.at_end(doc_parser.n_structural_indexes) ) { if ( !structurals.at_end(parser.n_structural_indexes) ) {
log_error("More than one JSON value at the root of the document, or extra characters at the end of the JSON!"); log_error("More than one JSON value at the root of the document, or extra characters at the end of the JSON!");
return on_error(TAPE_ERROR); return on_error(TAPE_ERROR);
} }
@ -281,7 +281,7 @@ struct structural_parser {
log_error("Unclosed objects or arrays!"); log_error("Unclosed objects or arrays!");
return on_error(TAPE_ERROR); return on_error(TAPE_ERROR);
} }
if (doc_parser.containing_scope[depth].tape_index != 0) { if (parser.containing_scope[depth].tape_index != 0) {
log_error("IMPOSSIBLE: root scope tape index did not start at 0!"); log_error("IMPOSSIBLE: root scope tape index did not start at 0!");
return on_error(TAPE_ERROR); return on_error(TAPE_ERROR);
} }
@ -290,19 +290,19 @@ struct structural_parser {
} }
really_inline error_code on_error(error_code new_error_code) noexcept { really_inline error_code on_error(error_code new_error_code) noexcept {
doc_parser.error = new_error_code; parser.error = new_error_code;
return new_error_code; return new_error_code;
} }
really_inline error_code on_success(error_code success_code) noexcept { really_inline error_code on_success(error_code success_code) noexcept {
doc_parser.error = success_code; parser.error = success_code;
doc_parser.valid = true; parser.valid = true;
return success_code; return success_code;
} }
WARN_UNUSED really_inline error_code error() { WARN_UNUSED really_inline error_code error() {
/* We do not need the next line because this is done by doc_parser.init_stage2(), /* We do not need the next line because this is done by parser.init_stage2(),
* pessimistically. * pessimistically.
* doc_parser.is_valid = false; * parser.is_valid = false;
* At this point in the code, we have all the time in the world. * At this point in the code, we have all the time in the world.
* Note that we know exactly where we are in the document so we could, * Note that we know exactly where we are in the document so we could,
* without any overhead on the processing code, report a specific * without any overhead on the processing code, report a specific
@ -310,7 +310,7 @@ struct structural_parser {
* We could even trigger special code paths to assess what happened * We could even trigger special code paths to assess what happened
* carefully, * carefully,
* all without any added cost. */ * all without any added cost. */
if (depth >= doc_parser.max_depth()) { if (depth >= parser.max_depth()) {
return on_error(DEPTH_ERROR); return on_error(DEPTH_ERROR);
} }
switch (structurals.current_char()) { switch (structurals.current_char()) {
@ -340,16 +340,16 @@ struct structural_parser {
} }
really_inline void init() { really_inline void init() {
current_string_buf_loc = doc_parser.doc.string_buf.get(); current_string_buf_loc = parser.doc.string_buf.get();
doc_parser.current_loc = 0; parser.current_loc = 0;
doc_parser.valid = false; parser.valid = false;
doc_parser.error = UNINITIALIZED; parser.error = UNINITIALIZED;
} }
WARN_UNUSED really_inline error_code start(size_t len, ret_address finish_state) { WARN_UNUSED really_inline error_code start(size_t len, ret_address finish_state) {
log_start(); log_start();
init(); // sets is_valid to false init(); // sets is_valid to false
if (len > doc_parser.capacity()) { if (len > parser.capacity()) {
return CAPACITY; return CAPACITY;
} }
// Advance to the first character as soon as possible // Advance to the first character as soon as possible
@ -479,7 +479,7 @@ object_continue:
} }
scope_end: scope_end:
CONTINUE( parser.doc_parser.ret_address[parser.depth] ); CONTINUE( parser.parser.ret_address[parser.depth] );
// //
// Array parser states // Array parser states

View File

@ -30,10 +30,10 @@ WARN_UNUSED error_code dom_parser_implementation::set_max_depth(size_t) noexcept
return SUCCESS; return SUCCESS;
} }
WARN_UNUSED error_code dom_parser_implementation::parse(const uint8_t *buf, size_t len, dom::parser &doc_parser) noexcept { WARN_UNUSED error_code dom_parser_implementation::parse(const uint8_t *buf, size_t len, dom::parser &parser) noexcept {
error_code code = stage1(buf, len, doc_parser, false); error_code code = stage1(buf, len, parser, false);
if (!code) { if (!code) {
code = stage2(buf, len, doc_parser); code = stage2(buf, len, parser);
} }
return code; return code;
} }

View File

@ -30,10 +30,10 @@ WARN_UNUSED error_code dom_parser_implementation::set_max_depth(size_t) noexcept
return SUCCESS; return SUCCESS;
} }
WARN_UNUSED error_code dom_parser_implementation::parse(const uint8_t *buf, size_t len, dom::parser &doc_parser) noexcept { WARN_UNUSED error_code dom_parser_implementation::parse(const uint8_t *buf, size_t len, dom::parser &parser) noexcept {
error_code code = stage1(buf, len, doc_parser, false); error_code code = stage1(buf, len, parser, false);
if (!code) { if (!code) {
code = stage2(buf, len, doc_parser); code = stage2(buf, len, parser);
} }
return code; return code;
} }