From 7166a99542af5ad2f5b53fe9fa9e5164dff7cfe5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kim=20Nguy=E1=BB=85n?= Date: Tue, 14 Feb 2012 18:29:38 +0100 Subject: [PATCH] Preliminary support for the grammar. --- src/OCamlDriver.cpp | 31 +++++++++++++++++++++++++++---- src/grammar.ml | 13 +++++++++++++ src/grammar.mli | 3 +++ src/main.ml | 8 +++++++- tests/docs/.gitignore | 1 + 5 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 src/grammar.ml create mode 100644 src/grammar.mli diff --git a/src/OCamlDriver.cpp b/src/OCamlDriver.cpp index c1a4fdd..cd7fed4 100644 --- a/src/OCamlDriver.cpp +++ b/src/OCamlDriver.cpp @@ -20,6 +20,7 @@ #include "XMLTree.h" #include "XMLTreeBuilder.h" +#include "Grammar.h" #include "Utils.h" #include "common_stub.hpp" @@ -31,6 +32,8 @@ #define XMLTREEBUILDER(x) (Obj_val(x)) +#define GRAMMAR(x) (Obj_val(x)) + #define TREENODEVAL(i) ((treeNode) (Int_val(i))) #define TAGVAL(i) ((TagType) (Int_val(i))) @@ -40,6 +43,7 @@ extern "C" { #include #include +#include } @@ -152,16 +156,13 @@ extern "C" value caml_xml_tree_save(value tree,value fd, value name){ extern "C" value caml_xml_tree_load(value fd, value name, value load_tc,value sf){ CAMLparam4(fd, name, load_tc, sf); - CAMLlocal2(result,tmp); + CAMLlocal1(result); XMLTree * tree; try { tree = XMLTree::Load(Int_val(fd),Bool_val(load_tc),Int_val(sf), String_val(name)); result = sxsi_alloc_custom(); - Obj_val(result) = tree; - tmp = sxsi_alloc_custom(); - Obj_val(tmp) = 3l; CAMLreturn(result); } catch (const std::exception& e){ CAMLRAISEMSG(e.what()); } @@ -893,3 +894,25 @@ BV_QUERY(suffix, Suffix) BV_QUERY(equals, Equals) BV_QUERY(contains, Contains) BV_QUERY(lessthan, LessThan) + + + +//////////////////////////////////////////// Grammar stuff + +extern "C" value caml_grammar_load(value file, value load_bp) +{ + CAMLparam2(file, load_bp); + CAMLlocal1(result); + Grammar *grammar; + int f1 = Int_val(file); + int f2 = dup(f1); + FILE * fd = fdopen(f2, "r"); + if (fd == NULL) + CAMLRAISEMSG("Error opening grammar file"); + grammar = Grammar::load(fd, Bool_val(load_bp)); + fclose(fd); + result = sxsi_alloc_custom(); + Obj_val(result) = grammar; + CAMLreturn(result); +} + diff --git a/src/grammar.ml b/src/grammar.ml new file mode 100644 index 0000000..6537cb2 --- /dev/null +++ b/src/grammar.ml @@ -0,0 +1,13 @@ +type t + +external load : Unix.file_descr -> bool -> t = "caml_grammar_load" + +let load filename bp = + let fd = Unix.openfile filename [ Unix.O_RDONLY ] 0o600 in + let g = + try load fd bp with + | e -> (Unix.close fd; raise e) + in + Unix.close fd; + g + diff --git a/src/grammar.mli b/src/grammar.mli new file mode 100644 index 0000000..3e355d4 --- /dev/null +++ b/src/grammar.mli @@ -0,0 +1,3 @@ +type t + +val load : string -> bool -> t diff --git a/src/main.ml b/src/main.ml index 8c7c81f..cd3d275 100644 --- a/src/main.ml +++ b/src/main.ml @@ -91,7 +91,13 @@ let () = Options.parse_cmdline() ;; let document = - if Filename.check_suffix !Options.input_file ".srx" + if Filename.check_suffix !Options.input_file ".g.bin" then + let g = time ~msg:"Loading grammar" (Grammar.load !Options.input_file) true in + begin + ignore(g); + exit 0 + end + else if Filename.check_suffix !Options.input_file ".srx" then time ~msg:"Loading file" diff --git a/tests/docs/.gitignore b/tests/docs/.gitignore index b1c234c..98b38d4 100644 --- a/tests/docs/.gitignore +++ b/tests/docs/.gitignore @@ -1,3 +1,4 @@ *.srx *.xml +*.bin -- 2.17.1