Module Syncom.Location

Character position ranges in source files.

A parser is typically responsible for constructing locations using positions generated by the lexer.

type t

The type of range over character positions in a source file.

Constructors

val ghost : t

ghost is the null location used for AST nodes having no correspondence to a source file.

val initial : string -> t

initial filename is the location at the beginning of the file with name filename.

val make : filename:string -> start_position:Position.t -> stop_position:Position.t -> t

make ~filename ~start_position ~stop_position is the non-ghost location starting at the character position start_position inclusively and ending at the character position stop_position exclusively in the file at filename.

val join : t -> t -> t

join l1 l2 is the smallest location that contains l1 and l2.

It is assumed that filename l1 = filename l2.

Ghost locations are left and right identities of join, meaning that:

  • If is_ghost l1, then join l1 l2 = l2.
  • If is_ghost l2, then join l1 l2 = l1.
val join_all : t -> t Support.List.t -> t

join_all init ls is each location in ls joined together, additionally with init.

val join_all_contramap : ('a -> t) -> t -> 'a Support.List.t -> t

join_all_contramap f init xs is each location contramapped by f from xs. joined together, additionally with init.

If xs = [x1; x2; ...; xn], then join_all_contramap f init xs = join_all init [f x1; f x2; ...; f xn].

val join_all1 : t Support.List1.t -> t
val join_all1_contramap : ('a -> t) -> 'a Support.List1.t -> t
val join_all2_contramap : ('a -> t) -> 'a Support.List2.t -> t
val start_position_as_location : t -> t
val stop_position_as_location : t -> t
val set_filename : string -> t -> t
val set_start : Position.t -> t -> t
val set_stop : Position.t -> t -> t
val set_start_line : int -> t -> t

set_start_line line location is location with its start line set to line. The start position's beginning of line and offset are not modified, so the output location may be invalid. This should only be used with locations that do not point to files, like ghost locations.

val set_stop_line : int -> t -> t

set_stop_line line location is like set_start_line line location but for the location's stop position.

Destructors

val filename : t -> string
val start_position : t -> Position.t
val stop_position : t -> Position.t
val start_offset : t -> int
val stop_offset : t -> int
val start_bol : t -> int
val stop_bol : t -> int
val start_line : t -> int
val stop_line : t -> int
val start_column : t -> int
val stop_column : t -> int
val spanned_offsets : t -> int

spanned_offsets location is stop_offset location - start_offset location.

Predicates and Comparisons

val is_ghost : t -> bool
module Ord_by_start : Support.Ord.ORD with type t = t
val compare_start : t -> t -> int
module Ord_by_stop : Support.Ord.ORD with type t = t
val compare_stop : t -> t -> int

Printing

val print : Support.Format.formatter -> t -> unit

Prints `<filename>, line <n>, column <n>'. Suitable for proper errors.

val pp : Support.Format.formatter -> t -> unit
val print_short : Support.Format.formatter -> t -> unit

Prints `line <n>, column <n>'. More suitable for debug prints.

val print_span_short : Support.Format.formatter -> t -> unit

Prints `<n>.<n> - <n>.<n>' indicating start and stop positions.

Interoperability

val make_from_lexing_positions : filename:string -> start_position:Stdlib.Lexing.position -> stop_position:Stdlib.Lexing.position -> t
val start_to_lexing_position : t -> Stdlib.Lexing.position
val stop_to_lexing_position : t -> Stdlib.Lexing.position
val to_lexing_positions : t -> Stdlib.Lexing.position * Stdlib.Lexing.position

Instances

Structural equality instance.

include Support.Eq.EQ with type t := t

Lexicographical ordering instance by filename, start position and stop position in that sequence.

include Support.Ord.ORD with type t := t
val compare : t -> t -> int

compare a b compares a and b for ordering.

  • compare a b < 0 if a precedes b (denoted a < b),
  • compare a b = 0 if a is equal to b (denoted a = b),
  • compare a b > 0 if a succeeds b (denoted a > b).

This should satisfy the following properties:

  • Comparability: (compare a b <= 0 || compare b a >= 0) = true,
  • Transitivity: if (compare a b <= 0) = true and (compare b c <= 0) = true, then (compare a c <= 0) = true,
  • Reflexivity: (compare a a = 0) = true,
  • Antisymmetry: if (compare a b <= 0) = true and (compare a b >= 0) = true then (compare a b = 0) = true.
val (<) : t -> t -> bool
val (<=) : t -> t -> bool
val (>) : t -> t -> bool
val (>=) : t -> t -> bool
val max : t -> t -> t

max a b is a if a >= b and b otherwise.

val min : t -> t -> t

min a b is a if a <= b and b otherwise.

include Support.Eq.EQ with type t := t
val equal : t -> t -> bool

equal a b is true if and only if a and b are equal. This should satisfy the following properties:

  • Reflexivity: equal a a = true
  • Symmetry: equal a b is equivalent to equal b a
  • Transitivity: if equal a b = true and equal b c = true, then equal a c = true
val (=) : t -> t -> bool

Operator alias of equal.

val (<>) : t -> t -> bool

Negation of operator (=).