Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you are looking for 'subrules' (that Spirit V1/classical used to have). These are obsolete now.</p> <p>Have a look at </p> <ul> <li><p>c++11 <code>auto</code> and <code>BOOST_AUTO</code> </p> <pre><code>auto subexpression = int_ &gt;&gt; ',' &gt;&gt; double_; qi::rule&lt;It&gt; rule = "A:" &gt;&gt; subexpression &gt;&gt; "Rest:" &gt;&gt; (subexpression % eol); </code></pre> <p>There used to be issues with using <code>auto</code> on Spirit rules (notably with MSVC) (see <a href="http://boost-spirit.com/home/articles/qi-example/zero-to-60-mph-in-2-seconds/" rel="nofollow">Zero to 60 MPH in 2 seconds!</a> and comments) but I have been informed this (soon) <a href="http://boost.2283326.n4.nabble.com/What-happened-to-subrules-tc4636688.html" rel="nofollow">is no longer an issue</a>: </p> <blockquote> <pre><code>Yep. Anyway,FYI, it's fixed in Spirit-3. You can use auto all you want. Regards, -- Joel de Guzman </code></pre> </blockquote></li> <li><p><a href="http://www.boost.org/doc/libs/1_52_0/libs/spirit/doc/html/spirit/qi/reference/auxiliary/lazy.html" rel="nofollow">qi::lazy</a></p></li> <li><a href="http://www.boost.org/doc/libs/1_52_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">inherited arguments</a> - introduced in the <a href="http://www.boost.org/doc/libs/1_52_0/libs/spirit/doc/html/spirit/qi/tutorials/mini_xml___asts_.html" rel="nofollow">Mini XML - ASTs</a> tutorial</li> </ul> <p>Here is a proof of concept that passes a common subrule to different 'compound' rules to allow for wrapping in <code>()</code>, <code>[]</code> or <code>{}</code>:</p> <pre><code>#include &lt;boost/spirit/include/qi.hpp&gt; #include &lt;boost/spirit/include/phoenix.hpp&gt; namespace qi = boost::spirit::qi; namespace phx = boost::phoenix; typedef std::string::const_iterator It; template &lt;typename R&gt; void test(const std::string&amp; input, R const&amp; rule) { It f(input.begin()), l(input.end()); bool ok = qi::phrase_parse(f,l,rule,qi::space); std::cout &lt;&lt; "'" &lt;&lt; input &lt;&lt; "'\tparse " &lt;&lt; (ok?"success":"failure") &lt;&lt; "\n"; } int main() { typedef qi::rule&lt;It, qi::space_type&gt; common_rule; typedef qi::rule&lt;It, void(common_rule), qi::space_type&gt; compound_rule; common_rule common = qi::int_; compound_rule in_parens = qi::lit('(') &gt;&gt; qi::_r1 &gt;&gt; ')', in_brackets = qi::lit('[') &gt;&gt; qi::_r1 &gt;&gt; ']', in_braces = qi::lit('{') &gt;&gt; qi::_r1 &gt;&gt; '}'; test("{ 231 }" , in_braces (phx::ref(common )) ); test("{ hello }", in_braces (phx::val("hello")) ); test("( 231 )" , in_parens (phx::ref(common )) ); test("( hello )", in_parens (phx::val("hello")) ); test("[ 231 ]" , in_brackets(phx::ref(common )) ); test("[ hello ]", in_brackets(phx::val("hello")) ); } </code></pre> <p>Output:</p> <pre><code>'{ 231 }' parse success '{ hello }' parse success '( 231 )' parse success '( hello )' parse success '[ 231 ]' parse success '[ hello ]' parse success </code></pre> <p>PS. Note the above is <strong><em>not</em></strong> a typical Spirit grammar. This way doesn't play too well when the 'common' rule would expose different attributes.</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. This table or related slice is empty.
    1. 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