boost::spirit::qi::grammar for Replacing/Removing/append before the parsing of number is executed #761
Replies: 2 comments
-
|
Hmm, An ugly workaround solution: #if 1
#include <boost/spirit/home/x3.hpp>
namespace x3 = boost::spirit::x3;
#else
#include <boost/spirit/home/qi.hpp>
namespace x3 = boost::spirit::qi;
#endif
#include <limits>
#include <iostream>
template <typename T>
struct my_hex_policy
{
// no nan, no inf, no fraction, no exponent, just plain int
bool parse_nan(auto&...) const { return false; }
bool parse_inf(auto&...) const { return false; }
bool parse_frac_n(auto&...) const { return false; }
static bool const allow_leading_dot = false;
static bool const allow_trailing_dot = false;
static bool const expect_dot = false;
bool parse_dot(auto&...) const { return false; }
bool parse_exp(auto&...) const { return false; }
bool parse_exp_n(auto&...) const { return false; }
static bool parse_sign(auto& first, auto last)
{
return x3::extract_sign(first, last);
}
bool parse_n(auto& iter, auto last, T& n) const {
static_assert(std::numeric_limits<T>::is_integer);
return x3::parse(iter, last, "0x")
&& x3::parse(iter, last, x3::uint_parser<T, 16>{}, n);
}
};
template <typename T>
x3::real_parser<T, my_hex_policy<T>> const my_hex{};
int main()
{
for (auto const& test : { "-0x123", "+0x123", "0x123" }) {
std::cout << test << ": ";
int n;
auto iter = test + 0, end = test + strlen(test);
if (x3::parse(iter, end, my_hex<int>, n)) {
std::cout << n;
if (iter != end)
std::cout << " (left unparsed: '" << std::string(iter, end) << "')";
std::cout << '\n';
}
else {
std::cout << "failed to parse";
}
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Hello Nikita, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello Everyone,
The following grammar can be used for parsing and conversion of String-Hexadezimal/String-Dezimal to number
From string "123" to number 123 OR
From string "0x123" to number 291
I would like to know, if it is possible to extend the grammar above to parser the following String-Hexadezimal to number:
From string "-0x123" to number -291
From string "+0x123" to number 291
That means . the string to parsed by qi::int_parser<int,16>() shall applied for "-123" or for "+123".
"0x" shall be ignored AND the sign ('-' or '+') shall be considered in the parsing by qi::int_parser<int,16>()
Thanks in advance for the response
Omar
Beta Was this translation helpful? Give feedback.
All reactions