location.ml 2.74 KB
Newer Older
1
type loc = int * int
2
type source = [ `None | `File of string | `Stream | `String of string ]
3
type viewport = [ `Html | `Text ]
4

5
exception Location of loc * exn
6
exception Generic of string
7

8
9
10
let raise_generic s = raise (Generic s)
let raise_loc_generic loc s = raise (Location (loc, Generic s))

11
let noloc = (-1,-1)
12

13
let source = ref `None
14
15
let set_source s = source := s

16
17
18
let viewport = ref `Text
let set_viewport v = viewport := v

19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
33
34

let print_loc ppf (i,j) =  
35
36
37
38
39
40
41
42
43
44
45
46
47
  match !source with
    | `None -> Format.fprintf ppf "somewhere (no source defined !)"
    | `Stream | `String _ ->
	Format.fprintf ppf "at chars %i-%i" i j
    | `File fn ->
	let (l1,c1) = get_line_number fn i
	and (l2,c2) = get_line_number fn j in
	if l1 = l2 then
	  Format.fprintf ppf "at line %i (chars %i-%i)"
	    l1 c1 c2
	else
	  Format.fprintf ppf "at lines %i (char %i) - %i (char %i)"
	    l1 c1 l2 c2
48

49
let extr s i j =
50
51
  Netencoding.Html.encode_from_latin1 
    (String.sub s i (j - i))
52
53
54
55
56

let dump_loc ppf (i,j) =
  match (!source, !viewport) with
    | (`String s, `Html) ->
	if (i < 0) then
57
	  Format.fprintf ppf "<b>GHOST LOCATION</b>@\n"
58
	else
59
	  Format.fprintf ppf "<i>%s</i>@." (extr s i j)
60
61
62
    | _ -> ()

let rec beg_of_line s i =
63
64
  if (i = 0) || (s.[i-1] = '\n') || (s.[i-1] = '\r')
  then i else beg_of_line s (i - 1)
65
66

let rec end_of_line s i =
67
68
  if (i = String.length s) || (s.[i] = '\n') || (s.[i] = '\r')
  then i else end_of_line s (i + 1)
69
70
71
72

let html_hilight ppf (i,j) =
  match (!source, !viewport) with
    | `String s, `Html ->
73
74
75
	if (i < 0) then
	  Format.fprintf ppf "<b>GHOST LOCATION</b>@\n"
	else
76
77
78
	let i0 = beg_of_line s i in
	let j0 = end_of_line s j in
	Format.fprintf ppf 
79
	  "<i>%s<font color=red><b>%s</b></font>%s</i>@." 
80
81
82
83
84
85
	    (extr s i0 i)
	    (extr s i j)
	    (extr s j j0)
    | _ -> ()
	

86
87
88
89
90
type 'a located = { loc : loc; descr : 'a }

type expr = A | B of expr located

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


93
let protect ppf f =
94
95
  match !viewport with
    | `Html ->
96
97
98
99
100
101
	let b = Buffer.create 63 in
	let ppf' = Format.formatter_of_buffer b in
	f ppf';
	Format.pp_print_flush ppf' ();
	let s = Buffer.contents b in
	let s = Netencoding.Html.encode_from_latin1 s in
102
	Format.fprintf ppf "@[%s@]" s
103
    | _ -> f ppf
104
105
106
107
108
109
110
111
112
113
114



let protected = ref false
let set_protected p = protected := p
let is_protected () = !protected

let protect_op op =
  if (!protected) then
    raise 
      (Generic (op ^ ": operation not authorized in the web prototype"))