select_parser.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <html>
  2. <head>
  3. <title>The Select Parser</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. <link rel="stylesheet" href="theme/style.css" type="text/css">
  6. <style type="text/css">
  7. <!--
  8. .style1 {font-family: "Courier New", Courier, mono}
  9. .style2 {font-family: "Courier New", Courier, mono; font-style: italic; }
  10. .style3 {font-family: "Courier New", Courier, mono; color: #FF0000; }
  11. -->
  12. </style>
  13. </head>
  14. <body>
  15. <table width="100%" border="0" background="theme/bkd2.gif" cellspacing="2">
  16. <tr>
  17. <td width="10"> </td>
  18. <td width="85%"> <font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>The Select Parser </b></font></td>
  19. <td width="112"><a href="http://spirit.sf.net"><img src="theme/spirit.gif" width="112" height="48" align="right" border="0"></a></td>
  20. </tr>
  21. </table>
  22. <br>
  23. <table border="0">
  24. <tr>
  25. <td width="10"></td>
  26. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  27. <td width="30"><a href="the_lazy_parser.html"><img src="theme/l_arr.gif" border="0"></a></td>
  28. <td width="30"><a href="switch_parser.html"><img src="theme/r_arr.gif" border="0"></a></td>
  29. </tr>
  30. </table>
  31. <p>Select parsers may be used to identify a single parser from a given list
  32. of parsers, which successfully recognizes the current input sequence. Example:</p>
  33. <pre> rule<span class="special">&lt;&gt;</span> rule_select <span class="special">=</span>
  34. select_p<span class="special">
  35. (</span>
  36. parser_a<span class="special">
  37. ,</span> parser_b<span class="special">
  38. <span class="comment">/* ... */</span>
  39. ,</span> parser_n
  40. <span class="special">)</span><span class="special">;</span></pre>
  41. <p>The parsers (parser_a, parser_b etc.) are tried sequentially from left to right until a parser matches the current input sequence.
  42. If there is a matching parser found, the <tt>select_p</tt> parser returns
  43. the parser's position (zero based index). For instance, in the example above, <tt>1</tt> is returned if parser_b
  44. matches.</p>
  45. <p>There are two predefined parsers of the select parser family: <tt>select_p</tt>
  46. and <tt>select_fail_p</tt>. These parsers differ in the way the no match
  47. case is handled (when none of the parsers match the current input sequence).
  48. While the <tt>select_p</tt> parser will return <tt>-1</tt>
  49. if no matching parser is found, the <tt>select_fail_p</tt> parser will not match
  50. at all.</p>
  51. <p>The following sample shows how the select parser may be used very conveniently
  52. in conjunction with a <a href="switch_parser.html">switch parser</a>:</p>
  53. <pre> <span class="keyword">int</span> choice <span class="special">=</span> <span class="literal">-1</span><span class="special">;</span>
  54. rule<span class="special">&lt;&gt;</span> rule_select <span class="special">=</span>
  55. select_fail_p<span class="special">(</span><span class="literal">'a'</span><span class="special">,</span> <span class="literal">'b'</span><span class="special">,</span> <span class="literal">'c'</span><span class="special">,</span> <span class="literal">'d'</span><span class="special">)[</span>assign_a<span class="special">(</span>choice<span class="special">)]</span>
  56. &gt;&gt; switch_p(var<span class="special">(</span>choice)) <span class="special">
  57. [</span><br> case_p<span class="special">&lt;</span><span class="literal">0</span><span class="special">&gt;(</span>int_p<span class="special">),</span><br> case_p<span class="special">&lt;</span><span class="literal">1</span><span class="special">&gt;(</span>ch_p<span class="special">(</span><span class="literal">','</span><span class="special">)),</span><br> case_p<span class="special">&lt;</span><span class="literal">2</span><span class="special">&gt;(</span>str_p<span class="special">(</span><span class="string">&quot;bcd&quot;</span><span class="special">)),</span><br> default_p<br> <span class="special">]</span><br><span class="special"> ;</span></pre>
  58. <p>This example shows a rule, which matches:</p>
  59. <ul>
  60. <li><span class="literal"> 'a' </span>followed
  61. by an integer</li>
  62. <li><span class="literal">'b' </span>followed by a<span class="literal">
  63. ','</span></li>
  64. <li><span class="literal">'c'</span> followed by <span class="string">&quot;bcd&quot;</span></li>
  65. <li>a single <span class="literal">'d'</span>. </li>
  66. </ul>
  67. <p>For other input sequences the
  68. give rule does not match at all.</p>
  69. <table width="80%" border="0" align="center">
  70. <tr>
  71. <td class="note_box"><p><img src="theme/alert.gif" width="16" height="16"> <tt>BOOST_SPIRIT_SELECT_LIMIT</tt><br>
  72. <br>
  73. The number of possible entries inside the <tt>select_p</tt> parser is limited by the Spirit compile time constant <tt>BOOST_SPIRIT_SELECT_LIMIT</tt>, which defaults to 3. This value should not be greater than the compile time constant given by <tt>PHOENIX_LIMIT</tt> (see <a href="../phoenix/index.html">phoenix</a>). Example:</p>
  74. <p class="style1"><span class="comment">// Define these before including anything else <br>
  75. </span><span class="style3">#define</span> PHOENIX_LIMIT 10<br>
  76. <span class="preprocessor">#define</span> BOOST_SPIRIT_SELECT_LIMIT 10 </p></td>
  77. </tr>
  78. </table>
  79. <br>
  80. <table border="0">
  81. <tr>
  82. <td width="10"></td>
  83. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  84. <td width="30"><a href="the_lazy_parser.html"><img src="theme/l_arr.gif" border="0"></a></td>
  85. <td width="30"><a href="switch_parser.html"><img src="theme/r_arr.gif" border="0"></a></td>
  86. </tr>
  87. </table>
  88. <br>
  89. <hr size="1">
  90. <p class="copyright">Copyright &copy; 2003-2004 Hartmut Kaiser <br>
  91. <br>
  92. <font size="2">Use, modification and distribution is subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) </font> </p>
  93. <p>&nbsp;</p>
  94. </body>
  95. </html>