Module regex/error

regex/error
Stability: unstable — the enum is EXHAUSTIVE by construction (`derive(Error)` matches the messages against the variants in declaration order, so a variant added, removed or reordered is a compile error), which is exactly what makes it unsafe to freeze while the engine is still growing. Every new syntax the parser learns to reject adds a variant, and a caller who matched all fourteen arms without a `_` then fails to compile — so additions here are not additive in practice. Freezing waits on the parser's grammar settling, or on a documented "non-exhaustive, always write a `_` arm" promise. The shape (one variant per fixable fault, byte offsets into the pattern, `Error` so it can travel as an `AnyError`) is settled and matches D1. — stable modules only change additively; this one may still change.

Typed error for std/regex pattern and flag compilation.

Regex.new is a pure fallible transform, so it reports failure as Result(Regex, RegexError) — never as a string (see .github/instructions/yo-design.instructions.md, "std error handling"). Every variant is matchable, and RegexError implements Error so it can also be thrown as an AnyError.

Positions are byte offsets into the pattern, recording where the parser stopped — not necessarily where the construct began.

Example

{ Regex, RegexError } :: import("std/regex");

match(
  Regex.new(`a{3,1}`),
  .Ok(re) => (),
  .Err(e) => match(
    e,
    .QuantifierOutOfOrder(min, max, pos) => println(`bad {${min},${max}}`),
    _ => println(e)
  )
);

Stability

unstable — the enum is EXHAUSTIVE by construction (derive(Error) matches the messages against the variants in declaration order, so a variant added, removed or reordered is a compile error), which is exactly what makes it unsafe to freeze while the engine is still growing. Every new syntax the parser learns to reject adds a variant, and a caller who matched all fourteen arms without a _ then fails to compile — so additions here are not additive in practice. Freezing waits on the parser's grammar settling, or on a documented "non-exhaustive, always write a _ arm" promise. The shape (one variant per fixable fault, byte offsets into the pattern, Error so it can travel as an AnyError) is settled and matches D1.

Types

RegexError enum
RegexError

Everything Regex.new can reject, split by what the user has to fix.

Variants

VariantFieldsDescription
UnexpectedEndpos: usize

The pattern ended where an expression was expected, e.g. a| inside a {m,n} quantifier or after an operator.

TrailingBackslashpos: usize

The pattern ends with a \ that escapes nothing.

UnterminatedCharClasspos: usize

A [ character class was never closed with ].

UnterminatedGrouppos: usize

A ( group was never closed with ).

UnmatchedCloseParenpos: usize

A ) appeared with no ( to close.

InvalidQuantifierpos: usize

A {m,n} quantifier is malformed — a missing bound, or a missing ,/}.

QuantifierOutOfOrdermin: usize, max: usize, pos: usize

A {m,n} quantifier whose upper bound is below its lower bound.

InvalidBackreferencegroup: usize, pos: usize

\1\9 naming a group the pattern does not have.

InvalidNamedBackreferencepos: usize

\k not followed by <name>, or a \k<name that is never closed.

UnknownGroupNamename: String, pos: usize

\k<name> naming a group the pattern does not define.

InvalidUnicodePropertypos: usize

\p / \P not followed by {name}, or a \p{name that is never closed.

UnknownUnicodePropertyname: String, pos: usize

\p{name} / \P{name} naming a property this engine does not know.

InvalidFlagflag: u8

A flag character outside gimsuy.

DuplicateFlagflag: u8

The same flag character given more than once.

Trait Implementations

impl(generic(T : Type), where(T <: ToString), T : (ToString))
impl(generic(T : Type), where(T <: ToString), T : (ToString), Format)
format : fn(self : Self, spec : str) -> String

Render self under spec. An unrecognised spec degrades to the plain to_string() rendering rather than failing.

Parameters

NameTypeNotes
selfSelf
specstr

Returns: String

Methods
to_string : (RegexError) fn(self : RegexError) -> String

Parameters

NameTypeNotes
selfRegexError

Returns: String

source : (RegexError) fn(self : RegexError) -> Option(dyn( + ToString))

The error that caused this one, or .None at the root of the chain.

Rust's Error::source. Defaulted to .None, so an error with nothing underneath it implements the trait by saying only what it is; a wrapper overrides it to hand back what it wrapped. Walking the chain to the root cause is not expressible yet — the returned Dyn loses the Error trait on an erased receiver, so a caller can print one link but cannot follow it (#521, issues/self-trait-in-a-return-type-loses-the-trait-on-an-erased-receiver.md).

Parameters

NameTypeNotes
selfRegexError

Returns: Option(dyn( + ToString))