9 namespace speculative {
10 template<
typename RowSink,
typename Parser = CSVParserCore<std::vector<CSVRow>>>
11 class SpeculativeParseValidator {
13 SpeculativeParseValidator(
14 Parser& repair_parser,
16 ParserDFAState initial_state = ParserDFAState()
17 ) : repair_parser_(repair_parser),
19 expected_start_state_(initial_state) {}
21 ParserChunkResult validate_and_release(ParsedChunkRows&& chunk) {
22 if (!parser_dfa_state_equal(chunk.parse_result.initial_state, this->expected_start_state_)) {
23 chunk = repair_parsed_chunk_rows(
26 this->expected_start_state_
28 this->repair_count_++;
31 const ParserChunkResult parse_result = chunk.parse_result;
32 this->release(std::move(chunk));
36 void finish(
bool flush_pending =
true) {
38 this->release_pending_suffix();
42 size_t repair_count() const noexcept {
43 return this->repair_count_;
46 ParserDFAState expected_start_state() const noexcept {
47 return this->expected_start_state_;
50 bool has_pending_suffix() const noexcept {
51 return !this->pending_suffix_.empty();
54 const CSVRowFragment& pending_suffix() const noexcept {
55 return this->pending_suffix_;
59 void release(ParsedChunkRows&& chunk) {
60 if (!chunk.prefix_fragment.empty()) {
61 if (!this->pending_suffix_.empty()) {
62 this->pending_suffix_ = concatenate_row_fragments(
63 this->pending_suffix_,
68 this->pending_suffix_ = chunk.prefix_fragment;
71 if (chunk.parse_result.complete_prefix_length == 0) {
72 this->expected_start_state_ = chunk.parse_result.ending_state;
76 this->release_pending_suffix();
79 csv_append_rows(this->output_, std::move(chunk.complete_rows));
81 this->pending_suffix_ = chunk.suffix_fragment;
82 this->expected_start_state_ = chunk.parse_result.ending_state;
85 void release_pending_suffix() {
86 if (this->pending_suffix_.empty()) {
90 auto rows = materialize_row_fragment(this->repair_parser_, this->pending_suffix_);
91 csv_append_rows(this->output_, std::move(rows));
93 this->pending_suffix_ = CSVRowFragment();
96 Parser& repair_parser_;
98 ParserDFAState expected_start_state_;
99 CSVRowFragment pending_suffix_;
100 size_t repair_count_ = 0;
The all encompassing namespace.