abla/regex

Generated from source.

Import with import "abla/regex".

Source: abla/regex/entry.ab

RegexError (class)

class RegexError(val message: string, val offset: int)

A failed pattern compilation's message and zero-based byte offset in the pattern.

Regex (class)

class Regex(
    val pattern: string,
    val groupCount: int,
    val program: array<int>,
    val classes: array<array<int>>,
    // 256 bytes, one per byte value: 1 when a match can start with that byte (every byte of 128 and over is 1),
    // or "" when a match can start anywhere (an empty match, an assertion or `.` is reachable at the start).
    val first: string,
    // Optional frozen plans for small capture-free literals. The public
    // program and classes retain their normal representation.
    val literalChoices: array<string>? = null,
    val literalDfa: string = "",
    // Runtime search plan, or null when the pattern has no fixed-width ASCII fast path.
    val fixedBytes: RegexFixedBytes? = null
)

program holds (operation, x, y) triples; classes holds [low, high, ...] code point ranges. A failed compile returns an empty Regex.

Regex.matches (method)

fun matches(text: string): bool

True when the whole text matches.

Regex.matchFull (method)

fun matchFull(text: string): RegexMatch?

The match of the whole text with its groups, or null.

Regex.find (method)

fun find(text: string, start: int = 0): RegexMatch?

The leftmost match starting at or after byte offset start, or null.

Regex.findWithin (method)

fun findWithin(text: string, maximumSteps: int, start: int = 0): RegexSearchResult

Finds the leftmost match using at most maximumSteps search steps. An invalid start is a miss. Exhaustion returns its own result without a partial match.

Regex.findAllWithin (method)

fun findAllWithin(text: string, maximumSteps: int, start: int = 0): RegexFindAllResult

Every non-overlapping match from byte offset start, as findAll, using at most maximumSteps search steps in all (one budget for every search of the call; a step is as in findWithin). When the budget runs out the result has the matches completed before it and resumeAt: findAllWithin(text, steps, resumeAt) continues, and the matches of the calls joined are those of findAll. An invalid start, or a failed compile, is done with no match.

Regex.replaceWithin (method)

fun replaceWithin(text: string, replacement: string, maximumSteps: int): RegexReplaceResult

replace using at most maximumSteps search steps in all (a step is as in findWithin). When the budget runs out the result is RegexReplaceExhausted, with no partially replaced text.

Regex.findStart (method)

fun findStart(text: string, start: int = 0): int

Byte offset of the first match at or after start, or -1.

Regex.matchPrefixEnd (method)

fun matchPrefixEnd(text: string): int

Exclusive byte offset of a match starting at byte zero, or -1.

Regex.findWork (method)

fun findWork(text: string): int

The general VM work for find(text) (instructions added to a thread list and threads advanced). Never more than 2 (text.size + 1) program size, whatever the pattern.

Regex.findAllWork (method)

fun findAllWork(text: string): int

A diagnostic: the VM steps findAll(text) (and so replace) takes. Each match restarts the search where the previous one ended, so a pattern whose longer alternative keeps running after a shorter one has matched (a+b|a over aaaa…) takes up to (matches + 1) times the bound of findWork.

Regex.findAll (method)

fun findAll(text: string): array<RegexMatch>

Every non-overlapping match, left to right.

Regex.replace (method)

fun replace(text: string, replacement: string): string

Replace every match. In replacement, $0 is the whole match, $1 and up are groups (all digits after the $, ${12} also works), and $$ is a dollar sign. A group that did not take part, or does not exist, is empty; a $ followed by anything else stays a dollar sign.

RegexSearchResult (interface)

sealed interface RegexSearchResult

The outcomes of a bounded search.

RegexSearchMatch (class)

class RegexSearchMatch(val matched: RegexMatch) : RegexSearchResult

A completed search that found the leftmost match and its capture spans.

RegexSearchMiss (class)

class RegexSearchMiss : RegexSearchResult

A completed search without a match, or a search with a start offset out of range.

RegexSearchExhausted (class)

class RegexSearchExhausted : RegexSearchResult

A search that needed another step after spending its budget.

RegexFindAllResult (interface)

sealed interface RegexFindAllResult

The outcomes of findAllWithin.

RegexFindAllDone (class)

class RegexFindAllDone(val matches: array<RegexMatch>) : RegexFindAllResult

