summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxence Guesdon <maxence.guesdon@inria.fr>2003-11-24 21:20:51 +0000
committerMaxence Guesdon <maxence.guesdon@inria.fr>2003-11-24 21:20:51 +0000
commit05b2a15d5c4b87d1fa60587518f7860f3080cf47 (patch)
tree9695446f773f0520c64b8e0434a2287ad3ddd5c5
parent0d5a86e5204266235d183963156bcbfdc22618df (diff)
downloadocaml-05b2a15d5c4b87d1fa60587518f7860f3080cf47.tar.gz
OK - fixes: some bugs in the text parser
( ]} meaning end of code and somehting else instead of end of precode) OK - add: in Odoc_info: text_of_string, text_string_of_text, info_of_string git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@5974 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
-rw-r--r--ocamldoc/Changes.txt3
-rw-r--r--ocamldoc/Makefile8
-rw-r--r--ocamldoc/odoc_info.ml28
-rw-r--r--ocamldoc/odoc_info.mli25
-rw-r--r--ocamldoc/odoc_text.ml113
-rw-r--r--ocamldoc/odoc_text.mli5
-rw-r--r--ocamldoc/odoc_text_lexer.mll34
7 files changed, 195 insertions, 21 deletions
diff --git a/ocamldoc/Changes.txt b/ocamldoc/Changes.txt
index 7b92295039..3f432819a3 100644
--- a/ocamldoc/Changes.txt
+++ b/ocamldoc/Changes.txt
@@ -1,4 +1,7 @@
Current :
+OK - fixes: some bugs in the text parser
+ ( ]} meaning end of code and somehting else instead of end of precode)
+OK - add: in Odoc_info: text_of_string, text_string_of_text, info_of_string
OK - fix: better output of titles in html (use more the style)
OK - add: -intro option to use a file content as ocamldoc comment to use as
OK introduction for LaTeX document and HTML index page
diff --git a/ocamldoc/Makefile b/ocamldoc/Makefile
index 9e223cfb1b..772305bc4a 100644
--- a/ocamldoc/Makefile
+++ b/ocamldoc/Makefile
@@ -328,6 +328,14 @@ stdlib_man/Pervasives.o: $(STDLIB_MLIS)
-t "OCaml library" -man-mini -man-suffix 3o \
$(STDLIB_MLIS)
+autotest_stdlib: dummy
+ $(MKDIR) $@
+ $(OCAMLDOC_RUN) -g autotest/odoc_test.cmo\
+ $(INCLUDES) -keep-code \
+ ../stdlib/pervasives.ml ../stdlib/*.mli \
+ ../otherlibs/unix/unix.mli \
+ ../otherlibs/str/str.mli
+
# backup, clean and depend :
############################
diff --git a/ocamldoc/odoc_info.ml b/ocamldoc/odoc_info.ml
index ffb016a148..4e11dfe2ed 100644
--- a/ocamldoc/odoc_info.ml
+++ b/ocamldoc/odoc_info.ml
@@ -50,6 +50,8 @@ and text_element = Odoc_types.text_element =
and text = text_element list
+exception Text_syntax = Odoc_text.Text_syntax
+
type see_ref = Odoc_types.see_ref =
See_url of string
| See_file of string
@@ -176,7 +178,11 @@ let apply_if_equal f v1 v2 =
else
v2
-let info_of_comment_file f =
+let text_of_string = Odoc_text.Texter.text_of_string
+
+let text_string_of_text = Odoc_text.Texter.string_of_text
+
+let info_of_string s =
let dummy =
{
i_desc = None ;
@@ -191,19 +197,19 @@ let info_of_comment_file f =
i_custom = [] ;
}
in
+ let s2 = Printf.sprintf "(** %s *)" s in
+ let (_, i_opt) = Odoc_comments.Basic_info_retriever.first_special "-" s2 in
+ match i_opt with
+ None -> dummy
+ | Some i -> i
+
+let info_of_comment_file f =
try
- let s = Printf.sprintf "(** %s *)" (Odoc_misc.input_file_as_string f) in
- let (_, i_opt) = Odoc_comments.Basic_info_retriever.first_special f s in
- (
- match i_opt with
- None -> dummy
- | Some i -> i
- )
+ let s = Odoc_misc.input_file_as_string f in
+ info_of_string s
with
Sys_error s ->
- prerr_endline s;
- incr errors;
- dummy
+ failwith s
module Search =
struct
diff --git a/ocamldoc/odoc_info.mli b/ocamldoc/odoc_info.mli
index 02ed187336..a2b8529fee 100644
--- a/ocamldoc/odoc_info.mli
+++ b/ocamldoc/odoc_info.mli
@@ -59,6 +59,10 @@ type see_ref = Odoc_types.see_ref =
| See_file of string
| See_doc of string
+(** Raised when parsing string to build a {!Odoc_info.text}
+ structure. [(line, char, string) *)
+exception Text_syntax of int * int * string
+
(** The information in a \@see tag. *)
type see = see_ref * text
@@ -725,9 +729,26 @@ val apply_opt : ('a -> 'b) -> 'a option -> 'b option
are different, return the second one.*)
val apply_if_equal : ('a -> 'a) -> 'a -> 'a -> 'a
+(** [text_of_string s] returns the text structure from the
+ given string.
+ @raise Text_syntax if a syntax error is encountered. *)
+val text_of_string : string -> text
+
+(** [string_text_of_text text] returns the string representing
+ the given [text]. This string can then be parsed again
+ by {!Odoc_info.text_of_string}.*)
+val text_string_of_text : text -> string
+
+(** [info_of_string s] parses the given string
+ like a regular ocamldoc comment and return an
+ {!Odoc_info.info} structure.
+ @return an empty structure if there was a syntax error. TODO: change this
+*)
+val info_of_string : string -> info
+
(** [info_of_comment_file file] parses the given file
- and return an info structure. The content of the file
- must have the same syntax as the content of a special comment.
+ and return an {!Odoc_info.info} structure. The content of the
+ file must have the same syntax as the content of a special comment.
@raise Failure is the file could not be opened or there is a
syntax error.
*)
diff --git a/ocamldoc/odoc_text.ml b/ocamldoc/odoc_text.ml
index aa675692f7..b83c88a19f 100644
--- a/ocamldoc/odoc_text.ml
+++ b/ocamldoc/odoc_text.ml
@@ -13,6 +13,8 @@
exception Text_syntax of int * int * string (* line, char, string *)
+open Odoc_types
+
module Texter =
struct
(* builds a text structure from a string. *)
@@ -27,5 +29,116 @@ module Texter =
!Odoc_text_lexer.char_number,
s)
)
+
+ let count s c =
+ let count = ref 0 in
+ for i = 0 to String.length s - 1 do
+ if s.[i] = c then incr count
+ done;
+ !count
+
+ let escape_n s c n =
+ let remain = ref n in
+ let len = String.length s in
+ let b = Buffer.create (len + n) in
+ for i = 0 to len - 1 do
+ if s.[i] = c && !remain > 0 then
+ (
+ Printf.bprintf b "\\%c" c;
+ decr remain
+ )
+ else
+ Buffer.add_char b s.[i]
+ done;
+ Buffer.contents b
+
+ let escape_code s =
+ let open_brackets = count s '[' in
+ let close_brackets = count s ']' in
+ if open_brackets > close_brackets then
+ escape_n s '[' (open_brackets - close_brackets)
+ else
+ if close_brackets > open_brackets then
+ escape_n s ']' (close_brackets - open_brackets)
+ else
+ s
+
+ let escape_raw s =
+ let len = String.length s in
+ let b = Buffer.create len in
+ for i = 0 to len - 1 do
+ match s.[i] with
+ '[' | ']' | '{' | '}' ->
+ Printf.bprintf b "\\%c" s.[i]
+ | c ->
+ Buffer.add_char b c
+ done;
+ Buffer.contents b
+
+ let p = Printf.bprintf
+
+ let rec p_text b t =
+ List.iter (p_text_element b) t
+
+ and p_list b l =
+ List.iter
+ (fun t -> p b "{- " ; p_text b t ; p b "}\n")
+ l
+
+ and p_text_element b = function
+ | Raw s -> p b "%s" (escape_raw s)
+ | Code s -> p b "[%s]" (escape_code s)
+ | CodePre s -> p b "{[%s]}" s
+ | Verbatim s -> p b "{v %s v}" s
+ | Bold t -> p b "{b " ; p_text b t ; p b "}"
+ | Italic t -> p b "{i " ; p_text b t ; p b "}"
+ | Emphasize t -> p b "{e " ; p_text b t ; p b "}"
+ | Center t -> p b "{C " ; p_text b t ; p b "}"
+ | Left t -> p b "{L " ; p_text b t ; p b "}"
+ | Right t -> p b "{R " ; p_text b t ; p b "}"
+ | List l -> p b "{ul\n"; p_list b l; p b "}"
+ | Enum l -> p b "{ol\n"; p_list b l; p b "}"
+ | Newline -> p b "\n"
+ | Block t -> p_text b t
+ | Title (n, l_opt, t) ->
+ p b "{%d%s "
+ n
+ (match l_opt with
+ None -> ""
+ | Some s -> ":"^s
+ );
+ p_text b t ;
+ p b "}"
+ | Latex s -> p b "{%% %s%%}" s
+ | Link (s,t) ->
+ p b "{{:%s}" s;
+ p_text b t ;
+ p b "}"
+ | Ref (s,None) ->
+ p b "{!%s}" s
+ | Ref (s, Some k) ->
+ (
+ let sk = match k with
+ RK_module -> "module"
+ | RK_module_type -> "modtype"
+ | RK_class -> "class"
+ | RK_class_type -> "classtype"
+ | RK_value -> "val"
+ | RK_type -> "type"
+ | RK_exception -> "exception"
+ | RK_attribute -> "attribute"
+ | RK_method -> "method"
+ | RK_section _ -> "section"
+ in
+ p b "{!%s:%s}" sk s
+ )
+ | Superscript t -> p b "{^" ; p_text b t ; p b "}"
+ | Subscript t -> p b "{_" ; p_text b t ; p b "}"
+
+ let string_of_text s =
+ let b = Buffer.create 256 in
+ p_text b s;
+ Buffer.contents b
+
end
diff --git a/ocamldoc/odoc_text.mli b/ocamldoc/odoc_text.mli
index 6ed5bd0e44..bc121791dd 100644
--- a/ocamldoc/odoc_text.mli
+++ b/ocamldoc/odoc_text.mli
@@ -18,4 +18,7 @@ exception Text_syntax of int * int * string (* line, char, string *)
(** Transformation of strings to text structures. *)
module Texter :
- sig val text_of_string : string -> Odoc_types.text end
+ sig
+ val text_of_string : string -> Odoc_types.text
+ val string_of_text : Odoc_types.text -> string
+ end
diff --git a/ocamldoc/odoc_text_lexer.mll b/ocamldoc/odoc_text_lexer.mll
index 44f0e8c88b..f21ec9ae39 100644
--- a/ocamldoc/odoc_text_lexer.mll
+++ b/ocamldoc/odoc_text_lexer.mll
@@ -418,13 +418,33 @@ rule main = parse
if !verb_mode or !latex_mode or !ele_ref_mode then
Char (Lexing.lexeme lexbuf)
else
- if !code_pre_mode then
- (
- code_pre_mode := false;
- END_CODE_PRE
- )
- else
- Char (Lexing.lexeme lexbuf)
+ if !open_brackets >= 1 then
+ (
+ 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
+ } ;
+ decr char_number ;
+ if !open_brackets > 1 then
+ (
+ decr open_brackets;
+ Char "]"
+ )
+ else
+ (
+ open_brackets := 0;
+ END_CODE
+ )
+ )
+ else
+ if !code_pre_mode then
+ (
+ code_pre_mode := false;
+ END_CODE_PRE
+ )
+ else
+ Char (Lexing.lexeme lexbuf)
}
| begin_ele_ref end