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
|
{
(**************************************************************************)
(* *)
(* 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. *)
(* *)
(**************************************************************************)
(** The lexer for special comments. *)
open Lexing
open Odoc_parser
let line_number = ref 0
let string_buffer = Buffer.create 32
(** Reset the buffer *)
let reset_string_buffer () = Buffer.reset string_buffer
(** Add a character to the buffer *)
let add_char_string = Buffer.add_char string_buffer
(** Add a string to the buffer. *)
let add_string = Buffer.add_string string_buffer
let read_string () = Buffer.contents string_buffer
(** The variable which will contain the description string.
Is initialized when we encounter the start of a special comment. *)
let description = ref ""
let blank = "[ \013\009\012]"
(** The nested comments level. *)
let comments_level = ref 0
(** This function returns the given string without the leading and trailing blanks.*)
let remove_blanks s =
let l = Str.split_delim (Str.regexp "\n") s in
let l2 =
let rec iter liste =
match liste with
h :: q ->
let h2 = Str.global_replace (Str.regexp ("^"^blank^"+")) "" h in
if h2 = "" then
(
(* we remove this line and must remove leading blanks of the next one *)
iter q
)
else
(* we don't remove leading blanks in the remaining lines *)
h2 :: q
| _ ->
[]
in iter l
in
let l3 =
let rec iter liste =
match liste with
h :: q ->
let h2 = Str.global_replace (Str.regexp (blank^"+$")) "" h in
if h2 = "" then
(
(* we remove this line and must remove trailing blanks of the next one *)
iter q
)
else
(* we don't remove trailing blanks in the remaining lines *)
h2 :: q
| _ ->
[]
in
List.rev (iter (List.rev l2))
in
String.concat "\n" l3
(** Remove first blank characters of each line of a string, until the first '*' *)
let remove_stars s =
Str.global_replace (Str.regexp ("^"^blank^"*\\*")) "" s
let validate_encoding raw_name =
match Misc.Utf8_lexeme.normalize raw_name with
| Error s -> failwith (Format.asprintf "Invalid encoding %s" s)
| Ok name -> name
let validate_ident raw_name =
let name = validate_encoding raw_name in
match Misc.Utf8_lexeme.validate_identifier name with
| Misc.Utf8_lexeme.Valid -> name
| Misc.Utf8_lexeme.Invalid_character u ->
failwith (Format.asprintf "Invalid character U+%X" (Uchar.to_int u))
| Misc.Utf8_lexeme.Invalid_beginning u ->
failwith (Format.asprintf "Invalid first character U+%X" (Uchar.to_int u))
let validate_exception_uident raw_name =
let name = validate_ident raw_name in
if Misc.Utf8_lexeme.is_capitalized name then name else
failwith (Format.asprintf "Invalid exception name: %s" name)
}
let blank = [ ' ' '\013' '\009' '\012']
let nl_blank = blank | '\010'
let notblank = [^ ' ' '\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 identchar_ext = identchar | utf8
let identstart_ext = lowercase | uppercase | utf8
let ident_ext = identstart_ext identchar_ext*
rule main = parse
[' ' '\013' '\009' '\012'] +
{
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length (Lexing.lexeme lexbuf));
main lexbuf
}
| [ '\010' ]
{
incr line_number;
incr Odoc_comments_global.nb_chars;
main lexbuf
}
| "(**)"
{
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length (Lexing.lexeme lexbuf));
Description ("", None)
}
| "(**"("*"+)")"
{
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length (Lexing.lexeme lexbuf));
main lexbuf
}
| "(***"
{
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length (Lexing.lexeme lexbuf));
incr comments_level;
main lexbuf
}
| "(**"
{
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length (Lexing.lexeme lexbuf));
incr comments_level;
if !comments_level = 1 then
(
reset_string_buffer ();
description := "";
special_comment lexbuf
)
else
main lexbuf
}
| eof
{ EOF }
| "*)"
{
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length (Lexing.lexeme lexbuf));
decr comments_level ;
main lexbuf
}
| "(*"
{
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length (Lexing.lexeme lexbuf));
incr comments_level ;
main lexbuf
}
| _
{
incr Odoc_comments_global.nb_chars;
main lexbuf
}
and special_comment = parse
| "*)"
{
let s = Lexing.lexeme lexbuf in
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length s);
if !comments_level = 1 then
(
(* there is just a description *)
let s2 = read_string () in
let s3 = remove_blanks s2 in
let s4 =
if !Odoc_global.remove_stars then
remove_stars s3
else
s3
in
Description (s4, None)
)
else
(
add_string s;
decr comments_level;
special_comment lexbuf
)
}
| "(*"
{
let s = Lexing.lexeme lexbuf in
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length s);
incr comments_level ;
add_string s;
special_comment lexbuf
}
| "\\@"
{
let s = Lexing.lexeme lexbuf in
let c = (Lexing.lexeme_char lexbuf 1) in
add_char_string c;
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length s);
special_comment lexbuf
}
| "@"lowercase+
{
(* we keep the description before we go further *)
let s = read_string () in
description := remove_blanks s;
reset_string_buffer ();
let len = String.length (Lexing.lexeme lexbuf) in
lexbuf.Lexing.lex_curr_pos <- lexbuf.Lexing.lex_curr_pos - len;
lexbuf.Lexing.lex_curr_p <-
{ lexbuf.Lexing.lex_curr_p with
pos_cnum = lexbuf.Lexing.lex_curr_p.pos_cnum - len
} ;
(* we don't increment the Odoc_comments_global.nb_chars *)
special_comment_part2 lexbuf
}
| _
{
let c = (Lexing.lexeme_char lexbuf 0) in
add_char_string c;
if c = '\010' then incr line_number;
incr Odoc_comments_global.nb_chars;
special_comment lexbuf
}
and special_comment_part2 = parse
| "*)"
{
let s = Lexing.lexeme lexbuf in
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length s);
if !comments_level = 1 then
(* finally we return the description we kept *)
let desc =
if !Odoc_global.remove_stars then
remove_stars !description
else
!description
in
let remain = read_string () in
let remain2 =
if !Odoc_global.remove_stars then
remove_stars remain
else
remain
in
Description (desc, Some remain2)
else
(
add_string s ;
decr comments_level ;
special_comment_part2 lexbuf
)
}
| "(*"
{
let s = Lexing.lexeme lexbuf in
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length s);
add_string s;
incr comments_level ;
special_comment_part2 lexbuf
}
| _
{
let c = (Lexing.lexeme_char lexbuf 0) in
add_char_string c;
if c = '\010' then incr line_number;
incr Odoc_comments_global.nb_chars;
special_comment_part2 lexbuf
}
and elements = parse
| blank+
{
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length (Lexing.lexeme lexbuf));
elements lexbuf
}
| [ '\010' ]
{ incr line_number;
incr Odoc_comments_global.nb_chars;
elements lexbuf }
| "@"
{
raise (Failure (Odoc_messages.should_escape_at_sign))
}
| "@param" nl_blank+ (identchar+ as id) nl_blank+ { T_PARAM id }
| "@param" nl_blank+ (identchar_ext+ as raw_id) nl_blank+ {
let id = validate_ident raw_id in
T_PARAM id
}
| "@param" { failwith "usage: @param id description"}
| "@before" nl_blank+ (notblank+ as v) nl_blank+ {
let v = validate_encoding v in
T_BEFORE v }
| "@before" { failwith "usage: @before version description"}
| "@raise" nl_blank+ (ident_ext ('.' ident_ext)* as exn_path) nl_blank+
{ let raw_path = String.split_on_char '.' exn_path in
let path = List.map validate_exception_uident raw_path in
let id = String.concat "." path in
T_RAISES id }
| "@raise" { failwith "usage: @raise Exception description"}
| "@"lowercase+
{
let s = Lexing.lexeme lexbuf in
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length s);
let s2 = String.sub s 1 ((String.length s) - 1) in
match s2 with
| "author" ->
T_AUTHOR
| "version" ->
T_VERSION
| "see" ->
T_SEE
| "since" ->
T_SINCE
| "deprecated" ->
T_DEPRECATED
| "return" ->
T_RETURN
| s ->
if !Odoc_global.no_custom_tags then
raise (Failure (Odoc_messages.not_a_valid_tag s))
else
T_CUSTOM s
}
| ("\\@" | [^'@'])+
{
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length (Lexing.lexeme lexbuf));
let s = Lexing.lexeme lexbuf in
let s = Str.global_replace (Str.regexp_string "\\@") "@" s in
let s = remove_blanks s in
Desc s
}
| eof
{
EOF
}
| _ {
let s = Lexing.lexeme lexbuf in
failwith ("Unexpected character '"^s^"'")
}
and simple = parse
[' ' '\013' '\009' '\012'] +
{
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length (Lexing.lexeme lexbuf));
simple lexbuf
}
| [ '\010' ]
{ incr line_number;
incr Odoc_comments_global.nb_chars;
simple lexbuf
}
| "(**"("*"+)
{
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length (Lexing.lexeme lexbuf));
incr comments_level;
simple lexbuf
}
| "(*"("*"+)")"
{
let s = Lexing.lexeme lexbuf in
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length s);
simple lexbuf
}
| "(**"
{
let s = Lexing.lexeme lexbuf in
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length s);
incr comments_level;
simple lexbuf
}
| "(*"
{
let s = Lexing.lexeme lexbuf in
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length s);
incr comments_level;
if !comments_level = 1 then
(
reset_string_buffer ();
description := "";
special_comment lexbuf
)
else
(
add_string s;
simple lexbuf
)
}
| eof
{ EOF }
| "*)"
{
let s = Lexing.lexeme lexbuf in
Odoc_comments_global.nb_chars := !Odoc_comments_global.nb_chars + (String.length s);
decr comments_level ;
simple lexbuf
}
| _
{
incr Odoc_comments_global.nb_chars;
simple lexbuf
}
|