X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;f=src%2FOCamlDriver.cpp;h=7a12c7ffc3b4fce220cf78b39af005658873160f;hb=bca508d0e826bc09f8975ba28720a867a828782f;hp=c1a4fdda93b726b40040dbb23b997328e1e6b900;hpb=dabbda9e7f33734a3be4125f029b850ea39e2f19;p=SXSI%2Fxpathcomp.git diff --git a/src/OCamlDriver.cpp b/src/OCamlDriver.cpp index c1a4fdd..7a12c7f 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,75 @@ 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); +} + +extern "C" value caml_grammar_get_symbol_at(value grammar, value symbol, value preorder) +{ + CAMLparam3(grammar, symbol, preorder); + CAMLreturn(Val_long(GRAMMAR(grammar)->getSymbolAt(Long_val(symbol), Int_val(preorder)))); +} + +extern "C" value caml_grammar_first_child(value grammar, value rule, value pos) +{ + CAMLparam1(grammar); + CAMLreturn(Val_int(GRAMMAR(grammar)->firstChild(Long_val(rule), Int_val(pos)))); +} + +extern "C" value caml_grammar_next_sibling(value grammar, value rule, value pos) +{ + CAMLparam1(grammar); + CAMLreturn(Val_int(GRAMMAR(grammar)->nextSibling(Long_val(rule), Int_val(pos)))); +} + +extern "C" value caml_grammar_is_nil(value grammar, value rule) +{ + CAMLparam1(grammar); + CAMLreturn(Val_bool(GRAMMAR(grammar)->isNil(Long_val(rule)))); +} + +extern "C" value caml_grammar_get_tag(value grammar, value symbol) +{ + CAMLparam1(grammar); + CAMLlocal1(res); + const char * s = (GRAMMAR(grammar)->getTagName(Long_val(symbol) >> 2)).c_str(); + res = caml_copy_string(s); + CAMLreturn(res); +} + +extern "C" value caml_grammar_get_id1(value grammar, value rule) +{ + CAMLparam1(grammar); + CAMLreturn(Val_long(GRAMMAR(grammar)->getID1(Long_val(rule)))); +} + +extern "C" value caml_grammar_get_id2(value grammar, value rule) +{ + CAMLparam1(grammar); + CAMLreturn(Val_long(GRAMMAR(grammar)->getID2(Long_val(rule)))); +} + +extern "C" value caml_grammar_get_param_pos(value grammar, value rule) +{ + CAMLparam1(grammar); + CAMLreturn(Val_int(GRAMMAR(grammar)->getParamPos(Long_val(rule)))); +}