more_compound_attributes.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>More About Attributes of Compound Components</title>
  5. <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
  7. <link rel="home" href="../../index.html" title="Spirit X3 3.0.4">
  8. <link rel="up" href="../abstracts.html" title="Abstracts">
  9. <link rel="prev" href="compound_attributes.html" title="Attributes of Compound Components">
  10. <link rel="next" href="nonterminal_attributes.html" title="Attributes of Nonterminals">
  11. </head>
  12. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  13. <table cellpadding="2" width="100%"><tr>
  14. <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
  15. <td align="center"><a href="../../../../../../../index.html">Home</a></td>
  16. <td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
  17. <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
  18. <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
  19. <td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
  20. </tr></table>
  21. <hr>
  22. <div class="spirit-nav">
  23. <a accesskey="p" href="compound_attributes.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../abstracts.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="nonterminal_attributes.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
  24. </div>
  25. <div class="section">
  26. <div class="titlepage"><div><div><h3 class="title">
  27. <a name="spirit_x3.abstracts.more_compound_attributes"></a><a class="link" href="more_compound_attributes.html" title="More About Attributes of Compound Components">More About
  28. Attributes of Compound Components</a>
  29. </h3></div></div></div>
  30. <p>
  31. While parsing input, it is often desirable to combine some constant elements
  32. with variable parts. For instance, let us look at the example of parsing
  33. or formatting a complex number, which is written as <code class="computeroutput"><span class="special">(</span><span class="identifier">real</span><span class="special">,</span> <span class="identifier">imag</span><span class="special">)</span></code>,
  34. where <code class="computeroutput"><span class="identifier">real</span></code> and <code class="computeroutput"><span class="identifier">imag</span></code> are the variables representing the
  35. real and imaginary parts of our complex number. This can be achieved by writing:
  36. </p>
  37. <pre class="programlisting"><span class="char">'('</span> <span class="special">&gt;&gt;</span> <span class="identifier">double_</span> <span class="special">&gt;&gt;</span> <span class="string">", "</span> <span class="special">&gt;&gt;</span> <span class="identifier">double_</span> <span class="special">&gt;&gt;</span> <span class="char">')'</span>
  38. </pre>
  39. <p>
  40. Literals (such as <code class="computeroutput"><span class="char">'('</span></code> and <code class="computeroutput"><span class="string">", "</span></code>) do <span class="emphasis"><em>not</em></span>
  41. expose any attribute (well actually, they do expose the special type <code class="computeroutput"><span class="identifier">unused_type</span></code>, but in this context <code class="computeroutput"><span class="identifier">unused_type</span></code> is interpreted as if the component
  42. does not expose any attribute at all). It is very important to understand
  43. that the literals don't consume any of the elements of a fusion sequence
  44. passed to this component sequence. As said, they just don't expose any attribute
  45. and don't produce (consume) any data. The following example shows this:
  46. </p>
  47. <pre class="programlisting"><span class="comment">// the following parses "(1.0, 2.0)" into a pair of double</span>
  48. <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">input</span><span class="special">(</span><span class="string">"(1.0, 2.0)"</span><span class="special">);</span>
  49. <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">iterator</span> <span class="identifier">strbegin</span> <span class="special">=</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
  50. <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">p</span><span class="special">;</span>
  51. <span class="identifier">x3</span><span class="special">::</span><span class="identifier">parse</span><span class="special">(</span><span class="identifier">strbegin</span><span class="special">,</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span>
  52. <span class="char">'('</span> <span class="special">&gt;&gt;</span> <span class="identifier">x3</span><span class="special">::</span><span class="identifier">double_</span> <span class="special">&gt;&gt;</span> <span class="string">", "</span> <span class="special">&gt;&gt;</span> <span class="identifier">x3</span><span class="special">::</span><span class="identifier">double_</span> <span class="special">&gt;&gt;</span> <span class="char">')'</span><span class="special">,</span> <span class="comment">// parser grammar</span>
  53. <span class="identifier">p</span><span class="special">);</span> <span class="comment">// attribute to fill while parsing</span>
  54. </pre>
  55. <p>
  56. where the first element of the pair passed in as the data to generate is
  57. still associated with the first <code class="computeroutput"><span class="identifier">double_</span></code>,
  58. and the second element is associated with the second <code class="computeroutput"><span class="identifier">double_</span></code>
  59. parser.
  60. </p>
  61. <p>
  62. This behavior should be familiar as it conforms to the way other input and
  63. output formatting libraries such as <code class="computeroutput"><span class="identifier">scanf</span></code>,
  64. <code class="computeroutput"><span class="identifier">printf</span></code> or <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">format</span></code>
  65. are handling their variable parts. In this context you can think about <span class="emphasis"><em>Spirit.X3</em></span>'s
  66. primitive components (such as the <code class="computeroutput"><span class="identifier">double_</span></code>
  67. above) as of being type safe placeholders for the attribute values.
  68. </p>
  69. <div class="tip"><table border="0" summary="Tip">
  70. <tr>
  71. <td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../images/tip.png"></td>
  72. <th align="left">Tip</th>
  73. </tr>
  74. <tr><td align="left" valign="top"><p>
  75. <span class="bold"><strong>For sequences only:</strong></span> To keep it simple,
  76. unlike __Spirit.qi__, <span class="emphasis"><em>Spirit.X3</em></span> does not support more
  77. than one attribute anymore in the <code class="computeroutput"><span class="identifier">parse</span></code>
  78. and <code class="computeroutput"><span class="identifier">phrase_parse</span></code> function.
  79. Just use <code class="computeroutput"><span class="identifier">std</span><span class="special">:</span><span class="identifier">tuple</span><span class="error">'</span><span class="special">.</span> <span class="identifier">Be</span> <span class="identifier">sure</span> <span class="identifier">to</span>
  80. <span class="identifier">include</span> </code>boost/fusion/adapted/std_tuple.hpp'
  81. in this case.
  82. </p></td></tr>
  83. </table></div>
  84. <p>
  85. Let's take a look at this from a more formal perspective:
  86. </p>
  87. <pre class="programlisting"><span class="identifier">a</span><span class="special">:</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">Unused</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">&gt;&gt;</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">A</span>
  88. </pre>
  89. <p>
  90. which reads as:
  91. </p>
  92. <div class="blockquote"><blockquote class="blockquote"><p>
  93. Given <code class="computeroutput"><span class="identifier">a</span></code> and <code class="computeroutput"><span class="identifier">b</span></code> are parsers, and <code class="computeroutput"><span class="identifier">A</span></code>
  94. is the attribute type of <code class="computeroutput"><span class="identifier">a</span></code>,
  95. and <code class="computeroutput"><span class="identifier">unused_type</span></code> is the
  96. attribute type of <code class="computeroutput"><span class="identifier">b</span></code>, then
  97. the attribute type of <code class="computeroutput"><span class="identifier">a</span> <span class="special">&gt;&gt;</span> <span class="identifier">b</span></code>
  98. (<code class="computeroutput"><span class="identifier">a</span> <span class="special">&lt;&lt;</span>
  99. <span class="identifier">b</span></code>) will be <code class="computeroutput"><span class="identifier">A</span></code>
  100. as well. This rule applies regardless of the position the element exposing
  101. the <code class="computeroutput"><span class="identifier">unused_type</span></code> is at.
  102. </p></blockquote></div>
  103. <p>
  104. This rule is the key to the understanding of the attribute handling in sequences
  105. as soon as literals are involved. It is as if elements with <code class="computeroutput"><span class="identifier">unused_type</span></code> attributes 'disappeared' during
  106. attribute propagation. Notably, this is not only true for sequences but for
  107. any compound components. For instance, for alternative components the corresponding
  108. rule is:
  109. </p>
  110. <pre class="programlisting"><span class="identifier">a</span><span class="special">:</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">Unused</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">|</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">A</span>
  111. </pre>
  112. <p>
  113. again, allowing to simplify the overall attribute type of an expression.
  114. </p>
  115. </div>
  116. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  117. <td align="left"></td>
  118. <td align="right"><div class="copyright-footer">Copyright &#169; 2001-2018 Joel de Guzman,
  119. Hartmut Kaiser<p>
  120. Distributed under the Boost Software License, Version 1.0. (See accompanying
  121. file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
  122. </p>
  123. </div></td>
  124. </tr></table>
  125. <hr>
  126. <div class="spirit-nav">
  127. <a accesskey="p" href="compound_attributes.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../abstracts.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="nonterminal_attributes.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
  128. </div>
  129. </body>
  130. </html>