Scoop API

scoop:scoop.regex:0.1.0

src/errors.scoop

Selected production source · src/errors.scoop

1package scoop.regex
3/** A PCRE2 execution failure category, distinct from an ordinary unsuccessful match. */
4public 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 Other
19/** A pattern compilation error with a Unicode scalar offset into the original pattern. */
20public 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: String
25) : Exception(Some(detail))
27/** An error in a replacement template; an available offset counts Unicode scalars. */
28public 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. */
35public 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: String
39) : Exception(Some(detail))
41internal 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 }
50internal fun matchFailure(code: Int): RegexMatchException {
51 @Unsafe {
52 val kind = regexFailureKind(code)
53 return RegexMatchException(when (kind) {
54 case 0 -> RegexFailureKind.MatchLimit
55 case 1 -> RegexFailureKind.DepthLimit
56 case 2 -> RegexFailureKind.HeapLimit
57 case 3 -> RegexFailureKind.JitStackLimit
58 case 4 -> RegexFailureKind.Memory
59 else -> RegexFailureKind.Other
60 }, code, regexDetail(code))
61 }