.TH "Lexing" 3 2024-05-31 OCamldoc "OCaml library" .SH NAME Lexing \- The run-time library for lexers generated by ocamllex. .SH Module Module Lexing .SH Documentation .sp Module .BI "Lexing" : .B sig end .sp The run\-time library for lexers generated by .ft B ocamllex .ft R \&. .sp .sp .sp .PP .SS Positions .PP .I type position = { pos_fname : .B string ; pos_lnum : .B int ; pos_bol : .B int ; pos_cnum : .B int ; } .sp A value of type .ft B position .ft R describes a point in a source file\&. .ft B pos_fname .ft R is the file name; .ft B pos_lnum .ft R is the line number; .ft B pos_bol .ft R is the offset of the beginning of the line (number of characters between the beginning of the lexbuf and the beginning of the line); .ft B pos_cnum .ft R is the offset of the position (number of characters between the beginning of the lexbuf and the position)\&. The difference between .ft B pos_cnum .ft R and .ft B pos_bol .ft R is the character offset within the line (i\&.e\&. the column number, assuming each character is one column wide)\&. .sp See the documentation of type .ft B lexbuf .ft R for information about how the lexing engine will manage positions\&. .sp .I val dummy_pos : .B position .sp A value of type .ft B position .ft R , guaranteed to be different from any valid position\&. .sp .PP .SS Lexer buffers .PP .I type lexbuf = { refill_buff : .B lexbuf -> unit ; .B mutable lex_buffer : .B bytes ; .B mutable lex_buffer_len : .B int ; .B mutable lex_abs_pos : .B int ; .B mutable lex_start_pos : .B int ; .B mutable lex_curr_pos : .B int ; .B mutable lex_last_pos : .B int ; .B mutable lex_last_action : .B int ; .B mutable lex_eof_reached : .B bool ; .B mutable lex_mem : .B int array ; .B mutable lex_start_p : .B position ; .B mutable lex_curr_p : .B position ; } .sp 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\&. .sp Lexers can optionally maintain the .ft B lex_curr_p .ft R and .ft B lex_start_p .ft R position fields\&. This "position tracking" mode is the default, and it corresponds to passing .ft B ~with_position:true .ft R 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 .ft B ~with_position:false .ft R ), the lexing engine will not touch the position fields and the lexer actions should be careful not to do it either; the .ft B lex_curr_p .ft R and .ft B lex_start_p .ft R field will then always hold the .ft B dummy_pos .ft R invalid position\&. Not tracking positions avoids allocations and memory writes and can significantly improve the performance of the lexer in contexts where .ft B lex_start_p .ft R and .ft B lex_curr_p .ft R are not needed\&. .sp Position tracking mode works as follows\&. At each token, the lexing engine will copy .ft B lex_curr_p .ft R to .ft B lex_start_p .ft R , then change the .ft B pos_cnum .ft R field of .ft B lex_curr_p .ft R by updating it with the number of characters read since the start of the .ft B lexbuf .ft R \&. 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 .ft B new_line .ft R )\&. .sp .I val from_channel : .B ?with_positions:bool -> in_channel -> lexbuf .sp Create a lexer buffer on the given input channel\&. .ft B Lexing\&.from_channel inchan .ft R returns a lexer buffer which reads from the input channel .ft B inchan .ft R , at the current reading position\&. .sp .I val from_string : .B ?with_positions:bool -> string -> lexbuf .sp 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\&. .sp .I val from_function : .B ?with_positions:bool -> (bytes -> int -> int) -> lexbuf .sp 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 .ft B s .ft R and a byte count .ft B n .ft R \&. The function should put .ft B n .ft R bytes or fewer in .ft B s .ft R , starting at index 0, and return the number of bytes provided\&. A return value of 0 means end of input\&. .sp .I val set_position : .B lexbuf -> position -> unit .sp Set the initial tracked input position for .ft B lexbuf .ft R to a custom value\&. Ignores .ft B pos_fname .ft R \&. See .ft B Lexing\&.set_filename .ft R for changing this field\&. .sp .B "Since" 4.11 .sp .I val set_filename : .B lexbuf -> string -> unit .sp Set filename in the initial tracked position to .ft B file .ft R in .ft B lexbuf .ft R \&. .sp .B "Since" 4.11 .sp .I val with_positions : .B lexbuf -> bool .sp Tell whether the lexer buffer keeps track of position fields .ft B lex_curr_p .ft R / .ft B lex_start_p .ft R , as determined by the corresponding optional argument for functions that create lexer buffers (whose default value is .ft B true .ft R )\&. .sp When .ft B with_positions .ft R is .ft B false .ft R , lexer actions should not modify position fields\&. Doing it nevertheless could re\-enable the .ft B with_position .ft R mode and degrade performances\&. .sp .PP .SS Functions for lexer semantic actions .PP .PP 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 .ft B lexbuf .ft R , which, in the code generated by .ft B ocamllex .ft R , is bound to the lexer buffer passed to the parsing function\&. .PP .I val lexeme : .B lexbuf -> string .sp .ft B Lexing\&.lexeme lexbuf .ft R returns the string matched by the regular expression\&. .sp .I val lexeme_char : .B lexbuf -> int -> char .sp .ft B Lexing\&.lexeme_char lexbuf i .ft R returns character number .ft B i .ft R in the matched string\&. .sp .I val lexeme_start : .B lexbuf -> int .sp .ft B Lexing\&.lexeme_start lexbuf .ft R returns the offset in the input stream of the first character of the matched string\&. The first character of the stream has offset 0\&. .sp .I val lexeme_end : .B lexbuf -> int .sp .ft B Lexing\&.lexeme_end lexbuf .ft R 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\&. .sp .I val lexeme_start_p : .B lexbuf -> position .sp Like .ft B lexeme_start .ft R , but return a complete .ft B position .ft R instead of an offset\&. When position tracking is disabled, the function returns .ft B dummy_pos .ft R \&. .sp .I val lexeme_end_p : .B lexbuf -> position .sp Like .ft B lexeme_end .ft R , but return a complete .ft B position .ft R instead of an offset\&. When position tracking is disabled, the function returns .ft B dummy_pos .ft R \&. .sp .I val new_line : .B lexbuf -> unit .sp Update the .ft B lex_curr_p .ft R 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\&. .sp .B "Since" 3.11 .sp .PP .SS Miscellaneous functions .PP .I val flush_input : .B lexbuf -> unit .sp Discard the contents of the buffer and reset the current position to 0\&. The next use of the lexbuf will trigger a refill\&. .sp