Skip to content

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.

  • Literal is one or more characters in order.
  • Class is one character from a set of code points: inclusive ranges in

order. With none it matches nothing.

  • Look is a position: the start or end of the text or a line, or a word

boundary.

type Assertion =
| Start
| End
| StartLF
| EndLF
| StartCRLF
| EndCRLF
| WordAscii
| WordAsciiNegate
| WordUnicode
| WordUnicodeNegate
| WordStartAscii
| WordEndAscii
| WordStartUnicode
| WordEndUnicode
| WordStartHalfAscii
| WordEndHalfAscii
| WordStartHalfUnicode
| WordEndHalfUnicode

What a Look asserts about its position:

  • the start or end of the text: \\A, \\z, and ^, $ without m
  • the start or end of a line, where \\n ends 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
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.

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 i is from bounds[i] to

bounds[i + 1] - 1, the last up to U+10FFFF.

  • next[s * classes + i] is the state after state s reads a character

of class i.

  • State 0 is a dead end: it matches nothing more.
  • The text matches if it ends in a state s with accepting[s].
type Inst =
| Chars(List[(Int, Int)])
| Split(Int, Int)
| Jump(Int)
| Save(Int)
| Assert(Assertion)
| Done

One step of a compiled regex (compile):

  • Chars: one character from these ranges, then the next step.
  • Split(a, b): both a and b, a preferred.
  • Jump(a): go on at a.
  • Save(k): the position is capture slot k, 2 * index where a group

starts and 2 * index + 1 where it ends.

  • Assert(a): go on only if a holds here.
  • Done: a match.
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.

type SmtMode =
| Exactly
| Under
| Over

How to_smt writes a class: as it is, or with at most max_smt_ranges() ranges, fewer characters (Under) or more (Over).

fn show(r: Regex) -> String

A 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.

fn match_code(text: String, pattern: String) -> Int

full_match for compiled code: 1 or 0, or -1 when pattern isn’t a regex (pattern_error says why).

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.

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.

fn max_steps() -> Int

The 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.

fn class_of(rs: List[(Int, Int)]) -> Regex

A class. One that matches a single character is that literal.

fn repetition(low: Int, high: Option[Int], greedy: Bool, sub: Regex) -> Regex

sub repeated from min to max times.

fn alternation(rs: List[Regex]) -> Regex

Any one of the regexes: nested alternations flattened, single characters and classes merged into one class, a common prefix taken out.

fn concat(rs: List[Regex]) -> Regex

The regexes in sequence. Nested sequences are flattened, adjacent literals joined, and Empty left out.

fn is_full_match(prog: List[Inst], text: String) -> Bool

Whether prog (compile) matches the whole of text.

fn pattern_error(pattern: String) -> String

Why pattern isn’t a regex ("" if it is).

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.

fn matcher(pattern: String) -> Matcher

How to match pattern. Compiled code builds it while compiling, for a literal pattern.

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.

fn max_dfa_states() -> Int

The most states, and table cells (states × classes), a DFA may have.

fn max_dfa_cells() -> Int
fn max_product_states() -> Int

The most pairs of states relations looks at.

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).

fn max_extra_repeats() -> Int

The most repetitions past its minimum samples makes of an unbounded repetition (*, +, {n,}).

fn run_matcher(m: Matcher, text: String) -> Bool

Whether m matches the whole of text. Never for NotARegex.

fn run_automaton(a: Automaton, text: String) -> Bool

Whether the automaton ends in an accepting state on text.

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.

fn max_smt_char() -> Int where it == 131071

The 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.

fn max_smt_ranges() -> Int where it == 24

Classes with more ranges than this are approximated.

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().

fn accepting_flags(a: Automaton) -> List[Int]

1 for each accepting state of a, 0 for the others, as run_parts takes them.

fn run_parts(
text: String,
bounds: List[Int],
classes: Int,
next: List[Int],
accepting: List[Int],
start: Int,
) -> Bool

run_automaton on an automaton’s parts, with accepting as accepting_flags gives it. Compiled code keeps a literal pattern’s parts as constants.