Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function%mikmatch {|/ "hello" / u|} -> "matched" (* matches "hello world" *)

Available in patterns:

**POSIX character classes:** `lower`, `upper`, `alpha`, `digit`, `alnum`, `punct`, `graph`, `print`, `blank`, `space`, `cntrl`, `xdigit`
**POSIX character classes:** `lower`, `upper`, `alpha`, `digit`, `alnum`, `punct`, `graph`, `print`, `blank`, `space`, `cntrl`, `xdigit` (7-bit ASCII, i.e. POSIX locale)

**Control sequences:**
- `bos` - beginning of string (`^`)
Expand Down
26 changes: 13 additions & 13 deletions lib/mik_lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ let new_line lexbuf =
Lexing.new_line lexbuf

let predefined_classes = [
("lower", {|[[:lower:]]|});
("upper", {|[[:upper:]]|});
("alpha", {|[[:alpha:]]|});
("digit", {|[[:digit:]]|});
("alnum", {|[[:alnum:]]|});
("punct", {|[[:punct:]]|});
("graph", {|[[:graph:]]|});
("print", {|[[:print:]]|});
("blank", {|[[:blank:]]|});
("cntrl", {|[[:cntrl:]]|});
("xdigit", {|[[:xdigit:]]|});
("space", {|[[:space:]]|});
(* ("word", {|[[:word:]]|}); *)
("lower", {|[a-z]|});
("upper", {|[A-Z]|});
("alpha", {|[A-Za-z]|});
("digit", {|[0-9]|});
("alnum", {|[0-9A-Za-z]|});
("punct", {|[!-/:-@[-`{-~]|});
("graph", {|[!-~]|});
("print", {|[ -~]|});
("blank", {|[\t ]|});
("cntrl", "[\x00-\x1f\x7f]");
("xdigit", {|[0-9A-Fa-f]]|});
("space", {|[\t-\r ]|});
(* ("word", {|[0-9A-Za-z_]|}); *)
("eos", {|$|});
("eol", {|$|[\n]|});
("bnd", {|\b|});
Expand Down
23 changes: 22 additions & 1 deletion tests/test_ppx_mikmatch.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ let test_basic_matching _ =
assert_equal "matched hello" (match_hello "hello");
assert_equal "no match" (match_hello "world");

let match_lower = function%mikmatch {| lower |} -> "lower" | _ -> "not lower" in
assert_equal "lower" (match_lower "a");
assert_equal "not lower" (match_lower "A");
assert_equal "not lower" (match_lower "\xb5");

let match_cntrl = function%mikmatch {| cntrl |} -> "control character" | _ -> "not a control character" in
assert_equal "control character" (match_cntrl "\x00");
assert_equal "control character" (match_cntrl "\x01");
assert_equal "control character" (match_cntrl "\t");
assert_equal "control character" (match_cntrl "\n");
assert_equal "control character" (match_cntrl "\x1f");
assert_equal "control character" (match_cntrl "\x7f");
assert_equal "not a control character" (match_cntrl "");
assert_equal "not a control character" (match_cntrl "\x00\x00");
assert_equal "not a control character" (match_cntrl " ");
assert_equal "not a control character" (match_cntrl "~");
assert_equal "not a control character" (match_cntrl "\x80");
assert_equal "not a control character" (match_cntrl "\x81");
assert_equal "not a control character" (match_cntrl "\x9f");
assert_equal "not a control character" (match_cntrl "\xff");

let match_digit = function%mikmatch {| digit |} -> "single digit" | _ -> "not a digit" in
assert_equal "single digit" (match_digit "5");
assert_equal "not a digit" (match_digit "a");
Expand Down Expand Up @@ -422,7 +443,7 @@ let test_mixed_matching _ =

assert_equal "got a" (no_default_case "a");
assert_equal "got b" (no_default_case "b");
assert_raises (Failure "File tests/test_ppx_mikmatch.ml, lines 417-419, characters 24-33: String did not match any mikmatch cases.")
assert_raises (Failure "File tests/test_ppx_mikmatch.ml, lines 438-440, characters 24-33: String did not match any mikmatch cases.")
(fun () -> no_default_case "c")

type mode =
Expand Down
Loading