scoop:scoop.regex:0.1.0
src/errors.scoop
Selected production source · src/errors.scoop
1
package scoop.regex3
/** A PCRE2 execution failure category, distinct from an ordinary unsuccessful match. */4
public enum RegexFailureKind {5
/** The engine's match work limit was reached. */6
MatchLimit,7
/** The interpreter's depth limit was reached. */8
DepthLimit,9
/** The interpreter's heap limit was reached. */10
HeapLimit,11
/** The JIT stack was exhausted. */12
JitStackLimit,13
/** Native allocation failed. */14
Memory,15
/** Another native execution error occurred. */16
Other17
}19
/** A pattern compilation error with a Unicode scalar offset into the original pattern. */20
public class RegexSyntaxException public constructor(21
/** The supplied pattern. */ public val pattern: String,22
/** The Unicode scalar position reported by compilation. */ public val offset: Long,23
/** The original PCRE2 error code. */ public val nativeCode: Int,24
/** The copied native diagnostic. */ public val detail: String25
) : Exception(Some(detail))27
/** An error in a replacement template; an available offset counts Unicode scalars. */28
public class RegexReplacementException public constructor(29
/** The original PCRE2 error code. */ public val nativeCode: Int,30
/** The copied native diagnostic. */ public val detail: String,31
/** The template position, if the engine reports one. */ public val offset: Long?32
) : Exception(Some(detail))34
/** An execution or allocation failure, never converted into an ordinary nonmatch. */35
public class RegexMatchException public constructor(36
/** The execution failure category. */ public val kind: RegexFailureKind,37
/** The original PCRE2 error code. */ public val nativeCode: Int,38
/** The copied native diagnostic. */ public val detail: String39
) : Exception(Some(detail))41
internal fun regexDetail(code: Int): String {42
val buffer = MutableArray<UInt8>(256L) { _ -> 0.toUByte() }43
@Unsafe {44
return buffer.withDataPointer { data, count ->45
@Unsafe { return@withDataPointer String.fromUtf8(data, regexMessage(code, data, count)) }46
}47
}48
}50
internal fun matchFailure(code: Int): RegexMatchException {51
@Unsafe {52
val kind = regexFailureKind(code)53
return RegexMatchException(when (kind) {54
case 0 -> RegexFailureKind.MatchLimit55
case 1 -> RegexFailureKind.DepthLimit56
case 2 -> RegexFailureKind.HeapLimit57
case 3 -> RegexFailureKind.JitStackLimit58
case 4 -> RegexFailureKind.Memory59
else -> RegexFailureKind.Other60
}, code, regexDetail(code))61
}62
}