Semantic actions calling lambda - c++

I'm trying to parse a time string using boost spirit and not sure why this doesn't work.
auto fill_ts_nanos = [&t] (int h, int m, int s, int ms) -> int
{ t.tv_nsec = ( ( h * 3600 + m * 60 + s ) * 1000 + ms ) * 1000000; return t.tv_sec; };
auto fill_suffix = [&suffix] (string &s) { suffix=s; };
auto parse_ok = qi::parse(input.begin(), input.end(),
( qi::int_ >> qi::char_(":") >> qi::int_ >> qi::char_(":") >>
qi::int_ >> qi::char_(".") >> qi::int_ )
[boost::bind(fill_ts_nanos, qi::_1, qi::_3, qi::_5, qi::_7
>> qi::char_(",") >> qi::as_string[*qi::char_][fill_suffix] ;
A sample input is "04:00:00.512,2251812698588658"

After guessing a lot of details (e.g. what the type of t is supposed to be), here's the fixed code with some debug output:
Live On Coliru
Note: I fixed the signed-ness of the numbers as well as I changed the types to prevent overflow.
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <ctime>
#include <chrono>
#include <iomanip>
namespace qi = boost::spirit::qi;
namespace px = boost::phoenix;
using namespace std::chrono_literals;
using namespace qi::labels;
int main() {
timespec t;
std::string suffix;
auto fill_ts_nanos = [&t](int h, unsigned m, unsigned s, unsigned ms) -> long {
t = {};
t.tv_nsec = ((h * 3600 + m * 60 + s) * 1000 + ms) * 1000000l;
return t.tv_sec;
};
auto fill_suffix = [&suffix](std::string &s) { suffix = s; };
std::string const input = "04:00:00.512,2251812698588658";
auto parse_ok = qi::parse(input.begin(), input.end(),
(qi::int_ >> ':' >> qi::uint_ >> ':' >> qi::uint_ >> '.' >> qi::uint_)
[px::bind(fill_ts_nanos, _1, _2, _3, _4) ]
>> ',' >> qi::as_string[*qi::char_]
[fill_suffix] );
std::printf("%lld.%.9ld\n", (long long)t.tv_sec, t.tv_nsec);
auto ns = t.tv_nsec * 1ns;
std::cout << std::fixed << std::setprecision(6);
std::cout << "hours: " << (ns / 1.0h) << "\n";
std::cout << "minutes: " << (ns / 1.0min) << "\n";
std::cout << "seconds: " << (ns / 1.0s) << "\n";
std::cout << "suffix: " << suffix << "\n";
return parse_ok? 0:255;
}
Prints
0.14400512000000
hours: 4.000142
minutes: 240.008533
seconds: 14400.512000
suffix: 2251812698588658
Suggestions
I'd try to simplify this by a lot, e.g., by creating a rule:
qi::rule<It, long()> timespec_ =
(qi::int_ >> ':' >> qi::uint_ >> ':' >> qi::uint_ >> '.' >> qi::uint_)
[ _val = ((_1 * 3600 + _2 * 60 + _3) * 1000 + _4) * 1000000l ];
Which means you can then parse with nothing else but:
timespec t {};
std::string suffix;
It f = input.begin(), l = input.end();
parse(f, l, timespec_ >> ',' >> *qi::char_, t.tv_nsec, suffix);
This has the same output:
Live On Coliru
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <ctime>
#include <chrono>
#include <iomanip>
namespace qi = boost::spirit::qi;
namespace px = boost::phoenix;
using namespace std::chrono_literals;
using namespace qi::labels;
using It = std::string::const_iterator;
qi::rule<It, long()> timespec_ =
(qi::int_ >> ':' >> qi::uint_ >> ':' >> qi::uint_ >> '.' >> qi::uint_)
[ _val = ((_1 * 3600 + _2 * 60 + _3) * 1000 + _4) * 1000000l ];
int main() {
std::string const input = "04:00:00.512,2251812698588658";
timespec t {};
std::string suffix;
It f = input.begin(), l = input.end();
if (parse(f, l, timespec_ >> ',' >> *qi::char_, t.tv_nsec, suffix)) {
std::printf("%lld.%.9ld\n", (long long)t.tv_sec, t.tv_nsec);
auto ns = t.tv_nsec * 1ns;
std::cout << std::fixed << std::setprecision(6);
std::cout << "hours: " << (ns / 1.0h) << "\n";
std::cout << "minutes: " << (ns / 1.0min) << "\n";
std::cout << "seconds: " << (ns / 1.0s) << "\n";
std::cout << "suffix: " << suffix << "\n";
} else {
std::cout << "Parse failed\n";
}
if (f!=l) {
std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
}
}

In my desperation I've also figured out how the container version works. See below,
auto test_fn = [&t](auto c) {
t.tv_nsec = ( ( at_c<0>(c) * 3600 +
at_c<2>(c) * 60 +
at_c<4>(c) ) * 1000 +
at_c<6>(c) ) * 1000000;
auto parse_ok = qi::parse(input.begin(), input.end(),
( qi::int_ >> qi::char_(":") >> qi::int_ >> qi::char_(":") >>
qi::int_ >> qi::char_(".") >> qi::int_ )[ test_fn ]
Admittedly, it's quite ugly.

Related

boost regex to match IF statements

I need to write a boost regex to match the following string and seperate it into three tokens depending on the parameters to the IF block
=IF(ISNUMBER(SEARCH("Windows",GETWORKSPACE(1))),ON.TIME(NOW()+"00:00:02","abcdef"),CLOSE(TRUE))
Ideally these should come to
token1 = "ISNUMBER(SEARCH("Windows",GETWORKSPACE(1)))"
token2 = "ON.TIME(NOW()+"00:00:02","abcdef")"
token3 = "CLOSE(TRUE)"
I had originally written a simple regex as "(?<=\=IF\()(.),(.),(.*)(?=\))" which gives out incorrect tokens because the greedy qualifier takes too much of the first token. I am currently getting
token1 = "ISNUMBER(SEARCH("Windows",GETWORKSPACE(1))),ON.TIME(NOW()+"00:00:02""
token2 = ""abcdef")"
token3 = "CLOSE(TRUE)"
Also tried "(?<=\\=IF\\()([A-Za-z(),:\"]*?),([A-Za-z(),.:\"]*?),([A-Z(),:\"]*?)(?=\\))" with no luck. Can someone please suggest a regex ?
You need a simple parser.
Here's one with my favorite Boost swiss-army knife for quick parsers.
I've created a very flexible "token" grammar that honours (nested) parentheses and double-quoted string literals (potentially with embedded escaped quotes and parentheses):
token = raw [ *(
'(' >> -token_list >> ')'
| '[' >> -token_list >> ']'
| '{' >> -token_list >> '}'
| string_literal
| lexeme[ + ~char_(")]}([{\"',") ]
) ];
Where token_list and string_literal are defined as
string_literal = lexeme [
'"' >> *('\\' >> char_ | ~char_('"')) >> '"'
];
token_list = token % ',';
Now the parser expression for an =IF(condition, true_part, false_part) is simply:
if_expr
= '=' >> no_case["if"]
>> '(' >> token >> ',' >> token >> ',' >> token >> ')';
For fun I made the IF keyword case-insensitive
DEMO
Live On Coliru
//#define BOOST_SPIRIT_X3_DEBUG
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/adapted/std_tuple.hpp>
#include <iostream>
#include <iomanip>
namespace x3 = boost::spirit::x3;
namespace parser {
using namespace x3;
static rule<struct token_, std::string> const token = "token";
static auto const string_literal = lexeme [
'"' >> *('\\' >> char_ | ~char_('"')) >> '"'
];
static auto const token_list = token % ',';
static auto const token_def = raw [ *(
'(' >> -token_list >> ')'
| '[' >> -token_list >> ']'
| '{' >> -token_list >> '}'
| string_literal
| +~char_(")]}([{\"',") // glue together everything else
) ];
BOOST_SPIRIT_DEFINE(token)
static auto const if_expr
= '=' >> no_case["if"]
>> '(' >> token >> ',' >> token >> ',' >> token >> ')';
}
int main() {
for (std::string const& input : {
R"(=IF(ISNUMBER,ON.TIME,CLOSE))",
R"(=IF(ISNUMBER(SEARCH("Windows")),ON.TIME(NOW()+"00:00:02","abcdef"),CLOSE(TRUE)))",
R"(=IF(ISNUMBER(SEARCH("Windows",GETWORKSPACE(1))),ON.TIME(NOW()+"00:00:02","abcdef"),CLOSE(TRUE)))",
" = if( isnumber, on .time, close ) ",
R"( = if( "foo, bar", if( isnumber, on .time, close ), IF("[ISN(UM}B\"ER")) )",
})
{
auto f = input.begin(), l = input.end();
std::cout << "=== " << std::quoted(input) << ":\n";
std::string condition, true_part, false_part;
auto attr = std::tie(condition, true_part, false_part);
if (phrase_parse(f, l, parser::if_expr, x3::blank, attr)) {
std::cout << "Parsed: \n"
<< " - condition: " << std::quoted(condition) << "\n"
<< " - true_part: " << std::quoted(true_part) << "\n"
<< " - false_part: " << std::quoted(false_part) << "\n";
} else {
std::cout << "Parse failed\n";
}
if (f!=l) {
std::cout << "Remaining unparsed: " << std::quoted(std::string(f,l)) << "\n";
}
}
}
Prints
=== "=IF(ISNUMBER,ON.TIME,CLOSE)":
Parsed:
- condition: "ISNUMBER"
- true_part: "ON.TIME"
- false_part: "CLOSE"
=== "=IF(ISNUMBER(SEARCH(\"Windows\")),ON.TIME(NOW()+\"00:00:02\",\"abcdef\"),CLOSE(TRUE))":
Parsed:
- condition: "ISNUMBER(SEARCH(\"Windows\"))"
- true_part: "ON.TIME(NOW()+\"00:00:02\",\"abcdef\")"
- false_part: "CLOSE(TRUE)"
=== "=IF(ISNUMBER(SEARCH(\"Windows\",GETWORKSPACE(1))),ON.TIME(NOW()+\"00:00:02\",\"abcdef\"),CLOSE(TRUE))":
Parsed:
- condition: "ISNUMBER(SEARCH(\"Windows\",GETWORKSPACE(1)))"
- true_part: "ON.TIME(NOW()+\"00:00:02\",\"abcdef\")"
- false_part: "CLOSE(TRUE)"
=== " = if( isnumber, on .time, close ) ":
Parsed:
- condition: "isnumber"
- true_part: "on .time"
- false_part: "close "
=== " = if( \"foo, bar\", if( isnumber, on .time, close ), IF(\"[ISN(UM}B\\\"ER\")) ":
Parsed:
- condition: "\"foo, bar\""
- true_part: "if( isnumber, on .time, close )"
- false_part: "IF(\"[ISN(UM}B\\\"ER\")"

Parsing list of 'key = value' with unknown keys with Boost::Spirit

I've a string like the following one:
[GENERAL]
FMax Antenna = 3000
FMin Antenna = 2000
Invalid key = Invalid value
EMin Antenna = -50
EMax Antenna = 80
I want to parse it in order to save the value of FMin Antenna, FMax Antenna, EMin Antenna, EMax Antenna in a structure. I've created a Spirit parser, but it works partially.
Since the file can have many key = value rows, I need to parse only what I need (the key values that I must read) ignoring other pairs.
Both key and value can be alphanumeric strings with spaces and tabs.
I've defined inside the parser the keys that I want to read but when I encounter an unknown key, I cannot read keys that follows it (in the example case, I can't read EMin Antenna and EMax Antenna because are defined after an unknown key).
I've tried the code below: If I parse file1, that contains only keys that I want to read, it works, but if I add unknown key = value pairs in the middle of the file, like in file2, it stops to read all subsequent lines.
How can I fix it and continue to parse the file after unknown key-value pairs?
#include <boost/optional/optional_io.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
const std::string file1 = R"xx(
[GENERAL]
FMax Antenna = 3000
FMin Antenna = 2000
EMin Antenna = -50
EMax Antenna = 80
)xx";
const std::string file2 = R"xx(
[GENERAL]
FMax Antenna = 3000
FMin Antenna = 2000
EMin Antenna = -50
pappa pio = po po
EMax Antenna = 80
Ciao = 55
)xx";
struct Data {
double minFrequency = 0.0;
double maxFrequency = 0.0;
double minElevation = 0.0;
double maxElevation = 0.0;
};
BOOST_FUSION_ADAPT_STRUCT(
Data,
(double, minFrequency)
(double, maxFrequency)
(double, minElevation)
(double, maxElevation)
)
template <typename It, typename Skipper = qi::space_type>
struct grammar : qi::grammar<It, Data(), Skipper> {
grammar() : grammar::base_type(start) {
auto minFrequency = bind(&Data::minFrequency, qi::_val);
auto maxFrequency = bind(&Data::maxFrequency, qi::_val);
auto minElevation = bind(&Data::minElevation, qi::_val);
auto maxElevation = bind(&Data::maxElevation, qi::_val);
start = qi::no_case["[GENERAL]"] >> *(
("FMin Antenna" >> qi::lit('=') >> qi::int_)[minFrequency = qi::_1] |
("FMax Antenna" >> qi::lit('=') >> qi::int_)[maxFrequency = qi::_1] |
("EMin Antenna" >> qi::lit('=') >> qi::int_)[minElevation = qi::_1] |
("EMax Antenna" >> qi::lit('=') >> qi::int_)[maxElevation = qi::_1] |
(+(qi::alnum | qi::blank) >> qi::lit('=') >> +(qi::alnum | qi::blank)) // Issue here?
);
}
private:
qi::rule<It, Data(), Skipper> start;
};
int main() {
using It = std::string::const_iterator;
Data parsed1, parsed2;
bool ok = qi::phrase_parse(file1.begin(), file1.end(), grammar<It>(), qi::space, parsed1);
std::cout << "--- File 1 ---" << std::endl;
std::cout << "parsed = " << std::boolalpha << ok << std::endl;
std::cout << "min freq = " << parsed1.minFrequency << std::endl;
std::cout << "max freq = " << parsed1.maxFrequency << std::endl;
std::cout << "min elev = " << parsed1.minElevation << std::endl;
std::cout << "max elev = " << parsed1.maxElevation << std::endl;
std::cout << "--- File 2 ---" << std::endl;
ok = qi::phrase_parse(file2.begin(), file2.end(), grammar<It>(), qi::space, parsed2);
std::cout << "parsed = " << std::boolalpha << ok << std::endl;
std::cout << "min freq = " << parsed2.minFrequency << std::endl;
std::cout << "max freq = " << parsed2.maxFrequency << std::endl;
std::cout << "min elev = " << parsed2.minElevation << std::endl;
std::cout << "max elev = " << parsed2.maxElevation << std::endl;
return 0;
}
Ouput:
--- File 1 ---
parsed = true
min freq = 2000
max freq = 3000
min elev = -50
max elev = 80
--- File 2 ---
parsed = true
min freq = 2000
max freq = 3000
min elev = -50
max elev = 0 <-- This should be 80 like in the first parsing
You're confused about skippers.
Newlines are significant in your grammar, which is why you need a skipper that doesn't eat them
In your rules, you match +(alnum|blank) which is never gonna work because the skipper eats everything that matches blank anyways
(See Boost spirit skipper issues for background)
Other notes:
You don't need Fusion adaptation unless you want auto-magic attribute propagation. You're not using it right now.
Solving It
I'd make things very explicit:
known =
("FMin Antenna" >> lit('=') >> int_)[minFrequency = _1] |
("FMax Antenna" >> lit('=') >> int_)[maxFrequency = _1] |
("EMin Antenna" >> lit('=') >> int_)[minElevation = _1] |
("EMax Antenna" >> lit('=') >> int_)[maxElevation = _1]
;
unknown = +alnum >> '=' >> +alnum;
setting = (known(_r1) | unknown) >> +eol;
start =
no_case["[GENERAL]"] >> eol
>> *setting(_val);
Splitting up in rules is a little tricky, because *setting would try to synthesize a container attribute, making it impossible to propagate to the actual Data attribute.
I solved it by passing the attribute by reference in an inherited attribute, which disables automatic attribute propagation.
Alternatively, you could add a semantic action of any kind to inhibit automatic attribute propagation
DEMO
Live On Coliru
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/qi.hpp>
#include <iomanip>
namespace qi = boost::spirit::qi;
struct Data {
double minFrequency = 0.0;
double maxFrequency = 0.0;
double minElevation = 0.0;
double maxElevation = 0.0;
};
template <typename It, typename Skipper = qi::blank_type>
struct grammar : qi::grammar<It, Data(), Skipper> {
grammar() : grammar::base_type(start) {
using namespace qi;
auto minFrequency = bind(&Data::minFrequency, _r1);
auto maxFrequency = bind(&Data::maxFrequency, _r1);
auto minElevation = bind(&Data::minElevation, _r1);
auto maxElevation = bind(&Data::maxElevation, _r1);
known =
("FMin Antenna" >> lit('=') >> int_)[minFrequency = _1] |
("FMax Antenna" >> lit('=') >> int_)[maxFrequency = _1] |
("EMin Antenna" >> lit('=') >> int_)[minElevation = _1] |
("EMax Antenna" >> lit('=') >> int_)[maxElevation = _1]
;
unknown = +alnum >> '=' >> +alnum;
setting = (known(_r1) | unknown) >> +eol;
start =
no_case["[GENERAL]"] >> eol
>> *setting(_val);
}
private:
qi::rule<It, Data(), Skipper> start;
qi::rule<It, void(Data&), Skipper> setting, known;
qi::rule<It, Skipper> unknown;
};
int main() {
using It = std::string::const_iterator;
grammar<It> const g;
for (std::string const file : {
"[GENERAL]\nFMax Antenna = 3000\nFMin Antenna = 2000\nEMin Antenna = -50\nEMax Antenna = 80\n",
"[GENERAL]\nFMax Antenna = 3000\nFMin Antenna = 2000\nEMin Antenna = -50\npappa pio = po po\nEMax Antenna = 80\nCiao = 55\n",
})
{
Data parsed;
It f = begin(file), l = end(file);
bool ok = qi::phrase_parse(f, l, g, qi::blank, parsed);
std::cout << "--- File ---" << "\n";
std::cout << "parsed = " << std::boolalpha << ok << "\n";
if (ok) {
std::cout << "min freq = " << parsed.minFrequency << "\n";
std::cout << "max freq = " << parsed.maxFrequency << "\n";
std::cout << "min elev = " << parsed.minElevation << "\n";
std::cout << "max elev = " << parsed.maxElevation << "\n";
}
if (f!=l) {
std::cout << "Remaining unparsed: ";
while (f!=l) {
char c = *f++;
if (isprint(c)) std::cout << c;
else std::cout << "\\x" << std::setw(2) << std::setfill('0') << std::hex << static_cast<int>(c);
}
}
}
}
Prints
--- File ---
parsed = true
min freq = 2000
max freq = 3000
min elev = -50
max elev = 80
--- File ---
parsed = true
min freq = 2000
max freq = 3000
min elev = -50
max elev = 80

