/[svn]/parser/location.ml
ViewVC logotype

Contents of /parser/location.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 672 - (show annotations)
Tue Jul 10 17:53:24 2007 UTC (5 years, 11 months ago) by abate
File size: 4372 byte(s)
[r2003-09-23 19:41:35 by cvscast] Constantes structurees + suite nettoyage

Original author: cvscast
Date: 2003-09-23 19:41:36+00:00
1 (* TODO: handle encodings of the input for pretty printing
2 fragments of code *)
3
4 type source = [ `None | `File of string | `Stream | `String of string ]
5 type loc = source * int * int
6 type precise = [ `Full | `Char of int ]
7
8 type viewport = [ `Html | `Text ]
9
10 let merge_loc ((s1,i1,j1) as loc1) ((s2,i2,j2) as loc2) =
11 if s1 = s2 then
12 if i1 = -1 then loc2 else if i2 = -1 then loc1 else
13 (s1, min i1 i2, max j1 j2)
14 else loc1
15
16 let source = ref `None
17 let source_stack = ref []
18 let push_source s = source_stack := !source :: !source_stack; source := s
19 let pop_source () =
20 match !source_stack with
21 | [] -> assert false
22 | s::rem -> source_stack := rem; source := s
23
24 let current_dir () =
25 match !source with
26 | `File s -> Filename.dirname s
27 | _ -> ""
28
29 let warning_ppf = ref Format.std_formatter
30
31 exception Location of loc * precise * exn
32 exception Generic of string
33
34 let raise_loc i j exn = raise (Location ((!source,i,j),`Full,exn))
35 let raise_generic s = raise (Generic s)
36 let raise_loc_generic loc s = raise (Location (loc, `Full, Generic s))
37
38 let noloc = (`None,-1,-1)
39 let nopos = (-1,-1)
40
41 let viewport = ref `Text
42 let set_viewport v = viewport := v
43
44 (* Note: this is incorrect. Directives #utf8,... should
45 not be recognized inside comments and strings !
46 The clean solution is probably to have the real lexer
47 count the lines. *)
48
49 let get_line_number src i =
50 let enc = ref Ulexing.Latin1 in
51 let ic = open_in_bin src in
52 let lb = Ulexing.from_var_enc_channel enc ic in
53 let rec count line start = lexer
54 | '\n' | "\n\r" | '\r' ->
55 aux (line + 1) (Ulexing.lexeme_end lb)
56 | "#utf8" ->
57 enc := Ulexing.Utf8;
58 aux line start
59 | "#ascii" ->
60 enc := Ulexing.Ascii;
61 aux line start
62 | "#latin1" ->
63 enc := Ulexing.Latin1;
64 aux line start
65 | _ ->
66 aux line start
67 and aux line start =
68 if (Ulexing.lexeme_start lb >= i) then (line, i - start)
69 else count line start lb
70 in
71 let r = aux 1 0 in
72 close_in ic;
73 r
74
75 let print_precise ppf = function
76 | `Full -> ()
77 | `Char i -> Format.fprintf ppf " (char # %i)" i
78
79 let print_loc ppf ((src,i,j),w) =
80 match src with
81 | `None -> Format.fprintf ppf "somewhere (no source defined !)"
82 | `Stream | `String _ ->
83 Format.fprintf ppf "at chars %i-%i%a" i j print_precise w
84 | `File fn ->
85 let (l1,c1) = get_line_number fn i
86 and (l2,c2) = get_line_number fn j in
87 if l1 = l2 then
88 Format.fprintf ppf "at line %i (chars %i-%i)%a, file %s"
89 l1 c1 c2 print_precise w fn
90 else
91 Format.fprintf ppf "at lines %i (char %i) - %i (char %i)%a, file %s"
92 l1 c1 l2 c2 print_precise w fn
93
94 let extr s i j =
95 try
96 Netencoding.Html.encode_from_latin1
97 (String.sub s i (j - i))
98 with e -> failwith "Location.extr"
99
100 let dump_loc ppf ((src,i,j),w) =
101 match (src, !viewport) with
102 | (`String s, `Html) ->
103 if (i < 0) then
104 Format.fprintf ppf "<b>GHOST LOCATION</b>@\n"
105 else
106 Format.fprintf ppf "<i>%s</i>@." (extr s i j)
107 | _ -> ()
108
109 let rec beg_of_line s i =
110 if (i <= 0) || (s.[i-1] = '\n') || (s.[i-1] = '\r')
111 then i else beg_of_line s (i - 1)
112
113 let rec end_of_line s i =
114 if (i >= String.length s) || (s.[i] = '\n') || (s.[i] = '\r')
115 then i else end_of_line s (i + 1)
116
117 let html_hilight ppf ((src,i,j),w) =
118 match (src, !viewport) with
119 | `String s, `Html ->
120 (try
121 if (i < 0) then
122 Format.fprintf ppf "<b>GHOST LOCATION</b>@\n"
123 else
124 let i0 = beg_of_line s i in
125 let j0 = end_of_line s j in
126 Format.fprintf ppf
127 "<i>%s<font color=\"red\"><b>%s</b></font>%s</i>@."
128 (extr s i0 i)
129 (extr s i j)
130 (extr s j j0)
131 with e -> failwith "html_hilight")
132 | _ -> ()
133
134
135 type 'a located = { loc : loc; descr : 'a }
136
137 let mk (i,j) x = { loc = (!source,i,j); descr = x }
138 let mk_loc loc x = { loc = loc; descr = x }
139 let mknoloc x = { loc = noloc; descr = x }
140 let loc_of_pos (i,j) = (!source,i,j)
141
142 let protect ppf f =
143 match !viewport with
144 | `Html ->
145 let b = Buffer.create 63 in
146 let ppf' = Format.formatter_of_buffer b in
147 f ppf';
148 Format.pp_print_flush ppf' ();
149 let s = Buffer.contents b in
150 let s = Netencoding.Html.encode_from_latin1 s in
151 Format.fprintf ppf "@[%s@]" s
152 | _ -> f ppf
153
154
155
156 let protected = ref false
157 let set_protected p = protected := p
158 let is_protected () = !protected
159
160 let protect_op op =
161 if (!protected) then
162 raise
163 (Generic (op ^ ": operation not authorized in the web prototype"))

CVS Admin">CVS Admin
ViewVC Help
Powered by ViewVC 1.1.5