directives.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <html>
  2. <head>
  3. <title>Directives</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. <body>
  8. <table width="100%" border="0" background="theme/bkd2.gif" cellspacing="2">
  9. <tr>
  10. <td width="10">
  11. </td>
  12. <td width="85%">
  13. <font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>Directives</b></font>
  14. </td>
  15. <td width="112"><a href="http://spirit.sf.net"><img src="theme/spirit.gif" width="112" height="48" align="right" border="0"></a></td>
  16. </tr>
  17. </table>
  18. <br>
  19. <table border="0">
  20. <tr>
  21. <td width="10"></td>
  22. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  23. <td width="30"><a href="epsilon.html"><img src="theme/l_arr.gif" border="0"></a></td>
  24. <td width="30"><a href="scanner.html"><img src="theme/r_arr.gif" border="0"></a></td>
  25. </tr>
  26. </table>
  27. <p>Parser directives have the form: <b>directive[expression]</b></p>
  28. <p>A directive modifies the behavior of its enclosed expression, essentially <em>decorating</em>
  29. it. The framework pre-defines a few directives. Clients of the framework are
  30. free to define their own directives as needed. Information on how this is done
  31. will be provided later. For now, we shall deal only with predefined directives.</p>
  32. <h2>lexeme_d</h2>
  33. <p>Turns off white space skipping. At the phrase level, the parser ignores white
  34. spaces, possibly including comments. Use <tt>lexeme_d</tt> in situations where
  35. we want to work at the character level instead of the phrase level. Parsers
  36. can be made to work at the character level by enclosing the pertinent parts
  37. inside the lexeme_d directive. For example, let us complete the example presented
  38. in the <a href="introduction.html">Introduction</a>. There, we skipped the definition
  39. of the <tt>integer</tt> rule. Here's how it is actually defined:</p>
  40. <pre><code><font color="#000000"><span class=identifier> </span><span class=identifier>integer </span><span class=special>= </span><span class=identifier>lexeme_d</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=special>| </span><span class=literal>'-'</span><span class=special>) </span><span class=special>&gt;&gt; </span><span class=special>+</span><span class=identifier>digit </span><span class=special>];</span></font></code></pre>
  41. <p>The <tt>lexeme_d</tt> directive instructs the parser to work on the character
  42. level. Without it, the <tt>integer</tt> rule would have allowed erroneous embedded
  43. white spaces in inputs such as <span class="quotes">&quot;1 2 345&quot;</span>
  44. which will be parsed as <span class="quotes">&quot;12345&quot;</span>.</p>
  45. <h2>as_lower_d</h2>
  46. <p>There are times when we want to inhibit case sensitivity. The <tt>as_lower_d</tt>
  47. directive converts all characters from the input to lower-case.</p>
  48. <table width="80%" border="0" align="center">
  49. <tr>
  50. <td class="note_box"><img src="theme/alert.gif" width="16" height="16"><b>
  51. as_lower_d behavior</b> <br>
  52. <br>
  53. It is important to note that only the input is converted to lower case.
  54. Parsers enclosed inside the <tt>as_lower_d</tt> expecting upper case characters
  55. will fail to parse. Example: <tt>as_lower_d[<span class="quotes">'X'</span>]</tt>
  56. will never succeed because it expects an upper case <tt class="quotes">'X'</tt>
  57. that the <tt>as_lower_d</tt> directive will never supply.</td>
  58. </tr>
  59. </table>
  60. <p>For example, in Pascal, keywords and identifiers are case insensitive. Pascal
  61. ignores the case of letters in identifiers and keywords. Identifiers Id, ID
  62. and id are indistinguishable in Pascal. Without the as_lower_d directive, it
  63. would be awkward to define a rule that recognizes this. Here's a possibility:</p>
  64. <pre><code><font color="#000000"><span class=special> </span><span class=identifier>r </span><span class=special>= </span><span class=identifier>str_p</span><span class=special>(</span><span class=string>"id"</span><span class=special>) </span><span class=special>| </span><span class=string>"Id" </span><span class=special>| </span><span class=string>"iD" </span><span class=special>| </span><span class=string>"ID"</span><span class=special>;</span></font></code></pre>
  65. <p>Now, try doing that with the case insensitive Pascal keyword <span class="quotes">&quot;BEGIN&quot;</span>.
  66. The <tt>as_lower_d</tt> directive makes this simple:</p>
  67. <pre><code><font color="#000000"><span class=special> </span><span class=identifier>r </span><span class=special>= </span><span class=identifier>as_lower_d</span><span class=special>[</span><span class=string>"begin"</span><span class=special>];</span></font></code></pre>
  68. <table width="80%" border="0" align="center">
  69. <tr>
  70. <td class="note_box"><div align="justify"><img src="theme/note.gif" width="16" height="16">
  71. <b>Primitive arguments</b> <br>
  72. <br>
  73. The astute reader will notice that we did not explicitly wrap <span class="quotes">&quot;begin&quot;</span>
  74. inside an <tt>str_p</tt>. Whenever appropriate, directives should be able
  75. to allow primitive types such as <tt>char</tt>, <tt>int</tt>, <tt>wchar_t</tt>,
  76. <tt>char const<span class="operators">*</span></tt>, <tt>wchar_t const<span class="operators">*</span></tt>
  77. and so on. Examples: <tt><br>
  78. <br>
  79. </tt><code><span class=identifier>as_lower_d</span><tt><span class=special>[</span><span class=string>"hello"</span><span class=special>]
  80. </span><span class=comment>// same as as_lower_d[str_p("hello")]</span></tt><code></code><span class=identifier><br>
  81. as_lower_d</span><span class=special>[</span><span class=literal>'x'</span><span class=special>]
  82. </span><span class=comment>// same as as_lower_d[ch_p('x')]</span></code></div></td>
  83. </tr>
  84. </table>
  85. <h3>no_actions_d</h3>
  86. <p>There are cases where you want <a href="semantic_actions.html">semantic actions</a>
  87. not to be triggered. By enclosing a parser in the <tt>no_actions_d</tt> directive,
  88. all semantic actions directly or indirectly attached to the parser will not
  89. fire. </p>
  90. <pre><code><font color="#000000"><span class=special> </span>no_actions_d<span class=special>[</span><span class=identifier>expression</span><span class=special>]</span></font></code><code><font color="#000000"><span class=special></span></font></code></pre>
  91. <h3>Tweaking the Scanner Type</h3>
  92. <p><img src="theme/note.gif" width="16" height="16"> How does <tt>lexeme_d, as_lower_d</tt>
  93. and <font color="#000000"><tt>no_actions_d</tt></font> work? These directives
  94. do their magic by tweaking the scanner policies. Well, you don't need to know
  95. what that means for now. Scanner policies are discussed <a href="indepth_the_scanner.html">later</a>.
  96. However, it is important to note that when the scanner policy is tweaked, the
  97. result is a different scanner. Why is this important to note? The <a href="rule.html">rule</a>
  98. is tied to a particular scanner (one or more scanners, to be precise). If you
  99. wrap a rule inside a <tt>lexeme_d, as_lower_d</tt> or <font color="#000000"><tt>no_actions_d,</tt>the
  100. compiler will complain about <a href="faq.html#scanner_business">scanner mismatch</a>
  101. unless you associate the required scanner with the rule. </font></p>
  102. <p><tt>lexeme_scanner</tt>, <tt>as_lower_scanner</tt> and <tt>no_actions_scanner</tt>
  103. are your friends if the need to wrap a rule inside these directives arise. Learn
  104. bout these beasts in the next chapter on <a href="scanner.html#lexeme_scanner">The
  105. Scanner and Parsing</a>.</p>
  106. <h2>longest_d</h2>
  107. <p>Alternatives in the Spirit parser compiler are short-circuited (see <a href="operators.html">Operators</a>).
  108. Sometimes, this is not what is desired. The <tt>longest_d</tt> directive instructs
  109. the parser not to short-circuit alternatives enclosed inside this directive,
  110. but instead makes the parser try all possible alternatives and choose the one
  111. matching the longest portion of the input stream.</p>
  112. <p>Consider the parsing of integers and real numbers:</p>
  113. <pre><code><font color="#000000"><span class=comment> </span><span class=identifier>number </span><span class=special>= </span><span class=identifier>real </span><span class=special>| </span><span class=identifier>integer</span><span class=special>;</span></font></code></pre>
  114. <p>A number can be a real or an integer. This grammar is ambiguous. An input <span class="quotes">&quot;1234&quot;</span>
  115. should potentially match both real and integer. Recall though that alternatives
  116. are short-circuited . Thus, for inputs such as above, the real alternative always
  117. wins. However, if we swap the alternatives:</p>
  118. <pre><code><font color="#000000"><span class=special> </span><span class=identifier>number </span><span class=special>= </span><span class=identifier>integer </span><span class=special>| </span><span class=identifier>real</span><span class=special>;</span></font></code></pre>
  119. <p>we still have a problem. Now, an input <span class="quotes">&quot;123.456&quot;</span>
  120. will be partially matched by integer until the decimal point. This is not what
  121. we want. The solution here is either to fix the ambiguity by factoring out the
  122. common prefixes of real and integer or, if that is not possible nor desired,
  123. use the <tt>longest_d</tt> directive:</p>
  124. <pre><code><font color="#000000"><span class=special> </span><span class=identifier>number </span><span class=special>= </span><span class=identifier>longest_d</span><span class=special>[ </span><span class=identifier>integer </span><span class=special>| </span><span class=identifier>real </span><span class=special>];</span></font></code></pre>
  125. <h2>shortest_d</h2>
  126. <p>Opposite of the <tt>longest_d</tt> directive.</p>
  127. <table width="80%" border="0" align="center">
  128. <tr>
  129. <td class="note_box"><img src="theme/note.gif" width="16" height="16"> <b>Multiple
  130. alternatives</b> <br>
  131. <br>
  132. The <tt>longest_d</tt> and <tt>shortest_d</tt> directives can accept two
  133. or more alternatives. Examples:<br>
  134. <br>
  135. <font color="#000000"><span class=identifier><code>longest</code></span><code><span class=special>[
  136. </span><span class=identifier>a </span><span class=special>| </span><span class=identifier>b
  137. </span><span class=special>| </span><span class=identifier>c </span><span class=special>];
  138. </span><span class=identifier><br>
  139. shortest</span><span class=special>[ </span><span class=identifier>a </span><span class=special>|
  140. </span><span class=identifier>b </span><span class=special>| </span><span class=identifier>c
  141. </span><span class=special>| </span><span class=identifier>d </span><span class=special>];</span></code></font></td>
  142. </tr>
  143. </table>
  144. <h2>limit_d</h2>
  145. <p>Ensures that the result of a parser is constrained to a given min..max range
  146. (inclusive). If not, then the parser fails and returns a no-match.</p>
  147. <p><b>Usage:</b></p>
  148. <pre><code><font color="#000000"><span class=special> </span><span class=identifier>limit_d</span><span class=special>(</span><span class=identifier>min</span><span class=special>, </span><span class=identifier>max</span><span class=special>)[</span><span class=identifier>expression</span><span class=special>]</span></font></code></pre>
  149. <p>This directive is particularly useful in conjunction with parsers that parse
  150. specific scalar ranges (for example, <a href="numerics.html">numeric parsers</a>).
  151. Here's a practical example. Although the numeric parsers can be configured to
  152. accept only a limited number of digits (say, 0..2), there is no way to limit
  153. the result to a range (say -1.0..1.0). This design is deliberate. Doing so would
  154. have undermined Spirit's design rule that <i><span class="quotes">&quot;the
  155. client should not pay for features that she does not use&quot;</span></i>. We
  156. would have stored the min, max values in the numeric parser itself, used or
  157. unused. Well, we could get by by using static constants configured by a non-type
  158. template parameter, but that is not acceptable because that way, we can only
  159. accommodate integers. What about real numbers or user defined numbers such as
  160. big-ints?</p>
  161. <p><b>Example</b>, parse time of the form <b>HH:MM:SS</b>:</p>
  162. <pre><code><font color="#000000"><span class=special> </span><span class=identifier>uint_parser</span><span class=special>&lt;</span><span class=keyword>int</span><span class=special>, </span><span class=number>10</span><span class=special>, </span><span class=number>2</span><span class=special>, </span><span class=number>2</span><span class=special>&gt; </span><span class=identifier>uint2_p</span><span class=special>;
  163. </span><span class=identifier>r </span><span class=special>= </span><span class=identifier>lexeme_d
  164. </span><span class=special>[
  165. </span><span class=identifier>limit_d</span><span class=special>(</span><span class=number>0u</span><span class=special>, </span><span class=number>23u</span><span class=special>)[</span><span class=identifier>uint2_p</span><span class=special>] </span><span class=special>&gt;&gt; </span><span class=literal>':' </span><span class=comment>// Hours 00..23
  166. </span><span class=special>&gt;&gt; </span><span class=identifier>limit_d</span><span class=special>(</span><span class=number>0u</span><span class=special>, </span><span class=number>59u</span><span class=special>)[</span><span class=identifier>uint2_p</span><span class=special>] </span><span class=special>&gt;&gt; </span><span class=literal>':' </span><span class=comment>// Minutes 00..59
  167. </span><span class=special>&gt;&gt; </span><span class=identifier>limit_d</span><span class=special>(</span><span class=number>0u</span><span class=special>, </span><span class=number>59u</span><span class=special>)[</span><span class=identifier>uint2_p</span><span class=special>] </span><span class=comment>// Seconds 00..59
  168. </span><span class=special>];</span></font></code>
  169. </pre>
  170. <h2>min_limit_d</h2>
  171. <p>Sometimes, it is useful to unconstrain just the maximum limit. This will allow
  172. for an interval that's unbounded in one direction. The directive min_limit_d
  173. ensures that the result of a parser is not less than minimun. If not, then the
  174. parser fails and returns a no-match.</p>
  175. <p><b>Usage:</b></p>
  176. <pre><code><font color="#000000"><span class=special> </span><span class=identifier>min_limit_d</span><span class=special>(</span><span class=identifier>min</span><span class=special>)[</span><span class=identifier>expression</span><span class=special>]</span></font></code></pre>
  177. <p><b>Example</b>, ensure that a date is not less than 1900</p>
  178. <pre><code><font color="#000000"><span class=special> </span><span class=identifier>min_limit_d</span><span class=special>(</span><span class=number>1900u</span><span class=special>)[</span><span class=identifier>uint_p</span><span class=special>]</span></font></code></pre>
  179. <h2>max_limit_d</h2>
  180. <p>Opposite of <tt>min_limit_d</tt>. Take note that <tt>limit_d[p]</tt> is equivalent
  181. to:</p>
  182. <pre><code><font color="#000000"><span class=special> </span><span class=identifier>min_limit_d</span><span class=special>(</span><span class=identifier>min</span><span class=special>)[</span><span class=identifier>max_limit_d</span><span class=special>(</span><span class=identifier>max</span><span class=special>)[</span><span class=identifier>p</span><span class=special>]]</span></font></code><code><font color="#000000"><span class=special></span></font></code></pre>
  183. <table border="0">
  184. <tr>
  185. <td width="10"></td>
  186. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  187. <td width="30"><a href="epsilon.html"><img src="theme/l_arr.gif" border="0"></a></td>
  188. <td width="30"><a href="scanner.html"><img src="theme/r_arr.gif" border="0"></a></td>
  189. </tr>
  190. </table>
  191. <br>
  192. <hr size="1">
  193. <p class="copyright">Copyright &copy; 1998-2003 Joel de Guzman<br>
  194. <br>
  195. <font size="2">Use, modification and distribution is subject to the Boost Software
  196. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  197. http://www.boost.org/LICENSE_1_0.txt) </font> </p>
  198. <p>&nbsp;</p>
  199. </body>
  200. </html>