-
Notifications
You must be signed in to change notification settings - Fork 246
Expand file tree
/
Copy pathcsv_2.cpp
More file actions
191 lines (161 loc) · 5.37 KB
/
csv_2.cpp
File metadata and controls
191 lines (161 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) 2016-2026 Dr. Colin Hirsch and Daniel Frey
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
#include <exception>
#include <iostream>
#include <utility>
#include <vector>
#include <tao/pegtl.hpp>
namespace pegtl = TAO_PEGTL_NAMESPACE;
namespace example
{
// Simple CSV-file format for a known-at-compile-time number of values per
// line, the values are strings that can use quotes when they contain commas,
// if quotes are used they have to be the first character (of the line or
// after the comma); quoted strings can't contain quotes, no string can have
// LF or CR; last line has to end with tao::pegtl::eol.
// Example file contents parsed by this grammar (excluding C++ comment intro):
// a,b,c
// "foo","bar","baz"
// ",,,",13,42
// aha """,yes, this works
// clang-format off
template< char C > struct string_without : pegtl::star< pegtl::not_one7< C, '\n', '\r' > > {};
struct plain_value : string_without< ',' > {};
struct quoted_value : pegtl::seq< pegtl::one< '"' >, string_without< '"' >, pegtl::one< '"' > > {};
struct value : pegtl::sor< quoted_value, plain_value > {};
template< unsigned N > struct line : pegtl::seq< value, pegtl::rep< N - 1, pegtl::one< ',' >, value >, pegtl::eol > {};
template< unsigned N > struct file : pegtl::until< pegtl::eof, line< N > > { static_assert( N != 0 ); };
// clang-format on
// Meta-programming helper:
template< unsigned N, typename T >
struct tuple_help;
template< unsigned N, typename... S >
struct tuple_help< N, std::tuple< S... > >
{
using tuple_t = typename tuple_help< N - 1, std::tuple< std::string, S... > >::tuple_t;
};
template< typename... S >
struct tuple_help< 0, std::tuple< S... > >
{
using tuple_t = std::tuple< S... >;
};
// Ad-hoc helper to initialize a tuple from a vector:
template< unsigned I >
struct tuple_init
{
template< typename... S >
static void init( std::tuple< S... >& t, std::vector< std::string >& v )
{
std::get< I >( t ) = std::move( v[ I ] );
tuple_init< I - 1 >::init( t, v );
}
};
template<>
struct tuple_init< 0 >
{
template< typename... S >
static void init( std::tuple< S... >& t, std::vector< std::string >& v )
{
std::get< 0 >( t ) = std::move( v[ 0 ] );
}
};
// Data structure to store the result of a parsing run:
template< unsigned N >
struct result_data
{
using tuple_t = typename tuple_help< N, std::tuple<> >::tuple_t;
std::vector< std::string > temp;
std::vector< tuple_t > result;
};
// Action class to fill in the above data structure:
template< typename Rule >
struct action
{};
template<>
struct action< plain_value >
{
template< typename ActionInput, unsigned N >
static void apply( const ActionInput& in, result_data< N >& data )
{
data.temp.push_back( in.string() );
}
};
template<>
struct action< string_without< '"' > >
: action< plain_value >
{};
template< unsigned N >
struct action< line< N > >
{
using tuple_t = typename tuple_help< N, std::tuple<> >::tuple_t;
template< typename ActionInput >
static void apply( const ActionInput& in, result_data< N >& data )
{
if( data.temp.size() != N ) {
std::cerr << "column count mismatch " << in.current_position() << std::endl;
std::terminate();
}
tuple_t temp;
tuple_init< N - 1 >::init( temp, data.temp );
data.result.emplace_back( std::move( temp ) );
data.temp.clear();
}
};
// Another helper to print tuples of arbitrary sizes:
inline void print_string( const std::string& s )
{
// Needs more elaborate escaping in practice...
if( s.find( ',' ) != std::string::npos ) {
std::cout << '"' << s << '"';
}
else {
std::cout << s;
}
}
template< unsigned I >
struct print_help
{
template< typename... S >
static void print( const std::tuple< S... >& t )
{
print_help< I - 1 >::print( t );
std::cout << ',';
print_string( std::get< I >( t ) );
}
};
template<>
struct print_help< 0 >
{
template< typename... S >
static void print( const std::tuple< S... >& t )
{
print_string( std::get< 0 >( t ) );
}
};
template< typename... S >
void print_tuple( const std::tuple< S... >& t )
{
constexpr unsigned size = sizeof...( S );
static_assert( size, "empty tuple doesn't work here" );
print_help< size - 1 >::print( t );
std::cout << std::endl;
}
} // namespace example
int main( int argc, char** argv ) // NOLINT(bugprone-exception-escape)
{
for( int i = 1; i < argc; ++i ) {
pegtl::text_file_input< pegtl::lazy::lf_crlf > in( argv[ i ] );
constexpr unsigned number_of_columns = 3;
example::result_data< number_of_columns > data;
if( pegtl::parse< pegtl::seq< example::file< number_of_columns > >, example::action >( in, data ) ) {
for( const auto& line : data.result ) {
example::print_tuple( line );
}
}
else {
std::cerr << "parse error" << std::endl;
}
}
return 0;
}