Vince's CSV Parser
Loading...
Searching...
No Matches
validator.hpp
1#pragma once
2
3#include "chunks.hpp"
4
5#if CSV_ENABLE_THREADS
6
7namespace csv {
8 namespace internals {
9 namespace speculative {
10 template<typename RowSink, typename Parser = CSVParserCore<std::vector<CSVRow>>>
11 class SpeculativeParseValidator {
12 public:
13 SpeculativeParseValidator(
14 Parser& repair_parser,
15 RowSink& output,
16 ParserDFAState initial_state = ParserDFAState()
17 ) : repair_parser_(repair_parser),
18 output_(output),
19 expected_start_state_(initial_state) {}
20
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(
24 this->repair_parser_,
25 chunk,
26 this->expected_start_state_
27 );
28 this->repair_count_++;
29 }
30
31 const ParserChunkResult parse_result = chunk.parse_result;
32 this->release(std::move(chunk));
33 return parse_result;
34 }
35
36 void finish(bool flush_pending = true) {
37 if (flush_pending) {
38 this->release_pending_suffix();
39 }
40 }
41
42 size_t repair_count() const noexcept {
43 return this->repair_count_;
44 }
45
46 ParserDFAState expected_start_state() const noexcept {
47 return this->expected_start_state_;
48 }
49
50 bool has_pending_suffix() const noexcept {
51 return !this->pending_suffix_.empty();
52 }
53
54 const CSVRowFragment& pending_suffix() const noexcept {
55 return this->pending_suffix_;
56 }
57
58 private:
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_,
64 chunk.prefix_fragment
65 );
66 }
67 else {
68 this->pending_suffix_ = chunk.prefix_fragment;
69 }
70
71 if (chunk.parse_result.complete_prefix_length == 0) {
72 this->expected_start_state_ = chunk.parse_result.ending_state;
73 return;
74 }
75
76 this->release_pending_suffix();
77 }
78
79 csv_append_rows(this->output_, std::move(chunk.complete_rows));
80
81 this->pending_suffix_ = chunk.suffix_fragment;
82 this->expected_start_state_ = chunk.parse_result.ending_state;
83 }
84
85 void release_pending_suffix() {
86 if (this->pending_suffix_.empty()) {
87 return;
88 }
89
90 auto rows = materialize_row_fragment(this->repair_parser_, this->pending_suffix_);
91 csv_append_rows(this->output_, std::move(rows));
92
93 this->pending_suffix_ = CSVRowFragment();
94 }
95
96 Parser& repair_parser_;
97 RowSink& output_;
98 ParserDFAState expected_start_state_;
99 CSVRowFragment pending_suffix_;
100 size_t repair_count_ = 0;
101 };
102 }
103 }
104}
105
106#endif
The all encompassing namespace.