Library

regex

Thompson NFA regex engine.

1 file lib/regex/regex.vader 19 exports

MVP scope :

  • Literal bytes, ., escapes (\n / \t / \r / \\ / \. …).
  • Char classes : [abc], [^abc], [a-z] plus the \d / \D / \w / \W / \s / \S shorthands (ASCII-only).
  • Quantifiers : *, +, ?, {n}, {n,}, {n,m}, all greedy.
  • Alternation a|b.
  • Groups (…): every group captures, addressable by index 1..N.
  • Anchors ^, $ (whole-input, not multiline).

Hors-scope (post-MVP) : non-greedy *?, named groups (?<x>…), backrefs \1, lookaround (?=…) / (?!…), Unicode properties.

Algorithm : Thompson VM (Cox, "Regular Expression Matching: the Virtual Machine Approach"). Each compiled Pattern is an array of Inst ops ; matching runs a BFS over a Thread set, and add_thread deduplicates by pc within each step (the seen generation array), so a step holds at most one thread per instruction. That per-pc dedup is what makes the worst case O(n × m) (input length × program length), no catastrophic backtracking, and what bounds the epsilon-closure recursion on cyclic programs like (a*)*. Captures use the Save N op writing the current input offset into the thread's groups array.

Pure Vader, zero host imports: WASM-ready as-is.

Types

Pattern :: struct

Compiled regex. Treat as opaque: the field layout may change.

Match :: struct

A successful match. start and end are byte offsets into the input string (end exclusive). groups[0] is always the whole match ; groups[i] (1 ≤ i ≤ group_count) is the i-th capture, or empty if the group never matched.

Fields

start: i64
No description.
end: i64
No description.
groups: string[]
No description.
RegexError :: struct

Error returned by compile when the pattern doesn't parse.

Fields

msg: string
No description.
position: i64
No description.
Implements Error message :: fn(self) -> string
InstChar :: struct

Match a single literal codepoint c. Advances input by one char.

Fields

c: char
No description.
InstAny :: struct

Match any codepoint except \n.

InstClass :: struct

Match any codepoint in ranges. Each [lo, hi] is inclusive ; negated == true flips the membership test.

Fields

ranges: CharRange[]
No description.
negated: bool
No description.
CharRange :: struct

An inclusive [lo, hi] codepoint range inside an InstClass. Part of the public compiled-instruction surface: InstClass.ranges exposes these, so a consumer introspecting a compiled Inst can read them.

Fields

lo: char
No description.
hi: char
No description.
InstMatch :: struct

Accept state.

InstJump :: struct

Unconditional jump to target.

Fields

target: i64
No description.
InstSplit :: struct

Non-deterministic split: spawn one thread at a, another at b.

Fields

a: i64
No description.
b: i64
No description.
InstSave :: struct

Record the current input offset into groups[slot] (and advance pc). Slot 0 / 1 = group 0 start/end (whole match) ; 2 * i / 2 * i + 1 = group i start/end.

Fields

slot: i64
No description.
InstAnchorStart :: struct

^: match only at input position 0.

InstAnchorEnd :: struct

$: match only at input length.

Functions

compile :: fn(pattern: string) -> Pattern | RegexError

Compile a regex source string into a Pattern. Anchors ^/$ inside the pattern bind to the input boundaries. The pattern is implicitly anchored at the start during matches (whole input) and unanchored during find / find_all / replace_all.

Parameters

patternstring No description.

Returns

matches :: fn(self: Pattern, input: string) -> bool

True iff input is matched by pattern from offset 0 to its end (anchored on both sides).

Parameters

selfPattern No description.
inputstring No description.

Returns

bool
find :: fn(self: Pattern, input: string) -> Match | null

Find the first match anywhere in input (leftmost). Returns null when there's no match.

Parameters

selfPattern No description.
inputstring No description.

Returns

Match | null
find_all :: fn(self: Pattern, input: string) -> Match[]

All non-overlapping matches in input, left-to-right. Empty matches advance by one codepoint to avoid an infinite loop.

Parameters

selfPattern No description.
inputstring No description.

Returns

replace_all :: fn(self: Pattern, input: string, repl: string) -> string

Replace every non-overlapping match with repl. Supports $0..$9 backrefs inside repl.

Parameters

selfPattern No description.
inputstring No description.
replstring No description.

Returns

string
Written by vader doc. Icons: Codicons, CC BY 4.0. Fonts: Atkinson Hyperlegible, JetBrains Mono, OFL.