Note that there are some explanatory texts on larger screens.

plurals
  1. POboost spirit parse c and cpp style comments
    text
    copied!<p>I am working on parser for which the comments are meaningful. And before you say it ... the reason is if statement A has a comment in the source in the target statement resulting out of the description of A should be commented with the comment of A. Or comments propagation. Anyways, for this purpose I have to parse and collect the comments not just to skip them.</p> <p>This is what i have so far: </p> <pre><code>#include &lt;boost/config/warning_disable.hpp&gt; #include &lt;boost/spirit/include/qi.hpp&gt; #include &lt;boost/spirit/include/phoenix.hpp&gt; #include &lt;boost/spirit/home/support/iterators/line_pos_iterator.hpp&gt; #include &lt;boost/spirit/repository/include/qi_confix.hpp&gt; #include &lt;boost/spirit/include/phoenix_fusion.hpp&gt; #include &lt;boost/spirit/include/phoenix_stl.hpp&gt; using namespace boost::spirit; #include &lt;boost/fusion/include/adapt_struct.hpp&gt; //////////////////////////////// // extra facilities struct get_line_f { template &lt;typename&gt; struct result { typedef size_t type; }; template &lt;typename It&gt; size_t operator()(It const&amp; 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&lt;std::string&gt; text; std::string source; }; BOOST_FUSION_ADAPT_STRUCT(Comment, (std::vector&lt;std::string&gt;, text) (std::string, source) (size_t, beginLine) (size_t, endLine) ) // //////////////////////////////// template &lt;typename Iterator&gt; struct comment : qi::grammar&lt;Iterator, Comment(), qi::space_type&gt; { 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&lt;0&gt;(_val) = _1] ] [ at_c&lt;1&gt;(_val) = construct&lt;std::string&gt;(begin(_1), end(_1)), at_c&lt;2&gt;(_val) = get_line_(begin(_1)), at_c&lt;3&gt;(_val) = get_line_(end(_1)) ] ; } boost::phoenix::function&lt;get_line_f&gt; get_line_; qi::rule&lt;Iterator, Comment(), qi::space_type&gt; start; qi::rule&lt;Iterator, std::string()&gt; c_comment; qi::rule&lt;Iterator, std::string()&gt; cpp_comment; qi::rule&lt;Iterator, std::vector&lt;std::string&gt;()&gt; comments; }; </code></pre> <p>This is the testing code:</p> <pre><code>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&lt;std::string::const_iterator&gt; Iterator; for (size_t i = 0; i &lt; sizeof(str) / sizeof(str[0]); ++i) { comment&lt;Iterator&gt; 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 &amp;&amp; iter == end) std::cout &lt;&lt; comment.beginLine &lt;&lt; comment.source &lt;&lt; "\n"; else std::cout &lt;&lt; "Parsing failed\n"; } } </code></pre> <p>Edit: This code now is compiling and working ... thanks for the help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload