taocpp / PEGTL

Parsing Expression Grammar Template Library
Boost Software License 1.0
1.94k stars 228 forks source link

Add CSV parser example #33

Closed AriaFallah closed 7 years ago

AriaFallah commented 8 years ago

Hey Colin, great library! I'm looking forward to using it. I actually got here through googling C++ CSV Parser, which took me here where you wrote:

CSV isn't a precisely defined format; on multiple occasions I slapped together a simple CSV parser for whatever file format was thrown my way with our PEGTL parser library (PEG based, C++11, header-only, production quality, small and light - and with documentation).

Unfortunately, I didn't see a CSV parser in your examples folder, and I was hoping you could add one such that I could see how it'd be done idiomatically with PEGTL.

ColinH commented 7 years ago

This greatly depends on whether you know the number of fields and their contents (e.g. any string, or just an integer?) at compile time... I'll whack together two small examples to show this.

ColinH commented 7 years ago

Done in commit e90e9fd85d1a0756359ad6007e2eb7f9b5cf9350.

ColinH commented 7 years ago

I have extended the first example, examples/csv1.cc, to also build a simple data structure with the parsed data.

AriaFallah commented 7 years ago

Thanks a lot for this! It's very much appreciated.

ColinH commented 7 years ago

Good to hear, thanks! I just added actions to examples/csv2.cc, too, which requires a bit of fiddling around to bridge from run-time-sized std::vector to compile-time-sized std::tuple. In real-world code you would probably use a custom class to store a line.