C++ Boost-C类语言

C++ Boost-C类语言,c++,boost,boost-spirit,C++,Boost,Boost Spirit,我试图用boost的精神重新实现iPhone越狱开发中的logos解析perl脚本 输入的一个例子是: %hook SBLockScreenView -(void)setCustomSlideToUnlockText:(id)arg1 { arg1 = @"Changed the slider"; %orig(arg1); } %end 到目前为止,我已经: namespace logos { namespace qi = boost::spirit::qi; namespac

我试图用boost的精神重新实现iPhone越狱开发中的logos解析perl脚本

输入的一个例子是:

%hook SBLockScreenView
-(void)setCustomSlideToUnlockText:(id)arg1
{
  arg1 = @"Changed the slider";
  %orig(arg1);
}
%end
到目前为止,我已经:

namespace logos
{
  namespace qi = boost::spirit::qi;
  namespace ascii = boost::spirit::ascii;

  struct class_hook
  {
    std::string class_name;
    std::string method_signature;
    std::string method_body;
  };

  template <typename Iterator>
  struct class_hook_parser : qi::grammar<Iterator, class_hook(), ascii::space_type>
  {
    class_hook_parser() : class_hook_parser::base_type(start)
    {
      using qi::int_;
      using qi::lit;
      using qi::on_error;
      using qi::fail;
      using qi::double_;
      using qi::lexeme;
      using ascii::char_;

      hooked_class %= lexeme[+(char_("a-zA-Z") - '-')];
      method_sig %= lexeme[+(char_) - '{'];
      method_body %= lexeme[+(char_ - '}')];

      start %=
        lit("%hook")
        >> hooked_class
        >> method_sig
        >> method_body
        >> lit("%end")
        ;

      on_error<fail>
    (
     start,
     boost::phoenix::ref(std::cout) << "Something errored!" << std::endl);

    }
    qi::rule<Iterator, std::string(), ascii::space_type> hooked_class;
    qi::rule<Iterator, std::string(), ascii::space_type> method_sig;
    qi::rule<Iterator, std::string(), ascii::space_type> method_body;
    qi::rule<Iterator, class_hook(), ascii::space_type> start;

  };

}

BOOST_FUSION_ADAPT_STRUCT(logos::class_hook,
              (std::string, class_name)
              (std::string, method_signature)
              (std::string, method_body))

typedef std::string::const_iterator iterator_type;
typedef logos::class_hook_parser<iterator_type> class_hook_parser;

using boost::spirit::ascii::space;
std::string::const_iterator
  iter = std::begin(tweak_source_code),
  end = std::end(tweak_source_code);

class_hook_parser g;
logos::class_hook emp;
bool r = phrase_parse(iter, end, g, space, emp);
if (r) {
  std::cout << "Got: " << boost::fusion::as_vector(emp) << std::endl;
}
else std::cout << "Something isn't working" << std::endl;
名称空间徽标
{
名称空间qi=boost::spirit::qi;
名称空间ascii=boost::spirit::ascii;
结构类钩子
{
std::字符串类名称;
std::字符串方法\u签名;
std::字符串方法\u体;
};
模板
结构类钩子解析器:qi::grammar
{
class_hook_parser():class_hook_parser::base_type(开始)
{
使用qi::int_;
使用qi::lit;
使用qi::on_错误;
使用qi::失败;
使用qi::double;
使用气:词素;
使用ascii::char;
hooked_class%=词素[+(char_uz(“a-zA-Z”)-“-”)];
方法_sig%=词素[+(字符)-'{'];
方法_body%=词素[+(char_-'}');
起始百分比=
发光(“%hook”)
>>钩形类
>>方法
>>方法u体
>>点亮(“%end”)
;
论错误
(
开始
你的意思是

+(char_ - '{')
而不是

+(char_) - '{'
很可能,您会要求主体以签名中被拒绝的
{
开头。以下是我的固定版本:

    hooked_class = +char_("a-zA-Z");
    method_sig   = +(char_ - '{');
    method_body  = '{' >> +(char_ - '}') >> '}';
注:

  • 删除skipper也可以删除
    lexeme[]
    指令
  • 从“a-zA-Z”集合中拒绝
    -
    是无用的(它不在其中…)
  • 方法\u sig
    现在包括所有空白(包括尾随的换行符)
  • 使用BOOST_SPIRIT_DEBUG来了解语法为何以神秘的方式工作
另见:

//#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

namespace logos
{
    namespace qi    = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;

    struct class_hook
    {
        std::string class_name;
        std::string method_signature;
        std::string method_body;
    };

    template <typename Iterator>
        struct class_hook_parser : qi::grammar<Iterator, class_hook(), ascii::space_type>
    {
        class_hook_parser() : class_hook_parser::base_type(start)
        {
            using qi::int_;
            using qi::lit;
            using qi::on_error;
            using qi::fail;
            using qi::double_;
            using qi::lexeme;
            using ascii::char_;

            hooked_class = +char_("a-zA-Z");
            method_sig   = +(char_ - '{');
            method_body  = '{' >> +(char_ - '}') >> '}';

            start        = "%hook"
                       >> hooked_class
                       >> method_sig
                       >> method_body
                       >> "%end"
                       ;

            on_error<fail> (start,
                    boost::phoenix::ref(std::cout) << "Something errored\n"
                );

            BOOST_SPIRIT_DEBUG_NODES((hooked_class)(method_sig)(method_body)(start))
        }
      private:
        qi::rule<Iterator, std::string()> hooked_class, method_sig, method_body;
        qi::rule<Iterator, class_hook(), ascii::space_type> start;
    };
}

BOOST_FUSION_ADAPT_STRUCT(logos::class_hook, class_name, method_signature, method_body)

int main() {
    typedef std::string::const_iterator iterator_type;
    typedef logos::class_hook_parser<iterator_type> class_hook_parser;

    std::string const tweak_source_code = R"(
%hook SBLockScreenView
-(void)setCustomSlideToUnlockText:(id)arg1
{
  arg1 = @"Changed the slider";
  %orig(arg1);
}
%end
        )";

    using boost::spirit::ascii::space;
    iterator_type iter = std::begin(tweak_source_code), end = std::end(tweak_source_code);

    class_hook_parser g;
    logos::class_hook emp;

    bool r = phrase_parse(iter, end, g, space, emp);

    if (r) {
        std::cout << "Got: " << boost::fusion::as_vector(emp) << "\n";
    } else {
        std::cout << "Something isn't working\n";
    }
}
你是说

+(char_ - '{')
而不是

+(char_) - '{'
很可能,您会要求主体以签名中被拒绝的
{
开头。以下是我的固定版本:

    hooked_class = +char_("a-zA-Z");
    method_sig   = +(char_ - '{');
    method_body  = '{' >> +(char_ - '}') >> '}';
注:

  • 删除skipper也可以删除
    lexeme[]
    指令
  • 从“a-zA-Z”集合中拒绝
    -
    是无用的(它不在其中…)
  • 方法\u sig
    现在包括所有空白(包括尾随的换行符)
  • 使用BOOST_SPIRIT_DEBUG来了解语法为何以神秘的方式工作
另见:

//#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

namespace logos
{
    namespace qi    = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;

    struct class_hook
    {
        std::string class_name;
        std::string method_signature;
        std::string method_body;
    };

    template <typename Iterator>
        struct class_hook_parser : qi::grammar<Iterator, class_hook(), ascii::space_type>
    {
        class_hook_parser() : class_hook_parser::base_type(start)
        {
            using qi::int_;
            using qi::lit;
            using qi::on_error;
            using qi::fail;
            using qi::double_;
            using qi::lexeme;
            using ascii::char_;

            hooked_class = +char_("a-zA-Z");
            method_sig   = +(char_ - '{');
            method_body  = '{' >> +(char_ - '}') >> '}';

            start        = "%hook"
                       >> hooked_class
                       >> method_sig
                       >> method_body
                       >> "%end"
                       ;

            on_error<fail> (start,
                    boost::phoenix::ref(std::cout) << "Something errored\n"
                );

            BOOST_SPIRIT_DEBUG_NODES((hooked_class)(method_sig)(method_body)(start))
        }
      private:
        qi::rule<Iterator, std::string()> hooked_class, method_sig, method_body;
        qi::rule<Iterator, class_hook(), ascii::space_type> start;
    };
}

BOOST_FUSION_ADAPT_STRUCT(logos::class_hook, class_name, method_signature, method_body)

int main() {
    typedef std::string::const_iterator iterator_type;
    typedef logos::class_hook_parser<iterator_type> class_hook_parser;

    std::string const tweak_source_code = R"(
%hook SBLockScreenView
-(void)setCustomSlideToUnlockText:(id)arg1
{
  arg1 = @"Changed the slider";
  %orig(arg1);
}
%end
        )";

    using boost::spirit::ascii::space;
    iterator_type iter = std::begin(tweak_source_code), end = std::end(tweak_source_code);

    class_hook_parser g;
    logos::class_hook emp;

    bool r = phrase_parse(iter, end, g, space, emp);

    if (r) {
        std::cout << "Got: " << boost::fusion::as_vector(emp) << "\n";
    } else {
        std::cout << "Something isn't working\n";
    }
}

冒着写另一个问题的风险,我能做些什么来概括这一点,比如说这些钩子声明的列表?我想象它像vector.push_back(解析的_结果)事实上,这是另一个问题。看这里:冒着写另一个问题的风险,我能做些什么来概括这个问题,比如说这些钩子声明的列表?我想象它像向量。推回(解析的结果)?事实上,这是另一个问题。看这里: