@gmb/bitmark-parser 6.11.1 → 7.0.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 (45) hide show
  1. package/README.md +222 -54
  2. package/dist/browser/bitmark-parser.min.js +4 -4
  3. package/dist/browser/bitmark-parser.min.js.map +1 -1
  4. package/dist/browser/cjs/index.cjs +547 -325
  5. package/dist/browser/cjs/index.cjs.map +1 -1
  6. package/dist/browser/cjs/index.d.cts +342 -68
  7. package/dist/browser/esm/index.d.ts +342 -68
  8. package/dist/browser/esm/index.js +546 -324
  9. package/dist/browser/esm/index.js.map +1 -1
  10. package/dist/browser/esm/worker-entry.js +497 -242
  11. package/dist/browser/esm/worker-entry.js.map +1 -1
  12. package/dist/browser/wasm/bitmark_browser_full_wasm_bg.wasm +0 -0
  13. package/dist/browser/wasm/bitmark_json_wasm_bg.wasm +0 -0
  14. package/dist/browser/wasm/bitmark_wasm_bg.wasm +0 -0
  15. package/dist/index.cjs +51 -88
  16. package/dist/index.cjs.map +1 -1
  17. package/dist/index.d.cts +342 -68
  18. package/dist/index.d.ts +342 -68
  19. package/dist/index.js +50 -87
  20. package/dist/index.js.map +1 -1
  21. package/dist/legacy.cjs +8 -30
  22. package/dist/legacy.cjs.map +1 -1
  23. package/dist/legacy.d.cts +0 -2
  24. package/dist/legacy.d.ts +0 -2
  25. package/dist/legacy.js +8 -30
  26. package/dist/legacy.js.map +1 -1
  27. package/dist/worker-entry.cjs +1 -5
  28. package/dist/worker-entry.cjs.map +1 -1
  29. package/package.json +6 -6
  30. package/schema/bitmark.schema.json +496 -139
  31. package/wasm/bitmark_wasm.d.ts +27 -23
  32. package/wasm/bitmark_wasm.js +198 -112
  33. package/wasm/bitmark_wasm_bg.wasm +0 -0
  34. package/wasm/bitmark_wasm_bg.wasm.d.ts +2 -2
  35. package/wasm/package.json +1 -1
  36. package/wasm-bitmark-json/bitmark_json_wasm.d.ts +18 -12
  37. package/wasm-bitmark-json/bitmark_json_wasm.js +170 -72
  38. package/wasm-bitmark-json/bitmark_json_wasm_bg.wasm +0 -0
  39. package/wasm-bitmark-json/bitmark_json_wasm_bg.wasm.d.ts +1 -1
  40. package/wasm-bitmark-json/package.json +1 -1
  41. package/wasm-browser-full/bitmark_browser_full_wasm.d.ts +27 -23
  42. package/wasm-browser-full/bitmark_browser_full_wasm.js +198 -112
  43. package/wasm-browser-full/bitmark_browser_full_wasm_bg.wasm +0 -0
  44. package/wasm-browser-full/bitmark_browser_full_wasm_bg.wasm.d.ts +2 -2
  45. package/wasm-browser-full/package.json +1 -1
package/dist/index.js CHANGED
@@ -10,7 +10,7 @@ import { createRequire } from "module";
10
10
  // src/generated/package_info.ts
