Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Fully working demo: <strong><a href="http://coliru.stacked-crooked.com/view?id=a5d6bd0dfd0fd832103f5165dac13ead-f468fd186ebe6d5f25efae86bd4f0130" rel="nofollow">live on Coliru</a></strong></p> <p>What this adds over a naive approach is that <code>raw</code> values are optionally ended at <code>]</code> if the state is <code>in_group</code>.</p> <p>I elected pass the state using an <em>inherited attribute</em> (<code>bool</code>).</p> <p>This implementation allows <em>nested</em> sub-groups as well, e.g.: <code>"[w1/[w2/w3]2/w4]3"</code></p> <pre><code>#define BOOST_SPIRIT_DEBUG #include &lt;boost/spirit/include/qi.hpp&gt; #include &lt;boost/spirit/include/phoenix.hpp&gt; namespace phx = boost::phoenix; int main() { typedef std::string::const_iterator It; const std::string input = "[w1/[w2/w3]2/w4]3"; std::vector&lt;std::string&gt; v; It first(input.begin()), last(input.end()); using namespace boost::spirit::qi; rule&lt;It, std::string(bool in_group)&gt; raw; rule&lt;It, std::vector&lt;std::string&gt;(bool in_group), space_type&gt; group, delimited; _r1_type in_group; // friendly alias for the inherited attribute raw = eps(in_group) &gt;&gt; +~char_("/]") | +~char_("/"); delimited = (group(in_group)|raw(in_group)) % '/'; group = ('[' &gt;&gt; delimited(in_group=true) &gt;&gt; ']' &gt;&gt; int_) [ phx::while_(_2--) [ phx::insert(_val, phx::end(_val), phx::begin(_1), phx::end(_1)) ] ]; BOOST_SPIRIT_DEBUG_NODES((raw)(delimited)(group)); bool r = phrase_parse(first, last, delimited(false), space,v); if (r) std::copy(v.begin(), v.end(), std::ostream_iterator&lt;std::string&gt;(std::cout, "\n")); } </code></pre> <p>Prints:</p> <pre><code>w1 w2 w3 w2 w3 w4 w1 w2 w3 w2 w3 w4 w1 w2 w3 w2 w3 w4 </code></pre> <p>(besides debug info)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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