Commit ec40e00c authored by Pietro Abate's avatar Pietro Abate

[r2003-06-03 20:45:50 by cvscast] expat

Original author: cvscast
Date: 2003-06-03 20:45:50+00:00
parent 6f721a9f
0.0.91
* Second alpha release
* Bug fixes and syntax changes (e.g. making "fun" optional in "let fun")
* Experimenting with expat support (see expat/README)
0.0.9
* First public release (alpha release)
......@@ -38,7 +38,7 @@ website: web/files
# Packaging
DISTRIB = $(DIRS) tools web depend INSTALL INSTALL.WIN32 CHANGES LICENSE README doc
DISTRIB = $(DIRS) expat tools web depend INSTALL INSTALL.WIN32 CHANGES LICENSE README doc
PACKAGE = cduce-$(VERSION)
.PHONY: package
......
......@@ -124,11 +124,6 @@ There are several sources of information concerning CDuce:
- and of course the interpreter source code !
------------------------------------------------------------------------------
Abstract
------------------------------------------------------------------------------
------------------------------------------------------------------------------
Feedback
......
This directory contains code written by Maas-Maarten Zeeman
and included here for convenience.
Home page: http://home.wanadoo.nl/maas/ocaml/
To build support for expat, do (in CDuce root directory):
make clean; make expat
Now, when the CDuce interpreter is run with the option --expat,
it will use expat instead of PXP for load_xml.
Note: the current wrapper for expat does not support inclusion
of external entities. Moreover I encounter random segfaults...
......@@ -5,7 +5,7 @@
(* LICENCE for details. *)
(***********************************************************************)
(* $Id: expat.ml,v 1.1 2003/05/31 10:32:42 cvscast Exp $ *)
(* $Id: expat.ml,v 1.2 2003/06/03 20:45:50 cvscast Exp $ *)
type expat_parser
......@@ -56,7 +56,8 @@ external parser_create_ns : encoding:string -> separator:char -> expat_parser =
(* calls needed to parse *)
external parse : expat_parser -> string -> unit = "expat_XML_Parse"
external parse_sub : expat_parser -> string -> int -> int -> unit = "expat_XML_Parse_sub"
external parse_sub : expat_parser -> string -> int -> int -> unit =
"expat_XML_ParseSub"
external final : expat_parser -> unit = "expat_XML_Final"
(* start element handler calls *)
......@@ -91,6 +92,24 @@ external set_comment_handler : expat_parser -> (string -> unit) -> unit =
external reset_comment_handler : expat_parser -> unit =
"expat_XML_ResetCommentHandler"
(* start cdata handler *)
external set_start_cdata_handler : expat_parser -> (unit -> unit) -> unit =
"expat_XML_SetStartCDataHandler"
external reset_start_cdata_handler : expat_parser -> unit =
"expat_XML_ResetStartCDataHandler"
(* end cdata handler *)
external set_end_cdata_handler : expat_parser -> (unit -> unit) -> unit =
"expat_XML_SetEndCDataHandler"
external reset_end_cdata_handler : expat_parser -> unit =
"expat_XML_ResetEndCDataHandler"
(* default handler *)
external set_default_handler : expat_parser -> (string -> unit) -> unit =
"expat_XML_SetDefaultHandler"
external reset_default_handler : expat_parser -> unit =
"expat_XML_ResetDefaultHandler"
(* some general parser query calls *)
external get_current_byte_index : expat_parser -> int =
"expat_XML_GetCurrentByteIndex"
......
......@@ -5,7 +5,7 @@
(* LICENCE for details. *)
(***********************************************************************)
(* $Id: expat.mli,v 1.1 2003/05/31 10:32:42 cvscast Exp $ *)
(* $Id: expat.mli,v 1.2 2003/06/03 20:45:50 cvscast Exp $ *)
(** The Ocaml Expat library provides an interface to the Expat XML Parser.
......@@ -40,6 +40,9 @@ val parser_create_ns : encoding:string -> separator:char -> expat_parser
(** Let the parser parse a chunk of an XML document.
@raise Expat_error error *)
val parse : expat_parser -> string -> unit
(** Let the parser parse a chunk of an XML document in a substring
@raise Expat_error error *)
val parse_sub : expat_parser -> string -> int -> int -> unit
(** Inform the parser that the entire document has been parsed. *)
......@@ -79,6 +82,19 @@ val reset_processing_instruction_handler : expat_parser -> unit
val set_comment_handler : expat_parser -> (string -> unit) -> unit
val reset_comment_handler : expat_parser -> unit
(** {6 CData Section handler setting and resetting *)
val set_start_cdata_handler : expat_parser -> (unit -> unit) -> unit
val reset_start_cdata_handler : expat_parser -> unit
val set_end_cdata_handler : expat_parser -> (unit -> unit) -> unit
val reset_end_cdata_handler : expat_parser -> unit
(** {6 Default Handler setting and resetting *)
val set_default_handler : expat_parser -> (string -> unit) -> unit
val reset_default_handler : expat_parser -> unit
(** {5 Parse Position Functions} *)
val get_current_byte_index : expat_parser -> int
......
......@@ -5,7 +5,7 @@
/* LICENCE for details. */
/***********************************************************************/
/* $Id: expat_stubs.c,v 1.2 2003/06/03 20:36:11 cvscast Exp $ */
/* $Id: expat_stubs.c,v 1.3 2003/06/03 20:45:50 cvscast Exp $ */
/* Stub code to interface Ocaml with Expat */
......@@ -26,13 +26,16 @@
* handler tuple which is registered as global root. Handlers for
* new functions should go here.
*/
#define NUM_HANDLERS 5
#define NUM_HANDLERS 8
enum expat_handler {
EXPAT_START_ELEMENT_HANDLER,
EXPAT_END_ELEMENT_HANDLER,
EXPAT_CHARACTER_DATA_HANDLER,
EXPAT_PROCESSING_INSTRUCTION_HANDLER,
EXPAT_COMMENT_HANDLER
EXPAT_COMMENT_HANDLER,
EXPAT_START_CDATA_HANDLER,
EXPAT_END_CDATA_HANDLER,
EXPAT_DEFAULT_HANDLER
};
static void
......@@ -42,7 +45,6 @@ xml_parser_finalize(value parser)
value *handlers = XML_GetUserData(xml_parser);
remove_global_root(handlers);
stat_free(handlers);
XML_ParserFree(xml_parser);
}
......@@ -92,6 +94,7 @@ static value
create_ocaml_expat_parser(XML_Parser xml_parser)
{
CAMLparam0();
CAMLlocal1(parser);
int i;
value *handlers;
......@@ -106,8 +109,12 @@ create_ocaml_expat_parser(XML_Parser xml_parser)
* handlers and register it as global root.
*/
handlers = stat_alloc(sizeof *handlers);
*handlers = Val_unit;
*handlers = Val_unit;
register_global_root(handlers);
/*
* Create a tuple which will hold the handlers.
*/
*handlers = alloc_tuple(NUM_HANDLERS);
for(i = 0; i < NUM_HANDLERS; i++) {
Field(*handlers, i) = Val_unit;
......@@ -228,26 +235,31 @@ expat_XML_Parse(value parser, value string)
CAMLreturn (Val_unit);
}
/*
* A sub-string variant of the parse call
*/
CAMLprim value
expat_XML_Parse_sub(value parser, value string, value pos, value len)
expat_XML_ParseSub(value parser, value string, value pos, value len)
{
CAMLparam2(parser, string);
CAMLparam4(parser, string, pos, len);
XML_Parser xml_parser = XML_Parser_val(parser);
if(!XML_Parse(xml_parser, String_val(string)+Int_val(pos), Int_val(len), 0)) {
if(!XML_Parse(xml_parser,
String_val(string) + Int_val(pos), Int_val(len), 0)) {
expat_error(XML_GetErrorCode(xml_parser));
}
CAMLreturn (Val_unit);
}
/*
* The final call
*/
CAMLprim value
expat_XML_Final(value parser)
{
CAMLparam1(parser);
CAMLparam1(parser);
XML_Parser xml_parser = XML_Parser_val(parser);
if(!XML_Parse(xml_parser, NULL, 0, 1)) {
......@@ -504,3 +516,136 @@ expat_XML_ResetCommentHandler(value parser)
CAMLparam1(parser);
CAMLreturn (set_comment_handler(parser, NULL, Val_unit));
}
/*
* Start CData handler, setting and resetting
*/
static void
start_cdata_handler(void *user_data)
{
CAMLparam0();
value *handlers = user_data;
callback(Field(*handlers, EXPAT_START_CDATA_HANDLER), Val_unit);
CAMLreturn0;
}
static value
set_start_cdata_handler(value parser,
XML_StartCdataSectionHandler c_handler,
value ocaml_handler)
{
CAMLparam2(parser, ocaml_handler);
XML_Parser xml_parser = XML_Parser_val(parser);
value *handlers = XML_GetUserData(xml_parser);
Store_field(*handlers, EXPAT_START_CDATA_HANDLER, ocaml_handler);
XML_SetStartCdataSectionHandler(xml_parser, c_handler);
CAMLreturn (Val_unit);
}
CAMLprim value
expat_XML_SetStartCDataHandler(value parser, value handler)
{
CAMLparam2(parser, handler);
CAMLreturn (set_start_cdata_handler(parser, start_cdata_handler, handler));
}
CAMLprim value
expat_XML_ResetStartCDataHandler(value parser)
{
CAMLparam1(parser);
CAMLreturn (set_start_cdata_handler(parser, NULL, Val_unit));
}
/*
* End CData handler, setting and resetting
*/
static void
end_cdata_handler(void *user_data)
{
CAMLparam0();
value *handlers = user_data;
callback(Field(*handlers, EXPAT_END_CDATA_HANDLER), Val_unit);
CAMLreturn0;
}
static value
set_end_cdata_handler(value parser,
XML_EndCdataSectionHandler c_handler,
value ocaml_handler)
{
CAMLparam2(parser, ocaml_handler);
XML_Parser xml_parser = XML_Parser_val(parser);
value *handlers = XML_GetUserData(xml_parser);
Store_field(*handlers, EXPAT_END_CDATA_HANDLER, ocaml_handler);
XML_SetEndCdataSectionHandler(xml_parser, c_handler);
CAMLreturn (Val_unit);
}
CAMLprim value
expat_XML_SetEndCDataHandler(value parser, value handler)
{
CAMLparam2(parser, handler);
CAMLreturn (set_end_cdata_handler(parser, end_cdata_handler, handler));
}
CAMLprim value
expat_XML_ResetEndCDataHandler(value parser)
{
CAMLparam1(parser);
CAMLreturn (set_end_cdata_handler(parser, NULL, Val_unit));
}
/*
* Default handler, setting and resetting
*/
static void
default_handler(void *user_data, const char *data, int len)
{
CAMLparam0();
CAMLlocal1(d);
value *handlers = user_data;
d = alloc_string(len);
memmove(String_val(d), data, len);
callback(Field(*handlers, EXPAT_DEFAULT_HANDLER), d);
CAMLreturn0;
}
static value
set_default_handler(value parser,
XML_DefaultHandler c_handler,
value ocaml_handler)
{
CAMLparam2(parser, ocaml_handler);
XML_Parser xml_parser = XML_Parser_val(parser);
value *handlers = XML_GetUserData(xml_parser);
Store_field(*handlers, EXPAT_DEFAULT_HANDLER, ocaml_handler);
XML_SetDefaultHandler(xml_parser, c_handler);
CAMLreturn (Val_unit);
}
CAMLprim value
expat_XML_SetDefaultHandler(value parser, value handler)
{
CAMLparam2(parser, handler);
CAMLreturn (set_default_handler(parser, default_handler, handler));
}
CAMLprim value
expat_XML_ResetDefaultHandler(value parser)
{
CAMLparam1(parser);
CAMLreturn (set_default_handler(parser, NULL, Val_unit));
}
......@@ -91,12 +91,14 @@ let load_expat s =
txt.pos <- 0;
create_elt accu !stack);
Expat.set_character_data_handler p (add_string txt);
(* Gc.full_major (); *)
(* Gc.compact (); *)
let ic = open_in s in
let rec loop () =
let n = input ic buf 0 buflen in
if (n > 0) then
(*(Expat.parse p (String.sub buf 0 n); loop ())*)
(Expat.parse_sub p buf 0 n; loop ())
(*(Expat.parse p (String.sub buf 0 n); loop ()) *)
(Expat.parse_sub p buf 0 n; loop ())
in
try
loop();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment