compound_attributes.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>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="primitive_attributes.html" title="Attributes of Primitive Components">
  10. <link rel="next" href="more_compound_attributes.html" title="More About Attributes of Compound Components">
  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="primitive_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="more_compound_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.compound_attributes"></a><a class="link" href="compound_attributes.html" title="Attributes of Compound Components">Attributes of
  28. Compound Components</a>
  29. </h3></div></div></div>
  30. <p>
  31. <span class="emphasis"><em>Spirit.X3</em></span> implement well defined attribute type propagation
  32. rules for all compound parsers, such as sequences, alternatives, Kleene star,
  33. etc. The main attribute propagation rule for a sequences is for instance:
  34. </p>
  35. <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">B</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">tuple</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span>
  36. </pre>
  37. <p>
  38. which reads as:
  39. </p>
  40. <div class="blockquote"><blockquote class="blockquote"><p>
  41. 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>
  42. is the attribute type of <code class="computeroutput"><span class="identifier">a</span></code>,
  43. and <code class="computeroutput"><span class="identifier">B</span></code> is the attribute
  44. type of <code class="computeroutput"><span class="identifier">b</span></code>, then the attribute
  45. type of <code class="computeroutput"><span class="identifier">a</span> <span class="special">&gt;&gt;</span>
  46. <span class="identifier">b</span></code> (<code class="computeroutput"><span class="identifier">a</span>
  47. <span class="special">&lt;&lt;</span> <span class="identifier">b</span></code>)
  48. will be <code class="computeroutput"><span class="identifier">tuple</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span></code>.
  49. </p></blockquote></div>
  50. <div class="note"><table border="0" summary="Note">
  51. <tr>
  52. <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../images/note.png"></td>
  53. <th align="left">Note</th>
  54. </tr>
  55. <tr><td align="left" valign="top"><p>
  56. The notation <code class="computeroutput"><span class="identifier">tuple</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span></code> is used as a placeholder expression
  57. for any fusion sequence holding the types A and B, such as <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fusion</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span></code> or <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span></code>
  58. (for more information see <a href="../../../../../../../libs/fusion/doc/html/index.html" target="_top">Boost.Fusion</a>).
  59. </p></td></tr>
  60. </table></div>
  61. <p>
  62. As you can see, in order for a type to be compatible with the attribute type
  63. of a compound expression it has to
  64. </p>
  65. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  66. <li class="listitem">
  67. either be convertible to the attribute type,
  68. </li>
  69. <li class="listitem">
  70. or it has to expose certain functionalities, i.e. it needs to conform
  71. to a concept compatible with the component.
  72. </li>
  73. </ul></div>
  74. <p>
  75. Each compound component implements its own set of attribute propagation rules.
  76. For a full list of how the different compound parsers consume attributes
  77. see the sections Parser Compound Attribute Rules.
  78. </p>
  79. <h5>
  80. <a name="spirit_x3.abstracts.compound_attributes.h0"></a>
  81. <span class="phrase"><a name="spirit_x3.abstracts.compound_attributes.the_attribute_of_sequence_parsers"></a></span><a class="link" href="compound_attributes.html#spirit_x3.abstracts.compound_attributes.the_attribute_of_sequence_parsers">The
  82. Attribute of Sequence Parsers</a>
  83. </h5>
  84. <p>
  85. Sequences require an attribute type to expose the concept of a fusion sequence,
  86. where all elements of that fusion sequence have to be compatible with the
  87. corresponding element of the component sequence. For example, the expression:
  88. </p>
  89. <pre class="programlisting"><span class="identifier">double_</span> <span class="special">&gt;&gt;</span> <span class="identifier">double_</span>
  90. </pre>
  91. <p>
  92. is compatible with any fusion sequence holding two types, where both types
  93. have to be compatible with <code class="computeroutput"><span class="keyword">double</span></code>.
  94. The first element of the fusion sequence has to be compatible with the attribute
  95. of the first <code class="computeroutput"><span class="identifier">double_</span></code>, and
  96. the second element of the fusion sequence has to be compatible with the attribute
  97. of the second <code class="computeroutput"><span class="identifier">double_</span></code>. If
  98. we assume to have an instance of a <code class="computeroutput"><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></code>,
  99. we can directly use the expressions above to do both, parse input to fill
  100. the attribute:
  101. </p>
  102. <pre class="programlisting"><span class="comment">// the following parses "1.0 2.0" into a pair of double</span>
  103. <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>
  104. <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>
  105. <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>
  106. <span class="identifier">x3</span><span class="special">::</span><span class="identifier">phrase_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>
  107. <span class="identifier">x3</span><span class="special">::</span><span class="identifier">double_</span> <span class="special">&gt;&gt;</span> <span class="identifier">x3</span><span class="special">::</span><span class="identifier">double_</span><span class="special">,</span> <span class="comment">// parser grammar</span>
  108. <span class="identifier">x3</span><span class="special">::</span><span class="identifier">space</span><span class="special">,</span> <span class="comment">// delimiter grammar</span>
  109. <span class="identifier">p</span><span class="special">);</span> <span class="comment">// attribute to fill while parsing</span>
  110. </pre>
  111. <div class="tip"><table border="0" summary="Tip">
  112. <tr>
  113. <td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../images/tip.png"></td>
  114. <th align="left">Tip</th>
  115. </tr>
  116. <tr><td align="left" valign="top"><p>
  117. <span class="bold"><strong>For sequences only:</strong></span> To keep it simple,
  118. unlike __Spirit.qi__, <span class="emphasis"><em>Spirit.X3</em></span> does not support more
  119. than one attribute anymore in the <code class="computeroutput"><span class="identifier">parse</span></code>
  120. and <code class="computeroutput"><span class="identifier">phrase_parse</span></code> function.
  121. 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>
  122. <span class="identifier">include</span> </code>boost/fusion/adapted/std_tuple.hpp'
  123. in this case.
  124. </p></td></tr>
  125. </table></div>
  126. <h5>
  127. <a name="spirit_x3.abstracts.compound_attributes.h1"></a>
  128. <span class="phrase"><a name="spirit_x3.abstracts.compound_attributes.the_attribute_of_alternative_parsers"></a></span><a class="link" href="compound_attributes.html#spirit_x3.abstracts.compound_attributes.the_attribute_of_alternative_parsers">The
  129. Attribute of Alternative Parsers</a>
  130. </h5>
  131. <p>
  132. Alternative parsers are all about - well - alternatives. In order to store
  133. possibly different result (attribute) types from the different alternatives
  134. we use the data type <a href="http://www.boost.org/doc/html/variant.html" target="_top">Boost.Variant</a>.
  135. The main attribute propagation rule of these components is:
  136. </p>
  137. <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">B</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">variant</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span>
  138. </pre>
  139. <p>
  140. Alternatives have a second very important attribute propagation rule:
  141. </p>
  142. <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">A</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>
  143. </pre>
  144. <p>
  145. often simplifying things significantly. If all sub expressions of an alternative
  146. expose the same attribute type, the overall alternative will expose exactly
  147. the same attribute type as well.
  148. </p>
  149. </div>
  150. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  151. <td align="left"></td>
  152. <td align="right"><div class="copyright-footer">Copyright &#169; 2001-2018 Joel de Guzman,
  153. Hartmut Kaiser<p>
  154. Distributed under the Boost Software License, Version 1.0. (See accompanying
  155. 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>)
  156. </p>
  157. </div></td>
  158. </tr></table>
  159. <hr>
  160. <div class="spirit-nav">
  161. <a accesskey="p" href="primitive_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="more_compound_attributes.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
  162. </div>
  163. </body>
  164. </html>