-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrange.ml
70 lines (54 loc) · 1.58 KB
/
range.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
63
64
65
66
67
68
69
70
type real = {
file_name : string;
start_line : int;
start_column : int;
last_line : int;
last_column : int;
}
type t =
| Dummy of string
| Real of real
let pp ppf rng =
match rng with
| Dummy(s) ->
Format.printf "(%s)" s
| Real(r) ->
if r.start_line = r.last_line then
Format.fprintf ppf "file '%s', line %d, characters %d-%d"
r.file_name r.start_line r.start_column r.last_column
else
Format.fprintf ppf "file '%s', line %d, character %d to line %d, character %d"
r.file_name r.start_line r.start_column r.last_line r.last_column
let from_positions (posS, posE) =
let fname = posS.Lexing.pos_fname in
let lnum = posS.Lexing.pos_lnum in
let cnumS = posS.Lexing.pos_cnum - posS.Lexing.pos_bol in
let cnumE = posE.Lexing.pos_cnum - posE.Lexing.pos_bol in
Real{
file_name = fname;
start_line = lnum;
start_column = cnumS;
last_line = lnum;
last_column = cnumE;
}
let from_lexbuf lexbuf =
let posS = Lexing.lexeme_start_p lexbuf in
let posE = Lexing.lexeme_end_p lexbuf in
from_positions (posS, posE)
let dummy s = Dummy(s)
let is_dummy = function
| Dummy(_) -> true
| _ -> false
let unite r1 r2 =
match (r1, r2) with
| (Real(_), Dummy(_)) -> r1
| (Dummy(_), Real(_)) -> r2
| (Dummy(s1), Dummy(s2)) -> Dummy(s1 ^ "/" ^ s2)
| (Real(x1), Real(x2)) ->
Real{
file_name = x1.file_name;
start_line = x1.start_line;
start_column = x1.start_column;
last_line = x2.last_line;
last_column = x2.last_column;
}