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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
|
{
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Maxence Guesdon, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 2001 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(** Generation of html code to display OCaml code. *)
open Lexing
type error =
| Illegal_character of char
| Unterminated_comment
| Unterminated_string
| Keyword_as_label of string
exception Error of error * int * int
let base_escape_strings = [
("&", "&") ;
("<", "<") ;
(">", ">") ;
]
let prelike_escape_strings = [
(" ", " ") ;
("\t", " ") ;
("\n", "<br>\n")
]
let pre = ref false
let fmt = ref Format.str_formatter
(** Escape the strings which would clash with html syntax,
and some other strings if we want to get a PRE style outside of
<pre> </pre>.*)
let escape s =
let escape_strings =
if !pre then
base_escape_strings
else
base_escape_strings @ prelike_escape_strings in
List.fold_left
(fun acc -> fun (s, s2) -> Str.global_replace (Str.regexp s) s2 acc)
s
escape_strings
(** Escape the strings which would clash with html syntax. *)
let escape_base s =
List.fold_left
(fun acc -> fun (s, s2) -> Str.global_replace (Str.regexp s) s2 acc)
s
base_escape_strings
(** The output functions *)
let print ?(esc=true) s =
Format.pp_print_string !fmt (if esc then escape s else s)
let print_class ?(esc=true) cl s =
print ~esc: false ("<span class=\""^cl^"\">"^
(if esc then escape s else s)^
"</span>")
(** The table of keywords with colors *)
let create_hashtable size init =
let tbl = Hashtbl.create size in
List.iter (fun (key, data) -> Hashtbl.add tbl key data) init;
tbl
(** The function used to return html code for the given comment body. *)
let html_of_comment = ref
(fun (_ : string) -> "<b>Odoc_ocamlhtml.html_of_comment not initialized</b>")
let keyword_table =
create_hashtable 149 [
"and", "keyword" ;
"as", "keyword" ;
"assert", "keyword" ;
"begin", "keyword" ;
"class", "keyword" ;
"constraint", "keyword" ;
"do", "keyword" ;
"done", "keyword" ;
"downto", "keyword" ;
"else", "keyword" ;
"end", "keyword" ;
"exception", "keyword" ;
"external", "keyword" ;
"false", "keyword" ;
"for", "keyword" ;
"fun", "keyword" ;
"function", "keyword" ;
"functor", "keyword" ;
"if", "keyword" ;
"in", "keyword" ;
"include", "keyword" ;
"inherit", "keyword" ;
"initializer", "keyword" ;
"lazy", "keyword" ;
"let", "keyword" ;
"match", "keyword" ;
"method", "keyword" ;
"module", "keyword" ;
"mutable", "keyword" ;
"new", "keyword" ;
"object", "keyword" ;
"of", "keyword" ;
"open", "keyword" ;
"or", "keyword" ;
"parser", "keyword" ;
"private", "keyword" ;
"rec", "keyword" ;
"sig", "keyword" ;
"struct", "keyword" ;
"then", "keyword" ;
"to", "keyword" ;
"true", "keyword" ;
"try", "keyword" ;
"type", "keyword" ;
"val", "keyword" ;
"virtual", "keyword" ;
"when", "keyword" ;
"while", "keyword" ;
"with", "keyword" ;
"mod", "keyword" ;
"land", "keyword" ;
"lor", "keyword" ;
"lxor", "keyword" ;
"lsl", "keyword" ;
"lsr", "keyword" ;
"asr", "keyword" ;
]
let kwsign_class = "keywordsign"
let constructor_class = "constructor"
let comment_class = "comment"
let string_class = "string"
let code_class = "code"
(** To buffer and print comments *)
let margin = ref 0
let comment_buffer = Buffer.create 32
let reset_comment_buffer () = Buffer.reset comment_buffer
let store_comment_char = Buffer.add_char comment_buffer
let make_margin () =
let rec iter n =
if n <= 0 then ""
else " "^(iter (n-1))
in
iter !margin
let print_comment () =
let s = Buffer.contents comment_buffer in
let len = String.length s in
let code =
if len < 1 then
"<span class=\""^comment_class^"\">(*"^(escape s)^"*)</span>"
else
match s.[0] with
'*' ->
(
try
let html = !html_of_comment (String.sub s 1 (len-1)) in
"</code><table><tr><td>"^(make_margin ())^"</td><td>"^
"<span class=\""^comment_class^"\">"^
"(**"^html^"*)"^
"</span></td></tr></table><code class=\""^code_class^"\">"
with
e ->
prerr_endline (Printexc.to_string e);
"<span class=\""^comment_class^"\">(*"^(escape s)^"*)</span>"
)
| _ ->
"<span class=\""^comment_class^"\">(*"^(escape s)^"*)</span>"
in
print ~esc: false code
(** To buffer string literals *)
let string_buffer = Buffer.create 32
let reset_string_buffer () = Buffer.reset string_buffer
let store_string_char = Buffer.add_char string_buffer
let get_stored_string () =
Buffer.contents string_buffer
(** To store the position of the beginning of a string and comment *)
let string_start_pos = ref 0
let comment_start_pos = ref []
(** Normalizing utf-8 *)
let normalize raw_name =
(* we are printing documentation, it is too late to be strict *)
match Misc.Utf8_lexeme.normalize raw_name with
| Error s -> s
| Ok name -> name
}
let blank = [' ' '\010' '\013' '\009' '\012']
let lowercase = ['a'-'z' '_']
let uppercase = ['A'-'Z']
let identchar = ['A'-'Z' 'a'-'z' '_' '\'' '0'-'9']
let utf8 = ['\192'-'\255'] ['\128'-'\191']*
let identstart_ext = uppercase | lowercase | utf8
let identchar_ext = identchar | utf8
let ident_ext = identstart_ext identchar_ext*
let symbolchar =
['!' '$' '%' '&' '*' '+' '-' '.' '/' ':' '<' '=' '>' '?' '@' '^' '|' '~']
let decimal_literal = ['0'-'9']+
let hex_literal = '0' ['x' 'X'] ['0'-'9' 'A'-'F' 'a'-'f']+
let oct_literal = '0' ['o' 'O'] ['0'-'7']+
let bin_literal = '0' ['b' 'B'] ['0'-'1']+
let float_literal =
['0'-'9']+ ('.' ['0'-'9']* )? (['e' 'E'] ['+' '-']? ['0'-'9']+)?
rule token = parse
blank
{
let s = Lexing.lexeme lexbuf in
(
match s with
" " -> incr margin
| "\t" -> margin := !margin + 8
| "\n" -> margin := 0
| _ -> ()
);
print s;
token lexbuf
}
| "_"
{ print "_" ; token lexbuf }
| "~" { print "~" ; token lexbuf }
| "~" (ident_ext as raw_id ) ':'
{ let s = Lexing.lexeme lexbuf in
let name = normalize raw_id in
if Hashtbl.mem keyword_table name then
raise (Error(Keyword_as_label name, Lexing.lexeme_start lexbuf,
Lexing.lexeme_end lexbuf));
print s ; token lexbuf }
| "?" { print "?" ; token lexbuf }
| "?" (ident_ext as raw_id) ':'
{
let name = normalize raw_id in
if Hashtbl.mem keyword_table name then
raise (Error(Keyword_as_label name, Lexing.lexeme_start lexbuf,
Lexing.lexeme_end lexbuf));
print "?"; print name ; print ":"; token lexbuf }
| (ident_ext as raw_id)
{ let s = normalize raw_id in
if Misc.Utf8_lexeme.is_capitalized s then
(print_class constructor_class (Lexing.lexeme lexbuf);
token lexbuf)
else try
let cl = Hashtbl.find keyword_table s in
(print_class cl s ; token lexbuf )
with Not_found ->
(print s ; token lexbuf )}
| decimal_literal | hex_literal | oct_literal | bin_literal
{ print (Lexing.lexeme lexbuf) ; token lexbuf }
| float_literal
{ print (Lexing.lexeme lexbuf) ; token lexbuf }
| "\""
{ reset_string_buffer();
let string_start = Lexing.lexeme_start lexbuf in
string_start_pos := string_start;
string lexbuf;
lexbuf.Lexing.lex_start_pos <-
string_start - lexbuf.Lexing.lex_abs_pos;
print_class string_class ("\""^(get_stored_string())^"\"") ;
token lexbuf }
| "'" [^ '\\' '\''] "'"
{ print_class string_class (Lexing.lexeme lexbuf) ;
token lexbuf }
| "'" '\\' ['\\' '\'' 'n' 't' 'b' 'r'] "'"
{ print_class string_class (Lexing.lexeme lexbuf ) ;
token lexbuf }
| "'" '\\' ['0'-'9'] ['0'-'9'] ['0'-'9'] "'"
{ print_class string_class (Lexing.lexeme lexbuf ) ;
token lexbuf }
| "(*"
{
reset_comment_buffer ();
comment_start_pos := [Lexing.lexeme_start lexbuf];
comment lexbuf ;
print_comment ();
token lexbuf }
| "(*)"
{ reset_comment_buffer ();
comment_start_pos := [Lexing.lexeme_start lexbuf];
comment lexbuf ;
print_comment ();
token lexbuf
}
| "*)"
{ lexbuf.Lexing.lex_curr_pos <- lexbuf.Lexing.lex_curr_pos - 1;
lexbuf.Lexing.lex_curr_p <-
{ lexbuf.Lexing.lex_curr_p with
pos_cnum = lexbuf.Lexing.lex_curr_p.pos_cnum - 1
} ;
print (Lexing.lexeme lexbuf) ;
token lexbuf
}
| "#" [' ' '\t']* ['0'-'9']+ [^ '\n' '\r'] * ('\n' | '\r' | "\r\n")
(* # linenum ... *)
{
print (Lexing.lexeme lexbuf);
token lexbuf
}
| "#" { print_class kwsign_class (Lexing.lexeme lexbuf) ; token lexbuf }
| "&" { print_class kwsign_class (Lexing.lexeme lexbuf) ; token lexbuf }
| "&&" { print_class kwsign_class (Lexing.lexeme lexbuf) ; token lexbuf }
| "`" { print_class kwsign_class (Lexing.lexeme lexbuf) ; token lexbuf }
| "'" { print_class kwsign_class (Lexing.lexeme lexbuf) ; token lexbuf }
| "(" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| ")" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "*" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "," { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "??" { print_class kwsign_class (Lexing.lexeme lexbuf) ; token lexbuf }
| "->" { print_class kwsign_class (Lexing.lexeme lexbuf) ; token lexbuf }
| "." { print (Lexing.lexeme lexbuf) ; token lexbuf }
| ".." { print (Lexing.lexeme lexbuf) ; token lexbuf }
| ":" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "::" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| ":=" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| ":>" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| ";" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| ";;" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "<" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "<-" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "=" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "[" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "[|" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "[<" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "]" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "{" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "{<" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "|" { print_class kwsign_class (Lexing.lexeme lexbuf) ; token lexbuf }
| "||" { print_class kwsign_class (Lexing.lexeme lexbuf) ; token lexbuf }
| "|]" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| ">" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| ">]" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "}" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| ">}" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "!=" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "+" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "-" { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "-." { print (Lexing.lexeme lexbuf) ; token lexbuf }
| "!" symbolchar *
{ print (Lexing.lexeme lexbuf) ; token lexbuf }
| ['~' '?'] symbolchar +
{ print_class kwsign_class (Lexing.lexeme lexbuf) ; token lexbuf }
| ['=' '<' '>' '|' '&' '$'] symbolchar *
{ print (Lexing.lexeme lexbuf) ; token lexbuf }
| ['@' '^'] symbolchar *
{ print (Lexing.lexeme lexbuf) ; token lexbuf }
| ['+' '-'] symbolchar *
{ print (Lexing.lexeme lexbuf) ; token lexbuf }
| "**" symbolchar *
{ print (Lexing.lexeme lexbuf) ; token lexbuf }
| ['*' '/' '%'] symbolchar *
{ print (Lexing.lexeme lexbuf) ; token lexbuf }
| eof { () }
| _
{ raise (Error(Illegal_character ((Lexing.lexeme lexbuf).[0]),
Lexing.lexeme_start lexbuf, Lexing.lexeme_end lexbuf)) }
and comment = parse
"(*"
{ comment_start_pos := Lexing.lexeme_start lexbuf :: !comment_start_pos;
store_comment_char '(';
store_comment_char '*';
comment lexbuf;
}
| "*)"
{ match !comment_start_pos with
| [] -> assert false
| [_] -> comment_start_pos := []
| _ :: l ->
store_comment_char '*';
store_comment_char ')';
comment_start_pos := l;
comment lexbuf;
}
(* These filters are useless
| "\""
{ reset_string_buffer();
string_start_pos := Lexing.lexeme_start lexbuf;
store_comment_char '"';
begin
try string lexbuf; add_comment_string ((get_stored_string()^"\""))
with Error (Unterminated_string, _, _) ->
let st = List.hd !comment_start_pos in
raise (Error (Unterminated_string_in_comment, st, st + 2))
end;
comment lexbuf }
| "'" [^ '\\' '\''] "'"
{
store_comment_char '\'';
store_comment_char (Lexing.lexeme_char lexbuf 1);
store_comment_char '\'';
comment lexbuf }
| "'\\" ['\\' '\'' 'n' 't' 'b' 'r'] "'"
{
store_comment_char '\'';
store_comment_char '\\';
store_comment_char(char_for_backslash(Lexing.lexeme_char lexbuf 1)) ;
store_comment_char '\'';
comment lexbuf }
| "\\" ['0'-'9'] ['0'-'9'] ['0'-'9']
{
store_comment_char(char_for_decimal_code lexbuf 1);
comment lexbuf }
| "\\x" ['0'-'9' 'A'-'Z' 'a'-'z' ] ['0'-'9' 'A'-'Z' 'a'-'z']
{
store_comment_char(char_for_hexa_code lexbuf 2);
string lexbuf }
| "''"
{
store_comment_char '\'';
store_comment_char '\'';
comment lexbuf }
*)
| eof
{ let st = List.hd !comment_start_pos in
raise (Error (Unterminated_comment, st, st + 2));
}
| _
{ store_comment_char(Lexing.lexeme_char lexbuf 0);
comment lexbuf }
and string = parse
'"'
{ () }
| '\\' ("\010" | "\013" | "\013\010") [' ' '\009'] *
{ string lexbuf }
| '\\' ['\\' '"' 'n' 't' 'b' 'r' ]
{ Buffer.add_string string_buffer (Lexing.lexeme lexbuf) ;
string lexbuf }
| '\\' ['0'-'9'] ['0'-'9'] ['0'-'9']
{
Buffer.add_string string_buffer (Lexing.lexeme lexbuf) ;
string lexbuf
}
| '\\' 'x' ['0'-'9' 'A'-'Z' 'a'-'z' ] ['0'-'9' 'A'-'Z' 'a'-'z']
{ Buffer.add_string string_buffer (Lexing.lexeme lexbuf) ;
string lexbuf }
| eof
{ raise (Error (Unterminated_string,
!string_start_pos, !string_start_pos+1)) }
| _
{ store_string_char(Lexing.lexeme_char lexbuf 0);
string lexbuf }
{
let html_of_code b ?(with_pre=true) code =
let old_pre = !pre in
let old_margin = !margin in
let old_comment_buffer = Buffer.contents comment_buffer in
let old_string_buffer = Buffer.contents string_buffer in
let buf = Buffer.create 256 in
let old_fmt = !fmt in
fmt := Format.formatter_of_buffer buf ;
pre := with_pre;
margin := 0;
let start = "<code class=\""^code_class^"\">" in
let ending = "</code>" in
let html =
(
try
print ~esc: false start ;
let lexbuf = Lexing.from_string code in
token lexbuf;
print ~esc: false ending ;
Format.pp_print_flush !fmt () ;
Buffer.contents buf
with
_ ->
(* flush str_formatter because we already output
something in it *)
Format.pp_print_flush !fmt () ;
start^code^ending
)
in
pre := old_pre;
margin := old_margin ;
Buffer.reset comment_buffer;
Buffer.add_string comment_buffer old_comment_buffer ;
Buffer.reset string_buffer;
Buffer.add_string string_buffer old_string_buffer ;
fmt := old_fmt ;
Buffer.add_string b html
}
|