package parsexp

  1. Overview
  2. Docs

Compact set of positions

type t

A t value represent a sequence of positions. The focus is on small memory footprint.

Given a s-expression and a sequence of positions, one can reconstruct the location of every sub s-expression. This is used to report location informations without having to annotate every node in the s-expression during parsing.

The s-expression parser saves the positions of each opening and closing parentheses as well as the positions of the first and last character of each atom.

Note that a t can hold the same given positions no more than twice. The parser stores the same position twice for non-quoted single character atoms.

type pos = {
  1. line : Base.int;
    (*

    Line number. The first line has number 1.

    *)
  2. col : Base.int;
    (*

    Column number. The first column has number 0.

    *)
  3. offset : Base.int;
    (*

    Number of bytes from the beginning of the input. The first byte has offset 0.

    *)
}

Represent a position in the input

include sig ... end
val sexp_of_pos : pos -> Sexplib.Sexp.t
val compare_pos : pos -> pos -> Base.int
val beginning_of_file : pos
type range = {
  1. start_pos : pos;
  2. end_pos : pos;
}

Range of positions, as reported in error messages. We follow the lexing conventions of OCaml, i.e. start_pos points to the first character and end_pos points to the position just after the last character.

This allow for instance to represent empty ranges with start_pos = end_pos.

include sig ... end
val sexp_of_range : range -> Sexplib.Sexp.t
val compare_range : range -> range -> Base.int
val make_range_incl : start_pos:pos -> last_pos:pos -> range

Make a range from two positions where both positions are inclusive, i.e. start_pos points to the first character and end_pos points to the last one. The character at last_pos is assumed to not be a newline character.

module Builder : sig ... end
val to_list : t -> pos Base.list

Build the list of all positions in t.

val to_array : t -> pos Base.array

Build the array of all positions in t.

val find : t -> Base.int -> Base.int -> range

find t start stop returns the range of positions starting at position with index start in t and ending at position with index stop.

find t i j is the same as:

let a = to_array t in
make_range_incl ~start_pos:a.(i) ~last_pos:a.(j)

but more efficient.

val find_sub_sexp_phys : t -> Base.Sexp.t -> sub:Base.Sexp.t -> range Base.option

find_sub_sexp_phys t sexp ~sub looks for sub in sexp and return its location, assuming t is the sequence of positions associated with sexp.

Comparison is done using physical equality.

val find_sub_sexp_in_list_phys : t -> Base.Sexp.t Base.list -> sub:Base.Sexp.t -> range Base.option
val memory_footprint_in_bytes : t -> Base.int

Returns how much memory is used by t

OCaml

Innovation. Community. Security.