Generating Spirit parser expressions from a variadic list of alternative parser expressions

前端 未结 2 446
别跟我提以往
别跟我提以往 2020-11-30 13:27

I\'m looking for the simplest way to implement variadic function which takes list of boost::spirit::qi rules and expands the list into expression of format: rule1 | rule2 |

2条回答
  •  广开言路
    2020-11-30 13:49

    Thank you for a quick hint! I've just tried your code and unless I do something wrong ... I get this output: Syntax error:abc 8.81 Parsed:-a atoken Syntax error:-b btoken Syntax error:-c ctoken Syntax error:-d dtoken – G. Civardi 2 hours ago

    Okay, so, I couldn't leave it alone :/

    Turns out there was Undefined Behaviour involved, because of the way in which parser expressions were being passed to expandBitwise and being copied: Boost Proto expression templates weren't designed to be copied as they may contain references to temporaries, whose lifetime ends at the end of their containing full-expression.

    See for more background, the discussion at Zero to 60 MPH in 2 seconds!

    After a long (long) time of tweaking with rule_.alias() and boost::proto::deepcopy I have reached the following solution (which, incidentally, doesn't need a helper function at all, anymore):

    template
    void mparse(const std::string& line,Tail& ...tail)
    {
        auto parser = boost::fusion::fold(
                    boost::tie(ph::bind(&TStruct::rule_, arg1)(tail)...),
                    qi::eps(false),
                    deepcopy_(arg2 | arg1)
                );
    
        auto f=begin(line), l=end(line);
    
        if( qi::phrase_parse(f, l, parser, ascii::space ) )
            std::cout << "Parsed:" << line << std::endl;
        else
            std::cout << "Syntax error:" << line << std::endl;
    
        if (f!=l)
            std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
    }
    

    The protection against UB is the deepcopy_() invocation, which is a trivial polymorphic callable adaptor for boost::proto::deepcopy:

    struct DeepCopy
    {
        template struct result { typedef typename boost::proto::result_of::deep_copy::type type; };
    
        template
            typename result::type
            operator()(E const& expr) const {
                return boost::proto::deep_copy(expr);
            }
    };
    
    static const ph::function deepcopy_;
    

    With this code, lo and behold, the output becomes:

    Syntax error:abc 8.81
    Remaining unparsed: 'abc 8.81'
    Parsed:-a atoken
    Parsed:-b btoken
    Parsed:-c ctoken
    Parsed:-d dtoken
    Bye
    

    As a bonus, the code now allows you to use Spirit's builtin debug() capabilities (uncomment that line):

    <-d>
      abc 8.81
      
    
    <-c>
      abc 8.81
      
    
    <-b>
      abc 8.81
      
    
    <-a>
      abc 8.81
      
    
    Syntax error:abc 8.81
    Remaining unparsed: 'abc 8.81'
    

    Tested with

    • Boost 1_54_0
    • GCC 4.7.2, 4.8.x, Clang 3.2
    • Note the #defines which are significant.

    FULL CODE

    #define BOOST_RESULT_OF_USE_DECLTYPE
    #define BOOST_SPIRIT_USE_PHOENIX_V3
    #include 
    #include 
    #include 
    #include 
    
    namespace qi    = boost::spirit::qi;
    namespace ph    = boost::phoenix;
    namespace ascii = boost::spirit::ascii;
    using namespace ph::arg_names;
    
    typedef qi::rule mrule_t;
    typedef qi::rule    wrule_t;
    
    struct TStruct
    {
        mrule_t     rule_;
        template
        TStruct( T& rVar,const std::string&name, R& rule ) :
            rule_( qi::lit(name) >> rule[ ph::ref(rVar) = qi::_1 ] )
        { 
            rule_.name(name);
            // debug(rule_);
        }
    };
    
    struct DeepCopy
    {
        template struct result { typedef typename boost::proto::result_of::deep_copy::type type; };
    
        template
            typename result::type
            operator()(E const& expr) const {
                return boost::proto::deep_copy(expr);
            }
    };
    
    static const ph::function deepcopy_;
    
    template
    void mparse(const std::string& line,Tail& ...tail)
    {
        auto parser = boost::fusion::fold(
                    boost::tie(ph::bind(&TStruct::rule_, arg1)(tail)...),
                    qi::eps(false),
                    deepcopy_(arg2 | arg1)
                );
    
        auto f=begin(line), l=end(line);
    
        if( qi::phrase_parse(f, l, parser, ascii::space ) )
            std::cout << "Parsed:" << line << std::endl;
        else
            std::cout << "Syntax error:" << line << std::endl;
    
        if (f!=l)
            std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
    }
    
    int main()
    {
        wrule_t rword=+~ascii::space;
    
        std::string par1,par2,par3,par4;
    
        TStruct r1( par1, "-a", rword );
        TStruct r2( par2, "-b", rword );
        TStruct r3( par3, "-c", rword );
        TStruct r4( par4, "-d", rword );
    
        mparse("abc 8.81"   ,r1,r2,r3,r4);
        mparse("-a atoken"  ,r1,r2,r3,r4);
        mparse("-b btoken"  ,r1,r2,r3,r4);
        mparse("-c ctoken"  ,r1,r2,r3,r4);
        mparse("-d dtoken"  ,r1,r2,r3,r4);
    
        std::cout << "Bye\n";
    }
    

提交回复
热议问题