Module regex/error
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
Everything Regex.new can reject, split by what the user has to fix.
Variants
| Variant | Fields | Description |
|---|---|---|
UnexpectedEnd | pos: usize | The pattern ended where an expression was expected, e.g. |
TrailingBackslash | pos: usize | The pattern ends with a |
UnterminatedCharClass | pos: usize | A |
UnterminatedGroup | pos: usize | A |
UnmatchedCloseParen | pos: usize | A |
InvalidQuantifier | pos: usize | A |
QuantifierOutOfOrder | min: usize, max: usize, pos: usize | A |
InvalidBackreference | group: usize, pos: usize |
|
InvalidNamedBackreference | pos: usize |
|
UnknownGroupName | name: String, pos: usize |
|
InvalidUnicodeProperty | pos: usize |
|
UnknownUnicodeProperty | name: String, pos: usize |
|
InvalidFlag | flag: u8 | A flag character outside |
DuplicateFlag | flag: 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) -> StringRender self under spec. An unrecognised spec degrades to the plain
to_string() rendering rather than failing.
Parameters
| Name | Type | Notes |
|---|---|---|
self | Self | |
spec | str |
Returns: String
Methods
to_string : (RegexError) fn(self : RegexError) -> Stringsource : (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
| Name | Type | Notes |
|---|---|---|
self | RegexError |