I am porting an OCaml project to ReasonML, but first to BuckleScript. I seeded the project directory with
bsb init .
and copied over the *.ml files. Compiling with
npm run build
gives me error:
[1/6] Building src/demo.cmj File "/d/ProgLang/reason/src/demo.ml", line 2, characters 7-17: Error: Unbound value Str.regexp [2/6] Building src/lex.cmj File "/d/ProgLang/reason/src/lex.ml", line 13, characters 26-44: Error: Unbound value Str.search_forward
My package.json has
"devDependencies": {
"bsb-native": "^4.0.7000"
}
and ./node_modules/bsb-native/vendor/ocaml/lib/ocaml contains files str.a str.cma str.cmi str.cmx str.cmxs str.mli, but no source like str.ml . Also, https://reasonml.github.io/api/Str.html documents the functions I need, but how can I link them in when targeting Javascript (node)?
I can live with both Javascript or native targets, but I want to upgrade from OCaml syntax to ReasonML. In case you need my demo.ml, here it is:
let qq=Str.regexp "/q/" and
() = Js.log "Hello, STR! BuckleScript"
The functionality exposed by the Str
module is actually implemented in C, and is therefore not easily portable to JavaScript. Any JavaScript implementation of Regular Expressions would likely be significantly slower than the built-in JavaScript implementation as well.
Furthermore, providing a common interface to multiple implementations isn't trivial since Regular Expression implementations differ in more or less subtle ways and aren't fully compatible with each other. Most aren't even regular, despite the name.
There's been some discussion around how a common interface for JS and native regexes can be accomplished, but there's no obvious solution and I'm not aware that anything has been concluded. But I would think that at the very least you'd have to implement a parser that would only accept the common subset of regex syntax, before passing it on to the underlying regex engine to be parsed again, which would obviously have a notable performance impact and is non-trivial to implement.
For now, you should be able to use Js.Re
for JavaScript regexes and conditional compilation in order to use Str
or some other implementation natively.
Another alternative might be to compile ocaml-re
to JavaScript using js-of-ocaml and then interface with that using external
s. Or port the project to bsb
if possible, to use it directly. That will likely increase the size of your code bundle significantly, however, and it's not particularly straightforward to accomplish.