{"id":854,"date":"2010-01-14T07:11:15","date_gmt":"2010-01-14T15:11:15","guid":{"rendered":"http:\/\/boost-spirit.com\/home\/2010\/01\/14\/why-might-i-want-to-use-the-directive-qiraw\/"},"modified":"2010-01-18T19:10:08","modified_gmt":"2010-01-19T03:10:08","slug":"why-might-i-want-to-use-the-directive-qiraw","status":"publish","type":"post","link":"http:\/\/boost-spirit.com\/home\/2010\/01\/14\/why-might-i-want-to-use-the-directive-qiraw\/","title":{"rendered":"Why might I want to use the directive qi::raw[ ]?"},"content":{"rendered":"<p>Over at the Freenet #boost IRC channel somebody (I think it was @VeXocide) suggested to write a &#8216;Tip of the Day&#8217; about the <em>Qi<\/em> directive <span style=\"font-family: Courier New;\">raw[]<\/span>. I was told this &#8216;was a major stumbling stone&#8217; while learning <em>Qi<\/em>. I always appreciate to get suggestions for articles, so here we go\u2026<\/p>\n<p><!--more--><\/p>\n<p>For those of you following the discussions around Spirit, the <span style=\"font-family: Courier New;\">raw[]<\/span> directive might be somewhat surprising. Almost every introduction talks about <em>Spirit<\/em> as being fully attributed. That means that every component exposes its own, specific attribute type representing the matched input (in <em>Qi<\/em>) or the data to emit output for (in <em>Karma<\/em>). This is a major change from earlier <em>Spirit<\/em> versions (we call those <em>Classic<\/em> today) which created so called transduction parsers. Transduction parsers &#8216;return&#8217; a pair of pointers (iterators) to the matched portion of the input without attempting to convert it to any specific result type.<\/p>\n<p>The directive <span style=\"font-family: Courier New;\">qi::raw[]<\/span> reintroduces transduction parsing into <em>Qi<\/em>. It is doing that by exposing as its attribute the pair of iterators to the input sequence matched by its embedded parser. To be concise, it exposes an instance\u00a0 of a <span style=\"font-family: Courier New;\">boost::iterator_range&lt;Iterator&gt;<\/span> containing the two iterators (where <span style=\"font-family: Courier New;\">Iterator<\/span> is the type of the underlying input stream as passed to <span style=\"font-family: Courier New;\">qi::parse<\/span>).<\/p>\n<p>Here is an example matching any valid C++ identifier:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nnamespace qi = boost::spirit::qi;\r\nstd::string input(&quot;abc123_&quot;);\r\nstd::string::const_iterator b = input.begin();\r\nstd::string ident;\r\nqi::parse(b, input.end(),\r\n    qi::raw[(qi::alpha | '_') &gt;&gt; *(qi::alnum | '_')], ident);\r\n<\/pre>\n<p>This example works as expected as the <span style=\"font-family: Courier New;\">std::string<\/span> attribute (the variable <span style=\"font-family: Courier New;\">ident<\/span>) is compatible with the <span style=\"font-family: Courier New;\">raw[]<\/span> component (the <span style=\"font-family: Courier New;\">std::string<\/span> implements a similar constructor as the <span style=\"font-family: Courier New;\">iterator_range<\/span> natively supported by <span style=\"font-family: Courier New;\">raw[]<\/span>). The embedded parser will match the C++ identifier without any attribute conversion as the <span style=\"font-family: Courier New;\">raw[]<\/span> invokes it with an attribute of the type <span style=\"font-family: Courier New;\">unused_type<\/span>, effectively disabling attribute handling. If the embedded parser succeeds the iterators pointing to the matched input sequence are used to initialize the <span style=\"font-family: Courier New;\">std::string<\/span> passed in as the attribute. Voila!<\/p>\n<p>In this example it would be just more complex (but still possible) to write a parser expression utilizing <em>Spirit&#8217;s<\/em> built in attribute propagation rules. At least you would need to write <span style=\"font-family: Courier New;\">char_(&#8216;_&#8217;)<\/span> instead of the plain <span style=\"font-family: Courier New;\">&#8216;_&#8217;<\/span>. Sometimes though, for other use cases,\u00a0 it is a lot more difficult or just impossible to employ the attribute magic. That is where <span style=\"font-family: Courier New;\">raw[]<\/span> really shines. It allows to embed complex parser expressions without having to worry about attribute matching and propagation. The embedded parser will run as fast as possible as it is invoked in &#8216;match mode&#8217; only, which skips attribute conversion.<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li><a href=\"#\" class=\"sharing-anchor sd-button share-more\"><span>Share<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><div class=\"sharing-hidden\"><div class=\"inner\" style=\"display: none;\"><ul><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-854\" class=\"share-facebook sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/14\/why-might-i-want-to-use-the-directive-qiraw\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span>Facebook<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-854\" class=\"share-twitter sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/14\/why-might-i-want-to-use-the-directive-qiraw\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-pinterest\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-pinterest-854\" class=\"share-pinterest sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/14\/why-might-i-want-to-use-the-directive-qiraw\/?share=pinterest\" target=\"_blank\" title=\"Click to share on Pinterest\" ><span>Pinterest<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-linkedin-854\" class=\"share-linkedin sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/14\/why-might-i-want-to-use-the-directive-qiraw\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\" ><span>LinkedIn<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-reddit\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/14\/why-might-i-want-to-use-the-directive-qiraw\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\" ><span>Reddit<\/span><\/a><\/li><li class=\"share-tumblr\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-tumblr sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/14\/why-might-i-want-to-use-the-directive-qiraw\/?share=tumblr\" target=\"_blank\" title=\"Click to share on Tumblr\" ><span>Tumblr<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Over at the Freenet #boost IRC channel somebody (I think it was @VeXocide) suggested to write a &#8216;Tip of the Day&#8217; about the Qi directive raw[]. I was told this &#8216;was a major stumbling stone&#8217; while learning Qi. I always appreciate to get suggestions for articles, so here we go\u2026<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li><a href=\"#\" class=\"sharing-anchor sd-button share-more\"><span>Share<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><div class=\"sharing-hidden\"><div class=\"inner\" style=\"display: none;\"><ul><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-854\" class=\"share-facebook sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/14\/why-might-i-want-to-use-the-directive-qiraw\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span>Facebook<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-854\" class=\"share-twitter sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/14\/why-might-i-want-to-use-the-directive-qiraw\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-pinterest\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-pinterest-854\" class=\"share-pinterest sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/14\/why-might-i-want-to-use-the-directive-qiraw\/?share=pinterest\" target=\"_blank\" title=\"Click to share on Pinterest\" ><span>Pinterest<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-linkedin-854\" class=\"share-linkedin sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/14\/why-might-i-want-to-use-the-directive-qiraw\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\" ><span>LinkedIn<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-reddit\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/14\/why-might-i-want-to-use-the-directive-qiraw\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\" ><span>Reddit<\/span><\/a><\/li><li class=\"share-tumblr\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-tumblr sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/2010\/01\/14\/why-might-i-want-to-use-the-directive-qiraw\/?share=tumblr\" target=\"_blank\" title=\"Click to share on Tumblr\" ><span>Tumblr<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div><\/div><\/div>","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"","spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[19,18],"tags":[8],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pIHdZ-dM","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/posts\/854"}],"collection":[{"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/comments?post=854"}],"version-history":[{"count":4,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/posts\/854\/revisions"}],"predecessor-version":[{"id":856,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/posts\/854\/revisions\/856"}],"wp:attachment":[{"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/media?parent=854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/categories?post=854"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/tags?post=854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}