location.ml 3.73 KB
Newer Older
1
type source = [ `None | `File of string | `Stream | `String of string ]
2
type loc = source * int * int
3
4
type precise = [ `Full | `Char of int ]

5
type viewport = [ `Html | `Text ]
6

7
8
9
10
let merge_loc ((s1,i1,j1) as loc1) ((s2,i2,j2) as loc2) =
  if s1 = s2 then 
    if i1 = -1 then loc2 else if i2 = -1 then loc1 else 
      (s1, min i1 i2, max j1 j2)
11
12
  else loc1

13
14
15
16
17
18
19
20
let source = ref `None
let source_stack = ref []
let push_source s = source_stack := !source :: !source_stack; source := s
let pop_source () = 
  match !source_stack with
    | [] -> assert false
    | s::rem -> source_stack := rem; source := s

21
22
23
24
25
let current_dir () =
  match !source with
    | `File s -> Filename.dirname s
    | _ -> ""

26
27
let warning_ppf = ref Format.std_formatter

28
exception Location of loc * precise * exn
29
exception Generic of string
30

31
let raise_loc i j exn = raise (Location ((!source,i,j),`Full,exn))
32
let raise_generic s = raise (Generic s)
33
let raise_loc_generic loc s = raise (Location (loc, `Full, Generic s))
34

35
36
let noloc = (`None,-1,-1)
let nopos = (-1,-1)
37

38
39
40
let viewport = ref `Text
let set_viewport v = viewport := v

41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
55

56
57
58
59
60
let print_precise ppf = function
  | `Full -> ()
  | `Char i -> Format.fprintf ppf " (char # %i)" i

let print_loc ppf ((src,i,j),w) =  
61
  match src with
62
63
    | `None -> Format.fprintf ppf "somewhere (no source defined !)"
    | `Stream | `String _ ->
64
	Format.fprintf ppf "at chars %i-%i%a" i j print_precise w
65
66
67
68
    | `File fn ->
	let (l1,c1) = get_line_number fn i
	and (l2,c2) = get_line_number fn j in
	if l1 = l2 then
69
70
	  Format.fprintf ppf "at line %i (chars %i-%i)%a, file %s"
	    l1 c1 c2 print_precise w fn
71
	else
72
73
	  Format.fprintf ppf "at lines %i (char %i) - %i (char %i)%a, file %s"
	    l1 c1 l2 c2 print_precise w fn
74

75
let extr s i j =
76
77
78
79
  try
    Netencoding.Html.encode_from_latin1 
      (String.sub s i (j - i))
  with e -> failwith "Location.extr"
80

81
let dump_loc ppf ((src,i,j),w) =
82
  match (src, !viewport) with
83
84
    | (`String s, `Html) ->
	if (i < 0) then
85
	  Format.fprintf ppf "<b>GHOST LOCATION</b>@\n"
86
	else
87
	  Format.fprintf ppf "<i>%s</i>@." (extr s i j)
88
89
90
    | _ -> ()

let rec beg_of_line s i =
91
92
  if (i = 0) || (s.[i-1] = '\n') || (s.[i-1] = '\r')
  then i else beg_of_line s (i - 1)
93
94

let rec end_of_line s i =
95
96
  if (i = String.length s) || (s.[i] = '\n') || (s.[i] = '\r')
  then i else end_of_line s (i + 1)
97

98
let html_hilight ppf ((src,i,j),w) =
99
  match (src, !viewport) with
100
    | `String s, `Html ->
101
102
103
	if (i < 0) then
	  Format.fprintf ppf "<b>GHOST LOCATION</b>@\n"
	else
104
105
106
	let i0 = beg_of_line s i in
	let j0 = end_of_line s j in
	Format.fprintf ppf 
107
	  "<i>%s<font color=\"red\"><b>%s</b></font>%s</i>@." 
108
109
110
111
112
113
	    (extr s i0 i)
	    (extr s i j)
	    (extr s j j0)
    | _ -> ()
	

114
115
type 'a located = { loc : loc; descr : 'a }

116
117
118
let mk (i,j) x = { loc = (!source,i,j); descr = x }
let mk_loc loc x = { loc = loc; descr = x }
let mknoloc x = { loc = noloc; descr = x }
119
let loc_of_pos (i,j) = (!source,i,j)
120

121
let protect ppf f =
122
123
  match !viewport with
    | `Html ->
124
125
126
127
128
129
	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
130
	Format.fprintf ppf "@[%s@]" s
131
    | _ -> f ppf
132
133
134
135
136
137
138
139
140
141
142



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"))