]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
2 | <html><head> | |
3 | ||
4 | <title>Dynamic Parsers</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> | |
5 | <link rel="stylesheet" href="theme/style.css" type="text/css"></head> | |
6 | <body> | |
7 | <table background="theme/bkd2.gif" border="0" cellspacing="2" width="100%"> | |
8 | <tbody><tr> | |
9 | <td width="10"> | |
10 | </td> | |
11 | <td width="85%"> <font face="Verdana, Arial, Helvetica, sans-serif" size="6"><b>Dynamic | |
12 | Parsers </b></font></td> | |
13 | <td width="112"><a href="http://spirit.sf.net"><img src="theme/spirit.gif" align="right" border="0" height="48" width="112"></a></td> | |
14 | </tr> | |
15 | </tbody></table> | |
16 | <br> | |
17 | <table border="0"> | |
18 | <tbody><tr> | |
19 | <td width="10"></td> | |
20 | <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td> | |
21 | <td width="30"><a href="closures.html"><img src="theme/l_arr.gif" border="0"></a></td> | |
22 | <td width="30"><a href="stored_rule.html"><img src="theme/r_arr.gif" border="0"></a></td> | |
23 | </tr> | |
24 | </tbody></table> | |
25 | <p>We see dynamic parsing everywhere in Spirit. A special group of | |
26 | parsers, aptly named dynamic parsers, form the most basic building | |
27 | blocks to dynamic parsing. This chapter focuses on these critters. | |
28 | You'll notice the similarity of these parsers with C++'s control | |
29 | structures. The similarity is not a coincidence. These parsers give an | |
30 | imperative flavor to parsing, and, since imperative constructs are not | |
31 | native to declarative EBNF, mimicking the host language, C++, should | |
32 | make their use immediately familiar. </p> | |
33 | <p>Dynamic parsers modify the parsing behavior according to conditions. Constructing | |
34 | dynamic parsers requires a condition argument and a body parser argument. Additional | |
35 | arguments are required by some parsers.</p> | |
36 | <h2>Conditions</h2> | |
37 | <p>Functions or functors returning values convertable to bool can be used as conditions. | |
38 | When the evaluation of the function/functor yields true it will be considered | |
39 | as meeting the condition.</p> | |
40 | <p>Parsers can be used as conditions, as well. When the parser matches the condition | |
41 | is met. Parsers used as conditions work in an all-or-nothing manner: the scanner | |
42 | will not be advanced when they don't match.</p> | |
43 | <p>A failure to meet the condition will not result in a parse error.</p> | |
44 | <h2>if_p</h2> | |
45 | <p><tt>if_p</tt> can be used with or without an else-part. The syntax is:</p> | |
46 | <pre> <span class="identifier">if_p</span><span class="special">(</span><span class="identifier">condition</span><span class="special">)[</span><span class="identifier">then</span><span class="special">-</span><span class="identifier">parser</span><span class="special">]</span></pre> | |
47 | <p><span class="special"></span>or</p> | |
48 | <pre><span class="identifier"> if_p</span><span class="special">(</span><span class="identifier">condition</span><span class="special">)[</span><span class="identifier">then</span><span class="special">-</span><span class="identifier">parser</span><span class="special">].</span><span class="identifier">else_p</span><span class="special">[</span><span class="identifier">else</span><span class="special">-</span><span class="identifier">parser</span><span class="special">]</span></pre> | |
49 | <p>When the condition is met the then-parser is used next in the parsing process. | |
50 | When the condition is not met and an else-parser is available the else-parser | |
51 | is used next. When the condition isn't met and no else-parser is available then | |
52 | the whole parser matches the empty sequence. (<img src="theme/alert.gif" height="16" width="16"> | |
53 | Note: older versions of <tt>if_p</tt> report a failure when the condition isn't | |
54 | met and no else-parser is available.)</p> | |
55 | <p>Example:</p> | |
56 | <pre> <span class="special"></span><span class="identifier">if_p</span><span class="special">(</span><span class="string">"0x"</span><span class="special">)[</span><span class="identifier">hex_p</span><span class="special">].</span><span class="identifier">else_p</span><span class="special">[</span><span class="identifier">uint_p</span><span class="special">]</span></pre> | |
57 | <h2>while_p, do_p</h2> | |
58 | <p><tt>while_p</tt>/<tt>do_p</tt> syntax is:</p> | |
59 | <pre> <span class="identifier">while_p</span><span class="special">(</span><span class="identifier">condition</span><span class="special">)[</span><span class="identifier">body</span><span class="special">-</span><span class="identifier">parser</span><span class="special">]<br> </span><span class="identifier">do_p</span><span class="special">[</span><span class="identifier">body</span><span class="special">-</span><span class="identifier">parser</span><span class="special">].</span><span class="identifier">while_p</span><span class="special">(</span><span class="identifier">condition</span><span class="special">)</span></pre> | |
60 | <p>As long as the condition is met the dynamic parser constructed by <tt>while_p</tt> | |
61 | will try to match the body-parser. <tt>do_p</tt> returns a parser that tries | |
62 | to match the body-parser and then behaves just like the parser returned by <tt>while_p</tt>. | |
63 | A failure to match the body-parser will cause a failure to be reported by the | |
64 | while/do-parser.</p> | |
65 | <p>Example:</p> | |
66 | <pre><span class="special"> </span><span class="identifier">uint_p</span><span class="special">[</span><span class="identifier">assign_a</span><span class="special">(</span><span class="identifier">sum</span><span class="special">)] >> </span><span class="identifier">while_p</span><span class="special">(</span><span class="literal">'+'</span><span class="special">)[</span><span class="identifier">uint_p</span><span class="special">[</span><span class="identifier">add</span><span class="special">(</span><span class="identifier">sum</span><span class="special">)]]<br> </span><span class="literal">'"' </span><span class="special">>> </span><span class="identifier">while_p</span><span class="special">(~</span><span class="identifier">eps_p</span><span class="special">(</span><span class="literal">'"'</span><span class="special">))[</span><span class="identifier">c_escape_ch_p</span><span class="special">[</span><span class="identifier">push_back_a</span><span class="special">(</span><span class="identifier">result</span><span class="special">)]] >> </span><span class="literal">'"'</span> | |
67 | </pre> | |
68 | <p>Assuming <span style="font-family: monospace;">add</span> is a user defined function object.<br></p><h2>for_p</h2> | |
69 | <p><tt>for_p</tt> requires four arguments. The syntax is:</p> | |
70 | <pre> <span class="literal"></span><span class="identifier">for_p</span><span class="special">(</span><span class="identifier">init</span><span class="special">, </span><span class="identifier">condition</span><span class="special">, </span><span class="identifier">step</span><span class="special">)[</span><span class="identifier">body</span><span class="special">-</span><span class="identifier">parser</span><span class="special">]</span></pre> | |
71 | <p>init and step have to be 0-ary functions/functors. for_p returns a parser that | |
72 | will:</p> | |
73 | <ol> | |
74 | <li> call init</li> | |
75 | <li>check the condition, if the | |
76 | condition isn't met then a match is returned. The match will cover | |
77 | everything that has been matched successfully up to this point.</li> | |
78 | <li> tries to match the body-parser. A failure to match the body-parser will cause a failure to be reported by the for-parser</li> | |
79 | <li> calls step</li> | |
80 | <li> goes to 2.</li> | |
81 | </ol> | |
82 | <table border="0"> | |
83 | <tbody><tr> | |
84 | <td width="10"></td> | |
85 | <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td> | |
86 | <td width="30"><a href="closures.html"><img src="theme/l_arr.gif" border="0"></a></td> | |
87 | <td width="30"><a href="stored_rule.html"><img src="theme/r_arr.gif" border="0"></a></td> | |
88 | </tr> | |
89 | </tbody></table> | |
90 | <br> | |
91 | <hr size="1"> | |
92 |