Source file Parser_location.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
let to_ppxlib_location ?(loc_ghost = false) (start_pos : Lexing.position)
(end_pos : Lexing.position) : Ppxlib.location =
{ loc_start = start_pos; loc_end = end_pos; loc_ghost }
let print_location label ({ loc_start; loc_end; _ } : Ppxlib.location) =
let loc_start_column = loc_start.pos_cnum - loc_start.pos_bol in
let loc_end_column = loc_end.pos_cnum - loc_end.pos_bol in
Printf.sprintf "%s start %d %d %d (L:%d c:%d) end %d %d %d (L:%d c:%d)" label
loc_start.pos_lnum loc_start.pos_bol loc_start.pos_cnum loc_start.pos_lnum
loc_start_column loc_end.pos_lnum loc_end.pos_bol loc_end.pos_cnum
loc_end.pos_lnum loc_end_column
let intersection (loc1 : Ppxlib.location) (loc2 : Ppxlib.location) :
Ppxlib.location =
let start_pos =
Lexing.
{
pos_fname = loc1.loc_start.pos_fname;
pos_lnum = loc1.loc_start.pos_lnum + loc2.loc_start.pos_lnum - 1;
pos_bol = loc1.loc_start.pos_bol + loc2.loc_start.pos_bol;
pos_cnum = loc1.loc_start.pos_cnum + loc2.loc_start.pos_cnum;
}
in
let end_pos =
Lexing.
{
pos_fname = loc1.loc_end.pos_fname;
pos_lnum = loc1.loc_start.pos_lnum + loc2.loc_end.pos_lnum - 1;
pos_bol = loc1.loc_start.pos_bol + loc2.loc_end.pos_bol;
pos_cnum = loc1.loc_start.pos_cnum + loc2.loc_end.pos_cnum;
}
in
{
Ppxlib.Location.loc_start = start_pos;
loc_end = end_pos;
loc_ghost = false;
}
let update_loc_with_delimiter (loc : Ppxlib.location) delimiter :
Ppxlib.location =
let offset =
match delimiter with None -> 0 | Some s -> String.length s + 1
in
let loc_start =
{ loc.loc_start with pos_cnum = loc.loc_start.pos_cnum + offset }
and loc_end = { loc.loc_end with pos_cnum = loc.loc_end.pos_cnum + offset } in
{ loc_start; loc_end; loc_ghost = false }
let update_pos_lnum (a : Ppxlib.location) (b : Ppxlib.location) =
let loc_start =
{
a.loc_start with
pos_lnum = a.loc_start.pos_lnum + b.loc_start.pos_lnum - 1;
}
in
let loc_end =
{
a.loc_end with
pos_lnum = a.loc_end.pos_lnum + b.loc_start.pos_lnum - 1;
}
in
{ a with loc_start; loc_end }