Every match, the same as findAll from the same start.

RegexFindAllExhausted (class)

class RegexFindAllExhausted(val matches: array<RegexMatch>, val resumeAt: int) : RegexFindAllResult

The budget ran out: the matches completed before it, and the byte offset a continuation starts at.

RegexReplaceResult (interface)

sealed interface RegexReplaceResult

The outcomes of replaceWithin.

RegexReplaceDone (class)

class RegexReplaceDone(val text: string) : RegexReplaceResult

The text with every match replaced, the same as replace.

RegexReplaceExhausted (class)

class RegexReplaceExhausted : RegexReplaceResult

The budget ran out; there is no partially replaced text.

RegexCaptureName (class)

class RegexCaptureName(val name: string, val slot: int, val begin: int, val end: int)

Holds a named capture's group slot and source span in the pattern.

RegexCompileResult (class)

class RegexCompileResult(
    val regex: Regex, val succeeded: bool, val error: RegexError,
    val captureNames: array<RegexCaptureName> = [], val requiredCaptures: array<bool> = []
)

A compiled regex when succeeded is true. On failure the regex is empty and error gives the reason and location.

NamedRegex (class)

class NamedRegex<Captures>(val regex: Regex, val project: (RegexMatch) -> Captures)

Matcher for a literal with named captures. The underlying Regex keeps the indexed match API.

NamedRegex.matches (method)

fun matches(text: string): bool

Returns true if the pattern matches the entire input.

NamedRegex.replace (method)

fun replace(text: string, replacement: string): string

Returns the input with every non-overlapping match replaced using the regex replacement syntax.

NamedRegex.matchFull (method)

fun matchFull(text: string): Captures?

Returns the named captures from a full-input match, or null if it fails.

NamedRegex.find (method)

fun find(text: string, start: int = 0): Captures?

Returns the first projected match at or after the byte offset, or null for no match or an invalid offset.

NamedRegex.findAll (method)

fun findAll(text: string): array<Captures>

Returns projected captures for every non-overlapping match in source order.

ablaRegexRequiredCapture (fun)

fun ablaRegexRequiredCapture(matched: RegexMatch, index: int): string

Used only for generated fields proved to participate on every successful path.

RegexMatch (class)

class RegexMatch(val text: string, val spans: array<int>)

A match over the original input text. Read whole-match or capture spans with the accessors, which use byte offsets.

RegexMatch.start (method)

fun start(): int

Returns the byte offset at the start of the whole match.

RegexMatch.end (method)

fun end(): int

Returns the exclusive byte offset at the end of the whole match.

RegexMatch.text0 (method)

fun text0(): string

Returns the substring covered by the whole match.

RegexMatch.groupCount (method)

fun groupCount(): int

Number of capturing groups, not counting the whole match.

RegexMatch.hasGroup (method)

fun hasGroup(index: int): bool

True when group index (0 is the whole match) took part in the match.

RegexMatch.groupStart (method)

fun groupStart(index: int): int

Returns the start byte offset for capture index, with zero meaning the whole match. Returns -1 for an out-of-range or unmatched capture.

RegexMatch.groupEnd (method)

fun groupEnd(index: int): int

Returns the exclusive end byte offset for capture index, with zero meaning the whole match. Returns -1 for an out-of-range or unmatched capture.

RegexMatch.group (method)

fun group(index: int): string?

The text of group index, or null when it did not take part.

RegexNode (class)

class RegexNode(val kind: int, val a: int, val b: int, val c: int, val kids: array<RegexNode>)

Node kinds: 0 empty, 1 char(a), 2 any, 3 class(a = index, b = negated), 4 concatenation, 5 alternation, 6 repeat(a = min, b = max or -1, c = lazy), 7 group(a = capture index or -1), 8 assertion(a: 0 ^, 1 $, 2 \b, 3 \B).

RegexParser (class)

class RegexParser(
    val src: string,
    var pos: int,
    var groups: int,
    var depth: int,
    var failed: bool,
    var message: string,
    var errorOffset: int,
    val classes: array<array<int>>,
    val captureNames: array<RegexCaptureName> = []
)

Tracks the cursor, capture slots, character classes, and first error while parsing a UTF-8 regex.

RegexParser.fail (method)