Boost Spirit X3 eol unexpected behaviour

This question in strictly related to boost-spirit-x3-parse-into-structs
I have this grammar
#include <iostream>
//#define BOOST_SPIRIT_X3_DEBUG
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
struct sectionInfo
{
std::string name;
int number = 0;
float pitch = 0.0f;
int visible = 0;
float minCutsTblSize = 0.0f;
//technology section attributes
float gridResolution = 0.0f;
float lengthPrecision = 0.0f;
};
const char START_SECTION = '{';
const char END_SECTION = '}';
const char QUOTE = '"';
const char EQUALS = '=';
const char* LAYER_SECTION = "Layer";
const char* TECHNOLOGY_SECTION = "Technology";
const char* NUMBER_ATTR = "layerNumber";
const char* VISIBLE_ATTR = "visible";
const char* COLOR_ATTR = "color";
const char* PITCH_ATTR = "pitch";
const char* MIN_CUTS_TBL_SIZE_ATTR = "minCutsTblSize";
const char* GRID_RESOLUTION_ATTR = "gridResolution";
const char* LENGTH_PRECISION_ATTR = "lengthPrecision";
namespace Parser {
namespace x3 = boost::spirit::x3;
namespace detail {
template <typename T> auto propagate(T member) {
return [=](auto& ctx) { x3::traits::move_to(x3::_attr(ctx), x3::_val(ctx).*member); };
}
template <typename T = sectionInfo, typename P>
auto rule(const char* debug, P p) { return x3::rule<struct _, T> {debug} = x3::skip(x3::space)[p]; };
auto quoted = rule<std::string>("quoted", x3::lexeme[QUOTE >> +(x3::char_ - QUOTE) >> QUOTE]);
template <typename T> auto make_member_parser(bool T::* const member) { return x3::bool_[propagate(member)]; }
template <typename T> auto make_member_parser(int T::* const member) { return x3::int_[propagate(member)]; }
template <typename T> auto make_member_parser(double T::* const member) { return x3::double_[propagate(member)]; }
template <typename T> auto make_member_parser(float T::* const member) { return x3::double_[propagate(member)]; }
template <typename T> auto make_member_parser(std::string T::* const member) { return quoted[propagate(member)]; }
auto property = [](auto label, auto member) {
return x3::as_parser(label) >> EQUALS >> make_member_parser(member);
};
}
using detail::rule;
using detail::propagate;
using detail::property;
using detail::quoted;
auto number = property(NUMBER_ATTR, &sectionInfo::number);
auto visible = property(VISIBLE_ATTR, &sectionInfo::visible);
auto pitch = property(PITCH_ATTR, &sectionInfo::pitch);
auto minCutsTblSize = property(MIN_CUTS_TBL_SIZE_ATTR, &sectionInfo::minCutsTblSize);
auto lengthPrecision = property(LENGTH_PRECISION_ATTR, &sectionInfo::lengthPrecision);
auto gridResolution = property(GRID_RESOLUTION_ATTR, &sectionInfo::gridResolution);
auto skipLine = *(x3::char_ - x3::eol);
x3::rule<struct sectionInfoId, sectionInfo> const layer = "layer";
x3::rule<struct mainRuleId, std::vector<sectionInfo>> const mainRule = "mainRule";
auto layer_def =
LAYER_SECTION >> quoted[propagate(&sectionInfo::name)] >> START_SECTION >> x3::eol
>> *( (number | visible | pitch | minCutsTblSize | lengthPrecision | gridResolution) >> +x3::eol )
>> END_SECTION;
auto skipper = x3::blank;
auto mainRule_def = *(*x3::eol >> layer >> *x3::eol);
BOOST_SPIRIT_DEFINE(layer, mainRule);
}
std::ostream& operator<<(std::ostream& os, const sectionInfo& s)
{
os
<< "name=" << " " << s.name << "\n"
<< "number=" << " " << s.number << "\n"
<< "visible=" << " " << s.visible << "\n"
<< "pitch=" << " " << s.pitch << "\n"
<< "minCutsTblSize=" << " " << s.minCutsTblSize << "\n"
<< "lengthPrecision=" << " " << s.lengthPrecision << "\n"
<< "gridResolution=" << " " << s.gridResolution << "\n\n";
return os;
}
int main() {
std::stringstream ss;
ss
<<"\r\nLayer \"UBMB\" {\r\n"
<< " layerNumber = 170\r\n"
<< " pitch = 33.6\r\n"
<< "}\r\n"
<< "\r\n"
<< "Layer \"RV\" {\r\n"
<< " gridResolution = 0.34\r\n"
<< " minCutsTblSize = 22.7\r\n"
<< " layerNumber = 85\r\n"
<< " visible = 2\r\n"
<< " pitch = 331\r\n"
<< "}\r\n"
<< " \r\n"
<< "Layer \"foffo\" {\r\n"
<< " layerNumber = 125\r\n"
<< " pitch = 0.005\r\n"
<< " gridResolution = 21.7\r\n"
<< " lengthPrecision = 0.15\r\n"
<< "}\r\n"
<< "\r\n";
std::vector<sectionInfo> sections;
auto sample = ss.str();
auto f = sample.begin(), l = sample.end();
bool ok = boost::spirit::x3::phrase_parse(
f, l,
Parser::mainRule,
Parser::skipper,
sections
);
if (ok && f==l)
{
std::cout << "\n\n Parsed successfully \n\n";
for(auto& s : sections)
{
std::cout << s;
}
}
else
std::cout << "Parse failed\n";
}
which successfully parses the input:
output is:
name= UBMB
number= 170
visible= 0
pitch= 33.6
minCutsTblSize= 0
lengthPrecision= 0
gridResolution= 0
name= RV
number= 85
visible= 2
pitch= 331
minCutsTblSize= 22.7
lengthPrecision= 0
gridResolution= 0.34
name= foffo
number= 125
visible= 0
pitch= 0.005
minCutsTblSize= 0
lengthPrecision= 0.15
gridResolution= 21.7
The problem arises because I need to skip some lines, i.e. the lines with properties not of interest (not defined in my grammar)
edit: For example, there may be a property dummy = "foo" which I want to skip.
To achieve this, the layer rule
auto layer_def = LAYER_SECTION >> quoted[propagate(&sectionInfo::name)] >> START_SECTION >> x3::eol
>> *( (number | visible | pitch | minCutsTblSize | lengthPrecision | gridResolution ) >> +x3::eol )
>> END_SECTION;
becomes
auto layer_def = LAYER_SECTION >> quoted[propagate(&sectionInfo::name)] >> START_SECTION >> x3::eol
>> *( (number | visible | pitch | minCutsTblSize | lengthPrecision | gridResolution | skipLine) >> +x3::eol )
>> END_SECTION;
The parser succeeds, but the output is now
name= UBMB number= 125 visible= 2 pitch= 0.005 minCutsTblSize= 22.7
lengthPrecision= 0.15 gridResolution= 21.7
which is wrong (only one section, properties taken here and there...)
It is evident the problem resides in the skipLine rule
auto skipLine = *(x3::char_ - x3::eol);
and I can't figure out why.
I thought it was obvious that the rule *(char - eol) >> eol
would match any line, but I guess it isn't..
Any clues?
Firstly, I'd simplify skipping. The skipper doesn't belong in the call-site because it's important to the grammar. Encapsulate it into the mainRule, and simplify:
auto mainRule_def = x3::skip(x3::blank) [ -layer % x3::eol ];
The -layer expression is a trick that accepts empty lines. Now using the parser becomes:
bool ok = boost::spirit::x3::parse(f, l, Parser::mainRule, sections);
Next: skipLine eats '}' as well, making everything fall apart: everything after that is taken as (invalid) properties for the same layer, and finally there is no END_SECTION so the grammar fails to match.
Simply:
auto skipLine = *(x3::char_ - x3::eol - END_SECTION);
Pro Tip:
When in doubt, debug: Live On Coliru
// debug it
auto skipLine = x3::rule<struct skipLine_> {"skipLine"} = *(x3::char_ - x3::eol/* - END_SECTION*/);
Full Demo
With some minor simplifications
Live On Coliru
#include <iostream>
#define BOOST_SPIRIT_X3_DEBUG
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/spirit/home/x3.hpp>
struct sectionInfo {
std::string name;
int number = 0;
float pitch = 0.0f;
int visible = 0;
float minCutsTblSize = 0.0f;
// technology section attributes
float gridResolution = 0.0f;
float lengthPrecision = 0.0f;
};
char const START_SECTION = '{';
char const END_SECTION = '}';
char const QUOTE = '"';
char const EQUALS = '=';
char const* LAYER_SECTION = "Layer";
char const* TECHNOLOGY_SECTION = "Technology";
char const* NUMBER_ATTR = "layerNumber";
char const* VISIBLE_ATTR = "visible";
char const* COLOR_ATTR = "color";
char const* PITCH_ATTR = "pitch";
char const* MIN_CUTS_TBL_SIZE_ATTR = "minCutsTblSize";
char const* GRID_RESOLUTION_ATTR = "gridResolution";
char const* LENGTH_PRECISION_ATTR = "lengthPrecision";
namespace Parser {
namespace x3 = boost::spirit::x3;
namespace detail {
template <typename T> auto propagate(T member) {
return [=](auto &ctx) { x3::traits::move_to(x3::_attr(ctx), x3::_val(ctx).*member); };
}
template <typename T = sectionInfo, typename P> auto rule(const char *debug, P p) {
return x3::rule<struct _, T>{ debug } = x3::skip(x3::space)[p];
};
auto quoted = rule<std::string>("quoted", x3::lexeme[QUOTE >> +(x3::char_ - QUOTE) >> QUOTE]);
#define MMP_(T, p) template <typename U> auto make_member_parser(T U::*const member) { return (p)[propagate(member)]; }
MMP_(bool, x3::bool_);
MMP_(int, x3::int_);
MMP_(double, x3::double_);
MMP_(float, x3::double_);
MMP_(std::string, quoted);
#undef MMP_
auto property = [](auto label, auto member) { return x3::as_parser(label) >> EQUALS >> make_member_parser(member); };
}
using detail::rule;
using detail::propagate;
using detail::property;
using detail::quoted;
auto number = property(NUMBER_ATTR, &sectionInfo::number);
auto visible = property(VISIBLE_ATTR, &sectionInfo::visible);
auto pitch = property(PITCH_ATTR, &sectionInfo::pitch);
auto minCutsTblSize = property(MIN_CUTS_TBL_SIZE_ATTR, &sectionInfo::minCutsTblSize);
auto lengthPrecision = property(LENGTH_PRECISION_ATTR, &sectionInfo::lengthPrecision);
auto gridResolution = property(GRID_RESOLUTION_ATTR, &sectionInfo::gridResolution);
auto skipLine = *(x3::char_ - x3::eol - END_SECTION);
x3::rule<struct sectionInfoId, sectionInfo> const layer = "layer";
x3::rule<struct mainRuleId, std::vector<sectionInfo> > const mainRule = "mainRule";
auto layer_def =
LAYER_SECTION >> quoted[propagate(&sectionInfo::name)]
>> START_SECTION >> x3::eol
>> *((number | visible | pitch | minCutsTblSize | lengthPrecision | gridResolution | skipLine) >> +x3::eol)
>> END_SECTION
;
auto mainRule_def = x3::skip(x3::blank) [ -layer % x3::eol ];
BOOST_SPIRIT_DEFINE(layer, mainRule);
}
std::ostream &operator<<(std::ostream &os, const sectionInfo &s) {
return os
<< "name=" << " " << s.name << "\n"
<< "number=" << " " << s.number << "\n"
<< "visible=" << " " << s.visible << "\n"
<< "pitch=" << " " << s.pitch << "\n"
<< "minCutsTblSize=" << " " << s.minCutsTblSize << "\n"
<< "lengthPrecision=" << " " << s.lengthPrecision << "\n"
<< "gridResolution=" << " " << s.gridResolution << "\n\n";
}
int main() {
std::string const sample =
"\r\nLayer \"UBMB\" {\r\n"
" layerNumber = 170\r\n"
" pitch = 33.6\r\n"
"}\r\n"
"\r\n"
"Layer \"RV\" {\r\n"
" gridResolution = 0.34\r\n"
" minCutsTblSize = 22.7\r\n"
" layerNumber = 85\r\n"
" visible = 2\r\n"
" pitch = 331\r\n"
"}\r\n"
" \r\n"
"Layer \"foffo\" {\r\n"
" layerNumber = 125\r\n"
" pitch = 0.005\r\n"
" gridResolution = 21.7\r\n"
" lengthPrecision = 0.15\r\n"
"}\r\n"
"\r\n";
std::vector<sectionInfo> sections;
{
auto f = sample.begin(), l = sample.end();
bool ok = boost::spirit::x3::parse(f, l, Parser::mainRule, sections);
if (ok && f == l) {
std::cout << "\n\n Parsed successfully \n\n";
for (auto &s : sections) {
std::cout << s;
}
} else
std::cout << "Parse failed\n";
}
}
Prints
Parsed successfully
name= UBMB
number= 170
visible= 0
pitch= 33.6
minCutsTblSize= 0
lengthPrecision= 0
gridResolution= 0
name= RV
number= 85
visible= 2
pitch= 331
minCutsTblSize= 22.7
lengthPrecision= 0
gridResolution= 0.34
name= foffo
number= 125
visible= 0
pitch= 0.005
minCutsTblSize= 0
lengthPrecision= 0.15
gridResolution= 21.7