11
11
  var PACKAGE_INFO = {
12
12
  "name": "@gmb/bitmark-parser",
13
- "version": "6.11.1",
13
+ "version": "7.0.0",
14
14
  "author": "Get More Brain Ltd <info@getmorebrain.com>",
15
15
  "license": "ISC",
16
16
  "description": "A parser for bitmark text, powered by WebAssembly."
@@ -49,8 +49,11 @@ function getActive() {
49
49
  }
50
50
  function setActive(feature, module) {
51
51
  for (const [type, data] of registrations) {
52
- const error = module.register(type, data);
53
- if (error) throw new RegisterError(error.replace(/^error:\s*/, ""));
52
+ try {
53
+ module.register(type, data);
54
+ } catch (e) {
55
+ throw new RegisterError(e instanceof Error ? e.message : String(e));
56
+ }
54
57
  }
55
58
  active = module;
56
59
  activeFeature = feature;
@@ -66,13 +69,6 @@ function variant() {
66
69
  }
67
70
 
68
71
  // src/core/hooked-transform.ts
69
- function isError(s) {
70
- return s.startsWith("error:");
71
- }
72
- function errorString(e) {
73
- const m = e instanceof Error ? e.message : String(e);
74
- return m.startsWith("error:") ? m : `error: ${m}`;
75
- }
76
72
  function stripArray(doc) {
77
73
  const t = doc.trim();
78
74
  return t.startsWith("[") && t.endsWith("]") ? t.slice(1, -1) : doc;
@@ -84,17 +80,13 @@ function patchJsonOf(patch) {
84
80
  }
85
81
  function buildUnits(wasm, input, inputIsBitmark) {
86
82
  if (inputIsBitmark) {
87
- const out2 = wasm.split_bits(input);
88
- if (isError(out2)) throw new Error(out2);
89
- const slices = JSON.parse(out2);
83
+ const slices = JSON.parse(wasm.split_bits(input));
90
84
  return slices.map((s) => ({
91
85
  view: { format: "bitmark", index: s.index, total: slices.length, source: s.source },
92
86
  source: s.source
93
87
  }));
94
88
  }
95
- const out = wasm.split_json_bits(input);
96
- if (isError(out)) throw new Error(out);
97
- const envelopes = JSON.parse(out);
89
+ const envelopes = JSON.parse(wasm.split_json_bits(input));
98
90
  return envelopes.map((env, index) => ({
99
91
  view: { format: "json", index, total: envelopes.length, bit: env.bit ?? {} },
100
92
  // Per-bit transform consumes a one-bit JSON document.
@@ -125,9 +117,7 @@ function planTransform(wasm, input, options) {
125
117
  if (patchJson === "") {
126
118
  resolved = units.map((_, index) => ({ source: index, bit: null, patch: [] }));
127
119
  } else {
128
- const out = wasm.resolve_patch_document(input, patchJson);
129
- if (isError(out)) throw new Error(out);
130
- resolved = JSON.parse(out);
120
+ resolved = JSON.parse(wasm.resolve_patch_document(input, patchJson));
131
121
  }
132
122
  const jobs = [];
133
123
  const views = [];
@@ -174,7 +164,6 @@ function computeBit(wasm, job) {
174
164
  2,
175
165
  job.spacesAroundValues
176
166
  );
177
- if (isError(out)) return out;
178
167
  return job.outputFormat === "json" ? stripArray(out) : out;
179
168
  }
180
169
  function recombine(wasm, elements, output, pretty, indent) {
@@ -183,16 +172,10 @@ function recombine(wasm, elements, output, pretty, indent) {
183
172
  return pretty ? wasm.prettify(arr, indent) : arr;
184
173
  }
185
174
  function runHookedTransform(wasm, input, options) {
186
- let plan;
187
- try {
188
- plan = planTransform(wasm, input, options);
189
- } catch (e) {
190
- return errorString(e);
191
- }
175
+ const plan = planTransform(wasm, input, options);
192
176
  const rendered = [];
193
177
  for (let i = 0; i < plan.jobs.length; i++) {
194
178
  const element = computeBit(wasm, plan.jobs[i]);
195
- if (isError(element)) return element;
196
179
  options.postHook?.(plan.views[i], element);
197
180
  rendered.push(element);
198
181
  }
@@ -201,20 +184,9 @@ function runHookedTransform(wasm, input, options) {
201
184
 
202
185
  // src/core/parallel.ts
203
186
  async function orchestrateParallel(wasm, input, options, runJobs) {
204
- let plan;
205
- try {
206
- plan = planTransform(wasm, input, options);
207
- } catch (e) {
208
- return errorString(e);
209
- }
210
- let elements;
211
- try {
212
- elements = await runJobs(plan.jobs, options.workers ?? 4);
213
- } catch (e) {
214
- return errorString(e);
215
- }
187
+ const plan = planTransform(wasm, input, options);
188
+ const elements = await runJobs(plan.jobs, options.workers ?? 4);
216
189
  for (let i = 0; i < elements.length; i++) {
217
- if (isError(elements[i])) return elements[i];
218
190
  options.postHook?.(plan.views[i], elements[i]);
219
191
  }
220
192
  return recombine(wasm, elements, plan.output, plan.pretty, plan.indent);
@@ -569,10 +541,7 @@ var Builder = class {
569
541
  }
570
542
  }
571
543
  this.parseStringTexts(bit);
572
- const out = wasm.convert(JSON.stringify([{ bit }]), "json", "json", "optimized", false, 2, 1, false, false);
573
- if (out.startsWith("error:")) {
574
- throw new Error(out.slice("error:".length).trim());
575
- }
544
+ const out = wasm.convert(JSON.stringify([{ bit }]), "json", "json", "optimized", false, 2, 1, false, false, "", "");
576
545
  const entries = JSON.parse(out);
577
546
  const built = entries[0]?.bit ?? bit;
578
547
  for (const [key, value] of Object.entries(bit)) {
@@ -709,8 +678,7 @@ var BitmarkParserGenerator = class {
709
678
  const emit = coreFormat === "bitmark" ? prettyIndent(bitmarkOptions.prettifyJson) : { pretty, indent };
710
679
  const spaces = spacesAround(bitmarkOptions.spacesAroundValues);
711
680
  const includeUnknown = !jsonOptions.excludeUnknownProperties;
712
- const out = wasm.convert(source, isBitmark ? "bitmark" : "json", coreFormat, "optimized", emit.pretty, emit.indent, spaces, includeUnknown, false);
713
- this.throwOnError(out);
681
+ const out = wasm.convert(source, isBitmark ? "bitmark" : "json", coreFormat, "optimized", emit.pretty, emit.indent, spaces, includeUnknown, false, "", "");
714
682
  if (outputFormat === "bitmark" || outputFormat === "text") {
715
683
  return this.deliver(out, opts.outputFile, opts.fileOptions);
716
684
  }
@@ -744,7 +712,6 @@ var BitmarkParserGenerator = class {
744
712
  );
745
713
  const spaces = spacesAround(bitmarkOptions.spacesAroundValues);
746
714
  const out = wasm.canonicalize(source, isBitmark ? "bitmark" : "json", "optimized", pretty, indent, spaces);
747
- this.throwOnError(out);
748
715
  if (isBitmark) return this.deliver(out, opts.outputFile, opts.fileOptions);
749
716
  if (opts.outputFile) return this.deliver(out, opts.outputFile, opts.fileOptions);
750
717
  if (pretty || jsonOptions.stringify) return out;
@@ -761,7 +728,6 @@ var BitmarkParserGenerator = class {
761
728
  const { pretty, indent } = prettyIndent(opts.prettify);
762
729
  const coreFormat = format === "pojo" ? "json" : format;
763
730
  const out = wasm.info(type, coreFormat, opts.bit ?? "", pretty, indent, false, false, true, "");
764
- this.throwOnError(out);
765
731
  if (format === "pojo") return JSON.parse(out);
766
732
  return out;
767
733
  }
@@ -793,8 +759,7 @@ var BitmarkParserGenerator = class {
793
759
  const isHtmlInput = opts.inputFormat === "html" || opts.inputFormat === void 0 && typeof input === "string" && /<table[\s>]/iu.test(inStr);
794
760
  if (!isHtmlInput) {
795
761
  const { source, isBitmark } = this.resolveInput(input, void 0);
796
- const out2 = wasm.convert(source, isBitmark ? "bitmark" : "json", "html", "optimized", false, 2, 1, false, false);
797
- this.throwOnError(out2);
762
+ const out2 = wasm.convert(source, isBitmark ? "bitmark" : "json", "html", "optimized", false, 2, 1, false, false, "", "");
798
763
  return this.deliver(extractHtmlTables(out2).join("\n"), opts.outputFile, opts.fileOptions);
799
764
  }
800
765
  const bitType = opts.tableFormat === "table" ? "table" : "table-extended";
@@ -803,13 +768,11 @@ var BitmarkParserGenerator = class {
803
768
  const { pretty, indent } = prettyIndent(jsonOptions.prettify);
804
769
  const spaces = spacesAround(bitmarkOptions.spacesAroundValues);
805
770
  if (outputFormat === "html") {
806
- const out2 = wasm.convert(wrapped, "html", "html", "optimized", false, 2, 1, false, false);
807
- this.throwOnError(out2);
771
+ const out2 = wasm.convert(wrapped, "html", "html", "optimized", false, 2, 1, false, false, "", "");
808
772
  return this.deliver(extractHtmlTables(out2).join("\n"), opts.outputFile, opts.fileOptions);
809
773
  }
810
774
  const coreFormat = outputFormat === "ast" ? "json" : outputFormat;
811
- const out = wasm.convert(wrapped, "html", coreFormat, "optimized", pretty, indent, spaces, false, false);
812
- this.throwOnError(out);
775
+ const out = wasm.convert(wrapped, "html", coreFormat, "optimized", pretty, indent, spaces, false, false, "", "");
813
776
  if (outputFormat === "bitmark" || outputFormat === "text") {
814
777
  return this.deliver(out, opts.outputFile, opts.fileOptions);
815
778
  }
@@ -833,8 +796,7 @@ var BitmarkParserGenerator = class {
833
796
  const opts = Object.assign({}, options);
834
797
  const wasm = getLegacyWasm();
835
798
  const { source, isBitmark } = this.resolveInput(input, opts.inputFormat);
836
- const out = wasm.convert(source, isBitmark ? "bitmark" : "json", "json", "optimized", false, 2, 1, true, false);
837
- this.throwOnError(out);
799
+ const out = wasm.convert(source, isBitmark ? "bitmark" : "json", "json", "optimized", false, 2, 1, true, false, "", "");
838
800
  return makePseudoAst(JSON.parse(out));
839
801
  }
840
802
  /**
@@ -886,11 +848,9 @@ var BitmarkParserGenerator = class {
886
848
  }
887
849
  if (ast !== void 0) {
888
850
  const out2 = isBitmarkText ? wasm.convert_text(JSON.stringify(ast), "astToText", "bitmark++", textLocation) : wasm.convert_text(JSON.stringify(ast), "astToPlainText", "plainText", textLocation);
889
- this.throwOnError(out2);
890
851
  return this.deliver(out2, opts.outputFile, opts.fileOptions);
891
852
  }
892
853
  const out = wasm.convert_text(inStr, "textToAst", isBitmarkText ? "bitmark++" : "plainText", "body");
893
- this.throwOnError(out);
894
854
  const { pretty, indent } = prettyIndent(jsonOptions.prettify);
895
855
  const outStr = pretty ? wasm.prettify(out, indent) : out;
896
856
  if (opts.outputFile) return this.deliver(outStr, opts.outputFile, opts.fileOptions);
@@ -908,7 +868,6 @@ var BitmarkParserGenerator = class {
908
868
  "bitmark++",
909
869
  "body"
910
870
  );
911
- this.throwOnError(out);
912
871
  return out;
913
872
  }
914
873
  /**
@@ -923,7 +882,6 @@ var BitmarkParserGenerator = class {
923
882
  if (inputFormat === "bitmarkText") {
924
883
  const ast = wasm.convert_text(String(input), "textToAst", "bitmark++", "body");
925
884
  const out2 = wasm.convert_text(ast, "astToPlainText", "bitmark++", "body");
926
- this.throwOnError(out2);
927
885
  return out2;
928
886
  }
929
887
  if (typeof input === "string" && this.parseTextAstString(input) === void 0 && !wasm.is_bitmark(input)) {
@@ -940,8 +898,7 @@ var BitmarkParserGenerator = class {
940
898
  }
941
899
  }
942
900
  const { source, isBitmark } = this.resolveInput(input, void 0);
943
- const out = wasm.convert(source, isBitmark ? "bitmark" : "json", "text", "optimized", false, 2, 1, false, false);
944
- this.throwOnError(out);
901
+ const out = wasm.convert(source, isBitmark ? "bitmark" : "json", "text", "optimized", false, 2, 1, false, false, "", "");
945
902
  return out;
946
903
  }
947
904
  /** If `s` parses as a TextAst JSON array, return it. */
@@ -1045,12 +1002,6 @@ var BitmarkParserGenerator = class {
1045
1002
  encoding: fileOptions?.encoding ?? "utf8"
1046
1003
  });
1047
1004
  }
1048
- /** The core reports failures as `error:`-prefixed strings; bpg throws. */
1049
- throwOnError(out) {
1050
- if (out.startsWith("error:")) {
1051
- throw new Error(out.slice("error:".length).trim());
1052
- }
1053
- }
1054
1005
  };
1055
1006
  function extractHtmlTables(html) {
1056
1007
  const tables = [];
@@ -1087,7 +1038,6 @@ function extractHtmlTables(html) {
1087
1038
  }
1088
1039
  function bitmarkTextParse(text) {
1089
1040
  const out = getLegacyWasm().convert_text(text, "textToAst", "bitmark++", "body");
1090
- if (out.startsWith("error:")) throw new Error(out.slice("error:".length).trim());
1091
1041
  return JSON.parse(out);
1092
1042
  }
1093
1043
 
@@ -1417,11 +1367,6 @@ function patchEntry(path2, op, value) {
1417
1367
  var ALL_LANGUAGES = "all";
1418
1368
 
1419
1369
  // src/core/api.ts
1420
- function lex(input, options = {}) {
1421
- const wasm = getActive();
1422
- if (typeof wasm.lex !== "function") throw new UnsupportedFeatureError("lex", variant());
1423
- return wasm.lex(input, options.stage ?? "lex");
1424
- }
1425
1370
  function bitmarkToObjects(input, options = {}) {
1426
1371
  const out = getActive().convert(
1427
1372
  input,
@@ -1432,9 +1377,10 @@ function bitmarkToObjects(input, options = {}) {
1432
1377
  2,
1433
1378
  1,
1434
1379
  options.includeUnknownProperties ?? false,
1435
- false
1380
+ false,
1381
+ "",
1382
+ ""
1436
1383
  );
1437
- if (out.startsWith("error:")) throw new Error(out.slice("error:".length).trim());
1438
1384
  return JSON.parse(out);
1439
1385
  }
1440
1386
  function objectsToBitmark(bits, options = {}) {
@@ -1450,9 +1396,10 @@ function objectsToBitmark(bits, options = {}) {
1450
1396
  2,
1451
1397
  1,
1452
1398
  false,
1453
- false
1399
+ false,
1400
+ "",
1401
+ ""
1454
1402
  );
1455
- if (out.startsWith("error:")) throw new Error(out.slice("error:".length).trim());
1456
1403
  return out;
1457
1404
  }
1458
1405
  function convert(input, options = {}) {
@@ -1465,7 +1412,19 @@ function convert(input, options = {}) {
1465
1412
  options.indent ?? 2,
1466
1413
  options.spacesAroundValues ?? 1,
1467
1414
  options.includeUnknownProperties ?? false,
1468
- options.mappingReport ?? false
1415
+ options.mappingReport ?? false,
1416
+ options.positionEncoding ?? "",
1417
+ options.tokensLayout ?? ""
1418
+ );
1419
+ }
1420
+ function semanticTokens(input, options = {}) {
1421
+ return JSON.parse(
1422
+ convert(input, {
1423
+ inputFormat: "bitmark",
1424
+ outputFormat: "semantic-tokens",
1425
+ positionEncoding: options.positionEncoding,
1426
+ tokensLayout: options.tokensLayout
1427
+ })
1469
1428
  );
1470
1429
  }
1471
1430
  function canonicalize(input, options = {}) {
@@ -1479,9 +1438,7 @@ function canonicalize(input, options = {}) {
1479
1438
  );
1480
1439
  }
1481
1440
  function countBits(input) {
1482
- const out = getActive().count_bits(input);
1483
- if (out.startsWith("error:")) throw new Error(out);
1484
- return Number.parseInt(out, 10);
1441
+ return Number.parseInt(getActive().count_bits(input), 10);
1485
1442
  }
1486
1443
  function splitBits(input) {
1487
1444
  return JSON.parse(getActive().split_bits(input));
@@ -1517,7 +1474,6 @@ function diff(a, b, options = {}) {
1517
1474
  options.bboxTolerance ?? 8,
1518
1475
  options.spacesAroundValues ?? 0
1519
1476
  );
1520
- if (out.startsWith("error:")) throw new Error(out.slice("error:".length).trim());
1521
1477
  return out;
1522
1478
  }
1523
1479
  function diffBits(a, b, options = {}) {
@@ -1543,7 +1499,11 @@ function info(options = {}) {
1543
1499
  const onlyDeprecated = options.onlyDeprecated ?? false;
1544
1500
  const full = options.full ?? false;
1545
1501
  const language = typeof options.language === "string" ? options.language : options.language?.join(",") ?? "";
1546
- return getActive().info(
1502
+ const wasm = getActive();
1503
+ if (format !== "json" && typeof wasm.info_text_supported !== "function") {
1504
+ throw new UnsupportedFeatureError('info (format: "text")', variant());
1505
+ }
1506
+ return wasm.info(
1547
1507
  infoType,
1548
1508
  format,
1549
1509
  bit,
@@ -1556,8 +1516,11 @@ function info(options = {}) {
1556
1516
  );
1557
1517
  }
1558
1518
  function register(options) {
1559
- const result = getActive().register(options.type, options.data);
1560
- if (result) throw new RegisterError(result.replace(/^error:\s*/, ""));
1519
+ try {
1520
+ getActive().register(options.type, options.data);
1521
+ } catch (e) {
1522
+ throw new RegisterError(e instanceof Error ? e.message : String(e));
1523
+ }
1561
1524
  rememberRegistration(options.type, options.data);
1562
1525
  }
1563
1526
 
@@ -1606,10 +1569,10 @@ export {
1606
1569
  init,
1607
1570
  initSync,
1608
1571
  legacy_exports as legacy,
1609
- lex,
1610
1572
  objectsToBitmark,
1611
1573
  patchEntry,
1612
1574
  register,
1575
+ semanticTokens,
1613
1576
  splitBits,
1614
1577
  transform,
1615
1578
  transformParallel,