Overwriting and Avoiding multiple assignation to string-Variable in struct #747
-
Hello everyone, i would like to parse a string and the parsed values shall be assigned into a struct, which contains already default init-values: After parsing a given string, the initial-values of the struct shall be overwritten with the new values. Example String => "ABC;-6;7" String => "XYZ;-6" The customization points do not work: I will appeciate for some suggestion to resolve the problem or a new way how to do it Thank in advance for your response. #include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <compare>
#include <string>
#include <set>
#include <iostream>
using namespace boost::spirit;
struct vMinMaxValue
{
std::string name;
int valuMin;
int valuMax;
vMinMaxValue() :
name{"Undef"},
valuMin{-5000},
valuMax{5000}
{
};
};
BOOST_FUSION_ADAPT_STRUCT(
vMinMaxValue,
(std::string, name)
(int, valuMin)
(int, valuMax)
)
template <typename Iterator, typename Skipper>
struct my_grammar : qi::grammar<Iterator, vMinMaxValue(), Skipper>
{
my_grammar() : my_grammar::base_type{value}
{
txt = +(~boost::spirit::qi::char_(';')) ;
mxnH = qi::no_case[qi::lit("0x")] >> qi::int_parser<int,16>();
mxnD = qi::int_parser<int,10>();
nv = mxnH | mxnD ;
val1 = txt >> ';' >> nv ; /* Name and min value , max is already assigned in the contruct of struct */
val2 = txt >> ';' >> nv >> ';' >> nv; /* Name and min value and max sahll be overwritten */
value = val2|val1; /* Accept both options */
}
boost::spirit::qi::rule<Iterator, std::string()> txt;
qi::rule<Iterator, int()> mxnH, mxnD, nv;
qi::rule<Iterator, vMinMaxValue()> val0,val1,val2;
qi::rule<Iterator, vMinMaxValue(), Skipper> value;
};
int main()
{
std::string s;
std::getline(std::cin, s);
auto it = s.begin();
my_grammar<std::string::iterator, ascii::space_type> g;
vMinMaxValue v;
if (qi::phrase_parse(it, s.end(), g, ascii::space, v) && it == s.end())
{
std::cout << "name=\"" << v.name << "\" "
<< "valueMin=\"" << v.valuMin << "\" "
<< "valueMax=\"" << v.valuMax << "\" "
<< std::endl;
}
else
{
std::cout << "parsing stopped at '" << std::string(it, s.end()) << "'\n";
}
} Output of attached code:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
hello everyone, #include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/phoenix/operator.hpp>
#include <compare>
#include <string>
#include <set>
#include <iostream>
using namespace boost::spirit;
struct vMinMaxValue
{
std::string name;
int valuMin;
int valuMax;
vMinMaxValue() :
name{"Undef"},
valuMin{-5000},
valuMax{5000}
{
};
};
BOOST_FUSION_ADAPT_STRUCT(
vMinMaxValue,
(std::string, name)
(int, valuMin)
(int, valuMax)
)
template <typename Iterator, typename Skipper>
struct my_grammar : qi::grammar<Iterator, vMinMaxValue(), Skipper>
{
my_grammar() : my_grammar::base_type{value}
{
txt = boost::spirit::qi::eps [boost::spirit::qi::_val=""] >> +(~boost::spirit::qi::char_(';')) [boost::spirit::qi::_val += boost::spirit::qi::_1];
mxnH = qi::no_case[qi::lit("0x")] >> qi::int_parser<int,16>();
mxnD = qi::int_parser<int,10>();
nv = mxnH | mxnD ;
val1 = txt >> ';' >> nv ;
val2 = txt >> ';' >> nv >> ';' >> nv;
value = val2|val1;
}
boost::spirit::qi::rule<Iterator, std::string()> txt;
qi::rule<Iterator, int()> mxnH, mxnD, nv;
qi::rule<Iterator, vMinMaxValue()> val0,val1,val2;
qi::rule<Iterator, vMinMaxValue(), Skipper> value;
};
int
main()
{
std::string s;
std::getline(std::cin, s);
auto it = s.begin();
my_grammar<std::string::iterator, ascii::space_type> g;
vMinMaxValue v;
if (qi::phrase_parse(it, s.end(), g, ascii::space, v) && it == s.end())
{
std::cout << "name=\"" << v.name << "\" "
<< "valueMin=\"" << v.valuMin << "\" "
<< "valueMax=\"" << v.valuMax << "\" "
<< std::endl;
}
else
{
std::cout << "parsing stopped at '" << std::string(it, s.end()) << "'\n";
}
return(0);
}
|
Beta Was this translation helpful? Give feedback.
hello everyone,
probably useful for other person.
Add include #include <boost/phoenix/operator.hpp>
and
modify the line from:
txt = +(~boost::spirit::qi::char_(';')) ;
to:
txt = boost::spirit::qi::eps [boost::spirit::qi::val=""] >> +(~boost::spirit::qi::char(';')) [boost::spirit::qi::_val += boost::spirit::qi::_1];