Stdlib.Lexing(3)
omg hi bestie π welcome to Stdlib.Lexing(3) from the OCaml library manual β¨ this page is lowkey all about lexer buffers, positions, and the lil functions your semantic actions call. Let's go π«Ά
π NAME π
Stdlib.Lexing β no description. Yep, that's literally what the original NAME section says, so we're keeping it iconic and mysterious π
π¦ Module π¦
Module Stdlib.Lexing
Module Lexing
: (module Stdlib__Lexing)
π Documentation π
bestie, this is the main character section β¨
πΈ Positions πΈ
okay so first up, positions. A value of type position describes a point in a source file, and it's a record like this:
type position = {
pos_fname : string ;
pos_lnum : int ;
pos_bol : int ;
pos_cnum : int ;
}
pos_fnameis the file name. πpos_lnumis the line number. π’pos_bolis the offset of the beginning of the line β like the number of characters between the beginning of the lexbuf and the beginning of the line. πpos_cnumis the offset of the position β number of characters between the beginning of the lexbuf and the position. π
The difference between pos_cnum and pos_bol is the character offset within the line, i.e. the column number, assuming each character is one column wide. slay.
See the documentation of type lexbuf for information about how the lexing engine will manage positions π
val dummy_pos : positionβ a value of typeposition, guaranteed to be different from any valid position. It's the impostor position, bestie π
π¦ Lexer buffers π¦
The type of lexer buffers is:
type lexbuf = {
refill_buff : lexbuf -> unit ;
mutable lex_buffer : bytes ;
mutable lex_buffer_len : int ;
mutable lex_abs_pos : int ;
mutable lex_start_pos : int ;
mutable lex_curr_pos : int ;
mutable lex_last_pos : int ;
mutable lex_last_action : int ;
mutable lex_eof_reached : bool ;
mutable lex_mem : int array ;
mutable lex_start_p : position ;
mutable lex_curr_p : position ;
}
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. obsessed.
Lexers can optionally maintain the lex_curr_p and lex_start_p position fields. This "position tracking" mode is the default, and it corresponds to passing ~with_position:true 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 ~with_position:false), the lexing engine will not touch the position fields and the lexer actions should be careful not to do it either; the lex_curr_p and lex_start_p field will then always hold the dummy_pos invalid position. Not tracking positions avoids allocations and memory writes and can significantly improve the performance of the lexer in contexts where lex_start_p and lex_curr_p are not needed.
Position tracking mode works as follows. At each token, the lexing engine will copy lex_curr_p to lex_start_p, then change the pos_cnum field of lex_curr_p by updating it with the number of characters read since the start of the lexbuf. 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 new_line).
val from_channel : ?with_positions:bool -> in_channel -> lexbufβ create a lexer buffer on the given input channel.Lexing.from_channel inchanreturns a lexer buffer which reads from the input channelinchan, at the current reading position. π§val from_string : ?with_positions:bool -> string -> lexbufβ 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. πval from_functionβ 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 sequencesand a byte countn. The function should putnbytes or fewer ins, starting at index 0, and return the number of bytes provided. A return value of 0 means end of input.
val from_function : ?with_positions:bool -> (bytes -> int -> int) ->
lexbuf
val set_position : lexbuf -> position -> unitβ set the initial tracked input position forlexbufto a custom value. Ignorespos_fname. SeeLexing.set_filenamefor changing this field.Since 4.11β¨val set_filename : lexbuf -> string -> unitβ set filename in the initial tracked position to file inlexbuf.Since 4.11β¨val with_positions : lexbuf -> boolβ tell whether the lexer buffer keeps track of position fieldslex_curr_p/lex_start_p, as determined by the corresponding optional argument for functions that create lexer buffers (whose default value istrue).
β οΈ Whenwith_positionsisfalse, lexer actions should not modify position fields. Doing it nevertheless could re-enable thewith_positionmode and degrade performances.
π§ Functions for lexer semantic actions π§
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 lexbuf, which, in the code generated by ocamllex, is bound to the lexer buffer passed to the parsing function.
val lexeme : lexbuf -> stringβLexing.lexeme lexbufreturns the string matched by the regular expression. β¨val lexeme_char : lexbuf -> int -> charβLexing.lexeme_char lexbuf ireturns character numberiin the matched string. π€val lexeme_start : lexbuf -> intβLexing.lexeme_start lexbufreturns the offset in the input stream of the first character of the matched string. The first character of the stream has offset 0. π―val lexeme_end : lexbuf -> intβLexing.lexeme_end lexbufreturns 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. πval lexeme_start_p : lexbuf -> positionβ likelexeme_start, but return a complete position instead of an offset. When position tracking is disabled, the function returnsdummy_pos. πval lexeme_end_p : lexbuf -> positionβ likelexeme_end, but return a complete position instead of an offset. When position tracking is disabled, the function returnsdummy_pos. π«val new_line : lexbuf -> unitβ update thelex_curr_pfield of thelexbufto 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.Since 3.11π
π Miscellaneous functions π
val flush_input : lexbuf -> unitβ discard the contents of the buffer and reset the current position to 0. The next use of thelexbufwill trigger a refill. π§Ό
and that's the whole page, bestie π«Ά this lil manual moment was served by OCamldoc, dated 2026-06-22, and it's living at Stdlib.Lexing(3) π
β¨
π¦ where this page comes from
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms