@thomasfosterau/effect-parse 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/README.md +44 -1
  2. package/dist/Ascii.d.ts +0 -2
  3. package/dist/Ascii.d.ts.map +1 -1
  4. package/dist/Ascii.js +6 -6
  5. package/dist/Ascii.js.map +1 -1
  6. package/dist/Binary.d.ts +0 -2
  7. package/dist/Binary.d.ts.map +1 -1
  8. package/dist/Binary.js +1 -2
  9. package/dist/Bits.d.ts +0 -2
  10. package/dist/Bits.d.ts.map +1 -1
  11. package/dist/Bits.js +2 -3
  12. package/dist/Bits.js.map +1 -1
  13. package/dist/Compiler.d.ts +0 -2
  14. package/dist/Compiler.d.ts.map +1 -1
  15. package/dist/Compiler.js +1 -2
  16. package/dist/Encoding.d.ts +0 -2
  17. package/dist/Encoding.d.ts.map +1 -1
  18. package/dist/Encoding.js +1 -2
  19. package/dist/Expression.d.ts +0 -2
  20. package/dist/Expression.d.ts.map +1 -1
  21. package/dist/Expression.js +61 -18
  22. package/dist/Expression.js.map +1 -1
  23. package/dist/Input.d.ts +80 -7
  24. package/dist/Input.d.ts.map +1 -1
  25. package/dist/Input.js +149 -18
  26. package/dist/Input.js.map +1 -1
  27. package/dist/Nom.d.ts +1 -3
  28. package/dist/Nom.d.ts.map +1 -1
  29. package/dist/Nom.js +1 -2
  30. package/dist/ParseError.d.ts +0 -2
  31. package/dist/ParseError.d.ts.map +1 -1
  32. package/dist/ParseError.js +6 -10
  33. package/dist/ParseError.js.map +1 -1
  34. package/dist/Parser.d.ts +2 -2
  35. package/dist/Parser.d.ts.map +1 -1
  36. package/dist/Parser.js +58 -32
  37. package/dist/Parser.js.map +1 -1
  38. package/dist/ParserSchema.d.ts +198 -2
  39. package/dist/ParserSchema.d.ts.map +1 -1
  40. package/dist/ParserSchema.js +646 -18
  41. package/dist/ParserSchema.js.map +1 -1
  42. package/dist/ParserStream.d.ts +0 -2
  43. package/dist/ParserStream.d.ts.map +1 -1
  44. package/dist/ParserStream.js +15 -9
  45. package/dist/ParserStream.js.map +1 -1
  46. package/dist/Syntax.d.ts +0 -2
  47. package/dist/Syntax.d.ts.map +1 -1
  48. package/dist/Syntax.js +13 -11
  49. package/dist/Syntax.js.map +1 -1
  50. package/dist/Text.d.ts +0 -2
  51. package/dist/Text.d.ts.map +1 -1
  52. package/dist/Text.js +1 -2
  53. package/dist/Token.d.ts +0 -2
  54. package/dist/Token.d.ts.map +1 -1
  55. package/dist/Token.js +1 -2
  56. package/dist/internal/compiler/closure.d.ts +5 -2
  57. package/dist/internal/compiler/closure.d.ts.map +1 -1
  58. package/dist/internal/compiler/closure.js +160 -92
  59. package/dist/internal/compiler/closure.js.map +1 -1
  60. package/dist/internal/compiler/entries.d.ts +0 -2
  61. package/dist/internal/compiler/entries.d.ts.map +1 -1
  62. package/dist/internal/compiler/entries.js +6 -7
  63. package/dist/internal/compiler/entries.js.map +1 -1
  64. package/dist/internal/compiler/first.d.ts +0 -2
  65. package/dist/internal/compiler/first.d.ts.map +1 -1
  66. package/dist/internal/compiler/first.js +10 -10
  67. package/dist/internal/compiler/first.js.map +1 -1
  68. package/dist/internal/compiler/jit.d.ts +0 -2
  69. package/dist/internal/compiler/jit.d.ts.map +1 -1
  70. package/dist/internal/compiler/jit.js +52 -51
  71. package/dist/internal/compiler/jit.js.map +1 -1
  72. package/dist/internal/compiler/registry.d.ts +0 -2
  73. package/dist/internal/compiler/registry.d.ts.map +1 -1
  74. package/dist/internal/compiler/registry.js +1 -2
  75. package/dist/internal/compiler/sets.d.ts +2 -2
  76. package/dist/internal/compiler/sets.d.ts.map +1 -1
  77. package/dist/internal/compiler/sets.js +8 -3
  78. package/dist/internal/compiler/sets.js.map +1 -1
  79. package/dist/internal/entries.d.ts +0 -2
  80. package/dist/internal/entries.d.ts.map +1 -1
  81. package/dist/internal/entries.js +1 -2
  82. package/dist/internal/key.d.ts +10 -0
  83. package/dist/internal/key.d.ts.map +1 -0
  84. package/dist/internal/key.js +11 -0
  85. package/dist/internal/key.js.map +1 -0
  86. package/dist/internal/node.d.ts +11 -4
  87. package/dist/internal/node.d.ts.map +1 -1
  88. package/dist/internal/node.js +15 -3
  89. package/dist/internal/node.js.map +1 -1
  90. package/dist/internal/numbers.d.ts +0 -2
  91. package/dist/internal/numbers.d.ts.map +1 -1
  92. package/dist/internal/numbers.js +1 -2
  93. package/dist/internal/resume.d.ts +5 -6
  94. package/dist/internal/resume.d.ts.map +1 -1
  95. package/dist/internal/resume.js +1 -2
  96. package/dist/internal/resume.js.map +1 -1
  97. package/dist/internal/run.d.ts +0 -2
  98. package/dist/internal/run.d.ts.map +1 -1
  99. package/dist/internal/run.js +2 -3
  100. package/dist/internal/run.js.map +1 -1
  101. package/dist/internal/stack.d.ts +0 -2
  102. package/dist/internal/stack.d.ts.map +1 -1
  103. package/dist/internal/stack.js +12 -3
  104. package/dist/internal/stack.js.map +1 -1
  105. package/dist/internal/syntax.d.ts +30 -19
  106. package/dist/internal/syntax.d.ts.map +1 -1
  107. package/dist/internal/syntax.js +74 -63
  108. package/dist/internal/syntax.js.map +1 -1
  109. package/dist/internal/text.d.ts +5 -2
  110. package/dist/internal/text.d.ts.map +1 -1
  111. package/dist/internal/text.js +14 -5
  112. package/dist/internal/text.js.map +1 -1
  113. package/dist/internal/ties.d.ts +28 -0
  114. package/dist/internal/ties.d.ts.map +1 -0
  115. package/dist/internal/ties.js +66 -0
  116. package/dist/internal/ties.js.map +1 -0
  117. package/dist/internal/tokens.d.ts +10 -9
  118. package/dist/internal/tokens.d.ts.map +1 -1
  119. package/dist/internal/tokens.js +4 -8
  120. package/dist/internal/tokens.js.map +1 -1
  121. package/package.json +6 -6
  122. package/src/Ascii.ts +6 -5
  123. package/src/Bits.ts +1 -1
  124. package/src/Expression.ts +27 -23
  125. package/src/Input.ts +157 -21
  126. package/src/ParseError.ts +10 -8
  127. package/src/Parser.ts +67 -32
  128. package/src/ParserSchema.ts +982 -23
  129. package/src/ParserStream.ts +20 -8
  130. package/src/Syntax.ts +12 -9
  131. package/src/internal/compiler/closure.ts +202 -90
  132. package/src/internal/compiler/entries.ts +14 -11
  133. package/src/internal/compiler/first.ts +9 -8
  134. package/src/internal/compiler/jit.ts +95 -74
  135. package/src/internal/compiler/sets.ts +7 -0
  136. package/src/internal/key.ts +11 -0
  137. package/src/internal/node.ts +19 -2
  138. package/src/internal/resume.ts +5 -4
  139. package/src/internal/run.ts +1 -1
  140. package/src/internal/stack.ts +6 -1
  141. package/src/internal/syntax.ts +80 -84
  142. package/src/internal/text.ts +24 -4
  143. package/src/internal/ties.ts +83 -0
  144. package/src/internal/tokens.ts +20 -13