fun fail(message: string, offset: int): RegexNode

Records a parse failure with its source byte offset.

RegexParser.peek (method)

fun peek(): int

Returns the next pattern byte or -1 at the end of the source.

RegexParser.codePoint (method)

fun codePoint(): int

The code point at pos; the pattern was checked to be valid UTF-8.

RegexParser.advanceCodePoint (method)

fun advanceCodePoint(): void

Advances past one UTF-8 scalar from a valid current source position.

RegexParser.addClass (method)

fun addClass(ranges: array<int>): int

Copies the class's ranges into the parser table and returns its index.

RegexParser.parseAlternation (method)

fun parseAlternation(): RegexNode

Returns syntax for the current pipe-separated branches, recording an error for malformed pattern syntax.

RegexParser.parseConcatenation (method)

fun parseConcatenation(): RegexNode

Returns syntax for consecutive terms up to a pipe, closing group, or end of input.

RegexParser.repeatBraces (method)

fun repeatBraces(): array<int>

Reads {m}, {m,}, {m,n}, {,n} or {,} at pos. Returns [min, max, length] with max -1 for open, [] when the text is not a repeat, or [-1, message] is not representable, so a bad interval is reported through failed.

RegexParser.parseQuantifiers (method)

fun parseQuantifiers(atom: RegexNode, atomStart: int): RegexNode

Returns the atom with any following repetition applied and records an error for unsupported or malformed repeats.

RegexParser.parseAtom (method)

fun parseAtom(): RegexNode

Returns the next pattern atom and records an error for an invalid or unexpected token.

RegexParser.captureSlot (method)

fun captureSlot(open: int): int

Allocates and returns the next capture slot, recording a parse error when the group limit is exceeded.

RegexParser.namedCapture (method)

fun namedCapture(open: int): int

Parses and returns a named capture's slot, recording an error for invalid or repeated names.

RegexParser.parseGroup (method)

fun parseGroup(): RegexNode

Returns a grouped expression and records a parse error for invalid group syntax or a missing closing parenthesis.

RegexParser.escapeScalar (method)

fun escapeScalar(letter: int): int

Returns the Unicode scalar for an escaped letter, recording an error for malformed or unsupported escapes.

RegexParser.hexDigits (method)

fun hexDigits(count: int): int

Reads count hex digits at pos; -1 when they are not all there.

RegexParser.escapedScalar (method)

fun escapedScalar(): int

After \ in an atom or a class: a code point, or -1 with failed set. Shorthand letters are left to the callers.

RegexParser.parseEscape (method)

fun parseEscape(): RegexNode

Returns syntax for one escaped scalar, class, or assertion and records an error for malformed escapes.

RegexParser.parseClass (method)

fun parseClass(): RegexNode

Returns syntax for a character class, recording an error if its ranges or closing bracket are invalid.

RegexParser.classAtom (method)

fun classAtom(var ranges: array<int>): int

One class member: a code point, or -1 after adding a shorthand set to ranges (or after failing).

RegexEmitter (class)

class RegexEmitter(val program: array<int>, var failed: bool, var groupSlots: int, val limit: int)

Tracks the regex VM instructions and reports when the program exceeds its instruction limit.

RegexEmitter.count (method)

fun count(): int

Returns the number of encoded VM instructions.

RegexEmitter.add (method)

fun add(op: int, x: int, y: int): int

Appends an instruction and returns its index, marking the emitter failed if the program limit is exceeded.

RegexEmitter.setX (method)

fun setX(at: int, x: int): void

Replaces the first operand at an existing instruction index.

RegexEmitter.setY (method)

fun setY(at: int, y: int): void

Replaces the second operand at an existing instruction index.

RegexEmitter.emit (method)

fun emit(node: RegexNode): void

Appends the instructions for a parsed pattern node, stopping once the emitter records a failure.

RegexEmitter.fillSplit (method)

fun fillSplit(split: int, enter: int, leave: int, lazy: int): void

Greedy tries enter first; lazy tries leave first.

regexCompile (fun)

fun regexCompile(pattern: string): RegexCompileResult

Compile pattern. On failure error has the message and its byte offset. A pattern whose program has more than 100,000 instructions is refused ("the pattern is too large"); counted repeats multiply when nested, so (a{1,30}){1,30} is 1,863 instructions and ((a{1000}){1000}){1000} is far over. A search costs up to 2 (text size + 1) instructions steps, so for patterns from an untrusted source use regexCompileWithin.

