1Lexing(3) OCaml library Lexing(3)
2
3
4
6 Lexing - The run-time library for lexers generated by ocamllex.
7
9 Module Lexing
10
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
26
27 type position = {
28 pos_fname : string ;
29 pos_lnum : int ;
30 pos_bol : int ;
31 pos_cnum : int ;
32 }
33
34
35 A value of type position describes a point in a source file. pos_fname
36 is the file name; pos_lnum is the line number; pos_bol is the offset of
37 the beginning of the line (number of characters between the beginning
38 of the lexbuf and the beginning of the line); pos_cnum is the offset of
39 the position (number of characters between the beginning of the lexbuf
40 and the position). The difference between pos_cnum and pos_bol is the
41 character offset within the line (i.e. the column number, assuming each
42 character is one column wide).
43
44 See the documentation of type lexbuf for information about how the lex‐
45 ing engine will manage positions.
46
47
48
49 val dummy_pos : position
50
51 A value of type position , guaranteed to be different from any valid
52 position.
53
54
55
56
57 === Lexer buffers ===
58
59
60 type lexbuf = {
61 refill_buff : lexbuf -> unit ;
62
63 mutable lex_buffer : bytes ;
64
65 mutable lex_buffer_len : int ;
66
67 mutable lex_abs_pos : int ;
68
69 mutable lex_start_pos : int ;
70
71 mutable lex_curr_pos : int ;
72
73 mutable lex_last_pos : int ;
74
75 mutable lex_last_action : int ;
76
77 mutable lex_eof_reached : bool ;
78
79 mutable lex_mem : int array ;
80
81 mutable lex_start_p : position ;
82
83 mutable lex_curr_p : position ;
84 }
85
86
87 The type of lexer buffers. A lexer buffer is the argument passed to the
88 scanning functions defined by the generated scanners. The lexer buffer
89 holds the current state of the scanner, plus a function to refill the
90 buffer from the input.
91
92 At each token, the lexing engine will copy lex_curr_p to lex_start_p ,
93 then change the pos_cnum field of lex_curr_p by updating it with the
94 number of characters read since the start of the lexbuf . The other
95 fields are left unchanged by the lexing engine. In order to keep them
96 accurate, they must be initialised before the first use of the lexbuf,
97 and updated by the relevant lexer actions (i.e. at each end of line --
98 see also new_line ).
99
100
101
102 val from_channel : Pervasives.in_channel -> lexbuf
103
104 Create a lexer buffer on the given input channel. Lexing.from_channel
105 inchan returns a lexer buffer which reads from the input channel inchan
106 , at the current reading position.
107
108
109
110 val from_string : string -> lexbuf
111
112 Create a lexer buffer which reads from the given string. Reading starts
113 from the first character in the string. An end-of-input condition is
114 generated when the end of the string is reached.
115
116
117
118 val from_function : (bytes -> int -> int) -> lexbuf
119
120 Create a lexer buffer with the given function as its reading method.
121 When the scanner needs more characters, it will call the given func‐
122 tion, giving it a byte sequence s and a byte count n . The function
123 should put n bytes or fewer in s , starting at index 0, and return the
124 number of bytes provided. A return value of 0 means end of input.
125
126
127
128
129 === Functions for lexer semantic actions ===
130
131
132 === The following functions can be called from the semantic actions of
133 lexer definitions (the ML code enclosed in braces that computes the
134 value returned by lexing functions). They give access to the character
135 string matched by the regular expression associated with the semantic
136 action. These functions must be applied to the argument lexbuf, which,
137 in the code generated by ocamllex, is bound to the lexer buffer passed
138 to the parsing function. ===
139
140
141 val lexeme : lexbuf -> string
142
143
144 Lexing.lexeme lexbuf returns the string matched by the regular expres‐
145 sion.
146
147
148
149 val lexeme_char : lexbuf -> int -> char
150
151
152 Lexing.lexeme_char lexbuf i returns character number i in the matched
153 string.
154
155
156
157 val lexeme_start : lexbuf -> int
158
159
160 Lexing.lexeme_start lexbuf returns the offset in the input stream of
161 the first character of the matched string. The first character of the
162 stream has offset 0.
163
164
165
166 val lexeme_end : lexbuf -> int
167
168
169 Lexing.lexeme_end lexbuf returns the offset in the input stream of the
170 character following the last character of the matched string. The first
171 character of the stream has offset 0.
172
173
174
175 val lexeme_start_p : lexbuf -> position
176
177 Like lexeme_start , but return a complete position instead of an off‐
178 set.
179
180
181
182 val lexeme_end_p : lexbuf -> position
183
184 Like lexeme_end , but return a complete position instead of an offset.
185
186
187
188 val new_line : lexbuf -> unit
189
190 Update the lex_curr_p field of the lexbuf to reflect the start of a new
191 line. You can call this function in the semantic action of the rule
192 that matches the end-of-line character.
193
194
195 Since 3.11.0
196
197
198
199
200 === Miscellaneous functions ===
201
202
203 val flush_input : lexbuf -> unit
204
205 Discard the contents of the buffer and reset the current position to 0.
206 The next use of the lexbuf will trigger a refill.
207
208
209
210
211
212OCamldoc 2019-02-02 Lexing(3)