primitive_attributes.html 6.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Attributes of Primitive 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="parsing_expression_grammar.html" title="Parsing Expression Grammar">
  10. <link rel="next" href="compound_attributes.html" title="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="parsing_expression_grammar.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="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.primitive_attributes"></a><a class="link" href="primitive_attributes.html" title="Attributes of Primitive Components">Attributes
  28. of Primitive Components</a>
  29. </h3></div></div></div>
  30. <p>
  31. Parsers in <a href="http://boost-spirit.com" target="_top">Spirit</a> are fully
  32. attributed. <span class="emphasis"><em>Spirit.X3</em></span> parsers always <span class="emphasis"><em>expose</em></span>
  33. an attribute specific to their type. This is called <span class="emphasis"><em>synthesized
  34. attribute</em></span> as it is returned from a successful match representing
  35. the matched input sequence. For instance, numeric parsers, such as <code class="computeroutput"><span class="identifier">int_</span></code> or <code class="computeroutput"><span class="identifier">double_</span></code>,
  36. return the <code class="computeroutput"><span class="keyword">int</span></code> or <code class="computeroutput"><span class="keyword">double</span></code> value converted from the matched input
  37. sequence. Other primitive parser components have other intuitive attribute
  38. types, such as for instance <code class="computeroutput"><span class="identifier">int_</span></code>
  39. which has <code class="computeroutput"><span class="keyword">int</span></code>, or <code class="computeroutput"><span class="identifier">ascii</span><span class="special">::</span><span class="identifier">char_</span></code> which has <code class="computeroutput"><span class="keyword">char</span></code>.
  40. Primitive parsers apply the normal C++ convertibility rules: you can use
  41. any C++ type to receive the parsed value as long as the attribute type of
  42. the parser is convertible to the type provided. The following example shows
  43. how a synthesized parser attribute (the <code class="computeroutput"><span class="keyword">int</span></code>
  44. value) is extracted by calling the API function <code class="computeroutput"><span class="identifier">x3</span><span class="special">::</span><span class="identifier">parse</span></code>:
  45. </p>
  46. <pre class="programlisting"><span class="keyword">int</span> <span class="identifier">value</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
  47. <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">str</span><span class="special">(</span><span class="string">"123"</span><span class="special">);</span>
  48. <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">str</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
  49. <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">str</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">int_</span><span class="special">,</span> <span class="identifier">value</span><span class="special">);</span> <span class="comment">// value == 123</span>
  50. </pre>
  51. <p>
  52. For a full list of available parser primitives and their attribute types
  53. please see the sections X3 Parsers.
  54. </p>
  55. </div>
  56. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  57. <td align="left"></td>
  58. <td align="right"><div class="copyright-footer">Copyright &#169; 2001-2018 Joel de Guzman,
  59. Hartmut Kaiser<p>
  60. Distributed under the Boost Software License, Version 1.0. (See accompanying
  61. 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>)
  62. </p>
  63. </div></td>
  64. </tr></table>
  65. <hr>
  66. <div class="spirit-nav">
  67. <a accesskey="p" href="parsing_expression_grammar.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="compound_attributes.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
  68. </div>
  69. </body>
  70. </html>