|
| 1 | + |
| 2 | +# `%mik` extension |
| 3 | + |
| 4 | +Accepts `mikmatch` syntax, along with some nice to haves. |
| 5 | + |
| 6 | +## Grammar |
| 7 | +The grammar accepted by this extensions is the following |
| 8 | + |
| 9 | +```bnf |
| 10 | +<main_match_case> ::= "/" <pattern> "/" EOF |
| 11 | +
|
| 12 | +<main_let_expr> ::= <pattern> EOF |
| 13 | +
|
| 14 | +<pattern> ::= <alt_expr> |
| 15 | + | <alt_expr> ">>>" <func_name> "as" IDENT |
| 16 | +
|
| 17 | +<alt_expr> ::= <seq_expr> |
| 18 | + | <seq_expr> "|" <alt_expr> |
| 19 | +
|
| 20 | +<seq_expr> ::= <atom_expr> |
| 21 | + | <atom_expr> <seq_expr> |
| 22 | +
|
| 23 | +<atom_expr> ::= <basic_atom> |
| 24 | + | <basic_atom> "*" |
| 25 | + | <basic_atom> "+" |
| 26 | + | <basic_atom> "?" |
| 27 | + | <basic_atom> "{" INT (n) "}" # match n times |
| 28 | + | <basic_atom> "{" INT (n) "-" INT (m) "}" # match at least n times, at most m times |
| 29 | +
|
| 30 | +<basic_atom> ::= CHAR_LITERAL |
| 31 | + | STRING_LITERAL |
| 32 | + | EMPTY_STR |
| 33 | + | "_" |
| 34 | + | "^" |
| 35 | + | PREDEFINED_CLASS |
| 36 | + | IDENT |
| 37 | + | "[" <char_set> "]" # character class |
| 38 | + | "[" "^" <char_set> "]" # negative character class |
| 39 | + | "(" <pattern> ")" |
| 40 | + | "(" IDENT ")" |
| 41 | + | "(" IDENT "as" IDENT ")" |
| 42 | + | "(" IDENT "as" IDENT ":" INT_CONVERTER ")" |
| 43 | + | "(" IDENT "as" IDENT ":" FLOAT_CONVERTER ")" |
| 44 | + | "(" IDENT "as" IDENT ":=" <func_name> ")" |
| 45 | + | "(" <pattern> "as" IDENT ")" |
| 46 | + | "(" <pattern> "as" IDENT ":" INT_CONVERTER ")" |
| 47 | + | "(" <pattern> "as" IDENT ":" FLOAT_CONVERTER ")" |
| 48 | + | "(" <pattern> "as" IDENT ":=" <func_name> ")" |
| 49 | +
|
| 50 | +<func_name> ::= IDENT |
| 51 | + | MOD_IDENT # qualified names |
| 52 | +
|
| 53 | +<char_set> ::= <char_set_item> |
| 54 | + | <char_set_item> <char_set> |
| 55 | +
|
| 56 | +<char_set_item> ::= CHAR_LITERAL |
| 57 | + | CHAR_LITERAL "-" CHAR_LITERAL |
| 58 | + | STRING_LITERAL |
| 59 | + | PREDEFINED_CLASS |
| 60 | + | IDENT |
| 61 | +``` |
| 62 | + |
| 63 | +Where `PREDEFINED_CLASS` is one of: |
| 64 | + - **POSIX character classes:** `lower`, `upper`, `alpha`, `digit`, `alnum`, `punct`, `graph`, `print`, `blank`, `space`, `cntrl`, `xdigit` |
| 65 | + - **Control sequences:** `eos` (same as `$`), `eol` (end of string or newline), `bnd` (word boundary `\b`), `bos` (same as `^`), `any` (any character except newline) |
| 66 | + - **Empty string:** `""`, equivalent to `^$` (or `bos eos`) |
| 67 | + |
| 68 | +## Semantics and Examples |
| 69 | +### Variable substitution |
| 70 | +```ocaml |
| 71 | +let%mik re1 = {| "hello" |} |
| 72 | +let%mik re2 = {| re1 "world" |} |
| 73 | +
|
| 74 | +let do_something = function%mik |
| 75 | + | {|/ ... (re2) ... /|} -> ... |
| 76 | + | _ -> ... |
| 77 | +
|
| 78 | +(* will expand to *) |
| 79 | +let do_something = function%mik |
| 80 | + | {|/ ... ("hello" "world") ... /|} -> ... |
| 81 | + | _ -> ... |
| 82 | +``` |
| 83 | + |
| 84 | +### Variable capture |
| 85 | +```ocaml |
| 86 | +let%mik num = {| digit+ |} |
| 87 | +
|
| 88 | +let do_something = function%mik |
| 89 | + | {|/ ... (num as n) ... /|} -> ... (* (n : string) available here *) |
| 90 | + | _ -> ... |
| 91 | +``` |
| 92 | + |
| 93 | +Values are also available at the guard level: |
| 94 | + |
| 95 | +```ocaml |
| 96 | +let%mik num = {| digit+ |} |
| 97 | +
|
| 98 | +let do_something = function%mik |
| 99 | + | {|/ ... (num as n) ... /|} when n = 123 -> ... |
| 100 | + | _ -> ... |
| 101 | +``` |
| 102 | + |
| 103 | +#### Type conversion |
| 104 | +It is possible to convert variables to `int` of `float` on the fly: |
| 105 | + |
| 106 | +```ocaml |
| 107 | +let%mik num = {| digit+ |} |
| 108 | +
|
| 109 | +let do_something = function%mik |
| 110 | + | {|/ 'd' (num as n : int) ... /|} -> ... (* (n : int) available here *) |
| 111 | + | {|/ 'f' (num as n : float) ... /|} -> ... (* (n : float) available here *) |
| 112 | + | _ -> ... |
| 113 | +``` |
| 114 | + |
| 115 | +It is also possible to pass the variables into any `string -> 'a` function: |
| 116 | +```ocaml |
| 117 | +let%mik ip = {| (digit{1-3} '.'){3} digit{1-3}|} |
| 118 | +let parse_ip = String.split_on_char '.' |
| 119 | +let get_ip = function%mik |
| 120 | + | {|/ ... (ip as ip := parse_ip) ... /|} -> ... (* (ip : string list) available here *) |
| 121 | + | _ -> ... |
| 122 | +
|
| 123 | +let get_upper_name = function%mik |
| 124 | + | {|/ ... (['a'-'z'] as name := String.uppercase) ... /|} -> ... (* (name : string) available here *) |
| 125 | + | _ -> ... |
| 126 | +``` |
| 127 | + |
| 128 | +#### Piping to a catch all function |
| 129 | + |
| 130 | +Using the `>>>` syntax extension, you can pipe all bound named variables into a single function, and name its return value. |
| 131 | + |
| 132 | +```ocaml |
| 133 | +type example = { |
| 134 | + name : string; |
| 135 | + num : int; |
| 136 | + mode : [ `A | `B | `Default ]; |
| 137 | +} |
| 138 | +
|
| 139 | +let mk_example name num mode = match mode with |
| 140 | + | Some 'a' -> { name; num; mode = `A} |
| 141 | + | Some 'b' -> { name; num; mode = `B} |
| 142 | + | Some _ | None -> { name; num; mode = `Default} |
| 143 | +
|
| 144 | +let mk_example_re = function%mik |
| 145 | + | {|/ (['a'-'z'] as name := String.capitalize_ascii) ' ' (digit+ as num : int) ' ' ('a'|'b' as mode)? >>> mk_example as res /|} -> (* (res : example) available here, and all other bound variables *) |
| 146 | + | _ -> ... |
| 147 | +``` |
| 148 | + |
| 149 | +## Case Insensitive Match |
| 150 | + |
| 151 | +You can use `%mik_i`: `match%mik_i` and `function%mik_i`. (not available at the variable level) |
| 152 | + |
| 153 | +## Alternatives |
| 154 | +### Defining variables |
| 155 | +You have a choice between: |
| 156 | +```ocaml |
| 157 | +let%mik re = {|some regex|} |
| 158 | +(* and *) |
| 159 | +let re = {%mik|some regex|} |
| 160 | +``` |
| 161 | + |
| 162 | +No `/` delimiters are needed here. |
| 163 | + |
| 164 | +### Matching: |
| 165 | +#### `match%mik` and `function%mik` |
| 166 | + |
| 167 | +```ocaml |
| 168 | +function%mik |
| 169 | + | {|/ some regex /|} -> ... |
| 170 | + | {|/ another regex /|} -> ... |
| 171 | + ... |
| 172 | + | _ -> ... |
| 173 | +``` |
| 174 | + |
| 175 | +This match expression will compile all of the REs in the branches into one, and use marks to find which branch was executed. |
| 176 | +Efficient if you have multiple branches. |
| 177 | + |
| 178 | +#### General match/function |
| 179 | + |
| 180 | +```ocaml |
| 181 | +function |
| 182 | + | "some string" -> ... |
| 183 | + | {%mik|/ some regex /|} -> ... |
| 184 | + ... |
| 185 | + | "another string" -> ... |
| 186 | + | {%mik_i|/ another regex /|} -> ... (* case insensitive *) |
| 187 | + | _ -> ... |
| 188 | +``` |
| 189 | + |
| 190 | +This match expression will compile all of the REs **individually**, and test each one in sequence. |
| 191 | +Recommended if you only matching one RE. It is less efficient than the first option for more than one RE, but allows raw string matching. |
| 192 | + |
| 193 | +It keeps all of the features of the previous extension, explored in [Semantics](#Semantics_and_Examples) |
0 commit comments