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 | |
---|
15 | <body> |
---|
16 | <table width="100%" border="0" background="theme/bkd2.gif" cellspacing="2"> |
---|
17 | <tr> |
---|
18 | <td width="10"> </td> |
---|
19 | <td width="85%"> <font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>The Select Parser </b></font></td> |
---|
20 | <td width="112"><a href="http://spirit.sf.net"><img src="theme/spirit.gif" width="112" height="48" align="right" border="0"></a></td> |
---|
21 | </tr> |
---|
22 | </table> |
---|
23 | <br> |
---|
24 | <table border="0"> |
---|
25 | <tr> |
---|
26 | <td width="10"></td> |
---|
27 | <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td> |
---|
28 | <td width="30"><a href="the_lazy_parser.html"><img src="theme/l_arr.gif" border="0"></a></td> |
---|
29 | <td width="30"><a href="switch_parser.html"><img src="theme/r_arr.gif" border="0"></a></td> |
---|
30 | </tr> |
---|
31 | </table> |
---|
32 | <p>Select parsers may be used to identify a single parser from a given list |
---|
33 | of parsers, which successfully recognizes the current input sequence. Example:</p> |
---|
34 | <pre> rule<span class="special"><></span> rule_select <span class="special">=</span> |
---|
35 | select_p<span class="special"> |
---|
36 | (</span> |
---|
37 | parser_a<span class="special"> |
---|
38 | ,</span> parser_b<span class="special"> |
---|
39 | <span class="comment">/* ... */</span> |
---|
40 | ,</span> parser_n |
---|
41 | <span class="special">)</span><span class="special">;</span></pre> |
---|
42 | <p>The parsers (parser_a, parser_b etc.) are tried sequentially from left to right until a parser matches the current input sequence. |
---|
43 | If there is a matching parser found, the <tt>select_p</tt> parser returns |
---|
44 | the parser's position (zero based index). For instance, in the example above, <tt>1</tt> is returned if parser_b |
---|
45 | matches.</p> |
---|
46 | <p>There are two predefined parsers of the select parser family: <tt>select_p</tt> |
---|
47 | and <tt>select_fail_p</tt>. These parsers differ in the way the no match |
---|
48 | case is handled (when none of the parsers match the current input sequence). |
---|
49 | While the <tt>select_p</tt> parser will return <tt>-1</tt> |
---|
50 | if no matching parser is found, the <tt>select_fail_p</tt> parser will not match |
---|
51 | at all.</p> |
---|
52 | <p>The following sample shows how the select parser may be used very conveniently |
---|
53 | in conjunction with a <a href="switch_parser.html">switch parser</a>:</p> |
---|
54 | <pre> <span class="keyword">int</span> choice <span class="special">=</span> <span class="literal">-1</span><span class="special">;</span> |
---|
55 | rule<span class="special"><></span> rule_select <span class="special">=</span> |
---|
56 | 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> |
---|
57 | >> switch_p(var<span class="special">(</span>choice)) <span class="special"> |
---|
58 | [</span><br> case_p<span class="special"><</span><span class="literal">0</span><span class="special">>(</span>int_p<span class="special">),</span><br> case_p<span class="special"><</span><span class="literal">1</span><span class="special">>(</span>ch_p<span class="special">(</span><span class="literal">','</span><span class="special">)),</span><br> case_p<span class="special"><</span><span class="literal">2</span><span class="special">>(</span>str_p<span class="special">(</span><span class="string">"bcd"</span><span class="special">)),</span><br> default_p<br> <span class="special">]</span><br><span class="special"> ;</span></pre> |
---|
59 | <p>This example shows a rule, which matches:</p> |
---|
60 | <ul> |
---|
61 | <li><span class="literal"> 'a' </span>followed |
---|
62 | by an integer</li> |
---|
63 | <li><span class="literal">'b' </span>followed by a<span class="literal"> |
---|
64 | ','</span></li> |
---|
65 | <li><span class="literal">'c'</span> followed by <span class="string">"bcd"</span></li> |
---|
66 | <li>a single <span class="literal">'d'</span>. </li> |
---|
67 | </ul> |
---|
68 | <p>For other input sequences the |
---|
69 | give rule does not match at all.</p> |
---|
70 | <table width="80%" border="0" align="center"> |
---|
71 | <tr> |
---|
72 | <td class="note_box"><p><img src="theme/alert.gif" width="16" height="16"> <tt>BOOST_SPIRIT_SELECT_LIMIT</tt><br> |
---|
73 | <br> |
---|
74 | 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> |
---|
75 | <p class="style1"><span class="comment">// Define these before including anything else <br> |
---|
76 | </span><span class="style3">#define</span> PHOENIX_LIMIT 10<br> |
---|
77 | <span class="preprocessor">#define</span> BOOST_SPIRIT_SELECT_LIMIT 10 </p></td> |
---|
78 | </tr> |
---|
79 | </table> |
---|
80 | <br> |
---|
81 | <table border="0"> |
---|
82 | <tr> |
---|
83 | <td width="10"></td> |
---|
84 | <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td> |
---|
85 | <td width="30"><a href="the_lazy_parser.html"><img src="theme/l_arr.gif" border="0"></a></td> |
---|
86 | <td width="30"><a href="switch_parser.html"><img src="theme/r_arr.gif" border="0"></a></td> |
---|
87 | </tr> |
---|
88 | </table> |
---|
89 | <br> |
---|
90 | <hr size="1"> |
---|
91 | <p class="copyright">Copyright © 2003-2004 Hartmut Kaiser <br> |
---|
92 | <br> |
---|
93 | <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> |
---|
94 | <p> </p> |
---|
95 | </body> |
---|
96 | </html> |
---|