Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I parse end-of-line with boost::spirit::qi?
    text
    copied!<p>Shouldn't a simple <a href="http://www.boost.org/doc/libs/1_42_0/libs/spirit/doc/html/spirit/qi/quick_reference/qi_parsers/auxiliary.html" rel="nofollow noreferrer"><code>eol</code></a> do the trick?</p> <pre><code>#include &lt;algorithm&gt; #include &lt;boost/spirit/include/qi.hpp&gt; #include &lt;iostream&gt; #include &lt;string&gt; using boost::spirit::ascii::space; using boost::spirit::lit; using boost::spirit::qi::eol; using boost::spirit::qi::phrase_parse; struct fix : std::unary_function&lt;char, void&gt; { fix(std::string &amp;result) : result(result) {} void operator() (char c) { if (c == '\n') result += "\\n"; else if (c == '\r') result += "\\r"; else result += c; } std::string &amp;result; }; template &lt;typename Parser&gt; void parse(const std::string &amp;s, const Parser &amp;p) { std::string::const_iterator it = s.begin(), end = s.end(); bool r = phrase_parse(it, end, p, space); std::string label; fix f(label); std::for_each(s.begin(), s.end(), f); std::cout &lt;&lt; '"' &lt;&lt; label &lt;&lt; "\":\n" &lt;&lt; " - "; if (r &amp;&amp; it == end) std::cout &lt;&lt; "success!\n"; else std::cout &lt;&lt; "parse failed; r=" &lt;&lt; r &lt;&lt; '\n'; } int main() { parse("foo", lit("foo")); parse("foo\n", lit("foo") &gt;&gt; eol); parse("foo\r\n", lit("foo") &gt;&gt; eol); } </code></pre> <p>Output:</p> <pre>"foo": - success! "foo\n": - parse failed; r=0 "foo\r\n": - parse failed; r=0</pre> <p>Why do the latter two fail?</p> <hr> <p><strong>Related question:</strong></p> <p><a href="https://stackoverflow.com/questions/2428371/using-boostspirit-how-do-i-require-part-of-a-record-to-be-on-its-own-line">Using boost::spirit, how do I require part of a record to be on its own line?</a></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