forked from andrejbauer/spartan-type-theory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocation.ml
29 lines (22 loc) · 1011 Bytes
/
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
type location =
| Location of Lexing.position * Lexing.position (** delimited location *)
| Nowhere (** no location *)
type 'a t = { data : 'a ; loc : location }
let nowhere = Nowhere
let make loc1 loc2 = Location (loc1, loc2)
let of_lex lex =
Location (Lexing.lexeme_start_p lex, Lexing.lexeme_end_p lex)
let locate ?(loc=Nowhere) x = { data = x; loc = loc }
let print loc ppf =
match loc with
| Nowhere ->
Format.fprintf ppf "unknown location"
| Location (begin_pos, end_pos) ->
let begin_char = begin_pos.Lexing.pos_cnum - begin_pos.Lexing.pos_bol in
let end_char = end_pos.Lexing.pos_cnum - begin_pos.Lexing.pos_bol in
let begin_line = begin_pos.Lexing.pos_lnum in
let filename = begin_pos.Lexing.pos_fname in
if String.length filename != 0 then
Format.fprintf ppf "file %S, line %d, charaters %d-%d" filename begin_line begin_char end_char
else
Format.fprintf ppf "line %d, characters %d-%d" (begin_line - 1) begin_char end_char