extract parser driver

This commit is contained in:
2023-04-08 12:08:51 -07:00
parent a38a105e6f
commit c221dc9bf2
8 changed files with 139 additions and 129 deletions

View File

@@ -1,2 +1,3 @@
add_library(aocpp src/Startup.cpp src/Coord.cpp src/Parsing.cpp)
target_include_directories(aocpp PUBLIC include)
target_include_directories(aocpp PUBLIC include)
target_link_libraries(aocpp Boost::headers)

View File

@@ -2,12 +2,34 @@
#define AOCPP_PARSING_HPP_
#include <string>
#include <iostream>
#include <iterator>
#include <vector>
#include <boost/spirit/home/qi.hpp>
namespace aocpp {
auto SplitOn(std::string const& stuff, std::string const& sep) -> std::vector<std::string>;
/// Parse the complete input stream according to the rules defined in 'Grammar'.
/// Throws: std::runtime_error on failed parse
template <typename G>
auto ParseGrammar(G const& grammar, std::istream & in) -> typename G::start_type::attr_type
{
namespace qi = boost::spirit::qi;
auto result = typename G::start_type::attr_type {};
auto const content = std::string{std::istreambuf_iterator{in}, {}};
auto b = content.begin(); // updated on successful parse
auto const e = content.end();
if (!qi::parse(b, e, grammar, result) || b != e) {
throw std::runtime_error{"Bad input file: " + content};
}
return result;
}
}
#endif