- See also
- mim::plug::regex
A normalizing regex plugin.
Dependencies
import compile;
plugin core;
plugin mem;
plugin cps;
Types
RE
Char and string types:
let Char = I8;
lam Str(n: Nat): * = %mem.Ptr0 «n; Char»;
Res n is a match result: the memory, a success flag, and the new position.
lam Res (n: Nat): * = [%mem.M 0, Bool, Idx n];
RE is the matcher itself: it takes the memory, the string, and the current position.
let RE = {n: Nat} → [%mem.M 0, Str n, Idx n] → Res n;
Meta
%regex.conj
A sequence of REs.
E.g., \d\d\d matches 3 digits: %regex.conj (%regex.cls.d, %regex.cls.d, %regex.cls.d).
axm %regex.conj: {n: Nat} → «n; RE» → RE, normalize_conj, 2;
%regex.disj
Matches any of the sub-expressions, e.g. [0123456789].
axm %regex.disj: {n: Nat} → «n; RE» → RE, normalize_disj, 2;
Values
%regex.range
Wraps a range of literals. E.g., use %regex.range ('a', 'z') to match all lower-case letters.
axm %regex.range: «2; Char» → RE, normalize_range, 1;
%regex.lit
Wraps a literal.
lam %regex.lit(val: Char) = %regex.range (val, val);
%regex.not_
Matches anything but the ranges in the parameter - the parameter must only contain %regex.disjs and %regex.ranges.
axm %regex.not_: RE → RE, normalize_not, 1;
%regex.neg_lookahead
Succeeds iff the parameter does not match, without consuming input.
axm %regex.neg_lookahead: RE → RE, 1;
%regex.cls.*
| Subtag | Matches |
| d | digits [0-9] |
| D | No digits |
| w | word characters [a-zA-Z_0-9] |
| W | No word characters |
| s | whitespace [ \t\r\n] |
| S | No whitespace |
let %regex.cls.d = %regex.range ('0', '9');
let %regex.cls.D = %regex.not_ %regex.cls.d;
let %regex.cls.w = %regex.disj (%regex.range ('0', '9'), %regex.range ('a', 'z'), %regex.range ('A', 'Z'), %regex.lit '_');
let %regex.cls.W = %regex.not_ %regex.cls.w;
let %regex.cls.s = %regex.disj (%regex.range ('\t', '\n'), %regex.lit '\r', %regex.lit ' ');
let %regex.cls.S = %regex.not_ %regex.cls.s;
%regex.any
Matches any character.
%regex.empty
Matches the empty string.
Quantifiers
%regex.quant.*
The usual quantifiers:
- optional: ? - zero or one
- star: * - zero or more
- plus: + - one or more
axm %regex.quant(optional,star,plus): RE → RE, normalize_quant, 1;
Phases
%regex.lower_regex
Compiles the regex combinators into an executable matcher (via a DFA).
axm %regex.lower_regex: %compile.Phase;