std.regex
Regular expressions in regex-syntax’s syntax, matched by a Pike VM or a DFA.
It also gives strata verify its model of matches(s, pattern), sample texts for fuzzing, and the relations between two patterns.
type Regex = | Empty | Literal(String) | Class(List[(Int, Int)]) | Look(Assertion) | Repeat { min: Int, max: Option[Int], greedy: Bool, sub: Regex } | Capture { index: Int, name: Option[String], sub: Regex } | Concat(List[Regex]) | Alternation(List[Regex])A regular expression: what parse makes of a pattern.
Literalis one or more characters in order.Classis one character from a set of code points: inclusive ranges in
order. With none it matches nothing.
Lookis a position: the start or end of the text or a line, or a word
boundary.
Assertion
Section titled “Assertion”type Assertion = | Start | End | StartLF | EndLF | StartCRLF | EndCRLF | WordAscii | WordAsciiNegate | WordUnicode | WordUnicodeNegate | WordStartAscii | WordEndAscii | WordStartUnicode | WordEndUnicode | WordStartHalfAscii | WordEndHalfAscii | WordStartHalfUnicode | WordEndHalfUnicodeWhat a Look asserts about its position:
- the start or end of the text:
\\A,\\z, and^,$withoutm - the start or end of a line, where
\\nends lines ((?m)^,LF) or
\\r, \\n and \\r\\n do ((?mR)^, CRLF)
- a word boundary (
\\b) or not (\\B,Negate), with ASCII word
characters ((?-u)) or Unicode’s (\\w)
- a word starting (
\\b{start},\\<) or ending Half: only no word character before (\\b{start-half}) or after
Matcher
Section titled “Matcher”type Matcher = | Deterministic(Automaton) | Pike(List[Inst]) | NotARegex(String)How a pattern is matched: by a DFA, by the Pike VM, or not at all because it isn’t a regex. The Pike VM takes patterns a DFA can’t do: word boundaries, lines, or too many states.
Automaton
Section titled “Automaton”type Automaton = { bounds: List[Int], classes: Int, next: List[Int], accepting: List[Bool], start: Int,}A deterministic automaton over code points.
- The characters are split into classes. Class
iis frombounds[i]to
bounds[i + 1] - 1, the last up to U+10FFFF.
next[s * classes + i]is the state after statesreads a character
of class i.
- State 0 is a dead end: it matches nothing more.
- The text matches if it ends in a state
swithaccepting[s].
type Inst = | Chars(List[(Int, Int)]) | Split(Int, Int) | Jump(Int) | Save(Int) | Assert(Assertion) | DoneOne step of a compiled regex (compile):
Chars: one character from these ranges, then the next step.Split(a, b): bothaandb,apreferred.Jump(a): go on ata.Save(k): the position is capture slotk,2 * indexwhere a group
starts and 2 * index + 1 where it ends.
Assert(a): go on only ifaholds here.Done: a match.
SmtModel
Section titled “SmtModel”type SmtModel = | ExactModel(String) | Between(String, String)How the prover models matches(s, pattern). ExactModel(r) is s in
r.
A class of more than max_smt_ranges() ranges (\\w, \\p{L}) takes Z3
too long. Between(under, over) holds a subset of the class’s ranges and
a superset with fewer gaps.
Since regex operators are monotone, under <= pattern <= over.
SmtMode
Section titled “SmtMode”type SmtMode = | Exactly | Under | OverHow to_smt writes a class: as it is, or with at most max_smt_ranges()
ranges, fewer characters (Under) or more (Over).
Functions
Section titled “Functions”fn show(r: Regex) -> StringA regex as text that shows its tree exactly: lit(61 62) with code points
in hex, cls(30-39 61-66), look(Start), rep(0,*,greedy,…),
cap(1,name,…) with - for no name, cat(…), alt(…), empty.
match_code
Section titled “match_code”fn match_code(text: String, pattern: String) -> Intfull_match for compiled code: 1 or 0, or -1 when pattern isn’t a regex
(pattern_error says why).
full_match
Section titled “full_match”fn full_match(text: String, pattern: String) -> Result[Bool, String]Whether pattern matches the whole of text, as matches(text, pattern)
means, or why pattern isn’t a regex.
compile
Section titled “compile”fn compile(r: Regex) -> Result[List[Inst], String]The regex as steps of a Thompson NFA ending with Done. Fails when it has
too many steps.
max_steps
Section titled “max_steps”fn max_steps() -> IntThe most steps a compiled regex may have. a{1000}{1000} would have a
million.
fn parse(pattern: String) -> Result[Regex, String]A pattern read as a Regex, or why it isn’t one.
class_of
Section titled “class_of”fn class_of(rs: List[(Int, Int)]) -> RegexA class. One that matches a single character is that literal.
repetition
Section titled “repetition”fn repetition(low: Int, high: Option[Int], greedy: Bool, sub: Regex) -> Regexsub repeated from min to max times.
alternation
Section titled “alternation”fn alternation(rs: List[Regex]) -> RegexAny one of the regexes: nested alternations flattened, single characters and classes merged into one class, a common prefix taken out.
concat
Section titled “concat”fn concat(rs: List[Regex]) -> RegexThe regexes in sequence. Nested sequences are flattened, adjacent literals
joined, and Empty left out.
is_full_match
Section titled “is_full_match”fn is_full_match(prog: List[Inst], text: String) -> BoolWhether prog (compile) matches the whole of text.
pattern_error
Section titled “pattern_error”fn pattern_error(pattern: String) -> StringWhy pattern isn’t a regex ("" if it is).
relations
Section titled “relations”fn relations(a: String, b: String) -> (Bool, Bool, Bool)How the languages of patterns a and b relate, as (a's within b's, b's within a's, none in common).
Each is false when it doesn’t hold or can’t be decided: a pattern a DFA
can’t match, or too large a search.
Decided on the product of the two DFAs. The prover uses it.
matcher
Section titled “matcher”fn matcher(pattern: String) -> MatcherHow to match pattern. Compiled code builds it while compiling, for a
literal pattern.
automaton
Section titled “automaton”fn automaton(prog: List[Inst]) -> Option[Automaton]The DFA of a compiled regex, by subset construction.
None if it asserts more than the start or end of the text, or has too
many states.
max_dfa_states
Section titled “max_dfa_states”fn max_dfa_states() -> IntThe most states, and table cells (states × classes), a DFA may have.
max_dfa_cells
Section titled “max_dfa_cells”fn max_dfa_cells() -> Intmax_product_states
Section titled “max_product_states”fn max_product_states() -> IntThe most pairs of states relations looks at.
samples
Section titled “samples”fn samples(pattern: String, n: Int, seed: Int) -> Result[List[String], String]n texts that pattern matches whole, made from its tree. The same
seed gives the same texts.
Assertions other than the text’s start and end may not hold in a text
made, so each text is kept only if it matches. There may be fewer than
n.
Err when the pattern isn’t a regex, or no text made matched (a\\bb).
max_extra_repeats
Section titled “max_extra_repeats”fn max_extra_repeats() -> IntThe most repetitions past its minimum samples makes of an unbounded
repetition (*, +, {n,}).
run_matcher
Section titled “run_matcher”fn run_matcher(m: Matcher, text: String) -> BoolWhether m matches the whole of text. Never for NotARegex.
run_automaton
Section titled “run_automaton”fn run_automaton(a: Automaton, text: String) -> BoolWhether the automaton ends in an accepting state on text.
smt_model
Section titled “smt_model”fn smt_model(pattern: String) -> Option[SmtModel]The prover’s model of pattern. None when its meaning can’t be written
exactly: word boundaries, line anchors, anchors not certain to be at the
very start or end, literals past max_smt_char(), or no regex at all.
max_smt_char
Section titled “max_smt_char”fn max_smt_char() -> Int where it == 131071The last code point the prover models as itself. The SMT characters past
it, to smt_alphabet_end(), stand for the code points past it, one
each, as compiler/smt’s ord reads them. A literal past it can’t be
written.
max_smt_ranges
Section titled “max_smt_ranges”fn max_smt_ranges() -> Int where it == 24Classes with more ranges than this are approximated.
smt_exact
Section titled “smt_exact”fn smt_exact(pattern: String) -> Option[String]pattern translated exactly, however large its classes; None also
when a class splits the code points past max_smt_char().
accepting_flags
Section titled “accepting_flags”fn accepting_flags(a: Automaton) -> List[Int]1 for each accepting state of a, 0 for the others, as run_parts
takes them.
run_parts
Section titled “run_parts”fn run_parts( text: String, bounds: List[Int], classes: Int, next: List[Int], accepting: List[Int], start: Int,) -> Boolrun_automaton on an automaton’s parts, with accepting as
accepting_flags gives it. Compiled code keeps a literal pattern’s parts
as constants.