@@ -1,16 +1,18 @@
1
1
  import * as Predicate from "effect/Predicate";
2
+ import * as Result from "effect/Result";
2
3
  import { Fail } from "../../Input.js";
3
4
  import { floatScan, floatTexts, scanFloatBytes, scanFloatString, toFloat } from "../numbers.js";
5
+ import { Key } from "../key.js";
4
6
  import * as Resume from "../resume.js";
5
7
  import { countTokens, indexOf } from "../tokens.js";
6
8
  import * as Option from "effect/Option";
7
- import { charOf, fastLimit, firstCodePoint, fold, validate } from "../text.js";
9
+ import { charOf, fastLimit, firstCodePoint, fold, utf16be, utf16le, validate } from "../text.js";
8
10
  import { addEntry, flushEntries, fromEntries, isPlainKey } from "../entries.js";
9
- import { nodeOf } from "../node.js";
11
+ import { nodeOf, scansFloat } from "../node.js";
10
12
  import { buildsEntries, pairOf } from "./entries.js";
11
13
  import { FirstSets, planAlt } from "./first.js";
12
- import { TableSize, kindOfEncoding, textSet, tokenSet } from "./sets.js";
13
- //#region src/internal/compiler/closure.ts
14
+ import { TableSize, acceptTable, kindOfEncoding, textSet, tokenSet } from "./sets.js";
15
+ // src/internal/compiler/closure.ts
14
16
  /**
15
17
  * The closure compiler: turns a parser's description into specialized
16
18
  * closures with the step signature. Compiled steps return a value, or
@@ -33,8 +35,6 @@ const INC = "Incomplete";
33
35
  */
