Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Like I commented, you need to use <code>qi::_r1</code> and <code>qi::_r2</code>, the placeholders for <a href="http://www.boost.org/doc/libs/1_55_0/libs/spirit/doc/html/spirit/qi/reference/parser_concepts/nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal._code__phrase_role__identifier___r1__phrase___code_______code__phrase_role__identifier__r10__phrase___code_" rel="nofollow"><em>inherited attributes</em></a><sup>[1]</sup>.</p> <p>For the rest I can assume things (as your code is incomplete and slightly unclear to me due to a non-standard approach):</p> <ul> <li>you probably want <code>ascii::blank_type</code> as the skipper, since otherwise <code>lit('\n')</code> would never match (unless with <code>no_skip[]</code> or <code>lexeme[]</code>).</li> <li><p>you have to take care to call the parser with an actual <code>CharType const*</code>, not a <code>CharType const (&amp;)[]</code>. The latter happens if you call it with e.g. <code>parser("name", 'a')</code>. Instead, either </p> <ul> <li>introduce a temporary variable</li> <li>a cast</li> <li>implicitely decay using e.g. <code>+"name"</code></li> </ul></li> </ul> <p>I've made up an example and it succeeds, see it <strong><a href="http://coliru.stacked-crooked.com/a/9821f9d05a44f5a3" rel="nofollow">Live on Coliru</a></strong></p> <p>Full code:</p> <pre><code>#define BOOST_SPIRIT_USE_PHOENIX_V3 #include &lt;boost/fusion/adapted.hpp&gt; #include &lt;boost/spirit/include/qi.hpp&gt; namespace qi = boost::spirit::qi; namespace ascii = boost::spirit::ascii; template &lt;typename StreamIterator, typename CharType=char&gt; struct json_start_elem_grammar_object : qi::grammar&lt;StreamIterator, void(const CharType*, CharType), ascii::blank_type&gt; { json_start_elem_grammar_object() : json_start_elem_grammar_object::base_type(start_elem, "start_elem") { using namespace qi; _r1_type _name; // prefer descriptive names _r2_type _preamble; start_elem = '"' &gt; lit(_name) &gt; '"' &gt; ':' &gt; lit(_preamble) &gt; -lit('\n') ; } qi::rule&lt;StreamIterator, void(const CharType*, CharType), ascii::blank_type&gt; start_elem; }; bool doParse(const std::string&amp; input) { typedef std::string::const_iterator It; auto f(begin(input)), l(end(input)); json_start_elem_grammar_object&lt;It&gt; p; try { bool ok = qi::phrase_parse(f, l, p(+"lol", 'q'), ascii::blank); if (ok) { std::cout &lt;&lt; "parse success\n"; } else std::cerr &lt;&lt; "parse failed: '" &lt;&lt; std::string(f,l) &lt;&lt; "'\n"; if (f!=l) std::cerr &lt;&lt; "trailing unparsed: '" &lt;&lt; std::string(f,l) &lt;&lt; "'\n"; return ok; } catch(const qi::expectation_failure&lt;It&gt;&amp; e) { std::string frag(e.first, e.last); std::cerr &lt;&lt; e.what() &lt;&lt; "'" &lt;&lt; frag &lt;&lt; "'\n"; } return false; } int main() { bool ok = doParse("\"lol\" : q\n"); return ok? 0 : 255; } </code></pre> <hr> <p><sup>[1]</sup> See also</p> <ul> <li>documentation for <a href="http://boost-spirit.com/home/articles/attribute_handling/the-magical-power-of-attributes-in-spirit-directives-and-non-terminals/" rel="nofollow">non-terminals</a></li> <li>the <a href="http://www.boost.org/doc/libs/1_55_0/libs/spirit/doc/html/spirit/qi/tutorials/mini_xml___asts_.html#spirit.qi.tutorials.mini_xml___asts_.inherited_attribute" rel="nofollow">mini-xml example from the tutorial</a></li> </ul>
 

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