Library
Module
Module type
Parameter
Class
Class type
Locations and ranges.
type string_source = {
title : string option;
The title of a string source. A diagnostic handler can use the title of a string source in lieu of a file path.
*)content : string;
The content of a string source
*)}
The string source of a position or a range.
type source = [
| `File of string
A file source specified by its file path.
*)| `String of string_source
A string (in-memory) source.
*) ]
The source of a position or a range. The `String
source can be used for representing inputs in REPL.
type position = {
source : source;
The source (e.g., the file) that contains the position.
*)offset : int;
The 0-indexed byte offset of the position relative to the beginning of the source.
*)start_of_line : int;
The 0-indexed byte offset pointing to the start of the line that contains the position.
*)line_num : int;
The 1-indexed line number of the line that contains the position.
*)}
The type of positions; this is isomorphic to Lexing.position
, but with arguably better field names.
An auxiliary type to package data with an optional range.
make (beginning, ending)
builds the range [beginning, ending)
(not including the byte at the ending position) from a pair of positions beginning
and ending
. A range is empty if its beginning and ending positions are the same.
eof pos
builds a special range referring to the end of the source. The input pos
must be pointing at the end position; for example, if the position referring to a string source, pos.offset
should be the length of the string.
view range
returns a view of the range.
val begin_line_num : t -> int
begin_line_num range
returns the 1-indexed line number of the beginning position.
val end_line_num : t -> int
end_line_num range
returns the 1-indexed line number of the ending position.
val begin_offset : t -> int
begin_offset range
returns the 0-indexed offset of the (inclusive) beginning position.
val end_offset : t -> int
end_offset range
returns the 0-indexed offset of the (exclusive) ending position.
val title : source -> string option
title src
gets the title of a string source or the path of a file source, or None
if it does not exist.
val of_lex_position : ?source:source -> Lexing.position -> position
of_lex_position pos
converts an OCaml lexer position pos
of type Lexing.position
into a position
. The input pos
must be byte-indexed. (Therefore, the OCaml tool ocamllex
is compatible, but the OCaml library sedlex
is not because it uses Unicode code points.)
val of_lex_range : ?source:source -> (Lexing.position * Lexing.position) -> t
of_lex_range (begining, ending)
takes a pair of OCaml lexer positions and creates a range. It is make (of_lex_position begining, of_lex_position ending)
.
val of_lexbuf : ?source:source -> Lexing.lexbuf -> t
of_lexbuf lexbuf
constructs a range from the current lexeme that lexbuf
points to. It is of_lex_range (Lexing.lexeme_start_p lexbuf, Lexing.lexeme_end_p lexbuf)
.
val locate_lex :
?source:source ->
(Lexing.position * Lexing.position) ->
'a ->
'a located
locate_lex ps v
is a helper function to create a value annotated with a range. It is locate (Some (of_lex_range ps)) v
and is designed to work with the OCaml parser generator Menhir. You can add the following code to your Menhir grammar to generate annotated data:
%inline locate(X): | e = X { Asai.Range.locate_lex $loc e }
Ugly printer for debugging
val dump_source : Format.formatter -> source -> unit
val dump_position : Format.formatter -> position -> unit
val dump : Format.formatter -> t -> unit