C++ 使用源代码进行精神分析

C++ 使用源代码进行精神分析,c++,boost,boost-spirit,boost-spirit-qi,C++,Boost,Boost Spirit,Boost Spirit Qi,我希望能够解析一个数字,存储其原始源,并跟踪其在源中的位置,将其保存在结构本身中 这就是我到目前为止所做的: #include <boost/config/warning_disable.hpp> #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/phoenix_core.hpp> #include <boost/spirit/include/phoenix_ope

我希望能够解析一个数字,存储其原始源,并跟踪其在源中的位置,将其保存在结构本身中

这就是我到目前为止所做的:

#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_object.hpp>
#include <boost/spirit/home/support/iterators/line_pos_iterator.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>

#include <iostream>
#include <iomanip>
#include <ios>
#include <string>
#include <complex>

#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>

struct Position
{
    Position()
        : line(-1)
    {
    }

    size_t line;
};

struct Number : public Position
{
    Number()
        : Position()
        , value(-1)
        , source()
    {
    }

    unsigned    value;
    std::string source;
};

using namespace boost::spirit;

BOOST_FUSION_ADAPT_STRUCT(Number,
                            (unsigned,    value)
                            (std::string, source)
                            (size_t,      line)
                          );

template <typename Iterator>
struct source_hex : qi::grammar<Iterator, Number()>
{
    source_hex() : source_hex::base_type(start)
    {
        using qi::eps;
        using qi::hex;
        using qi::lit;
        using qi::raw;
        using qi::_val;
        using qi::_1;
        using ascii::char_;

        namespace phx = boost::phoenix;
        using phx::at_c;
        using phx::begin;
        using phx::end;
        using phx::construct;

        start = raw[   (lit("0x") | lit("0X"))
                     >> hex [at_c<0>(_val) = _1]
                   ][at_c<2>(_val) = get_line(begin(_1))]
                    [at_c<1>(_val) = construct<std::string>(begin(_1), end(_1))]

        ;
    }

    qi::rule<Iterator, Number()> start;
};
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
结构位置
{
职位(
:行(-1)
{
}
尺寸线;
};
结构编号:公共位置
{
编号()
:职位()
,值(-1)
,来源()
{
}
无符号值;
std::字符串源;
};
使用名称空间boost::spirit;
增强融合适应结构(编号,
(无符号,值)
(标准::字符串,源)
(尺寸,线条)
);
模板
struct source_hex:qi::grammar
{
source_hex():source_hex::base_type(开始)
{
使用qi::eps;
使用qi::hex;
使用qi::lit;
用气:生的;
使用qi:(u val);
使用气::_1;
使用ascii::char;
名称空间phx=boost::phoenix;
使用phx::at_c;
使用phx::begin;
使用phx::end;
使用phx::construct;
开始=原始[(点亮(“0x”)|点亮(“0x”))
>>十六进制[at_c(_val)=u 1]
][at_c(_val)=获取_行(开始(_1))]
[at_c(_val)=构造(开始(_1),结束(_1))]
;
}
qi::规则开始;
};
测试代码为:

typedef line_pos_iterator<std::string::const_iterator> Iterator;
source_hex<Iterator> g;
Iterator iter(str.begin());
Iterator end(str.end());

Number number;
bool r = parse(iter, end, g, number);
if (r && iter == end) {
    std::cout << number.line << ": 0x" << std::setw(8) << std::setfill('0') << std::hex << number.value << " // " << number.source << "\n";
} else
    std::cout << "Parsing failed\n";
typedef line\u pos\u迭代器迭代器;
来源(g);
迭代器iter(str.begin());
迭代器端(str.end());
数量;
bool r=parse(iter,end,g,number);
if(r&&iter==end){
std::能看一看吗

#include <boost/spirit/repository/include/qi_iter_pos.hpp>
^多态参与者,用作:

    start = raw[ qi::no_case["0x"] >> hex [at_c<0>(_val) = _1] ]
               [ 
                   at_c<1>(_val) = construct<std::string>(begin(_1), end(_1)),
                   at_c<2>(_val) = get_line_(begin(_1)) 
               ]
    ;

    // with

boost::phoenix::function<get_line_f> get_line_;
start=raw[qi::no_case[“0x]”>>hex[at_c(_val)=_1]]
[ 
at_c(_val)=构造(开始(_1),结束(_1)),
at_c(_val)=获取_行(开始(_1))
]
;
//与
boost::phoenix::函数get\u line;
注意我改变了一些小的观点


完全运行演示并输出:

,显然我所做的是完全关闭的-因为get_行是在构造语法的过程中调用的。您需要将
get_行作为“懒惰”函子(Phoenix Actor)调用。请参阅使用它的示例(InFile解析器)
struct get_line_f
{
    template <typename> struct result { typedef size_t type; };
    template <typename It> size_t operator()(It const& pos_iter) const
    {
        return get_line(pos_iter);
    }
};
    start = raw[ qi::no_case["0x"] >> hex [at_c<0>(_val) = _1] ]
               [ 
                   at_c<1>(_val) = construct<std::string>(begin(_1), end(_1)),
                   at_c<2>(_val) = get_line_(begin(_1)) 
               ]
    ;

    // with

boost::phoenix::function<get_line_f> get_line_;