I want to parse the following (first Column is Identifier, second Column (date) is unique for each Identifier followed by a tuple of float's):
Max,2016-02-01,1.0,2.0,3.0
Max,2016-02-02,1.0,2.0,3.0
Rob,2016-02-01,1.0,2.0,3.0
Max,2016-02-03,1.0,2.0,3.0
my favorite structure would be
using ValueType = std::tuple<float, float, float>;
using ValueMap = std::map<std::time_t, ValueType>;
using DataType = std::unordered_map<std::string, ValueMap>;
Is this possible to create a valid grammar with attribute propagation (without semantic actions and/or later copy to this structure ) ?
The parsing grammar could look like:
namespace grammar
{
using namespace x3;
auto str2date = [](auto& ctx)
{
int y,m,d;
auto tub = std::tie(y,m,d);
fusion::copy(_attr(ctx), tub);
std::tm t{};
t.tm_year = y - 1900;
t.tm_mon = m - 1;
t.tm_mday = d;
auto time = std::mktime(&t);
_val(ctx) = time;
_pass(ctx) = time > 0;
};
auto date_ = rule<struct date_, std::time_t>{"date"}
= (int_ >> '-' >> int_ >> int_)[str2date];
auto values_= rule<struct values_, ValueType>{"values"}
= float_ >> ',' >> float_ >> ',' >> float_;
auto line_ = rule<struct line_, std::pair<std::time_t, ValueType>>{"line"}
= date_ >> ',' >> values_;
auto start_ = rule<struct start_, DataType>{"start"}
= (+~char_(',') >> ',' >> line_) % eol >> (eol | eoi);
};