1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="Start" href="index.html">
<link rel="previous" href="Stdlib.Lazy.html">
<link rel="next" href="Stdlib.List.html">
<link rel="Up" href="Stdlib.html">
<link title="Index of types" rel=Appendix href="index_types.html">
<link title="Index of extensions" rel=Appendix href="index_extensions.html">
<link title="Index of exceptions" rel=Appendix href="index_exceptions.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Index of module types" rel=Appendix href="index_module_types.html">
<link title="Arg" rel="Chapter" href="Arg.html">
<link title="Array" rel="Chapter" href="Array.html">
<link title="ArrayLabels" rel="Chapter" href="ArrayLabels.html">
<link title="Bigarray" rel="Chapter" href="Bigarray.html">
<link title="Bool" rel="Chapter" href="Bool.html">
<link title="Buffer" rel="Chapter" href="Buffer.html">
<link title="Bytes" rel="Chapter" href="Bytes.html">
<link title="BytesLabels" rel="Chapter" href="BytesLabels.html">
<link title="Callback" rel="Chapter" href="Callback.html">
<link title="CamlinternalFormat" rel="Chapter" href="CamlinternalFormat.html">
<link title="CamlinternalFormatBasics" rel="Chapter" href="CamlinternalFormatBasics.html">
<link title="CamlinternalLazy" rel="Chapter" href="CamlinternalLazy.html">
<link title="CamlinternalMod" rel="Chapter" href="CamlinternalMod.html">
<link title="CamlinternalOO" rel="Chapter" href="CamlinternalOO.html">
<link title="Char" rel="Chapter" href="Char.html">
<link title="Complex" rel="Chapter" href="Complex.html">
<link title="Condition" rel="Chapter" href="Condition.html">
<link title="Digest" rel="Chapter" href="Digest.html">
<link title="Dynlink" rel="Chapter" href="Dynlink.html">
<link title="Ephemeron" rel="Chapter" href="Ephemeron.html">
<link title="Event" rel="Chapter" href="Event.html">
<link title="Filename" rel="Chapter" href="Filename.html">
<link title="Float" rel="Chapter" href="Float.html">
<link title="Format" rel="Chapter" href="Format.html">
<link title="Fun" rel="Chapter" href="Fun.html">
<link title="Gc" rel="Chapter" href="Gc.html">
<link title="Genlex" rel="Chapter" href="Genlex.html">
<link title="Hashtbl" rel="Chapter" href="Hashtbl.html">
<link title="Int" rel="Chapter" href="Int.html">
<link title="Int32" rel="Chapter" href="Int32.html">
<link title="Int64" rel="Chapter" href="Int64.html">
<link title="Lazy" rel="Chapter" href="Lazy.html">
<link title="Lexing" rel="Chapter" href="Lexing.html">
<link title="List" rel="Chapter" href="List.html">
<link title="ListLabels" rel="Chapter" href="ListLabels.html">
<link title="Map" rel="Chapter" href="Map.html">
<link title="Marshal" rel="Chapter" href="Marshal.html">
<link title="MoreLabels" rel="Chapter" href="MoreLabels.html">
<link title="Mutex" rel="Chapter" href="Mutex.html">
<link title="Nativeint" rel="Chapter" href="Nativeint.html">
<link title="Obj" rel="Chapter" href="Obj.html">
<link title="Ocaml_operators" rel="Chapter" href="Ocaml_operators.html">
<link title="Oo" rel="Chapter" href="Oo.html">
<link title="Option" rel="Chapter" href="Option.html">
<link title="Parsing" rel="Chapter" href="Parsing.html">
<link title="Printexc" rel="Chapter" href="Printexc.html">
<link title="Printf" rel="Chapter" href="Printf.html">
<link title="Queue" rel="Chapter" href="Queue.html">
<link title="Random" rel="Chapter" href="Random.html">
<link title="Result" rel="Chapter" href="Result.html">
<link title="Scanf" rel="Chapter" href="Scanf.html">
<link title="Seq" rel="Chapter" href="Seq.html">
<link title="Set" rel="Chapter" href="Set.html">
<link title="Spacetime" rel="Chapter" href="Spacetime.html">
<link title="Stack" rel="Chapter" href="Stack.html">
<link title="StdLabels" rel="Chapter" href="StdLabels.html">
<link title="Stdlib" rel="Chapter" href="Stdlib.html">
<link title="Str" rel="Chapter" href="Str.html">
<link title="Stream" rel="Chapter" href="Stream.html">
<link title="String" rel="Chapter" href="String.html">
<link title="StringLabels" rel="Chapter" href="StringLabels.html">
<link title="Sys" rel="Chapter" href="Sys.html">
<link title="Thread" rel="Chapter" href="Thread.html">
<link title="ThreadUnix" rel="Chapter" href="ThreadUnix.html">
<link title="Uchar" rel="Chapter" href="Uchar.html">
<link title="Unit" rel="Chapter" href="Unit.html">
<link title="Unix" rel="Chapter" href="Unix.html">
<link title="UnixLabels" rel="Chapter" href="UnixLabels.html">
<link title="Weak" rel="Chapter" href="Weak.html"><link title="Positions" rel="Section" href="#1_Positions">
<link title="Lexer buffers" rel="Section" href="#1_Lexerbuffers">
<link title="Functions for lexer semantic actions" rel="Section" href="#1_Functionsforlexersemanticactions">
<link title="Miscellaneous functions" rel="Section" href="#1_Miscellaneousfunctions">
<title>Stdlib.Lexing</title>
</head>
<body>
<div class="navbar"><a class="pre" href="Stdlib.Lazy.html" title="Stdlib.Lazy">Previous</a>
<a class="up" href="Stdlib.html" title="Stdlib">Up</a>
<a class="post" href="Stdlib.List.html" title="Stdlib.List">Next</a>
</div>
<h1>Module <a href="type_Stdlib.Lexing.html">Stdlib.Lexing</a></h1>
<pre><span id="MODULELexing"><span class="keyword">module</span> Lexing</span>: <code class="type"><a href="Lexing.html">Lexing</a></code></pre><hr width="100%">
<h2 id="1_Positions">Positions</h2>
<pre><code><span id="TYPEposition"><span class="keyword">type</span> <code class="type"></code>position</span> = {</code></pre><table class="typetable">
<tr>
<td align="left" valign="top" >
<code> </code></td>
<td align="left" valign="top" >
<code><span id="TYPEELTposition.pos_fname">pos_fname</span> : <code class="type">string</code>;</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code> </code></td>
<td align="left" valign="top" >
<code><span id="TYPEELTposition.pos_lnum">pos_lnum</span> : <code class="type">int</code>;</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code> </code></td>
<td align="left" valign="top" >
<code><span id="TYPEELTposition.pos_bol">pos_bol</span> : <code class="type">int</code>;</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code> </code></td>
<td align="left" valign="top" >
<code><span id="TYPEELTposition.pos_cnum">pos_cnum</span> : <code class="type">int</code>;</code></td>
</tr></table>
<code>}</code>
<div class="info ">
<div class="info-desc">
<p>A value of type <code class="code">position</code> describes a point in a source file.
<code class="code">pos_fname</code> is the file name; <code class="code">pos_lnum</code> is the line number;
<code class="code">pos_bol</code> is the offset of the beginning of the line (number
of characters between the beginning of the lexbuf and the beginning
of the line); <code class="code">pos_cnum</code> is the offset of the position (number of
characters between the beginning of the lexbuf and the position).
The difference between <code class="code">pos_cnum</code> and <code class="code">pos_bol</code> is the character
offset within the line (i.e. the column number, assuming each
character is one column wide).</p>
<p>See the documentation of type <code class="code">lexbuf</code> for information about
how the lexing engine will manage positions.</p>
</div>
</div>
<pre><span id="VALdummy_pos"><span class="keyword">val</span> dummy_pos</span> : <code class="type"><a href="Lexing.html#TYPEposition">position</a></code></pre><div class="info ">
<div class="info-desc">
<p>A value of type <code class="code">position</code>, guaranteed to be different from any
valid position.</p>
</div>
</div>
<h2 id="1_Lexerbuffers">Lexer buffers</h2>
<pre><code><span id="TYPElexbuf"><span class="keyword">type</span> <code class="type"></code>lexbuf</span> = {</code></pre><table class="typetable">
<tr>
<td align="left" valign="top" >
<code> </code></td>
<td align="left" valign="top" >
<code><span id="TYPEELTlexbuf.refill_buff">refill_buff</span> : <code class="type"><a href="Lexing.html#TYPElexbuf">lexbuf</a> -> unit</code>;</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code> </code></td>
<td align="left" valign="top" >
<code><span class="keyword">mutable </span><span id="TYPEELTlexbuf.lex_buffer">lex_buffer</span> : <code class="type">bytes</code>;</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code> </code></td>
<td align="left" valign="top" >
<code><span class="keyword">mutable </span><span id="TYPEELTlexbuf.lex_buffer_len">lex_buffer_len</span> : <code class="type">int</code>;</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code> </code></td>
<td align="left" valign="top" >
<code><span class="keyword">mutable </span><span id="TYPEELTlexbuf.lex_abs_pos">lex_abs_pos</span> : <code class="type">int</code>;</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code> </code></td>
<td align="left" valign="top" >
<code><span class="keyword">mutable </span><span id="TYPEELTlexbuf.lex_start_pos">lex_start_pos</span> : <code class="type">int</code>;</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code> </code></td>
<td align="left" valign="top" >
<code><span class="keyword">mutable </span><span id="TYPEELTlexbuf.lex_curr_pos">lex_curr_pos</span> : <code class="type">int</code>;</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code> </code></td>
<td align="left" valign="top" >
<code><span class="keyword">mutable </span><span id="TYPEELTlexbuf.lex_last_pos">lex_last_pos</span> : <code class="type">int</code>;</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code> </code></td>
<td align="left" valign="top" >
<code><span class="keyword">mutable </span><span id="TYPEELTlexbuf.lex_last_action">lex_last_action</span> : <code class="type">int</code>;</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code> </code></td>
<td align="left" valign="top" >
<code><span class="keyword">mutable </span><span id="TYPEELTlexbuf.lex_eof_reached">lex_eof_reached</span> : <code class="type">bool</code>;</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code> </code></td>
<td align="left" valign="top" >
<code><span class="keyword">mutable </span><span id="TYPEELTlexbuf.lex_mem">lex_mem</span> : <code class="type">int array</code>;</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code> </code></td>
<td align="left" valign="top" >
<code><span class="keyword">mutable </span><span id="TYPEELTlexbuf.lex_start_p">lex_start_p</span> : <code class="type"><a href="Lexing.html#TYPEposition">position</a></code>;</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code> </code></td>
<td align="left" valign="top" >
<code><span class="keyword">mutable </span><span id="TYPEELTlexbuf.lex_curr_p">lex_curr_p</span> : <code class="type"><a href="Lexing.html#TYPEposition">position</a></code>;</code></td>
</tr></table>
<code>}</code>
<div class="info ">
<div class="info-desc">
<p>The type of lexer buffers. A lexer buffer is the argument passed
to the scanning functions defined by the generated scanners.
The lexer buffer holds the current state of the scanner, plus
a function to refill the buffer from the input.</p>
<p>Lexers can optionally maintain the <code class="code">lex_curr_p</code> and <code class="code">lex_start_p</code>
position fields. This "position tracking" mode is the default, and
it corresponds to passing <code class="code">~with_position:<span class="keyword">true</span></code> to functions that
create lexer buffers. In this mode, the lexing engine and lexer
actions are co-responsible for properly updating the position
fields, as described in the next paragraph. When the mode is
explicitly disabled (with <code class="code">~with_position:<span class="keyword">false</span></code>), the lexing
engine will not touch the position fields and the lexer actions
should be careful not to do it either; the <code class="code">lex_curr_p</code> and
<code class="code">lex_start_p</code> field will then always hold the <code class="code">dummy_pos</code> invalid
position. Not tracking positions avoids allocations and memory
writes and can significantly improve the performance of the lexer
in contexts where <code class="code">lex_start_p</code> and <code class="code">lex_curr_p</code> are not needed.</p>
<p>Position tracking mode works as follows. At each token, the lexing
engine will copy <code class="code">lex_curr_p</code> to <code class="code">lex_start_p</code>, then change the
<code class="code">pos_cnum</code> field of <code class="code">lex_curr_p</code> by updating it with the number of
characters read since the start of the <code class="code">lexbuf</code>. The other fields
are left unchanged by the lexing engine. In order to keep them
accurate, they must be initialised before the first use of the
lexbuf, and updated by the relevant lexer actions (i.e. at each end
of line -- see also <code class="code">new_line</code>).</p>
</div>
</div>
<pre><span id="VALfrom_channel"><span class="keyword">val</span> from_channel</span> : <code class="type">?with_positions:bool -> <a href="Stdlib.html#TYPEin_channel">in_channel</a> -> <a href="Lexing.html#TYPElexbuf">lexbuf</a></code></pre><div class="info ">
<div class="info-desc">
<p>Create a lexer buffer on the given input channel.
<code class="code"><span class="constructor">Lexing</span>.from_channel inchan</code> returns a lexer buffer which reads
from the input channel <code class="code">inchan</code>, at the current reading position.</p>
</div>
</div>
<pre><span id="VALfrom_string"><span class="keyword">val</span> from_string</span> : <code class="type">?with_positions:bool -> string -> <a href="Lexing.html#TYPElexbuf">lexbuf</a></code></pre><div class="info ">
<div class="info-desc">
<p>Create a lexer buffer which reads from
the given string. Reading starts from the first character in
the string. An end-of-input condition is generated when the
end of the string is reached.</p>
</div>
</div>
<pre><span id="VALfrom_function"><span class="keyword">val</span> from_function</span> : <code class="type">?with_positions:bool -> (bytes -> int -> int) -> <a href="Lexing.html#TYPElexbuf">lexbuf</a></code></pre><div class="info ">
<div class="info-desc">
<p>Create a lexer buffer with the given function as its reading method.
When the scanner needs more characters, it will call the given
function, giving it a byte sequence <code class="code">s</code> and a byte
count <code class="code">n</code>. The function should put <code class="code">n</code> bytes or fewer in <code class="code">s</code>,
starting at index 0, and return the number of bytes
provided. A return value of 0 means end of input.</p>
</div>
</div>
<pre><span id="VALset_position"><span class="keyword">val</span> set_position</span> : <code class="type"><a href="Lexing.html#TYPElexbuf">lexbuf</a> -> <a href="Lexing.html#TYPEposition">position</a> -> unit</code></pre><div class="info ">
<div class="info-desc">
<p>Set the initial tracked input position for <code class="code">lexbuf</code> to a custom value.
Ignores <code class="code">pos_fname</code>. See <a href="Lexing.html#VALset_filename"><code class="code"><span class="constructor">Lexing</span>.set_filename</code></a> for changing this field.</p>
</div>
<ul class="info-attributes">
<li><b>Since</b> 4.11</li>
</ul>
</div>
<pre><span id="VALset_filename"><span class="keyword">val</span> set_filename</span> : <code class="type"><a href="Lexing.html#TYPElexbuf">lexbuf</a> -> string -> unit</code></pre><div class="info ">
<div class="info-desc">
<p>Set filename in the initial tracked position to <code class="code">file</code> in
<code class="code">lexbuf</code>.</p>
</div>
<ul class="info-attributes">
<li><b>Since</b> 4.11</li>
</ul>
</div>
<pre><span id="VALwith_positions"><span class="keyword">val</span> with_positions</span> : <code class="type"><a href="Lexing.html#TYPElexbuf">lexbuf</a> -> bool</code></pre><div class="info ">
<div class="info-desc">
<p>Tell whether the lexer buffer keeps track of position fields
<code class="code">lex_curr_p</code> / <code class="code">lex_start_p</code>, as determined by the corresponding
optional argument for functions that create lexer buffers
(whose default value is <code class="code"><span class="keyword">true</span></code>).</p>
<p>When <code class="code">with_positions</code> is <code class="code"><span class="keyword">false</span></code>, lexer actions should not
modify position fields. Doing it nevertheless could
re-enable the <code class="code">with_position</code> mode and degrade performances.</p>
</div>
</div>
<h2 id="1_Functionsforlexersemanticactions">Functions for lexer semantic actions</h2><p>The following functions can be called from the semantic actions
of lexer definitions (the ML code enclosed in braces that
computes the value returned by lexing functions). They give
access to the character string matched by the regular expression
associated with the semantic action. These functions must be
applied to the argument <code class="code">lexbuf</code>, which, in the code generated by
<code class="code">ocamllex</code>, is bound to the lexer buffer passed to the parsing
function.</p>
<pre><span id="VALlexeme"><span class="keyword">val</span> lexeme</span> : <code class="type"><a href="Lexing.html#TYPElexbuf">lexbuf</a> -> string</code></pre><div class="info ">
<div class="info-desc">
<p><code class="code"><span class="constructor">Lexing</span>.lexeme lexbuf</code> returns the string matched by
the regular expression.</p>
</div>
</div>
<pre><span id="VALlexeme_char"><span class="keyword">val</span> lexeme_char</span> : <code class="type"><a href="Lexing.html#TYPElexbuf">lexbuf</a> -> int -> char</code></pre><div class="info ">
<div class="info-desc">
<p><code class="code"><span class="constructor">Lexing</span>.lexeme_char lexbuf i</code> returns character number <code class="code">i</code> in
the matched string.</p>
</div>
</div>
<pre><span id="VALlexeme_start"><span class="keyword">val</span> lexeme_start</span> : <code class="type"><a href="Lexing.html#TYPElexbuf">lexbuf</a> -> int</code></pre><div class="info ">
<div class="info-desc">
<p><code class="code"><span class="constructor">Lexing</span>.lexeme_start lexbuf</code> returns the offset in the
input stream of the first character of the matched string.
The first character of the stream has offset 0.</p>
</div>
</div>
<pre><span id="VALlexeme_end"><span class="keyword">val</span> lexeme_end</span> : <code class="type"><a href="Lexing.html#TYPElexbuf">lexbuf</a> -> int</code></pre><div class="info ">
<div class="info-desc">
<p><code class="code"><span class="constructor">Lexing</span>.lexeme_end lexbuf</code> returns the offset in the input stream
of the character following the last character of the matched
string. The first character of the stream has offset 0.</p>
</div>
</div>
<pre><span id="VALlexeme_start_p"><span class="keyword">val</span> lexeme_start_p</span> : <code class="type"><a href="Lexing.html#TYPElexbuf">lexbuf</a> -> <a href="Lexing.html#TYPEposition">position</a></code></pre><div class="info ">
<div class="info-desc">
<p>Like <code class="code">lexeme_start</code>, but return a complete <code class="code">position</code> instead
of an offset. When position tracking is disabled, the function
returns <code class="code">dummy_pos</code>.</p>
</div>
</div>
<pre><span id="VALlexeme_end_p"><span class="keyword">val</span> lexeme_end_p</span> : <code class="type"><a href="Lexing.html#TYPElexbuf">lexbuf</a> -> <a href="Lexing.html#TYPEposition">position</a></code></pre><div class="info ">
<div class="info-desc">
<p>Like <code class="code">lexeme_end</code>, but return a complete <code class="code">position</code> instead
of an offset. When position tracking is disabled, the function
returns <code class="code">dummy_pos</code>.</p>
</div>
</div>
<pre><span id="VALnew_line"><span class="keyword">val</span> new_line</span> : <code class="type"><a href="Lexing.html#TYPElexbuf">lexbuf</a> -> unit</code></pre><div class="info ">
<div class="info-desc">
<p>Update the <code class="code">lex_curr_p</code> field of the lexbuf to reflect the start
of a new line. You can call this function in the semantic action
of the rule that matches the end-of-line character. The function
does nothing when position tracking is disabled.</p>
</div>
<ul class="info-attributes">
<li><b>Since</b> 3.11.0</li>
</ul>
</div>
<h2 id="1_Miscellaneousfunctions">Miscellaneous functions</h2>
<pre><span id="VALflush_input"><span class="keyword">val</span> flush_input</span> : <code class="type"><a href="Lexing.html#TYPElexbuf">lexbuf</a> -> unit</code></pre><div class="info ">
<div class="info-desc">
<p>Discard the contents of the buffer and reset the current
position to 0. The next use of the lexbuf will trigger a
refill.</p>
</div>
</div>
</body></html>
|