Move on_error, on_success to stage 2 code

This commit is contained in:
John Keiser 2020-05-11 05:46:18 -07:00
parent 27bce09be8
commit 35afb6cae0
4 changed files with 42 additions and 63 deletions

View File

@ -1023,13 +1023,6 @@ public:
//
/** @private this should be called when parsing (right before writing the tapes) */
inline void init_stage2() noexcept;
really_inline error_code on_error(error_code new_error_code) noexcept; ///< @private
really_inline error_code on_success(error_code success_code) noexcept; ///< @private
// TODO we're not checking this bool
really_inline bool on_true_atom() noexcept; ///< @private
really_inline bool on_false_atom() noexcept; ///< @private
really_inline bool on_null_atom() noexcept; ///< @private
really_inline uint8_t *on_start_string() noexcept; ///< @private
really_inline bool on_end_string(uint8_t *dst) noexcept; ///< @private
really_inline bool on_number_s64(int64_t value) noexcept; ///< @private

View File

@ -10,37 +10,6 @@ namespace dom {
// Parser callbacks
//
inline void parser::init_stage2() noexcept {
current_string_buf_loc = doc.string_buf.get();
current_loc = 0;
valid = false;
error = UNINITIALIZED;
}
really_inline error_code parser::on_error(error_code new_error_code) noexcept {
error = new_error_code;
return new_error_code;
}
really_inline error_code parser::on_success(error_code success_code) noexcept {
error = success_code;
valid = true;
return success_code;
}
really_inline bool parser::on_true_atom() noexcept {
write_tape(0, internal::tape_type::TRUE_VALUE);
return true;
}
really_inline bool parser::on_false_atom() noexcept {
write_tape(0, internal::tape_type::FALSE_VALUE);
return true;
}
really_inline bool parser::on_null_atom() noexcept {
write_tape(0, internal::tape_type::NULL_VALUE);
return true;
}
really_inline uint8_t *parser::on_start_string() noexcept {
/* we advance the point, accounting for the fact that we have a NULL
* termination */

View File

@ -199,15 +199,15 @@ struct structural_parser {
switch (structurals.current_char()) {
case 't':
if (!atomparsing::is_valid_true_atom(structurals.current())) { return true; }
doc_parser.on_true_atom();
doc_parser.write_tape(0, internal::tape_type::TRUE_VALUE);
break;
case 'f':
if (!atomparsing::is_valid_false_atom(structurals.current())) { return true; }
doc_parser.on_false_atom();
doc_parser.write_tape(0, internal::tape_type::FALSE_VALUE);
break;
case 'n':
if (!atomparsing::is_valid_null_atom(structurals.current())) { return true; }
doc_parser.on_null_atom();
doc_parser.write_tape(0, internal::tape_type::NULL_VALUE);
break;
default:
return true;
@ -219,15 +219,15 @@ struct structural_parser {
switch (structurals.current_char()) {
case 't':
if (!atomparsing::is_valid_true_atom(structurals.current(), structurals.remaining_len())) { return true; }
doc_parser.on_true_atom();
doc_parser.write_tape(0, internal::tape_type::TRUE_VALUE);
break;
case 'f':
if (!atomparsing::is_valid_false_atom(structurals.current(), structurals.remaining_len())) { return true; }
doc_parser.on_false_atom();
doc_parser.write_tape(0, internal::tape_type::FALSE_VALUE);
break;
case 'n':
if (!atomparsing::is_valid_null_atom(structurals.current(), structurals.remaining_len())) { return true; }
doc_parser.on_null_atom();
doc_parser.write_tape(0, internal::tape_type::NULL_VALUE);
break;
default:
return true;
@ -264,17 +264,27 @@ struct structural_parser {
WARN_UNUSED really_inline error_code finish() {
// the string might not be NULL terminated.
if ( !structurals.at_end(doc_parser.n_structural_indexes) ) {
return doc_parser.on_error(TAPE_ERROR);
return on_error(TAPE_ERROR);
}
end_document();
if (depth != 0) {
return doc_parser.on_error(TAPE_ERROR);
return on_error(TAPE_ERROR);
}
if (doc_parser.containing_scope[depth].tape_index != 0) {
return doc_parser.on_error(TAPE_ERROR);
return on_error(TAPE_ERROR);
}
return doc_parser.on_success(SUCCESS);
return on_success(SUCCESS);
}
really_inline error_code on_error(error_code new_error_code) noexcept {
doc_parser.error = new_error_code;
return new_error_code;
}
really_inline error_code on_success(error_code success_code) noexcept {
doc_parser.error = success_code;
doc_parser.valid = true;
return success_code;
}
WARN_UNUSED really_inline error_code error() {
@ -289,11 +299,11 @@ struct structural_parser {
* carefully,
* all without any added cost. */
if (depth >= doc_parser.max_depth()) {
return doc_parser.on_error(DEPTH_ERROR);
return on_error(DEPTH_ERROR);
}
switch (structurals.current_char()) {
case '"':
return doc_parser.on_error(STRING_ERROR);
return on_error(STRING_ERROR);
case '0':
case '1':
case '2':
@ -305,20 +315,27 @@ struct structural_parser {
case '8':
case '9':
case '-':
return doc_parser.on_error(NUMBER_ERROR);
return on_error(NUMBER_ERROR);
case 't':
return doc_parser.on_error(T_ATOM_ERROR);
return on_error(T_ATOM_ERROR);
case 'n':
return doc_parser.on_error(N_ATOM_ERROR);
return on_error(N_ATOM_ERROR);
case 'f':
return doc_parser.on_error(F_ATOM_ERROR);
return on_error(F_ATOM_ERROR);
default:
return doc_parser.on_error(TAPE_ERROR);
return on_error(TAPE_ERROR);
}
}
really_inline void init() {
doc_parser.current_string_buf_loc = doc_parser.doc.string_buf.get();
doc_parser.current_loc = 0;
doc_parser.valid = false;
doc_parser.error = UNINITIALIZED;
}
WARN_UNUSED really_inline error_code start(size_t len, ret_address finish_state) {
doc_parser.init_stage2(); // sets is_valid to false
init(); // sets is_valid to false
if (len > doc_parser.capacity()) {
return CAPACITY;
}
@ -326,7 +343,7 @@ struct structural_parser {
structurals.advance_char();
// Push the root scope (there is always at least one scope)
if (start_document(finish_state)) {
return doc_parser.on_error(DEPTH_ERROR);
return on_error(DEPTH_ERROR);
}
return SUCCESS;
}

View File

@ -5,13 +5,13 @@ struct streaming_structural_parser: structural_parser {
// override to add streaming
WARN_UNUSED really_inline error_code start(UNUSED size_t len, ret_address finish_parser) {
doc_parser.init_stage2(); // sets is_valid to false
init(); // sets is_valid to false
// Capacity ain't no thang for streaming, so we don't check it.
// Advance to the first character as soon as possible
advance_char();
// Push the root scope (there is always at least one scope)
if (start_document(finish_parser)) {
return doc_parser.on_error(DEPTH_ERROR);
return on_error(DEPTH_ERROR);
}
return SUCCESS;
}
@ -19,17 +19,17 @@ struct streaming_structural_parser: structural_parser {
// override to add streaming
WARN_UNUSED really_inline error_code finish() {
if ( structurals.past_end(doc_parser.n_structural_indexes) ) {
return doc_parser.on_error(TAPE_ERROR);
return on_error(TAPE_ERROR);
}
end_document();
if (depth != 0) {
return doc_parser.on_error(TAPE_ERROR);
return on_error(TAPE_ERROR);
}
if (doc_parser.containing_scope[depth].tape_index != 0) {
return doc_parser.on_error(TAPE_ERROR);
return on_error(TAPE_ERROR);
}
bool finished = structurals.at_end(doc_parser.n_structural_indexes);
return doc_parser.on_success(finished ? SUCCESS : SUCCESS_AND_HAS_MORE);
return on_success(finished ? SUCCESS : SUCCESS_AND_HAS_MORE);
}
};