package ppx_mikmatch

  1. Overview
  2. Docs
Matching Regular Expressions with OCaml Patterns using Mikmatch's syntax

Install

dune-project
 Dependency

Authors

Maintainers

Sources

1.2.tar.gz
md5=607d525df297c70ec7104d7188d2aa5b
sha512=1f1da92620be9dbf1c7530c7d0bfc92bb736f4189ef97285b24b90bbb8ce24aaa0aa001c268a4029458def31dc972e813d163092caee5dc784f96380f9125618

doc/src/ppx_mikmatch.lib/regexp.ml.html

Source file regexp.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
open Regexp_types

let parse_exn ~target ?(pos = Lexing.dummy_pos) s =
  let lexbuf = Lexing.from_string s in
  lexbuf.lex_curr_p <- pos;
  lexbuf.lex_start_p <- pos;
  lexbuf.lex_abs_pos <- pos.pos_cnum;
  let mk_loc ?loc pos lexbuf =
    let open Lexing in
    let open Location in
    match loc with
    | Some loc -> loc
    | None ->
      (* no location from parser, use lexbuf positions *)
      {
        loc_ghost = false;
        loc_start = { pos with pos_cnum = pos.pos_cnum + lexbuf.lex_start_p.pos_cnum };
        loc_end = { pos with pos_cnum = pos.pos_cnum + lexbuf.lex_curr_p.pos_cnum };
      }
  in
  let main = match target with `Match -> Mik_parser.main_match_case | `Let -> Mik_parser.main_let_expr in
  try main Mik_lexer.token lexbuf with
  | Mik_lexer.Error msg ->
    let loc = mk_loc pos lexbuf in
    Location.raise_errorf ~loc "%s" msg
  | PError (loc, msg) ->
    let loc = mk_loc ~loc pos lexbuf in
    Location.raise_errorf ~loc "Syntax error: %s" msg
  | Mik_parser.Error ->
    let loc = mk_loc pos lexbuf in
    Location.raise_errorf ~loc "Syntax error"