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">
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; }
16 <table width=
"100%" border=
"0" background=
"theme/bkd2.gif" cellspacing=
"2">
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>
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>
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">
37 parser_a
<span class=
"special">
38 ,
</span> parser_b
<span class=
"special">
39 <span class=
"comment">/* ... */
</span>
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
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
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>
61 <li><span class=
"literal"> 'a'
</span>followed
63 <li><span class=
"literal">'b'
</span>followed by a
<span class=
"literal">
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>
68 <p>For other input sequences the
69 give rule does not match at all.
</p>
70 <table width=
"80%" border=
"0" align=
"center">
72 <td class=
"note_box"><p><img src=
"theme/alert.gif" width=
"16" height=
"16"> <tt>BOOST_SPIRIT_SELECT_LIMIT
</tt><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>
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>
91 <p class=
"copyright">Copyright
© 2003-
2004 Hartmut Kaiser
<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>