Boost spirit x3双重解析器与限制
boost spirit x3 double parser with restrictions
我目前使用boost spirit x3解析双精度:
boost::spirit::x3::real_parser<double, x3::strict_real_policies<double> > const strict_double = {};
,但它也解析双精度,如.356
和356.
,我想避免这种情况,并让用户写0.356
和356.0
代替。如何在这个现有解析器上应用这样的限制?有没有一种不用从头开始编写自己的双解析器的方法?
您可以很容易地创建一个自定义策略来做您想做的事情:
template <typename ValueType>
struct really_strict_real_policies : boost::spirit::x3::strict_real_policies<ValueType>
{
static bool const allow_leading_dot = false;
static bool const allow_trailing_dot = false;
};
完整示例(在WandBox上运行)
#include <iostream>
#include <boost/spirit/home/x3.hpp>
template <typename ValueType>
struct really_strict_real_policies : boost::spirit::x3::strict_real_policies<ValueType>
{
static bool const allow_leading_dot = false;
static bool const allow_trailing_dot = false;
};
template <typename Parser>
void parse(const std::string& input, const Parser& parser, bool expected)
{
std::string::const_iterator iter=input.begin(), end=input.end();
bool result = boost::spirit::x3::parse(iter,end,parser);
if( (result && (iter==end))==expected )
std::cout << "Yay" << std::endl;
}
int main()
{
boost::spirit::x3::real_parser<double, really_strict_real_policies<double> > const really_strict_double = {};
parse(".2",really_strict_double,false);
parse("2.",really_strict_double,false);
parse("2.2",really_strict_double,true);
}
相关文章:
- boost::spirit::x3 中的通用解析器生成器
- 如何处理Boost Spirit X3导致Visual Studio 2019 "static initialization order fiasco"?
- Boost Spirit X3:将(一些)空格解析为枚举
- Boost Spirit x3 条件(三元)运算符解析器
- 你如何从 Boost Spirit X3 词法解析器中获取字符串?
- 将 Boost.Spirit.X3 解析器拆分为多个 TU
- boost::spirit::x3 中的简单字符串解析器不起作用
- 自定义预期失败的完整错误消息(boost::spirit::x3)
- (如何)我可以在不安装完整的提升库的情况下使用 boost::spirit X3 吗?
- 如何在 Spirit X3 中正确指定锚定条件?
- Spirit.X3 与 lambda 返回不同的解析器类型
- Boost.Spirit X3 替代操作员
- Spirit X3:自定义数字解析器在结果中产生意外的前导零
- Boost.Spirit X3 解析器"no type named type in(...)"
- boost::spirit::x3 phrase_parse 在进入 Vector 之前进行算术运算
- 使用 Spirit x3,如何控制在每个不同的输入上调用哪个解析器?
- Spirit X3, ascii::cntrl why disparity with std::iscntrl?
- 在 Spirit X3 中使用布尔属性而不是可选属性
- Spirit X3,如何在非ASCII输入上失败
- 如何使用 Boost Spirit x3 编写具有两个后操作数语法的二进制运算符?