2013-10-03 1 views
2

나는 _ 알파로 시작하는 식별자를 구문 분석 파서를 만들 싶습니다 그리고 본문에 _ 알파, 납입 또는이있을 수 있습니다 :부스트 정신 분석 식별자

식별자에 '_'가있는 경우이 하늘의 이름을 반환하는 이유
#include <boost/config/warning_disable.hpp> 
#include <boost/spirit/include/qi.hpp> 
#include <boost/spirit/include/phoenix.hpp> 
#include <boost/spirit/home/support/iterators/line_pos_iterator.hpp> 
#include <boost/spirit/repository/include/qi_confix.hpp> 
#include <boost/spirit/include/phoenix_fusion.hpp> 
#include <boost/spirit/include/phoenix_stl.hpp> 

using namespace boost::spirit; 

#include <boost/fusion/include/adapt_struct.hpp> 

//////////////////////////////// 
// extra facilities 
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); 
    } 
}; 

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

    size_t line; 
}; 

struct Identifier : public Position 
{ 
    Identifier() 
     : Position() 
     , name() 
    { 
    } 

    std::string name; 
}; 

BOOST_FUSION_ADAPT_STRUCT(Identifier, 
          (std::string, name) 
          (size_t,  line) 
         ) 

// 
//////////////////////////////// 

template <typename Iterator> 
struct source_identifier: qi::grammar<Iterator, Identifier(), qi::space_type> 
{ 
    source_identifier() : source_identifier::base_type(start) 
    { 
     using qi::alpha; 
     using qi::alnum; 
     using qi::raw; 
     using qi::_val; 
     using qi::_1; 

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

     name %=  (qi::alpha | "_") 
       >> *(qi::alnum | "_"); 

     start = raw [ name[at_c<0>(_val) = _1] ] 
        [ 
         at_c<1>(_val) = get_line_(begin(_1)) 
        ] 
     ; 
    } 

    boost::phoenix::function<get_line_f> get_line_; 
    qi::rule<Iterator, Identifier(), qi::space_type> start; 
    qi::rule<Iterator, std::string()> name; 
}; 

,이 경우

답변

5

"_"가없는 작품은 모두 문자 '_' 일치하지만 어떤을 합성하지 않는, qi::lit("_")과 동일 속성. 원하는 것은 qi::char_('_')입니다. 자세한 내용은 here을 참조하십시오.