regexCompileWithin (fun)

fun regexCompileWithin(pattern: string, maximumInstructions: int): RegexCompileResult

As regexCompile, refusing a program of more than maximumInstructions instructions (at most 100,000).

RegexVm (class)

class RegexVm(
    val program: array<int>,
    val classes: array<array<int>>,
    val text: string,
    val n: int,
    val slots: int,
    val pcs: array<int>,
    val store: array<int>,
    val counts: array<int>,
    val seen: array<int>,
    var generation: int,
    val stackPcs: array<int>,
    var work: int
)

All capture slots live in one flat store of entries, slots ints each: entries [0, 2n) are the two thread lists (list 0 first, list 1 from n), [2n, 4n + 2) the work stack, then a start entry, a result entry and a work entry. seen marks the instructions already added in this generation. Holds the compiled program, input text, and scratch state for matching.

RegexVm.assertionHolds (method)

fun assertionHolds(kind: int, position: int): bool

Returns whether the requested anchor or word-boundary condition holds at the byte position.

RegexVm.startEntry (method)

fun startEntry(): int

Returns the scratch entry used to store the initial capture state.

RegexVm.resultEntry (method)

fun resultEntry(): int

Returns the scratch entry that holds the current match result.

RegexVm.workEntry (method)

fun workEntry(): int

Returns the scratch entry used for temporary capture state.

RegexVm.copyEntry (method)

fun copyEntry(from: int, to: int): void

Copies all capture slots from one valid scratch entry to another.

RegexVm.add (method)

fun add(list: int, pc0: int, source: int, position: int): void

Adds the thread at pc0 with the captures of entry source, and everything reachable without consuming text, in priority order.

RegexVm.add2 (method)

fun add2(list: int, pc0: int, start0: int, position: int): void

add for a program without capture groups (two slots: the match's start and end). Every thread added from one source shares its start, and a path's end is set by the one save 1 before the match instruction, so the captures are two scalars and the work stack holds only instruction numbers: no entry is copied.

RegexVm.classMatches (method)

fun classMatches(index: int, c: int): bool

Returns true if the code point matches the indexed character class and its negation rule. Binary search over the class's sorted, disjoint ranges.

RegexFixedBytes (class)

class RegexFixedBytes(val width: int, val starts: int, val accepts: int, val masks: string)

A Shift-And search plan for equal-width ASCII alternatives. Each bit represents one position in an alternative, and the table maps each input byte to the positions it can match. Regex compilation leaves this plan absent when the pattern is ineligible.

ablaRegexSyntaxIntegers (compile.fun)

compile fun ablaRegexSyntaxIntegers(values: array<int>): SyntaxExpression

Literal compilation shares the runtime parser and limits. The frozen matcher carries the program and any bounded specialization.

ablaRegexNamePart (compile.fun)

compile fun ablaRegexNamePart(text: string): string

Encode module identity and site key separately so no character is lost.

ablaRegexFinalizeCaptures (compile.fun)

compile fun ablaRegexFinalizeCaptures(source: string, origin: SyntaxExpression): string

Returns the generated capture declaration source for the compiler to publish.

ablaRegexNamedMatcher (compile.fun)

compile fun ablaRegexNamedMatcher(cursor: ParserCursor, pattern: SyntaxExpression,
    compiled: RegexCompileResult, constructor: SyntaxExpression): SyntaxExpression

Returns syntax that wraps the compiled regex with a generated typed capture projection. Only the stdlib Regex constructor runs at compile time; the generated factory is called at run time. Its name is one any module can declare, so a # site calling it by name could reach a dependency's function of that name with the using file's grants (#1139 area 1, round 5).

ablaRegexParseLiteral (compile.fun)

compile fun ablaRegexParseLiteral(cursor: ParserCursor): SyntaxExpression

Returns syntax for a regex literal and records a parser diagnostic for invalid literal or pattern syntax.

$regex (sub-language)

#compilerRegisterSubparser("regex", ablaRegexParseLiteral)

Registers the regex literal syntax, including the typed projection for named capture groups.

Built from Abla v0.2.79 source ba5d6b7d. Examples and generated signatures follow that source revision.