1 | <html> |
---|
2 | <head> |
---|
3 | <title>Numerics</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 | </head> |
---|
7 | |
---|
8 | <body> |
---|
9 | <table width="100%" border="0" background="theme/bkd2.gif" cellspacing="2"> |
---|
10 | <tr> |
---|
11 | <td width="10"> |
---|
12 | </td> |
---|
13 | <td width="85%"> |
---|
14 | <font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>Numerics</b></font> |
---|
15 | </td> |
---|
16 | <td width="112"><a href="http://spirit.sf.net"><img src="theme/spirit.gif" width="112" height="48" align="right" border="0"></a></td> |
---|
17 | </tr> |
---|
18 | </table> |
---|
19 | <br> |
---|
20 | <table border="0"> |
---|
21 | <tr> |
---|
22 | <td width="10"></td> |
---|
23 | <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td> |
---|
24 | <td width="30"><a href="operators.html"><img src="theme/l_arr.gif" border="0"></a></td> |
---|
25 | <td width="30"><a href="rule.html"><img src="theme/r_arr.gif" border="0"></a></td> |
---|
26 | </tr> |
---|
27 | </table> |
---|
28 | <p>Similar to <tt>chlit</tt>, <tt>strlit</tt> etc. numeric parsers are also primitives. |
---|
29 | Numeric parsers are placed on |
---|
30 | |
---|
31 | |
---|
32 | a section of their own |
---|
33 | |
---|
34 | to give this important building |
---|
35 | block better focus. The framework includes a couple of predefined objects for |
---|
36 | parsing signed and unsigned integers and real numbers. These parsers are fully |
---|
37 | parametric. Most of the important aspects of numeric parsing can be finely adjusted |
---|
38 | to suit. This includes the radix base, the minimum and maximum number of allowable |
---|
39 | digits, the exponent, the fraction etc. Policies control the real number parsers' |
---|
40 | behavior. There are some predefined policies covering the most common real number |
---|
41 | formats but the user can supply her own when needed. </p> |
---|
42 | <h2>uint_parser</h2> |
---|
43 | <p>This class is the simplest among the members of the numerics package. The <tt>uint_parser</tt> |
---|
44 | can parse unsigned integers of arbitrary length and size. The <tt>uint_parser</tt> |
---|
45 | parser can be used to parse ordinary primitive C/C++ integers or even user defined |
---|
46 | scalars such as bigints (unlimited precision integers). Like most of the classes |
---|
47 | in Spirit, the <tt>uint_parser</tt> is a template class. Template parameters |
---|
48 | fine tune its behavior. The uint_parser is so flexible that the other numeric |
---|
49 | parsers are implemented using it as the backbone.</p> |
---|
50 | <pre><code><font color="#000000"><span class=identifier> </span><span class=keyword>template </span><span class=special>< |
---|
51 | </span><span class=keyword>typename </span><span class=identifier>T </span><span class=special>= </span><span class="keyword">unsigned</span><span class=special>, |
---|
52 | </span><span class=keyword>int </span><span class=identifier>Radix </span><span class=special>= </span><span class=number>10</span><span class=special>, |
---|
53 | </span><span class=keyword>unsigned </span><span class=identifier>MinDigits </span><span class=special>= </span><span class=number>1</span><span class=special>, |
---|
54 | </span><span class=keyword>int </span><span class=identifier>MaxDigits </span><span class=special>= -</span><span class=number>1</span><span class=special>> |
---|
55 | </span><span class=keyword>struct </span><span class=identifier>uint_parser </span><span class=special>{ </span><span class=comment>/*...*/ </span><span class=special>};</span></font></code></pre> |
---|
56 | <table width="90%" border="0" align="center"> |
---|
57 | <tr> |
---|
58 | <td colspan="2" class="table_title">uint_parser template parameters</td> |
---|
59 | </tr> |
---|
60 | <tr> |
---|
61 | <td class="table_cells" width="21%"><b>T</b></td> |
---|
62 | <td class="table_cells" width="79%">The numeric base type of the numeric parser. |
---|
63 | Defaults to <tt>unsigned</tt></td> |
---|
64 | </tr> |
---|
65 | <tr> |
---|
66 | <td class="table_cells" width="21%"><b>Radix</b></td> |
---|
67 | <td class="table_cells" width="79%">The radix base. This can be either 2: |
---|
68 | binary, 8: octal, 10: decimal and 16: hexadecimal. Defaults to 10; decimal</td> |
---|
69 | </tr> |
---|
70 | <tr> |
---|
71 | <td class="table_cells" width="21%"><b>MinDigits</b></td> |
---|
72 | <td class="table_cells" width="79%">The minimum number of digits allowable</td> |
---|
73 | </tr> |
---|
74 | <tr> |
---|
75 | <td class="table_cells" width="21%"><b>MaxDigits</b></td> |
---|
76 | <td class="table_cells" width="79%">The maximum number of digits allowable. |
---|
77 | If this is -1, then the maximum limit becomes unbounded</td> |
---|
78 | </tr> |
---|
79 | </table> |
---|
80 | <br> |
---|
81 | <table width="90%" border="0" align="center"> |
---|
82 | <tr> |
---|
83 | <td colspan="2" class="table_title">Predefined uint_parsers</td> |
---|
84 | </tr> |
---|
85 | <tr> |
---|
86 | <td class="table_cells" width="21%"><b>bin_p</b></td> |
---|
87 | <td class="table_cells" width="79%"><code><span class=identifier>uint_parser</span><span class=special><</span><span class=keyword>unsigned</span><span class=special>, |
---|
88 | </span><span class=number>2</span><span class=special>, </span><span class=number>1</span><span class=special>, |
---|
89 | -</span><span class=number>1</span><span class=special>> </span><span class=keyword>const</span></code></td> |
---|
90 | </tr> |
---|
91 | <tr> |
---|
92 | <td class="table_cells" width="21%"><b>oct_p</b></td> |
---|
93 | <td class="table_cells" width="79%"><code><span class=identifier>uint_parser</span><span class=special><</span><span class=keyword>unsigned</span><span class=special>, |
---|
94 | </span><span class=number>8</span><span class=special>, </span><span class=number>1</span><span class=special>, |
---|
95 | -</span><span class=number>1</span><span class=special>> </span><span class=keyword>const</span></code></td> |
---|
96 | </tr> |
---|
97 | <tr> |
---|
98 | <td class="table_cells" width="21%"><b>uint_p</b></td> |
---|
99 | <td class="table_cells" width="79%"><code><span class=identifier>uint_parser</span><span class=special><</span><span class=keyword>unsigned</span><span class=special>, |
---|
100 | </span><span class=number>10</span><span class=special>, </span><span class=number>1</span><span class=special>, |
---|
101 | -</span><span class=number>1</span><span class=special>> </span><span class=keyword>const</span></code></td> |
---|
102 | </tr> |
---|
103 | <tr> |
---|
104 | <td class="table_cells" width="21%"><b>hex_p</b></td> |
---|
105 | <td class="table_cells" width="79%"><code><span class=identifier>uint_parser</span><span class=special><</span><span class=keyword>unsigned</span><span class=special>, |
---|
106 | </span><span class=number>16</span><span class=special>, </span><span class=number>1</span><span class=special>, |
---|
107 | -</span><span class=number>1</span><span class=special>> </span><span class=keyword>const</span></code></td> |
---|
108 | </tr> |
---|
109 | </table> |
---|
110 | <p>The following example shows how the uint_parser can be used to parse thousand |
---|
111 | separated numbers. The example can correctly parse numbers such as 1,234,567,890.</p> |
---|
112 | <pre><span class=keyword> </span><span class=identifier>uint_parser</span><span class=special><</span><span class=keyword>unsigned</span><span class=special>, </span><span class=number>10</span><span class=special>, </span><span class=number>1</span><span class=special>, </span><span class=number>3</span><span class=special>> </span><span class=identifier>uint3_p</span><span class=special>; </span><span class=comment>// 1..3 digits |
---|
113 | </span><span class=identifier>uint_parser</span><span class=special><</span><span class=keyword>unsigned</span><span class=special>, </span><span class=number>10</span><span class=special>, </span><span class=number>3</span><span class=special>, </span><span class=number>3</span><span class=special>> </span><span class=identifier>uint3_3_p</span><span class=special>; </span><span class=comment>// exactly 3 digits |
---|
114 | </span><span class=identifier>ts_num_p </span><span class=special>= </span><span class=special>(</span><span class=identifier>uint3_p </span><span class=special>>> </span><span class=special>*(</span><span class=literal>',' </span><span class=special>>> </span><span class=identifier>uint3_3_p</span><span class=special>)); </span><span class=comment>// our thousand separated number parser</span></pre> |
---|
115 | <p><tt>bin_p</tt>, <tt>oct_p</tt>, <tt>uint_p</tt> and <tt>hex_p</tt> are parser |
---|
116 | generator objects designed to be used within expressions. Here's an example |
---|
117 | of a rule that parses comma delimited list of numbers (We've seen this <a href="quick_start.html#list_of_numbers">before</a>):</p> |
---|
118 | <pre><code><span class=identifier> </span><span class=identifier>list_of_numbers </span><span class=special>=</span> <span class=identifier>real_p </span><span class=special>>> *(</span><span class=literal>','</span> <span class=special>>> </span><span class=identifier>real_p</span><span class=special>)</span></code>; |
---|
119 | </pre> |
---|
120 | <p></p> |
---|
121 | <p>Later, we shall see how we can extract the actual numbers parsed by the numeric |
---|
122 | parsers. We shall deal with this when we get to the section on <a href="semantic_actions.html#specialized_actions">specialized |
---|
123 | actions</a>.</p> |
---|
124 | <h2>int_parser</h2> |
---|
125 | <p>The <tt>int_parser</tt> can parse signed integers of arbitrary length and size. |
---|
126 | This is almost the same as the <tt>uint_parser</tt>. The only difference is |
---|
127 | the additional task of parsing the <tt><span class="quotes">'+'</span></tt> |
---|
128 | or <tt class="quotes">'-'</tt> sign preceding the number. The class interface |
---|
129 | is the same as that of the uint_parser.<br> |
---|
130 | </p> |
---|
131 | <table width="90%" border="0" align="center"> |
---|
132 | <tr> |
---|
133 | <td colspan="2" class="table_title">A predefined int_parser</td> |
---|
134 | </tr> |
---|
135 | <tr> |
---|
136 | <td class="table_cells" width="21%"><b>int_p</b></td> |
---|
137 | <td class="table_cells" width="79%"><span class=identifier><code>int_parser</code></span><code><span class=special><</span><span class=keyword>int</span><span class=special>, |
---|
138 | </span><span class=number>10</span><span class=special>, </span><span class=number>1</span><span class=special>, |
---|
139 | -</span><span class=number>1</span><span class=special>> </span><span class=keyword>const</span></code></td> |
---|
140 | </tr> |
---|
141 | </table> |
---|
142 | <h2>real_parser</h2> |
---|
143 | <p>The <tt>real_parser</tt> can parse real numbers of arbitrary length and size |
---|
144 | limited by its parametric type <tt>T</tt>. The <tt>real_parser</tt> is a template |
---|
145 | class with 2 template parameters. Here's the <tt>real_parser</tt> template interface:</p> |
---|
146 | <pre><span class=keyword> template</span><span class=special>< |
---|
147 | </span><span class=keyword>typename </span><span class=identifier>T </span><span class=special>= </span><span class=keyword>double</span><span class=special>, |
---|
148 | </span><span class=keyword>typename </span><span class=identifier>RealPoliciesT </span><span class=special>= </span><span class=identifier>ureal_parser_policies</span><span class=special><</span><span class=identifier>T</span><span class=special>> </span><span class=special>> |
---|
149 | </span><span class=keyword> struct </span><span class=identifier>real_parser</span><span class=special>;</span></pre> |
---|
150 | <p>The first template parameter is its numeric base type <tt>T</tt>. This defaults |
---|
151 | to <tt>double</tt>. </p> |
---|
152 | <table width="80%" border="0" align="center"> |
---|
153 | <tr> |
---|
154 | <td class="note_box"><img src="theme/bulb.gif" width="13" height="18"><b> |
---|
155 | Parsing special numeric types</b><br> |
---|
156 | <br> |
---|
157 | Notice that the numeric base type <tt>T</tt> can be specified by the user. |
---|
158 | This means that we can use the numeric parsers to parse user defined numeric |
---|
159 | types such as <tt>fixed_point</tt> (fixed point reals) and <tt>bigint</tt> |
---|
160 | (unlimited precision integers).</td> |
---|
161 | </tr> |
---|
162 | </table> |
---|
163 | <p>The second template parameter is a class that groups all the policies and defaults |
---|
164 | to <tt>ureal_parser_policies<T></tt>. Policies control the real number |
---|
165 | parsers' behavior. The default policies provided are designed to parse C/C++ |
---|
166 | style real numbers of the form <b>nnn.fff.Eeee</b> where <b>nnn</b> is the whole |
---|
167 | number part, <b>fff</b> is the fractional part, <b>E</b> is <tt class="quotes">'e'</tt> |
---|
168 | or <tt class="quotes">'E'</tt> and <b>eee</b> is the exponent optionally preceded |
---|
169 | by <tt class="quotes">'-'</tt> or <tt><span class="quotes">'+'</span></tt>. |
---|
170 | This corresponds to the following grammar, with the exception that plain integers |
---|
171 | without the decimal point are also accepted by default.</p> |
---|
172 | <pre><code><font color="#000000"><span class=keyword> </span><span class=identifier>floatingliteral |
---|
173 | </span><span class=special>= </span><span class=identifier>fractionalconstant </span><span class=special>>> </span><span class=special>!</span><span class=identifier>exponentpart |
---|
174 | </span><span class=special>| </span><span class=special>+</span><span class=identifier>digit_p </span><span class=special>>> </span><span class=identifier>exponentpart |
---|
175 | </span><span class=special>; |
---|
176 | |
---|
177 | </span><span class=identifier>fractionalconstant |
---|
178 | </span><span class=special>= </span><span class=special>*</span><span class=identifier>digit_p </span><span class=special>>> </span><span class=literal>'.' </span><span class=special>>> </span><span class=special>+</span><span class=identifier>digit_p |
---|
179 | </span><span class=special>| </span><span class=special>+</span><span class=identifier>digit_p </span><span class=special>>> </span><span class=literal>'.' |
---|
180 | </span><span class=special>; |
---|
181 | |
---|
182 | </span><span class=identifier>exponentpart |
---|
183 | </span><span class=special>= </span><span class=special>(</span><span class=literal>'e' </span><span class=special>| </span><span class=literal>'E'</span><span class=special>) </span><span class=special>>> </span><span class=special>!(</span><span class=literal>'+' </span><span class=special>| </span><span class=literal>'-'</span><span class=special>) </span><span class=special>>> </span><span class=special>+</span><span class=identifier>digit_p |
---|
184 | </span><span class=special>;</span></font></code></pre> |
---|
185 | <p>The default policies are provided to take care of the most common case (there |
---|
186 | are many ways to represent, and hence parse, real numbers). In most cases, the |
---|
187 | default setting of the <tt>real_parser</tt> is sufficient and can be used straight |
---|
188 | out of the box. Actually, there are four <tt>real_parser</tt>s pre-defined for |
---|
189 | immediate use:</p> |
---|
190 | <table width="90%" border="0" align="center"> |
---|
191 | <tr> |
---|
192 | <td colspan="2" class="table_title">Predefined real_parsers</td> |
---|
193 | </tr> |
---|
194 | <tr> |
---|
195 | <td class="table_cells" width="21%"><b>ureal_p</b></td> |
---|
196 | <td class="table_cells" width="79%"><span class=identifier><code>real_parser</code></span><code><span class=special><</span><span class=keyword>double</span><span class=special>, |
---|
197 | </span><span class=identifier>ureal_parser_policies</span><span class=special><</span><span class=keyword>double</span><span class=special>> |
---|
198 | > </span><span class=keyword>const</span></code></td> |
---|
199 | </tr> |
---|
200 | <tr> |
---|
201 | <td class="table_cells" width="21%"><b>real_p</b></td> |
---|
202 | <td class="table_cells" width="79%"><span class=identifier><code>real_parser</code></span><code><span class=special><</span><span class=keyword>double</span><span class=special>, |
---|
203 | </span><span class=identifier>real_parser_policies</span><span class=special><</span><span class=keyword>double</span><span class=special>> |
---|
204 | > </span><span class=keyword>const</span></code></td> |
---|
205 | </tr> |
---|
206 | <tr> |
---|
207 | <td class="table_cells" width="21%"><b>strict_ureal_p</b></td> |
---|
208 | <td class="table_cells" width="79%"><span class=identifier><code>real_parser</code></span><code><span class=special><</span><span class=keyword>double</span><span class=special>, |
---|
209 | </span><span class=identifier>strict_ureal_parser_policies</span><span class=special><</span><span class=keyword>double</span><span class=special>> |
---|
210 | > </span><span class=keyword>const</span></code></td> |
---|
211 | </tr> |
---|
212 | <tr> |
---|
213 | <td class="table_cells" width="21%"><b>strict_real_p</b></td> |
---|
214 | <td class="table_cells" width="79%"><span class=identifier><code>real_parser</code></span><code><span class=special><</span><span class=keyword>double</span><span class=special>, |
---|
215 | </span><span class=identifier>strict_real_parser_policies</span><span class=special><</span><span class=keyword>double</span><span class=special>> |
---|
216 | > </span><span class=keyword>const</span></code></td> |
---|
217 | </tr> |
---|
218 | </table> |
---|
219 | <p>We've seen <tt>real_p</tt> before. <tt>ureal_p</tt> is its unsigned variant.</p> |
---|
220 | <h3><a name="strict_reals"></a>Strict Reals </h3> |
---|
221 | <p>Integer numbers are considered a subset of real numbers, so <tt>real_p</tt> |
---|
222 | and <tt>ureal_p</tt> recognize integer numbers (without a dot) as real numbers. |
---|
223 | <tt>strict_real_p</tt> and <tt>strict_ureal_p</tt> are the equivalent parsers |
---|
224 | that <strong>require</strong> a dot to be present for a number to be considered |
---|
225 | a successful match.</p> |
---|
226 | <h2>Advanced: real_parser policies</h2> |
---|
227 | <p>The parser policies break down real number parsing into 6 steps:</p> |
---|
228 | <table width="90%" border="0" align="center"> |
---|
229 | <tr> |
---|
230 | <td class="table_cells">1</td> |
---|
231 | <td class="table_cells"><b>parse_sign</b></td> |
---|
232 | <td class="table_cells">Parse the prefix sign</td> |
---|
233 | </tr> |
---|
234 | <tr> |
---|
235 | <td class="table_cells">2</td> |
---|
236 | <td class="table_cells"><b>parse_n</b></td> |
---|
237 | <td class="table_cells">Parse the integer at the left of the decimal point</td> |
---|
238 | </tr> |
---|
239 | <tr> |
---|
240 | <td class="table_cells">3</td> |
---|
241 | <td class="table_cells"><b>parse_dot</b></td> |
---|
242 | <td class="table_cells">Parse the decimal point</td> |
---|
243 | </tr> |
---|
244 | <tr> |
---|
245 | <td class="table_cells">4</td> |
---|
246 | <td class="table_cells"><b>parse_frac_n</b></td> |
---|
247 | <td class="table_cells">Parse the fraction after the decimal point</td> |
---|
248 | </tr> |
---|
249 | <tr> |
---|
250 | <td class="table_cells">5</td> |
---|
251 | <td class="table_cells"><b>parse_exp</b></td> |
---|
252 | <td class="table_cells">Parse the exponent prefix (e.g. 'e')</td> |
---|
253 | </tr> |
---|
254 | <tr> |
---|
255 | <td class="table_cells">6</td> |
---|
256 | <td class="table_cells"><b>parse_exp_n</b></td> |
---|
257 | <td class="table_cells">Parse the actual exponent</td> |
---|
258 | </tr> |
---|
259 | </table> |
---|
260 | <p>And the interaction of these sub-parsing tasks is further controlled by these |
---|
261 | 3 policies:</p> |
---|
262 | <table width="90%" border="0" align="center"> |
---|
263 | <tr> |
---|
264 | <td class="table_cells">1</td> |
---|
265 | <td class="table_cells"><b>allow_leading_dot</b></td> |
---|
266 | <td class="table_cells">Allow a leading dot to be present (".1" becomes |
---|
267 | equivalent to "0.1")</td> |
---|
268 | </tr> |
---|
269 | <tr> |
---|
270 | <td class="table_cells">2</td> |
---|
271 | <td class="table_cells"><b>allow_trailing_dot</b></td> |
---|
272 | <td class="table_cells">Allow a trailing dot to be present ("1." becomes |
---|
273 | equivalent to "1.0")</td> |
---|
274 | </tr> |
---|
275 | <tr> |
---|
276 | <td class="table_cells">3</td> |
---|
277 | <td class="table_cells"><b>expect_dot</b></td> |
---|
278 | <td class="table_cells">Require a dot to be present (disallows "1" to |
---|
279 | be equivalent to "1.0")</td> |
---|
280 | </tr> |
---|
281 | </table> |
---|
282 | <p>[ <img src="theme/lens.gif" width="15" height="16"> From here on, required |
---|
283 | reading: <a href="scanner.html">The Scanner</a>, <a href="indepth_the_parser.html">In-depth |
---|
284 | The Parser</a> and <a href="indepth_the_scanner.html">In-depth The Scanner</a> |
---|
285 | ]</p> |
---|
286 | <h2>sign_parser and sign_p</h2> |
---|
287 | <p>Before we move on, a small utility parser is included here to ease the parsing |
---|
288 | of the <span class="quotes">'-'</span> or <span class="quotes">'+'</span> sign. |
---|
289 | While it is easy to write one:</p> |
---|
290 | <pre> <span class=identifier>sign_p </span><span class=special>= </span><span class=special>(</span><span class=identifier>ch_p</span><span class=special>(</span><span class=literal>'+'</span>) <span class=special>| </span><span class=literal>'-'</span><span class="special">)</span><span class=literal>;</span></pre> |
---|
291 | <p>it is not possible to extract the actual sign (positive or negative) without |
---|
292 | resorting to semantic actions. The sign_p parser has a bool attribute returned |
---|
293 | to the caller through the match object which, after parsing, is set to <strong>true</strong> |
---|
294 | if the parsed sign is negative. This attribute detects if the negative sign |
---|
295 | has been parsed. Examples:</p> |
---|
296 | <pre><span class=special> </span><span class=keyword>bool </span><span class=identifier>is_negative</span><span class=special>; |
---|
297 | </span><span class=identifier>r </span><span class=special>= </span><span class=identifier>sign_p</span><span class=special>[</span><span class=identifier>assign_a</span><span class=special>(</span><span class=identifier>is_negative</span><span class=special>)]</span><span class=special>;</span></pre> |
---|
298 | <p><span class=special></span>or simply...</p> |
---|
299 | <pre> <span class=comment>// directly extract the result from the match result's value</span> |
---|
300 | <span class=keyword>bool </span><span class=identifier>is_negative </span><span class=special>= </span><span class=identifier>sign_p</span><span class=special>.</span><span class=identifier>parse</span><span class=special>(</span><span class=identifier>scan</span><span class=special>).</span><span class=identifier>value</span><span class=special>();</span><span class=comment> </span></pre> |
---|
301 | <p>The sign_p parser expects attached semantic actions to have a signature (see |
---|
302 | <a href="semantic_actions.html#specialized_actions">Specialized Actions</a> |
---|
303 | for further detail) compatible with: </p> |
---|
304 | <p><b>Signature for functions:</b></p> |
---|
305 | <pre><code><font color="#000000"><span class=identifier> </span><span class=keyword>void </span><span class=identifier>func</span><span class=special>(</span><span class="keyword">bool</span><span class=identifier> is_negative</span><span class=special>);</span></font></code></pre> |
---|
306 | <p><b>Signature for functors:</b> </p> |
---|
307 | <pre><code><font color="#000000"><span class=special> </span><span class=keyword>struct </span><span class=identifier>ftor |
---|
308 | </span><span class=special>{ |
---|
309 | </span><span class=keyword>void </span><span class=keyword>operator</span><span class=special>()(</span><span class="keyword">bool</span><span class=identifier> is_negative</span><span class=special>) </span><span class=keyword>const</span><span class=special>; |
---|
310 | </span><span class=special>};</span></font></code></pre> |
---|
311 | <h2><span class=identifier>ureal_parser_policies</span></h2> |
---|
312 | <pre><span class=comment> </span><span class=keyword>template </span><span class=special><</span><span class=keyword>typename </span><span class=identifier>T</span><span class=special>> |
---|
313 | </span><span class=keyword>struct </span><span class=identifier>ureal_parser_policies |
---|
314 | </span><span class=special>{ |
---|
315 | </span><span class=keyword>typedef </span><span class=identifier>uint_parser</span><span class=special><</span><span class=identifier>T</span><span class=special>, </span><span class=number>10</span><span class=special>, </span><span class=number>1</span><span class=special>, -</span><span class=number>1</span><span class=special>> </span><span class=identifier>uint_parser_t</span><span class=special>; |
---|
316 | </span><span class=keyword>typedef </span><span class=identifier>int_parser</span><span class=special><</span><span class=identifier>T</span><span class=special>, </span><span class=number>10</span><span class=special>, </span><span class=number>1</span><span class=special>, -</span><span class=number>1</span><span class=special>> </span><span class=identifier>int_parser_t</span><span class=special>; |
---|
317 | |
---|
318 | </span><span class=keyword>static const bool</span> <span class=identifier>allow_leading_dot</span> <span class=special> =</span> <span class=literal>true</span><span class=special>;</span><span class=special> |
---|
319 | </span><span class=keyword>static const bool</span> <span class=identifier>allow_trailing_dot </span><span class=special>=</span> <span class=literal>true</span><span class=special>;</span><span class=special></span> |
---|
320 | <span class=special> </span><span class=keyword>static const bool</span> <span class=identifier>expect_dot</span> <span class=special> =</span> <span class=literal>false</span><span class=special>;</span><span class=special></span><span class=special><br> |
---|
321 | </span><span class=keyword>template </span><span class=special><</span><span class=keyword>typename </span><span class=identifier>ScannerT</span><span class=special>> |
---|
322 | </span><span class=keyword>static typename </span><span class=identifier>match_result</span><span class=special><</span><span class=identifier>ScannerT</span><span class=special>, </span><span class="identifier">nil_t</span><span class=special>>::</span><span class=identifier>type |
---|
323 | parse_sign</span><span class=special>(</span><span class=identifier>ScannerT</span><span class=special>& </span><span class=identifier>scan</span><span class=special>) |
---|
324 | { </span><span class=keyword>return </span><span class=identifier>scan</span><span class=special>.</span><span class=identifier>no_match</span><span class=special>(); } |
---|
325 | |
---|
326 | </span><span class=keyword>template </span><span class=special><</span><span class=keyword>typename </span><span class=identifier>ScannerT</span><span class=special>> |
---|
327 | </span><span class=keyword>static typename </span><span class=identifier>parser_result</span><span class=special><</span><span class=identifier>uint_parser_t</span><span class=special>, </span><span class=identifier>ScannerT</span><span class=special>>::</span><span class=identifier>type |
---|
328 | parse_n</span><span class=special>(</span><span class=identifier>ScannerT</span><span class=special>& </span><span class=identifier>scan</span><span class=special>) |
---|
329 | { </span><span class=keyword>return </span><span class=identifier>uint_parser_t</span><span class=special>().</span><span class=identifier>parse</span><span class=special>(</span><span class=identifier>scan</span><span class=special>); } |
---|
330 | |
---|
331 | </span><span class=keyword>template </span><span class=special><</span><span class=keyword>typename </span><span class=identifier>ScannerT</span><span class=special>> |
---|
332 | </span><span class=keyword>static typename </span><span class=identifier>parser_result</span><span class=special><</span><span class=identifier>chlit</span><span class=special><>, </span><span class=identifier>ScannerT</span><span class=special>>::</span><span class=identifier>type |
---|
333 | parse_dot</span><span class=special>(</span><span class=identifier>ScannerT</span><span class=special>& </span><span class=identifier>scan</span><span class=special>) |
---|
334 | { </span><span class=keyword>return </span><span class=identifier>ch_p</span><span class=special>(</span><span class=literal>'.'</span><span class=special>).</span><span class=identifier>parse</span><span class=special>(</span><span class=identifier>scan</span><span class=special>); } |
---|
335 | |
---|
336 | </span><span class=keyword>template </span><span class=special><</span><span class=keyword>typename </span><span class=identifier>ScannerT</span><span class=special>> |
---|
337 | </span><span class=keyword>static typename </span><span class=identifier>parser_result</span><span class=special><</span><span class=identifier>uint_parser_t</span><span class=special>, </span><span class=identifier>ScannerT</span><span class=special>>::</span><span class=identifier>type |
---|
338 | parse_frac_n</span><span class=special>(</span><span class=identifier>ScannerT</span><span class=special>& </span><span class=identifier>scan</span><span class=special>) |
---|
339 | { </span><span class=keyword>return </span><span class=identifier>uint_parser_t</span><span class=special>().</span><span class=identifier>parse</span><span class=special>(</span><span class=identifier>scan</span><span class=special>); } |
---|
340 | |
---|
341 | </span><span class=keyword>template </span><span class=special><</span><span class=keyword>typename </span><span class=identifier>ScannerT</span><span class=special>> |
---|
342 | </span><span class=keyword>static typename </span><span class=identifier>parser_result</span><span class=special><</span><span class=identifier>chlit</span><span class=special><>, </span><span class=identifier>ScannerT</span><span class=special>>::</span><span class=identifier>type |
---|
343 | parse_exp</span><span class=special>(</span><span class=identifier>ScannerT</span><span class=special>& </span><span class=identifier>scan</span><span class=special>) |
---|
344 | { </span><span class=keyword>return </span><span class=identifier>as_lower_d</span><span class=special>[</span><span class=literal>'e'</span><span class=special>].</span><span class=identifier>parse</span><span class=special>(</span><span class=identifier>scan</span><span class=special>); } |
---|
345 | |
---|
346 | </span><span class=keyword>template </span><span class=special><</span><span class=keyword>typename </span><span class=identifier>ScannerT</span><span class=special>> |
---|
347 | </span><span class=keyword>static typename </span><span class=identifier>parser_result</span><span class=special><</span><span class=keyword>int</span><span class=identifier>_parser_t</span><span class=special>, </span><span class=identifier>ScannerT</span><span class=special>>::</span><span class=identifier>type |
---|
348 | parse_exp_n</span><span class=special>(</span><span class=identifier>ScannerT</span><span class=special>& </span><span class=identifier>scan</span><span class=special>) |
---|
349 | { </span><span class=keyword>return int</span><span class=identifier>_parser_t</span><span class=special>().</span><span class=identifier>parse</span><span class=special>(</span><span class=identifier>scan</span><span class=special>); } |
---|
350 | }; |
---|
351 | </span></pre> |
---|
352 | <p><span class=special></span><span class=identifier>The default ureal_parser_policies |
---|
353 | uses the lower level integer numeric parsers to do its job. </span></p> |
---|
354 | <h2><span class=identifier>real_parser_policies</span></h2> |
---|
355 | <pre> <span class=keyword>template </span><span class=special><</span><span class=keyword>typename </span><span class=identifier>T</span><span class=special>> |
---|
356 | </span><span class=keyword>struct </span><span class=identifier>real_parser_policies </span><span class=special>: </span><span class=keyword>public </span><span class=identifier>ureal_parser_policies</span><span class=special><</span><span class=identifier>T</span><span class=special>> |
---|
357 | </span><span class=special>{ |
---|
358 | </span><span class=keyword>template </span><span class=special><</span><span class=keyword>typename </span><span class=identifier>ScannerT</span><span class=special>> |
---|
359 | </span><span class=keyword>static </span><span class=keyword>typename </span><span class=identifier>parser_result</span><span class=special><</span><span class=identifier>sign_parser</span><span class=special>, </span><span class=identifier>ScannerT</span><span class=special>>::</span><span class=identifier>type |
---|
360 | </span><span class=identifier>parse_sign</span><span class=special>(</span><span class=identifier>ScannerT</span><span class=special>& </span><span class=identifier>scan</span><span class=special>) |
---|
361 | </span><span class=special>{ </span><span class=keyword>return </span><span class=identifier>sign_p</span><span class=special>.</span><span class=identifier>parse</span><span class=special>(</span><span class=identifier>scan</span><span class=special>); </span><span class=special>} |
---|
362 | </span><span class=special>};</span></pre> |
---|
363 | <p>Notice how the real_parser_policies replaced <b><tt>parse_sign</tt></b> of |
---|
364 | the <b>u</b>real_parser_policies from which it is subclassed. The default real_parser_policies |
---|
365 | simply uses a <tt>sign_p</tt> instead of <tt>scan.no_match()</tt> in the <tt>parse_sign |
---|
366 | </tt> step. </p> |
---|
367 | <h2><span class=identifier>strict_ureal_parser_policies and strict_real_parser_policies</span></h2> |
---|
368 | <pre> <span class=keyword>template </span><span class=special><</span><span class=keyword>typename </span><span class=identifier>T</span><span class=special>> |
---|
369 | </span><span class=keyword>struct </span><span class=identifier>strict_ureal_parser_policies </span><span class=special>: </span><span class=keyword>public </span><span class=identifier>ureal_parser_policies</span><span class=special><</span><span class=identifier>T</span><span class=special>> |
---|
370 | </span><span class=special>{</span> |
---|
371 | <span class=special> </span><span class=keyword>static const bool</span> <span class=identifier>expect_dot</span><span class=special> =</span> <span class=literal>true</span><span class=special>;</span><span class=special></span><span class=special></span> |
---|
372 | <span class=special>};</span> |
---|
373 | |
---|
374 | <span class=keyword>template </span><span class=special><</span><span class=keyword>typename </span><span class=identifier>T</span><span class=special>> |
---|
375 | </span><span class=keyword>struct </span><span class=identifier>strict_real_parser_policies </span><span class=special>: </span><span class=keyword>public </span><span class=identifier>real_parser_policies</span><span class=special><</span><span class=identifier>T</span><span class=special>> |
---|
376 | </span><span class=special>{</span> |
---|
377 | <span class=special> </span><span class=keyword>static const bool</span> <span class=identifier>expect_dot</span><span class=special> =</span> <span class=literal>true</span><span class=special>;</span><span class=special></span><span class=special></span> |
---|
378 | <span class=special>};</span></pre> |
---|
379 | <p>Again, these policies replaced just the policies they wanted different from |
---|
380 | their superclasses.</p> |
---|
381 | <p><i>Specialized</i> real parser policies can reuse some of the defaults while |
---|
382 | replacing a few. For example, the following is a real number parser policy that |
---|
383 | parses thousands separated numbers with at most two decimal places and no exponent. |
---|
384 | </p> |
---|
385 | <p><img src="theme/lens.gif" width="15" height="16">The full source code can be |
---|
386 | viewed <a href="../example/fundamental/thousand_separated.cpp">here</a>. </p> |
---|
387 | <pre> |
---|
388 | <span class=identifier> </span><span class=keyword>template </span><span class=special><</span><span class=keyword>typename </span><span class=identifier>T</span><span class=special>> |
---|
389 | </span><span class=keyword>struct </span><span class=identifier>ts_real_parser_policies </span><span class=special>: </span><span class=keyword>public </span><span class=identifier>ureal_parser_policies</span><span class=special><</span><span class=identifier>T</span><span class=special>> |
---|
390 | { |
---|
391 | </span><span class=comment>// These policies can be used to parse thousand separated |
---|
392 | // numbers with at most 2 decimal digits after the decimal |
---|
393 | // point. e.g. 123,456,789.01 |
---|
394 | |
---|
395 | </span><span class=keyword>typedef </span><span class=identifier>uint_parser</span><span class=special><</span><span class=keyword>int</span><span class=special>, </span><span class=number>10</span><span class=special>, </span><span class=number>1</span><span class=special>, </span><span class=number>2</span><span class=special>> </span><span class=identifier>uint2_t</span><span class=special>; |
---|
396 | </span><span class=keyword>typedef </span><span class=identifier>uint_parser</span><span class=special><</span><span class=identifier>T</span><span class=special>, </span><span class=number>10</span><span class=special>, </span><span class=number>1</span><span class=special>, -</span><span class=number>1</span><span class=special>> </span><span class=identifier>uint_parser_t</span><span class=special>; |
---|
397 | </span><span class=keyword>typedef </span><span class=identifier>int_parser</span><span class=special><</span><span class=keyword>int</span><span class=special>, </span><span class=number>10</span><span class=special>, </span><span class=number>1</span><span class=special>, -</span><span class=number>1</span><span class=special>> </span><span class=identifier>int_parser_t</span><span class=special>; |
---|
398 | |
---|
399 | </span><span class=comment>////////////////////////////////// 2 decimal places Max |
---|
400 | </span><span class=keyword>template </span><span class=special><</span><span class=keyword>typename </span><span class=identifier>ScannerT</span><span class=special>> |
---|
401 | </span><span class=keyword>static typename </span><span class=identifier>parser_result</span><span class=special><</span><span class=identifier>uint2_t</span><span class=special>, </span><span class=identifier>ScannerT</span><span class=special>>::</span><span class=identifier>type |
---|
402 | parse_frac_n</span><span class=special>(</span><span class=identifier>ScannerT</span><span class=special>& </span><span class=identifier>scan</span><span class=special>) |
---|
403 | { </span><span class=keyword>return </span><span class=identifier>uint2_t</span><span class=special>().</span><span class=identifier>parse</span><span class=special>(</span><span class=identifier>scan</span><span class=special>); } |
---|
404 | |
---|
405 | </span><span class=special> </span><span class=comment>////////////////////////////////// No exponent<br></span> <span class=keyword>template </span><span class=special><</span><span class=keyword>typename </span><span class=identifier>ScannerT</span><span class=special>> |
---|
406 | </span><span class=keyword>static typename </span><span class=identifier>parser_result</span><span class=special><</span><span class=identifier>chlit</span><span class=special><>, </span><span class=identifier>ScannerT</span><span class=special>>::</span><span class=identifier>type |
---|
407 | parse_exp</span><span class=special>(</span><span class=identifier>ScannerT</span><span class=special>& </span><span class=identifier>scan</span><span class=special>) |
---|
408 | { </span><span class=keyword>return </span><span class=identifier>scan</span><span class=special>.</span><span class=identifier>no_match</span><span class=special>(); } |
---|
409 | |
---|
410 | </span><span class=special> </span><span class=comment>////////////////////////////////// No exponent<br></span> <span class=keyword>template </span><span class=special><</span><span class=keyword>typename </span><span class=identifier>ScannerT</span><span class=special>> |
---|
411 | </span><span class=keyword>static typename </span><span class=identifier>parser_result</span><span class=special><</span><span class=identifier>int_parser_t</span><span class=special>, </span><span class=identifier>ScannerT</span><span class=special>>::</span><span class=identifier>type |
---|
412 | parse_exp_n</span><span class=special>(</span><span class=identifier>ScannerT</span><span class=special>& </span><span class=identifier>scan</span><span class=special>) |
---|
413 | { </span><span class=keyword>return </span><span class=identifier>scan</span><span class=special>.</span><span class=identifier>no_match</span><span class=special>(); } |
---|
414 | |
---|
415 | </span><span class=comment>////////////////////////////////// Thousands separated numbers |
---|
416 | </span><span class=keyword>template </span><span class=special><</span><span class=keyword>typename </span><span class=identifier>ScannerT</span><span class=special>> |
---|
417 | </span><span class=keyword>static typename </span><span class=identifier>parser_result</span><span class=special><</span><span class=identifier>uint_parser_t</span><span class=special>, </span><span class=identifier>ScannerT</span><span class=special>>::</span><span class=identifier>type |
---|
418 | <a name="scanner_save"></a>parse_n</span><span class=special>(</span><span class=identifier>ScannerT</span><span class=special>& </span><span class=identifier>scan</span><span class=special>) |
---|
419 | { |
---|
420 | </span><span class=keyword>typedef typename </span><span class=identifier>parser_result</span><span class=special><</span><span class=identifier>uint_parser_t</span><span class=special>, </span><span class=identifier>ScannerT</span><span class=special>>::</span><span class=identifier>type RT</span><span class=special>; |
---|
421 | </span><span class="keyword">static </span><span class=identifier>uint_parser</span><span class=special><</span><span class=keyword>unsigned</span><span class=special>, </span><span class=number>10</span><span class=special>, </span><span class=number>1</span><span class=special>, </span><span class=number>3</span><span class=special>> </span><span class=identifier>uint3_p</span><span class=special>; |
---|
422 | </span><span class="keyword">static </span><span class=identifier>uint_parser</span><span class=special><</span><span class=keyword>unsigned</span><span class=special>, </span><span class=number>10</span><span class=special>, </span><span class=number>3</span><span class=special>, </span><span class=number>3</span><span class=special>> </span><span class=identifier>uint3_3_p</span><span class=special>; |
---|
423 | |
---|
424 | </span><span class=keyword>if </span><span class=special>(</span><span class=identifier>RT hit </span><span class=special>= </span><span class=identifier>uint3_p</span><span class=special>.</span><span class=identifier>parse</span><span class=special>(</span><span class=identifier>scan</span><span class=special>)) |
---|
425 | { |
---|
426 | </span><span class=identifier>T n</span><span class=special>; |
---|
427 | </span><span class="keyword">typedef typename </span>ScannerT<span class="special">::</span>iterator_t iterator_t;<span class=special> |
---|
428 | </span>iterator_t save<span class="special"> = </span>scan.first<span class="special">; |
---|
429 | </span><span class=keyword>while </span><span class=special>(</span><span class=identifier>match</span><span class=special><> </span><span class=identifier>next </span><span class=special>= (</span><span class=literal>',' </span><span class=special>>> </span><span class=identifier>uint3_3_p</span><span class=special>[</span><span class=identifier>assign_a</span><span class=special>(</span><span class=identifier>n</span><span class=special>)]).</span><span class=identifier>parse</span><span class=special>(</span><span class=identifier>scan</span><span class=special>)) |
---|
430 | { |
---|
431 | </span><span class=identifier>hit</span><span class=special>.</span><span class=identifier>value</span><span class=special>() *= </span><span class=number>1000</span><span class=special>; |
---|
432 | </span><span class=identifier>hit</span><span class=special>.</span><span class=identifier>value</span><span class=special>() += </span><span class=identifier>n</span><span class=special>; |
---|
433 | </span><span class=identifier>scan</span><span class=special>.</span><span class=identifier>concat_match</span><span class=special>(</span><span class=identifier>hit</span><span class=special>, </span><span class=identifier>next</span><span class=special>); |
---|
434 | </span><span class="identifier">save </span><span class=special><span class="special">= </span></span><span class="identifier">scan</span><span class="special">.</span><span class="identifier">first</span><span class=special><span class="special">;</span> |
---|
435 | } |
---|
436 | </span>scan<span class="special">.</span>first<span class="special"> = </span>save<span class="special">; |
---|
437 | </span><span class=keyword>return </span><span class=identifier>hit</span><span class=special>; |
---|
438 | |
---|
439 | </span><span class=special> // Note: On erroneous input such as "123,45", the result should<br> // be a partial match "123". 'save' is used to makes sure that<br> // the scanner position is placed at the last *valid* parse<br> // position.<br> } |
---|
440 | </span><span class=keyword>return </span><span class=identifier>scan</span><span class=special>.</span><span class=identifier>no_match</span><span class=special>(); |
---|
441 | } |
---|
442 | };</span></pre> |
---|
443 | <table border="0"> |
---|
444 | <tr> |
---|
445 | <td width="10"></td> |
---|
446 | <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td> |
---|
447 | <td width="30"><a href="operators.html"><img src="theme/l_arr.gif" border="0"></a></td> |
---|
448 | <td width="30"><a href="rule.html"><img src="theme/r_arr.gif" border="0"></a></td> |
---|
449 | </tr> |
---|
450 | </table> |
---|
451 | <br> |
---|
452 | <hr size="1"> |
---|
453 | <p class="copyright">Copyright © 1998-2002 Joel de Guzman<br> |
---|
454 | <br> |
---|
455 | <font size="2">Use, modification and distribution is subject to the Boost Software |
---|
456 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
457 | http://www.boost.org/LICENSE_1_0.txt) </font> </p> |
---|
458 | <p> </p> |
---|
459 | </body> |
---|
460 | </html> |
---|