34
36
  const Margin = 4;
35
37
  const lowerAscii = (c) => c >= 65 && c <= 90 ? c + 32 : c;
36
- const isSuccess = (r) => r._tag === "Success";
37
- const isSome = (o) => o._tag === "Some";
38
38
  var Compiler = class {
39
39
  kind;
40
40
  resumable;
@@ -146,7 +146,7 @@ var Compiler = class {
146
146
  case "TakeUntil":
147
147
  case "Rest":
148
148
  if (!this.native(node.encoding)) return this.fallback(p, node, "token parser on array input");
149
- return this.partialSafe(p, node.encoding === void 0 ? this.token(p, node, used) : this.text(p, node, node.encoding, used), node._tag === "Literal" || node._tag === "TakeUntil" ? node.literal.length * 4 : Margin);
149
+ return this.partialSafe(p, node.encoding === void 0 ? this.token(p, node, used) : this.text(p, node, node.encoding, used), Predicate.isTagged(node, "Literal") || Predicate.isTagged(node, "TakeUntil") ? node.literal.length * 4 : Margin);
150
150
  case "TillLineEnding":
151
151
  case "Identifier":
152
152
  case "Bom": return this.fallback(p, node, "leaf parser without a compiled form");
@@ -189,7 +189,7 @@ var Compiler = class {
189
189
  const a = c(s);
190
190
  if (a === Fail) return Fail;
191
191
  const r = f(a);
192
- if (isSuccess(r)) return r.success;
192
+ if (Result.isSuccess(r)) return r.success;
193
193
  s.mode = BT;
194
194
  return Fail;
195
195
  };
@@ -201,7 +201,7 @@ var Compiler = class {
201
201
  const a = c(s);
202
202
  if (a === Fail) return Fail;
203
203
  const o = f(a);
204
- if (isSome(o)) return o.value;
204
+ if (Option.isSome(o)) return o.value;
205
205
  s.mode = BT;
206
206
  return Fail;
207
207
  };
@@ -287,8 +287,8 @@ var Compiler = class {
287
287
  case "Opt":
288
288
  case "OrElseSucceed": {
289
289
  const c = this.compile(node.self, used);
290
- const orElse = !used ? void 0 : node._tag === "Opt" ? Option.none : node.value;
291
- const some = used && node._tag === "Opt";
290
+ const orElse = !used ? void 0 : Predicate.isTagged(node, "Opt") ? Option.none : node.value;
291
+ const some = used && Predicate.isTagged(node, "Opt");
292
292
  return (s) => {
293
293
  const start = s.pos;
294
294
  const a = c(s);
@@ -487,9 +487,9 @@ var Compiler = class {
487
487
  return general;
488
488
  }
489
489
  let sel = nodeOf(node.selector);
490
- const peek = sel._tag === "Peek";
490
+ const peek = Predicate.isTagged(sel, "Peek");
491
491
  if (peek) sel = nodeOf(sel.self);
492
- if (sel._tag !== "Any" || !this.native(sel.encoding)) return general;
492
+ if (!Predicate.isTagged(sel, "Any") || !this.native(sel.encoding)) return general;
493
493
  const enc = sel.encoding;
494
494
  const limit = enc === void 0 ? this.kind === 0 ? 55296 : TableSize : Math.min(fastLimit(enc), 55296);
495
495
  const size = Math.min(limit, TableSize);
@@ -522,11 +522,11 @@ var Compiler = class {
522
522
  repeat(node, used) {
523
523
  const { max, min } = node;
524
524
  const checkLoop = max === Infinity;
525
- const isFold = node._tag === "Fold";
525
+ const isFold = Predicate.isTagged(node, "Fold");
526
526
  const cond = isFold ? node.while : void 0;
527
527
  const c = this.compile(node.self, used);
528
528
  const resumable = this.resumable;
529
- const key = {};
529
+ const key = new Key();
530
530
  const tracked = used || cond !== void 0;
531
531
  const init = !tracked ? () => void 0 : isFold ? node.init : () => [];
532
532
  const add = !tracked ? (acc) => acc : isFold ? node.f : (acc, a) => {
@@ -635,7 +635,7 @@ var Compiler = class {
635
635
  };
636
636
  const { max, min } = node;
637
637
  const checkLoop = max === Infinity;
638
- if (node._tag === "Repeat") return (s) => {
638
+ if (Predicate.isTagged(node, "Repeat")) return (s) => {
639
639
  const acc = new EntriesAcc();
640
640
  for (let count = 0; count < max; count++) {
641
641
  const start = s.pos;
@@ -728,7 +728,7 @@ var Compiler = class {
728
728
  const c = this.compile(node.self, used);
729
729
  const sep = this.compile(node.separator, false);
730
730
  const resumable = this.resumable;
731
- const key = {};
731
+ const key = new Key();
732
732
  return (s) => {
733
733
  const entry = s.pos;
734
734
  let acc;
@@ -798,7 +798,7 @@ var Compiler = class {
798
798
  const sep = this.compile(node.separator, true);
799
799
  const op = node.op;
800
800
  const resumable = this.resumable;
801
- const key = {};
801
+ const key = new Key();
802
802
  if (!node.right) return (s) => {
803
803
  const entry = s.pos;
804
804
  let acc;
@@ -1068,20 +1068,18 @@ var Compiler = class {
1068
1068
  };
1069
1069
  }
1070
1070
  text(p, node, enc, used) {
1071
- const kind = this.kind;
1072
- const fast = fastLimit(enc);
1071
+ const { fast, unitAt, width: unitWidth } = unitReader(this.kind, enc);
1073
1072
  const decode = enc.decode;
1074
1073
  const width = enc.width;
1075
1074
  const toText = enc.text;
1076
- const unitAt = kind === 0 ? (input, i) => input.charCodeAt(i) : (input, i) => input[i];
1077
1075
  switch (node._tag) {
1078
1076
  case "Any": return (s) => {
1079
1077
  const pos = s.pos;
1080
1078
  const end = s.end;
1081
1079
  if (pos >= end) return fail(s);
1082
- const c = unitAt(s.input, pos);
1080
+ const c = unitAt(s.input, pos, end);
1083
1081
  if (c < fast) {
1084
- s.pos = pos + 1;
1082
+ s.pos = pos + unitWidth;
1085
1083
  return used ? charOf(c) : void 0;
1086
1084
  }
1087
1085
  const cp = decode(s.input, pos, end);
@@ -1101,11 +1099,11 @@ var Compiler = class {
1101
1099
  let pos = start;
1102
1100
  for (let i = 0; i < n; i++) {
1103
1101
  if (pos >= end) return failAt(s, pos);
1104
- const c = unitAt(input, pos);
1102
+ const c = unitAt(input, pos, end);
1105
1103
  let cp;
1106
1104
  if (c < fast) {
1107
1105
  cp = c;
1108
- pos++;
1106
+ pos += unitWidth;
1109
1107
  } else {
1110
1108
  cp = decode(input, pos, end);
1111
1109
  if (cp < 0) return failAt(s, pos);
@@ -1150,15 +1148,15 @@ var Compiler = class {
1150
1148
  const pos = s.pos;
1151
1149
  const end = s.end;
1152
1150
  if (pos >= end) return fail(s);
1153
- const c = unitAt(s.input, pos);
1151
+ const c = unitAt(s.input, pos, end);
1154
1152
  if (c < fast && c < TableSize) {
1155
1153
  if (accept[c] === 0) return fail(s);
1156
- s.pos = pos + 1;
1154
+ s.pos = pos + unitWidth;
1157
1155
  return used ? charOf(c) : void 0;
1158
1156
  }
1159
1157
  const cp = c < fast ? c : decode(s.input, pos, end);
1160
1158
  if (cp < 0 || test(cp) === negate) return fail(s);
1161
- s.pos = pos + (c < fast ? 1 : width(cp));
1159
+ s.pos = pos + (c < fast ? unitWidth : width(cp));
1162
1160
  return used ? charOf(cp) : void 0;
1163
1161
  };
1164
1162
  }
@@ -1171,7 +1169,7 @@ var Compiler = class {
1171
1169
  let pos = start;
1172
1170
  for (let got = 0; got < count; got++) {
1173
1171
  if (pos >= end) return failAt(s, pos);
1174
- if (unitAt(input, pos) < fast) pos++;
1172
+ if (unitAt(input, pos, end) < fast) pos += unitWidth;
1175
1173
  else {
1176
1174
  const cp = decode(input, pos, end);
1177
1175
  if (cp < 0) return failAt(s, pos);
@@ -1185,34 +1183,18 @@ var Compiler = class {
1185
1183
  case "TakeWhile": {
1186
1184
  const { max, min, negate } = node;
1187
1185
  const set = textSet(node.set);
1188
- const accept = acceptTable(set, negate);
1189
- const test = set.test;
1190
- const small = Math.min(fast, TableSize);
1191
- return (s) => {
1192
- const input = s.input;
1193
- const start = s.pos;
1194
- const end = s.end;
1195
- let i = start;
1196
- let count = 0;
1197
- while (count < max && i < end) {
1198
- const c = unitAt(input, i);
1199
- if (c < small) {
1200
- if (accept[c] === 0) break;
1201
- i++;
1202
- } else if (c < fast) {
1203
- if (test(c) === negate) break;
1204
- i++;
1205
- } else {
1206
- const cp = decode(input, i, end);
1207
- if (cp < 0 || test(cp) === negate) break;
1208
- i += width(cp);
1209
- }
1210
- count++;
1211
- }
1212
- if (count < min) return failAt(s, i);
1213
- s.pos = i;
1214
- return used ? toText(input, start, i) : void 0;
1186
+ const loop = {
1187
+ max,
1188
+ min,
1189
+ negate,
1190
+ accept: acceptTable(set, negate),
1191
+ test: set.test,
1192
+ small: Math.min(fast, TableSize),
1193
+ fast,
1194
+ used
1215
1195
  };
1196
+ if (unitWidth === 2) return takeWhileUtf16(loop, enc === utf16le ? 0 : 1, enc);
1197
+ return this.kind === 0 ? takeWhileString(loop, enc) : takeWhileBytes(loop, enc);
1216
1198
  }
1217
1199
  case "TakeUntil":
1218
1200
  case "Rest": return this.fallback(p, node, "Text parser without a compiled form");
@@ -1232,16 +1214,15 @@ var Compiler = class {
1232
1214
  return c(s) === Fail ? Fail : s.slice(start, s.pos);
1233
1215
  };
1234
1216
  }
1235
- const fast = fastLimit(enc);
1217
+ const { fast, unitAt, width } = unitReader(this.kind, enc);
1236
1218
  const toText = enc.text;
1237
- const unitAt = this.kind === 0 ? (input, i) => input.charCodeAt(i) : (input, i) => input[i];
1238
1219
  return (s) => {
1239
1220
  const start = s.pos;
1240
1221
  if (c(s) === Fail) return Fail;
1241
1222
  const end = s.pos;
1242
1223
  const input = s.input;
1243
1224
  let i = start;
1244
- while (i < end && unitAt(input, i) < fast) i++;
1225
+ while (i < end && unitAt(input, i, end) < fast) i += width;
1245
1226
  if (i < end && validate(enc, s, i, end) < 0) return fail(s);
1246
1227
  return used ? toText(input, start, end) : void 0;
1247
1228
  };
@@ -1253,11 +1234,7 @@ var Compiler = class {
1253
1234
  */
1254
1235
  float(node) {
1255
1236
  if (!floatTexts.has(node.self) || this.kind === 2) return void 0;
1256
- let taken = nodeOf(node.self);
1257
- while (taken._tag === "Expected") taken = nodeOf(taken.self);
1258
- if (taken._tag !== "Taken") return void 0;
1259
- const enc = taken.encoding;
1260
- if (enc !== void 0 && !(this.native(enc) && enc.unit === 1 && enc.asciiCompatible)) return void 0;
1237
+ if (!scansFloat(node.self, (enc) => this.native(enc))) return void 0;
1261
1238
  const c = this.compile(node.self, true);
1262
1239
  const scan = this.kind === 0 ? scanFloatString : scanFloatBytes;
1263
1240
  const resumable = this.resumable;
@@ -1293,7 +1270,16 @@ var Compiler = class {
1293
1270
  }
1294
1271
  return false;
1295
1272
  };
1296
- else control = (s) => {
1273
+ else if (ctrl < unitReader(kind, enc).fast) {
1274
+ const { unitAt, width } = unitReader(kind, enc);
1275
+ control = (s) => {
1276
+ if (s.pos < s.end && unitAt(s.input, s.pos, s.end) === ctrl) {
1277
+ s.pos += width;
1278
+ return true;
1279
+ }
1280
+ return false;
1281
+ };
1282
+ } else control = (s) => {
1297
1283
  if (s.pos >= s.end) return false;
1298
1284
  const cp = enc.decode(s.input, s.pos, s.end);
1299
1285
  if (cp !== ctrl) return false;
@@ -1301,7 +1287,7 @@ var Compiler = class {
1301
1287
  return true;
1302
1288
  };
1303
1289
  const resumable = this.resumable;
1304
- const key = {};
1290
+ const key = new Key();
1305
1291
  /** Fails as incomplete, keeping a checkpoint in a streaming parse. */
1306
1292
  const incomplete = (s, start, checkpoint) => {
1307
1293
  if (s.resume !== void 0) Resume.save(s, key, start, checkpoint);
@@ -1397,18 +1383,105 @@ const fail = (s) => {
1397
1383
  s.mode = BT;
1398
1384
  return Fail;
1399
1385
  };
1400
- /** Fails, leaving the cursor at `pos`: how far a token parser read (see `partialSafe`). */
1386
+ const takeWhileString = (loop, enc) => {
1387
+ const { accept, fast, max, min, negate, small, test, used } = loop;
1388
+ const { decode, text, width } = enc;
1389
+ return (s) => {
1390
+ const input = s.input;
1391
+ const start = s.pos;
1392
+ const end = s.end;
1393
+ let i = start;
1394
+ let count = 0;
1395
+ while (count < max && i < end) {
1396
+ const c = input.charCodeAt(i);
1397
+ if (c < small) {
1398
+ if (accept[c] === 0) break;
1399
+ i++;
1400
+ } else if (c < fast) {
1401
+ if (test(c) === negate) break;
1402
+ i++;
1403
+ } else {
1404
+ const cp = decode(input, i, end);
1405
+ if (cp < 0 || test(cp) === negate) break;
1406
+ i += width(cp);
1407
+ }
1408
+ count++;
1409
+ }
1410
+ if (count < min) return failAt(s, i);
1411
+ s.pos = i;
1412
+ return used ? text(input, start, i) : void 0;
1413
+ };
1414
+ };
1415
+ const takeWhileBytes = (loop, enc) => {
1416
+ const { accept, max, min, negate, small, test, used } = loop;
1417
+ const { decode, text, width } = enc;
1418
+ return (s) => {
1419
+ const input = s.input;
1420
+ const start = s.pos;
1421
+ const end = s.end;
1422
+ let i = start;
1423
+ let count = 0;
1424
+ while (count < max && i < end) {
1425
+ const c = input[i];
1426
+ if (c < small) {
1427
+ if (accept[c] === 0) break;
1428
+ i++;
1429
+ } else {
1430
+ const cp = decode(input, i, end);
1431
+ if (cp < 0 || test(cp) === negate) break;
1432
+ i += width(cp);
1433
+ }
1434
+ count++;
1435
+ }
1436
+ if (count < min) return failAt(s, i);
1437
+ s.pos = i;
1438
+ return used ? text(input, start, i) : void 0;
1439
+ };
1440
+ };
1441
+ /** For UTF-16 bytes; `lo` is the offset of a unit's low byte (0 for little endian). */
1442
+ const takeWhileUtf16 = (loop, lo, enc) => {
1443
+ const { accept, fast, max, min, negate, small, test, used } = loop;
1444
+ const { decode, text, width } = enc;
1445
+ const hi = 1 - lo;
1446
+ return (s) => {
1447
+ const input = s.input;
1448
+ const start = s.pos;
1449
+ const end = s.end;
1450
+ let i = start;
1451
+ let count = 0;
1452
+ while (count < max && i < end) {
1453
+ const c = i + 1 < end ? input[i + lo] | input[i + hi] << 8 : 65535;
1454
+ if (c < fast ? c < small ? accept[c] === 0 : test(c) === negate : false) break;
1455
+ if (c < fast) i += 2;
1456
+ else {
1457
+ const cp = decode(input, i, end);
1458
+ if (cp < 0 || test(cp) === negate) break;
1459
+ i += width(cp);
1460
+ }
1461
+ count++;
1462
+ }
1463
+ if (count < min) return failAt(s, i);
1464
+ s.pos = i;
1465
+ return used ? text(input, start, i) : void 0;
1466
+ };
1467
+ };
1468
+ const unitReader = (kind, enc) => {
1469
+ if (kind === 1 && (enc === utf16le || enc === utf16be)) return {
1470
+ width: 2,
1471
+ fast: 55296,
1472
+ unitAt: enc === utf16le ? (input, i, end) => i + 1 < end ? input[i] | input[i + 1] << 8 : 65535 : (input, i, end) => i + 1 < end ? input[i] << 8 | input[i + 1] : 65535
1473
+ };
1474
+ return {
1475
+ width: 1,
1476
+ fast: fastLimit(enc),
1477
+ unitAt: kind === 0 ? (input, i, _end) => input.charCodeAt(i) : (input, i, _end) => input[i]
1478
+ };
1479
+ };
1401
1480
  const failAt = (s, pos) => {
1402
1481
  s.pos = pos;
1403
1482
  s.mode = BT;
1404
1483
  return Fail;
1405
1484
  };
1406
- /** 1 where a small code is accepted (taking `negate` into account). */
1407
- const acceptTable = (set, negate) => {
1408
- const t = new Uint8Array(TableSize);
1409
- for (let c = 0; c < TableSize; c++) t[c] = set.table[c] === 1 !== negate ? 1 : 0;
1410
- return t;
1411
- };
1412
1485
  /** A literal matched against a string input, returning `value`. */
1413
1486
  const stringLiteral = (str, value) => {
1414
1487
  const len = str.length;
@@ -1465,25 +1538,20 @@ const compileClosure = (root, kind, resumable = false) => {
1465
1538
  };
1466
1539
  };
1467
1540
  /** The closure compiler as a backend. */
1468
- const backend = { build: (p, kind, resumable) => {
1469
- try {
1470
- const { fallbacks, step } = compileClosure(p, kind, resumable);
1471
- return {
1472
- backend: "closure",
1473
- step,
1474
- fallbacks,
1475
- error: void 0
1476
- };
1477
- } catch (error) {
1478
- return {
1479
- backend: void 0,
1480
- step: void 0,
1481
- fallbacks: [],
1482
- error
1483
- };
1484
- }
1485
- } };
1486
- //#endregion
1541
+ const backend = { build: (p, kind, resumable) => Result.match(Result.try(() => compileClosure(p, kind, resumable)), {
1542
+ onSuccess: ({ fallbacks, step }) => ({
1543
+ backend: "closure",
1544
+ step,
1545
+ fallbacks,
1546
+ error: void 0
1547
+ }),
1548
+ onFailure: (error) => ({
1549
+ backend: void 0,
1550
+ step: void 0,
1551
+ fallbacks: [],
1552
+ error
1553
+ })
1554
+ }) };
1487
1555
  export { backend, createClosureCompiler };
1488
1556
 
1489
1557
  //# sourceMappingURL=closure.js.map