3

我正在开发评论对其有意义的解析器。在你说它之前......原因是如果语句A在目标语句中的源中有注释,则由A的描述导致应该用A的注释进行注释。或者评论传播。无论如何,为此我必须解析和收集评论,而不仅仅是跳过它们。

这是我到目前为止所拥有的:

#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()
        : beginLine(-1)
        , endLine(-1)
    {
    }

    size_t beginLine;
    size_t endLine;
};

struct Comment : public Position
{
    Comment()
        : Position()
        , text()
    {
    }

    std::vector<std::string> text;
    std::string source;
};

BOOST_FUSION_ADAPT_STRUCT(Comment,
                            (std::vector<std::string>, text)
                            (std::string, source)
                            (size_t,      beginLine)
                            (size_t,      endLine)
                          )

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

template <typename Iterator>
struct comment : qi::grammar<Iterator, Comment(), qi::space_type>
{
    comment() : comment::base_type(start)
    {
        c_comment %= repository::confix("/*", "*/")[*(char_ - "*/")];

        cpp_comment %= repository::confix("//", eol | eoi)[*(char_ - eol)];

        comments %= *(c_comment | cpp_comment);

        start = raw[ comments[at_c<0>(_val) = _1] ]
                   [
                       at_c<1>(_val) = construct<std::string>(begin(_1), end(_1)),
                       at_c<2>(_val) = get_line_(begin(_1)),
                       at_c<3>(_val) = get_line_(end(_1))
                   ]
        ;
    }

    boost::phoenix::function<get_line_f> get_line_;
    qi::rule<Iterator, Comment(), qi::space_type> start;
    qi::rule<Iterator, std::string()> c_comment;
    qi::rule<Iterator, std::string()> cpp_comment;
    qi::rule<Iterator, std::vector<std::string>()> comments;

};

这是测试代码:

int main()
{
    std::string str[] =
    {
        "/*1234*/",
        "\n\n/*1234*/",
        "// foo bar\n",
        "// foo bar",
        "\n\n    // foo bar\n",
        "/*1234\n5678*/",

    };

    typedef line_pos_iterator<std::string::const_iterator> Iterator;

    for (size_t i = 0; i < sizeof(str) / sizeof(str[0]); ++i)
    {
        comment<Iterator> g;
        Iterator iter(str[i].begin());
        Iterator end(str[i].end());

        Comment comment;
        bool r = phrase_parse(iter, end, g, qi::space, comment);
        if (r && iter == end)
            std::cout << comment.beginLine << comment.source << "\n";
        else
            std::cout << "Parsing failed\n";
    }

}

编辑:此代码现在正在编译和工作......感谢您的帮助。

4

0 回答 0