Cannot add values to vector using boost spirit

I want to parse using the boost spirit the following string:
"{"DeliverNr":7424,"fruits":[["apple","banana","orange", "raspberry"]]}"
but I want to put into vector only three first fruits.
I have the following output:
it: { , dist: 0
Can anybody tell me what am I doing wrong ?
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/foreach.hpp>
#include <iostream>
#include <vector>
struct SomeStructF
{
std::string str1, str2, str3;
};
using namespace boost::spirit;
qi::rule<std::string::iterator> startRule;
qi::rule<std::string::iterator> endRule;
qi::rule<std::string::iterator, std::string()> stringRule;
qi::rule<std::string::iterator, SomeStructF()> valueRule;
qi::rule<std::string::iterator, std::vector<SomeStructF>()> valuesRule;
char const QUOTATION_MARK{ '"' };
char const OPEN_SQUARE_BRACKET{ '[' };
startRule =
+(qi::char_ - qi::char_(OPEN_SQUARE_BRACKET))
>> qi::char_(OPEN_SQUARE_BRACKET);
endRule = +qi::char_;
stringRule =
QUOTATION_MARK
>> *(qi::char_ - QUOTATION_MARK)
>> QUOTATION_MARK;
valueRule =
OPEN_SQUARE_BRACKET
>> stringRule
>> stringRule
>> stringRule;
valuesRule %=
startRule
>> valueRule
>> endRule;
std::vector<SomeStructF> res;
auto it = data.begin();
if (qi::parse(it, data.end(), valuesRule, res))
{
std::cout << "PARSE succeded" << std::endl;
}
std::cout << "it: " << *it << " , dist: " << std::distance(data.begin(), it) << std::endl;
std::cout << "res size " << res.size() << std::endl;
BOOST_FUSION_ADAPT_STRUCT(
parsers::SomeStructF,
(std::string, str1)
(std::string, str2)
(std::string, str3)
)
It's not particularly clear to me what you are asking. It seems to me it's easy enough to take into acocunt the commas, as you noted:
Live On Coliru
#define BOOST_SPIRIT_DEBUG
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <iomanip>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
namespace qi = boost::spirit::qi;
struct SomeStructF {
std::string str1, str2, str3;
};
BOOST_FUSION_ADAPT_STRUCT(
SomeStructF,
(std::string, str1)
(std::string, str2)
(std::string, str3)
)
int main()
{
qi::rule<std::string::iterator> startRule;
qi::rule<std::string::iterator> endRule;
qi::rule<std::string::iterator, std::string()> stringRule;
qi::rule<std::string::iterator, SomeStructF()> valueRule;
qi::rule<std::string::iterator, SomeStructF()> valuesRule;
char const QUOTATION_MARK{ '"' };
char const OPEN_SQUARE_BRACKET{ '[' };
startRule =
+(qi::char_ - qi::char_(OPEN_SQUARE_BRACKET))
>> qi::char_(OPEN_SQUARE_BRACKET);
endRule = +qi::char_;
stringRule =
QUOTATION_MARK
>> *(qi::char_ - QUOTATION_MARK)
>> QUOTATION_MARK;
valueRule =
OPEN_SQUARE_BRACKET
>> stringRule >> ','
>> stringRule >> ','
>> stringRule;
valuesRule %=
startRule
>> valueRule
>> endRule;
BOOST_SPIRIT_DEBUG_NODES((startRule)(endRule)(stringRule)(valueRule)(valuesRule));
std::string data = R"({"DeliverNr":7424,"fruits":
[["apple","banana","orange","raspberry"]]})";
std::vector<SomeStructF> res;
auto it = data.begin();
if (qi::parse(it, data.end(), valuesRule, res))
{
std::cout << "PARSE succeded" << std::endl;
std::cout << "res size " << res.size() << std::endl;
for (auto& el : res) {
std::cout << "el: {" << std::quoted(el.str1) << ","
<< std::quoted(el.str2) << ","
<< std::quoted(el.str3) << "}\n";
}
}
else
{
std::cout << "Parse did not succeed\n";
}
if (it != data.end())
std::cout << "Remaining unparsed: '" << std::string(it, data.end()) << "'\n";
}
Prints
PARSE succeded
res size 1
el: {"apple","banana","orange"}
Your grammar (nor your question) shows how you'd like to detect multiple SomeStructF so I can't really know what you want. Perhaps you want to simplify:
Live On Coliru
using It = std::string::const_iterator;
qi::rule<It, std::string()> quoted_ = '"' >> *~qi::char_('"') >> '"';
qi::rule<It, SomeStructF()/*, qi::space_type*/> value_ = quoted_ >> ',' >> quoted_ >> ',' >> quoted_;
using boost::spirit::repository::qi::seek;
BOOST_SPIRIT_DEBUG_NODES((quoted_)(value_));
std::string const data = R"({"DeliverNr":7424,"fruits":[["apple","banana","orange","raspberry"]]}
{"DeliverNr":7435,"botanics":[["pine","maple","oak","pernambucco"]]})";
std::vector<SomeStructF> res;
It it = data.begin();
if (qi::phrase_parse(it, data.end(), *seek["[[" >> value_], qi::space, res)) {
for (auto& el : res) {
std::cout << "el: {" << std::quoted(el.str1) << ","
<< std::quoted(el.str2) << ","
<< std::quoted(el.str3) << "}\n";
}
} else {
std::cout << "Parse did not succeed\n";
}
Which prints
el: {"apple","banana","orange"}
el: {"pine","maple","oak"}
Remaining unparsed: ',"pernambucco"]]}'

parsing 3 floats for glm::vec3 using boost::spirit::qi (error_invalid_expression)

I can parse one float and print it. (test1, test2)
Somehow I am unable to build a rule that parses three floats.
My final goal is to parse three floats and save them to a glm::vec3.
My rule seems to be incorrect:
qi::lexeme[qi::float_ << ' ' << qi::float_ << ' ' << qi::float_]
I am not sure if i am using BOOST_FUSION_ADAPT_STRUCT correctly
Here is a source to show what i came up with so far:
#include <iostream>
#include <string>
#include <glm\glm.hpp>
#include <boost\spirit\include\qi.hpp>
#include <boost\fusion\include\adapt_struct.hpp>
namespace qi = boost::spirit::qi;
void test1()
{
std::string test = "1.2";
auto it = test.begin();
if (qi::phrase_parse(it, test.end(), qi::float_, qi::space) && (it == test.end()))
std::cout << "test 1 ok" << std::endl;
else
std::cout << "test 1 not ok" << std::endl;
}
void test2()
{
std::string test = "1.2";
auto it = test.begin();
float f;
if (qi::phrase_parse(it, test.end(), qi::float_, qi::space, f) && (it == test.end()))
std::cout << "test 2 ok " << f << std::endl;
else
std::cout << "test 2 not ok" << std::endl;
}
void test3()
{
std::string test = "1.2 2.2 3.3";
qi::rule<std::string::iterator, qi::space_type> VEC3;
//error_invalid_expression
VEC3 = qi::lexeme[qi::float_ << ' ' << qi::float_ << ' ' << qi::float_];
auto it = test.begin();
if (qi::phrase_parse(it, test.end(), VEC3, qi::space) && (it == test.end()))
std::cout << "test 3 ok " << std::endl;
else
std::cout << "test 3 not ok" << std::endl;
}
BOOST_FUSION_ADAPT_STRUCT(
glm::vec3,
(float, x)
(float, y)
(float, z)
)
void test4()
{
std::string test = "1.2 2.2 3.3";
qi::rule<std::string::iterator, qi::space_type> VEC3;
//error_invalid_expression
VEC3 = qi::lexeme[qi::float_ << ' ' << qi::float_ << ' ' << qi::float_];
glm::vec3 result;
auto it = test.begin();
if (qi::phrase_parse(it, test.end(), VEC3, qi::space, result) && (it == test.end()))
{
std::cout << "test 4 ok (" << result.x << ", " << result.y << ", " << result.z << ")"<< std::endl;
}
else
std::cout << "test 4 not ok" << std::endl;
}
int main(int argc, char ** argv)
{
test1();
test2();
test3();
test4();
}
The test4 function contains everything I try to get done.
EDIT:
As suggested two things need to be changed:
void test4()
{
std::string test = "1.2 2.2 3.3";
qi::rule<std::string::iterator, glm::vec3(), qi::space_type> VEC3;
//error_invalid_expression
VEC3 = qi::lexeme[qi::float_ >> ' ' >> qi::float_ >> ' ' >> qi::float_];
glm::vec3 result;
auto it = test.begin();
if (qi::phrase_parse(it, test.end(), VEC3, qi::space, result) && (it == test.end()))
{
std::cout << "test 4 ok (" << result.x << ", " << result.y << ", " << result.z << ")"<< std::endl;
}
else
std::cout << "test 4 not ok" << std::endl;
}
I agree with Pete Becker. Simpler is usually better.
Now, since you are going to end up using spirit, let's look at what can be simpler:
use c++11 adapt:
BOOST_FUSION_ADAPT_STRUCT(glm::vec3, x, y, z)
you can do without the adapt (simpler in the demo)
you can (should) drop the skipper from the rule declaration if you're doing a toplevel lexeme[] directive anyways¹. See stackoverflow.com/questions/17072987/boost-spirit-skipper-issues/17073965#17073965
better yet, do without the rule for readability here. In reality, you would have a larger grammar with multiple rules. Be sure not to instantiate the grammar each parse (for efficiency).
Demo testbed Live On Coliru
//#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <glm/glm.hpp>
#include <iostream>
#include <string>
namespace qi = boost::spirit::qi;
glm::vec3 simpler(std::string const& test) {
auto it = test.begin();
glm::vec3 result;
using namespace qi;
if (parse(it, test.end(),
float_ >> ' ' >> float_ >> ' ' >> float_ >> eoi,
result.x, result.y, result.z))
{
return result;
}
throw std::runtime_error("invalid input");
}
glm::vec3 use_skipper(std::string const& test) {
auto it = test.begin();
glm::vec3 result;
using namespace qi;
if (phrase_parse(it, test.end(),
float_ >> float_ >> float_ >> eoi,
space, result.x, result.y, result.z))
{
return result;
}
throw std::runtime_error("invalid input");
}
BOOST_FUSION_ADAPT_STRUCT(glm::vec3, x, y, z)
glm::vec3 with_adapt(std::string const& test) {
auto it = test.begin();
glm::vec3 result;
using namespace qi;
if (phrase_parse(it, test.end(), float_ >> float_ >> float_ >>eoi, space, result))
{
return result;
}
throw std::runtime_error("invalid input");
}
int main() {
struct { glm::vec3 (&f)(std::string const&); std::string name; } tests[] = {
{simpler, "simpler"},
{use_skipper, "use_skipper"},
{with_adapt, "with_adapt"}
};
for (auto t : tests) try {
glm::vec3 v = t.f("1.2 2.2 3.3");
std::cout << t.name << " ok (" << v.x << ", " << v.y << ", " << v.z << ")\n";
} catch(std::exception const& e) {
std::cout << t.name << " fail (" << e.what() << ")\n";
}
}
You can be slightly more lightweight Using x3. Not really less work, but certainly less heavy on the compiler.
¹ subtle difference w.r.t. pre/post skipping, but that difference would be gone if there's a containing grammar with skipper
The sequence parser operator is >>, use that instead of << in your rules
VEC3 = qi::lexeme[qi::float_ >> ' ' >> qi::float_ >> ' ' >> qi::float_];
You've specified a whitespace skipper in your rule, so it can be further simplified by removing the lexeme directive and letting the skipper skip spaces automatically (unless you want to ensure there is a single space character in between the inputs).
VEC3 = qi::float_ >> qi::float_ >> qi::float_;
Also, if you want your rule to return a value, you need to add that signature to the rule type
qi::rule<std::string::iterator, glm::vec3(), qi::space_type> VEC3;
This is unrelated to the compilation error you're seeing, but use forward slashes in your #include directives, that works on all platforms.
#include <boost/spirit/include/qi.hpp>
Seems like an awful lot of work for something that's straightforward.
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::string test = "1.2 2.2 3.3";
float f1, f2, f3;
std::istringstream in(test);
in >> f1 >> f2 >> f3;
return 0;
}