Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use Hartmut Kaiser's <code>attribute_of_qi_component</code> metafunction from <a href="http://article.gmane.org/gmane.comp.parsers.spirit.general/18387" rel="nofollow">here</a>. This internally uses what ildjarn suggested for individual parsers, and in addition to that, also works with expressions such as <code>qi::int_ &gt;&gt; qi::lexeme[ qi::as_string[+qi::char_] ]</code> below.</p> <pre><code>#define BOOST_TEST_MODULE attr_of_qi_parsers #include &lt;boost/test/included/unit_test.hpp&gt; #include &lt;boost/spirit/include/qi.hpp&gt; #include &lt;string&gt; #include &lt;utility&gt; #include &lt;boost/fusion/include/adapted.hpp&gt; namespace qi = boost::spirit::qi; typedef std::pair&lt;int,int&gt; pair_type; typedef boost::fusion::vector2&lt;int,std::string&gt; vector_int_string; struct data { data(){} data(int m1, int m2): member1(m1), member2(m2) {} int member1; int member2; }; std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const data&amp; d) { os &lt;&lt; "{ 1st: " &lt;&lt; d.member1 &lt;&lt; ", 2nd: " &lt;&lt; d.member2 &lt;&lt; " }"; return os; } bool operator==(const data&amp; lhs, const data&amp; rhs) { return lhs.member1 == rhs.member1 &amp;&amp; lhs.member2 == rhs.member2; } BOOST_FUSION_ADAPT_STRUCT(data, (int, member1) (int, member2) ) //BOOST_CHECK_EQUAL requires that the arguments have defined operator&lt;&lt; //You can either use in the global namespace BOOST_TEST_DONT_PRINT_LOG_VALUE(pair_type); //or define operator&lt;&lt; in the namespace std. This is technically illegal but it works //namespace std //{ // std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const std::pair&lt;int,int&gt;&amp; p) // { // os &lt;&lt; "&lt;" &lt;&lt; p.first &lt;&lt; "," &lt;&lt; p.second &lt;&lt; "&gt;"; // return os; // } //} namespace tests { template &lt;typename Expr, typename Iterator = boost::spirit::unused_type&gt; struct attribute_of_qi_component { typedef typename boost::spirit::result_of::compile&lt; qi::domain, Expr &gt;::type parser_expression_type; typedef typename boost::spirit::traits::attribute_of&lt; parser_expression_type, boost::spirit::unused_type, Iterator &gt;::type type; }; template &lt;typename P&gt; typename attribute_of_qi_component&lt;P&gt;::type parse(P const&amp; p, const std::string&amp; input) { std::string::const_iterator f = input.begin(); std::string::const_iterator l = input.end(); typename attribute_of_qi_component&lt;P&gt;::type parsed; qi::phrase_parse(f, l, p, boost::spirit::ascii::space, parsed); return parsed; } BOOST_AUTO_TEST_CASE(int_parser) { BOOST_CHECK_EQUAL(parse(qi::int_, "23" ), 23); } BOOST_AUTO_TEST_CASE(int_and_string_parser) { BOOST_CHECK_EQUAL(parse(qi::int_ &gt;&gt; qi::lexeme[ qi::as_string[+qi::char_] ], "23 is a number"), vector_int_string(23,"is a number")); } BOOST_AUTO_TEST_CASE(pair_rule_parser){ qi::rule&lt;std::string::const_iterator,pair_type(),boost::spirit::ascii::space_type&gt; pair_rule = qi::int_ &gt;&gt; ',' &gt;&gt; qi::int_; BOOST_CHECK_EQUAL(parse(pair_rule,"1, 2"), std::make_pair(1,2)); } BOOST_AUTO_TEST_CASE(data_rule_parser){ qi::rule&lt;std::string::const_iterator,data(),boost::spirit::ascii::space_type&gt; data_rule = qi::int_ &gt;&gt; ',' &gt;&gt; qi::int_; BOOST_CHECK_EQUAL(parse(data_rule,"2, 4"), data(2,4)); } }//end of tests namespace </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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