Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yallop/ocaml-reex
Staged regular expression library for MetaOCaml
https://github.com/yallop/ocaml-reex
metaocaml multi-stage-programming ocaml regex regular-expressions
Last synced: 12 days ago
JSON representation
Staged regular expression library for MetaOCaml
- Host: GitHub
- URL: https://github.com/yallop/ocaml-reex
- Owner: yallop
- License: mit
- Created: 2022-04-02T13:55:39.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-06-30T20:45:26.000Z (over 1 year ago)
- Last Synced: 2025-01-03T17:14:26.735Z (27 days ago)
- Topics: metaocaml, multi-stage-programming, ocaml, regex, regular-expressions
- Language: OCaml
- Homepage:
- Size: 25.4 KB
- Stars: 9
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
A staged regular expression library for MetaOCaml, based on ["Regular-expression derivatives reexamined"][reex-paper]
## Installing `reex`
1. Install the [BER MetaOCaml][ber-metaocaml] compiler using [OPAM][opam]:
```
opam switch 4.14.1+BER
eval $(opam env)
```2. Add the [metaocaml-opam][metaocaml-opam] repository:
```
opam remote add metaocaml git+https://github.com/metaocaml/metaocaml-opam.git
```3. Install the `reex` and `reex_match` packages:
```
opam pin add reex reex_match
```## Using `reex`
The functions in the [`Reex`][reex] module construct regular expressions:
```ocaml
let letters = plus (range 'A' 'Z') (* [A-Z]+ *)
let keyword = str "let" <|> str "and" <|> str "in"
```Alternatively, the `Reex.regex` function builds a regular expression from a string:
```ocaml
let letters = regex "[A-Z]+"
let keyword = regex "let|and|in"
```The [`Reex_match.match_`][reex_match] function generates OCaml code that matches one or more regular expressions. For example, the following call
```ocaml
.
.~(match_ ~options:{default_options with match_type=`ranges} .. ..
[chr 'a' , (fun _ _ -> .<"a">.);
plus (chr 'b'), (fun _ _ -> .<"b">.)]) >.
```generates the following OCaml code for matching "a" or "b+":
```ocaml
fun i s ->
let rec f ~start ~index ~len s =
match String.unsafe_get s index with
| 'c'..'\255'|'\000'..'`' -> failwith "no match"
| 'b' -> g ~start ~index:(index + 1) ~len s
| 'a' -> h ~start ~index:(index + 1) ~len s
and g ~start ~index ~len s =
match String.unsafe_get s index with
| 'c'..'\255'|'\000'..'a' -> "b"
| 'b' -> g ~start ~index:(index + 1) ~len s
and h ~start ~index ~len s = "a" in
f ~start:i ~index:i ~len:(String.length s) s
```[reex-paper]: https://www.ccs.neu.edu/home/turon/re-deriv.pdf
[metaocaml-opam]: https://github.com/metaocaml/metaocaml-opam
[ber-metaocaml]: https://okmij.org/ftp/ML/MetaOCaml.html
[opam]: https://opam.ocaml.org/
[reex]: https://github.com/yallop/reex/blob/master/lib/reex.mli
[reex_match]: https://github.com/yallop/reex/blob/master/lib/reex_match.mli