location.ml 615 Bytes
Newer Older
1
2
3
4
type loc = int * int
exception Location of loc * exn

let noloc = (-1,-1)
5

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
let get_line_number src i =
  let ic = open_in_bin src in
  let rec aux pos line start =
    if (pos >= i) 
    then (line,i - start) 
    else
      match input_char ic with
	| '\r' when pos = start -> aux (pos + 1) line (pos + 1)
	| '\r' | '\n' -> aux (pos + 1) (line + 1) (pos + 1)
	| _ -> aux (pos + 1) line start
  in
  let r = aux 0 1 0 in
  close_in ic;
  r
    

22
23
24
25
26
27
28
type 'a located = { loc : loc; descr : 'a }

type expr = A | B of expr located

let rec recurs f x = f (recurs f) x.loc x.descr

let mk loc x = { loc = loc; descr = x }