1Lexing(3)                        OCaml library                       Lexing(3)
2
3
4

NAME

6       Lexing - The run-time library for lexers generated by ocamllex.
7

Module

9       Module   Lexing
10

Documentation

12       Module Lexing
13        : sig end
14
15
16       The run-time library for lexers generated by ocamllex .
17
18
19
20
21
22
23
24   Positions
25       type position = {
26        pos_fname : string ;
27        pos_lnum : int ;
28        pos_bol : int ;
29        pos_cnum : int ;
30        }
31
32
33       A value of type position describes a point in a source file.  pos_fname
34       is the file name; pos_lnum is the line number; pos_bol is the offset of
35       the  beginning  of the line (number of characters between the beginning
36       of the lexbuf and the beginning of the line); pos_cnum is the offset of
37       the  position (number of characters between the beginning of the lexbuf
38       and the position).  The difference between pos_cnum and pos_bol is  the
39       character offset within the line (i.e. the column number, assuming each
40       character is one column wide).
41
42       See the documentation of type lexbuf for information about how the lex‐
43       ing engine will manage positions.
44
45
46
47       val dummy_pos : position
48
49       A  value  of  type position , guaranteed to be different from any valid
50       position.
51
52
53
54
55   Lexer buffers
56       type lexbuf = {
57        refill_buff : lexbuf -> unit ;
58
59       mutable lex_buffer : bytes ;
60
61       mutable lex_buffer_len : int ;
62
63       mutable lex_abs_pos : int ;
64
65       mutable lex_start_pos : int ;
66
67       mutable lex_curr_pos : int ;
68
69       mutable lex_last_pos : int ;
70
71       mutable lex_last_action : int ;
72
73       mutable lex_eof_reached : bool ;
74
75       mutable lex_mem : int array ;
76
77       mutable lex_start_p : position ;
78
79       mutable lex_curr_p : position ;
80        }
81
82
83       The type of lexer buffers. A lexer buffer is the argument passed to the
84       scanning functions defined by the generated scanners.  The lexer buffer
85       holds the current state of the scanner, plus a function to  refill  the
86       buffer from the input.
87
88       Lexers  can optionally maintain the lex_curr_p and lex_start_p position
89       fields.  This "position tracking" mode is the default,  and  it  corre‐
90       sponds  to  passing  ~with_position:true to functions that create lexer
91       buffers. In  this  mode,  the  lexing  engine  and  lexer  actions  are
92       co-responsible  for properly updating the position fields, as described
93       in the next paragraph.  When the  mode  is  explicitly  disabled  (with
94       ~with_position:false  ),  the lexing engine will not touch the position
95       fields and the lexer actions should be careful not to do it either; the
96       lex_curr_p  and  lex_start_p  field will then always hold the dummy_pos
97       invalid position.  Not tracking positions avoids allocations and memory
98       writes  and  can  significantly improve the performance of the lexer in
99       contexts where lex_start_p and lex_curr_p are not needed.
100
101       Position tracking mode works as follows.  At  each  token,  the  lexing
102       engine  will  copy lex_curr_p to lex_start_p , then change the pos_cnum
103       field of lex_curr_p by updating it with the number of  characters  read
104       since the start of the lexbuf .  The other fields are left unchanged by
105       the lexing engine.  In order to keep them accurate, they must  be  ini‐
106       tialised  before  the first use of the lexbuf, and updated by the rele‐
107       vant lexer actions (i.e. at each end of line -- see also new_line ).
108
109
110
111       val from_channel : ?with_positions:bool -> in_channel -> lexbuf
112
113       Create a lexer buffer on the given input channel.   Lexing.from_channel
114       inchan returns a lexer buffer which reads from the input channel inchan
115       , at the current reading position.
116
117
118
119       val from_string : ?with_positions:bool -> string -> lexbuf
120
121       Create a lexer buffer which reads from the given string. Reading starts
122       from  the  first  character in the string. An end-of-input condition is
123       generated when the end of the string is reached.
124
125
126
127       val from_function : ?with_positions:bool -> (bytes -> int  ->  int)  ->
128       lexbuf
129
130       Create  a  lexer  buffer with the given function as its reading method.
131       When the scanner needs more characters, it will call  the  given  func‐
132       tion,  giving  it  a  byte sequence s and a byte count n . The function
133       should put n bytes or fewer in s , starting at index 0, and return  the
134       number of bytes provided. A return value of 0 means end of input.
135
136
137
138       val with_positions : lexbuf -> bool
139
140       Tell whether the lexer buffer keeps track of position fields lex_curr_p
141       / lex_start_p , as determined by the  corresponding  optional  argument
142       for functions that create lexer buffers (whose default value is true ).
143
144       When with_positions is false , lexer actions should not modify position
145       fields.  Doing it nevertheless could re-enable the  with_position  mode
146       and degrade performances.
147
148
149
150
151   Functions for lexer semantic actions
152       The  following  functions  can  be  called from the semantic actions of
153       lexer definitions (the ML code enclosed in  braces  that  computes  the
154       value  returned by lexing functions). They give access to the character
155       string matched by the regular expression associated with  the  semantic
156       action. These functions must be applied to the argument lexbuf , which,
157       in the code generated by ocamllex , is bound to the lexer buffer passed
158       to the parsing function.
159
160       val lexeme : lexbuf -> string
161
162
163       Lexing.lexeme  lexbuf returns the string matched by the regular expres‐
164       sion.
165
166
167
168       val lexeme_char : lexbuf -> int -> char
169
170
171       Lexing.lexeme_char lexbuf i returns character number i in  the  matched
172       string.
173
174
175
176       val lexeme_start : lexbuf -> int
177
178
179       Lexing.lexeme_start  lexbuf  returns  the offset in the input stream of
180       the first character of the matched string.  The first character of  the
181       stream has offset 0.
182
183
184
185       val lexeme_end : lexbuf -> int
186
187
188       Lexing.lexeme_end  lexbuf returns the offset in the input stream of the
189       character following the last character of the matched string. The first
190       character of the stream has offset 0.
191
192
193
194       val lexeme_start_p : lexbuf -> position
195
196       Like  lexeme_start  , but return a complete position instead of an off‐
197       set.   When  position  tracking  is  disabled,  the  function   returns
198       dummy_pos .
199
200
201
202       val lexeme_end_p : lexbuf -> position
203
204       Like  lexeme_end , but return a complete position instead of an offset.
205       When position tracking is disabled, the function returns dummy_pos .
206
207
208
209       val new_line : lexbuf -> unit
210
211       Update the lex_curr_p field of the lexbuf to reflect the start of a new
212       line.   You  can  call this function in the semantic action of the rule
213       that matches the end-of-line character.  The function does nothing when
214       position tracking is disabled.
215
216
217       Since 3.11.0
218
219
220
221
222   Miscellaneous functions
223       val flush_input : lexbuf -> unit
224
225       Discard the contents of the buffer and reset the current position to 0.
226       The next use of the lexbuf will trigger a refill.
227
228
229
230
231
232OCamldoc                          2019-07-30                         Lexing(3)
Impressum