2016-10-18 40 views
1

雙解析器加倍使用升壓精神X3與此解析器:提振精神X3與我目前解析限制

boost::spirit::x3::real_parser<double, x3::strict_real_policies<double> > const strict_double = {};

但它也解析像.356356.雙打,我想避免這種情況,並且具有用戶編寫0.356356.0。 Hoow能對這個現有的解析器應用這樣的限制嗎? 有沒有辦法從頭開始寫我自己的雙解析器?

回答

2

你可以很容易地創建一個自定義策略,你想要做什麼:

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; 
}; 

完整的示例(Running on 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); 
} 
+0

非常感謝你,這甚至超過了我的預期,我想我會實現我自己的雙解析器 – Exagon