@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.cjs CHANGED
@@ -45,10 +45,10 @@ __export(src_exports, {
45
45
  init: () => init,
46
46
  initSync: () => initSync,
47
47
  legacy: () => legacy_exports,
48
- lex: () => lex,
49
48
  objectsToBitmark: () => objectsToBitmark,
50
49
  patchEntry: () => patchEntry,
51
50
  register: () => register,
51
+ semanticTokens: () => semanticTokens,
52
52
  splitBits: () => splitBits,
53
53
  transform: () => transform,
54
54
  transformParallel: () => transformParallel,
@@ -68,7 +68,7 @@ var import_node_module = require("module");
68
68
  // src/generated/package_info.ts
69
69
  var PACKAGE_INFO = {
70
70
  "name": "@gmb/bitmark-parser",
71
- "version": "6.11.1",
71
+ "version": "7.0.0",
72
72
  "author": "Get More Brain Ltd <info@getmorebrain.com>",
73
73
  "license": "ISC",
74
74
  "description": "A parser for bitmark text, powered by WebAssembly."
@@ -107,8 +107,11 @@ function getActive() {
107
107
  }
108
108
  function setActive(feature, module2) {
109
109
  for (const [type, data] of registrations) {
110
- const error = module2.register(type, data);
111
- if (error) throw new RegisterError(error.replace(/^error:\s*/, ""));
110
+ try {
111
+ module2.register(type, data);
112
+ } catch (e) {
113
+ throw new RegisterError(e instanceof Error ? e.message : String(e));
114
+ }
112
115
  }
113
116
  active = module2;
114
117
  activeFeature = feature;
@@ -124,13 +127,6 @@ function variant() {
124
127
  }
125
128
 
126
129
  // src/core/hooked-transform.ts
127
- function isError(s) {
128
- return s.startsWith("error:");
129
- }
130
- function errorString(e) {
131
- const m = e instanceof Error ? e.message : String(e);
132
- return m.startsWith("error:") ? m : `error: ${m}`;
133
- }
134
130
  function stripArray(doc) {
135
131
  const t = doc.trim();
136
132
  return t.startsWith("[") && t.endsWith("]") ? t.slice(1, -1) : doc;
@@ -142,17 +138,13 @@ function patchJsonOf(patch) {
142
138
  }
143
139
  function buildUnits(wasm, input, inputIsBitmark) {
144
140
  if (inputIsBitmark) {
145
- const out2 = wasm.split_bits(input);
146
- if (isError(out2)) throw new Error(out2);
147
- const slices = JSON.parse(out2);
141
+ const slices = JSON.parse(wasm.split_bits(input));
148
142
  return slices.map((s) => ({
149
143
  view: { format: "bitmark", index: s.index, total: slices.length, source: s.source },
150
144
  source: s.source
151
145
  }));
152
146
  }
153
- const out = wasm.split_json_bits(input);
154
- if (isError(out)) throw new Error(out);
155
- const envelopes = JSON.parse(out);
147
+ const envelopes = JSON.parse(wasm.split_json_bits(input));
156
148
  return envelopes.map((env, index) => ({
157
149
  view: { format: "json", index, total: envelopes.length, bit: env.bit ?? {} },
158
150
  // Per-bit transform consumes a one-bit JSON document.
@@ -183,9 +175,7 @@ function planTransform(wasm, input, options) {
183
175
  if (patchJson === "") {
184
176
  resolved = units.map((_, index) => ({ source: index, bit: null, patch: [] }));
185
177
  } else {
186
- const out = wasm.resolve_patch_document(input, patchJson);
187
- if (isError(out)) throw new Error(out);
188
- resolved = JSON.parse(out);
178
+ resolved = JSON.parse(wasm.resolve_patch_document(input, patchJson));
189
179
  }
190
180
  const jobs = [];
191
181
  const views = [];
@@ -232,7 +222,6 @@ function computeBit(wasm, job) {
232
222
  2,
233
223
  job.spacesAroundValues
234
224
  );
235
- if (isError(out)) return out;
236
225
  return job.outputFormat === "json" ? stripArray(out) : out;
237
226
  }
238
227
  function recombine(wasm, elements, output, pretty, indent) {
@@ -241,16 +230,10 @@ function recombine(wasm, elements, output, pretty, indent) {
241
230
  return pretty ? wasm.prettify(arr, indent) : arr;
242
231
  }
243
232
  function runHookedTransform(wasm, input, options) {
244
- let plan;
245
- try {
246
- plan = planTransform(wasm, input, options);
247
- } catch (e) {
248
- return errorString(e);
249
- }
233
+ const plan = planTransform(wasm, input, options);
250
234
  const rendered = [];
251
235
  for (let i = 0; i < plan.jobs.length; i++) {
252
236
  const element = computeBit(wasm, plan.jobs[i]);
253
- if (isError(element)) return element;
254
237
  options.postHook?.(plan.views[i], element);
255
238
  rendered.push(element);
256
239
  }
@@ -259,20 +242,9 @@ function runHookedTransform(wasm, input, options) {
259
242
 
260
243
  // src/core/parallel.ts
261
244
  async function orchestrateParallel(wasm, input, options, runJobs) {
262
- let plan;
263
- try {
264
- plan = planTransform(wasm, input, options);
265
- } catch (e) {
266
- return errorString(e);
267
- }
268
- let elements;
269
- try {
270
- elements = await runJobs(plan.jobs, options.workers ?? 4);
271
- } catch (e) {
272
- return errorString(e);
273
- }
245
+ const plan = planTransform(wasm, input, options);
246
+ const elements = await runJobs(plan.jobs, options.workers ?? 4);
274
247
  for (let i = 0; i < elements.length; i++) {
275
- if (isError(elements[i])) return elements[i];
276
248
  options.postHook?.(plan.views[i], elements[i]);
277
249
  }
278
250
  return recombine(wasm, elements, plan.output, plan.pretty, plan.indent);
@@ -627,10 +599,7 @@ var Builder = class {
627
599
  }
628
600
  }
629
601
  this.parseStringTexts(bit);
630
- const out = wasm.convert(JSON.stringify([{ bit }]), "json", "json", "optimized", false, 2, 1, false, false);
631
- if (out.startsWith("error:")) {
632
- throw new Error(out.slice("error:".length).trim());
633
- }
602
+ const out = wasm.convert(JSON.stringify([{ bit }]), "json", "json", "optimized", false, 2, 1, false, false, "", "");
634
603
  const entries = JSON.parse(out);
635
604
  const built = entries[0]?.bit ?? bit;
636
605
  for (const [key, value] of Object.entries(bit)) {
@@ -767,8 +736,7 @@ var BitmarkParserGenerator = class {
767
736
  const emit = coreFormat === "bitmark" ? prettyIndent(bitmarkOptions.prettifyJson) : { pretty, indent };
768
737
  const spaces = spacesAround(bitmarkOptions.spacesAroundValues);
769
738
  const includeUnknown = !jsonOptions.excludeUnknownProperties;
770
- const out = wasm.convert(source, isBitmark ? "bitmark" : "json", coreFormat, "optimized", emit.pretty, emit.indent, spaces, includeUnknown, false);
771
- this.throwOnError(out);
739
+ const out = wasm.convert(source, isBitmark ? "bitmark" : "json", coreFormat, "optimized", emit.pretty, emit.indent, spaces, includeUnknown, false, "", "");
772
740
  if (outputFormat === "bitmark" || outputFormat === "text") {
773
741
  return this.deliver(out, opts.outputFile, opts.fileOptions);
774
742
  }
@@ -802,7 +770,6 @@ var BitmarkParserGenerator = class {
802
770
  );
803
771
  const spaces = spacesAround(bitmarkOptions.spacesAroundValues);
804
772
  const out = wasm.canonicalize(source, isBitmark ? "bitmark" : "json", "optimized", pretty, indent, spaces);
805
- this.throwOnError(out);
806
773
  if (isBitmark) return this.deliver(out, opts.outputFile, opts.fileOptions);
807
774
  if (opts.outputFile) return this.deliver(out, opts.outputFile, opts.fileOptions);
808
775
  if (pretty || jsonOptions.stringify) return out;
@@ -819,7 +786,6 @@ var BitmarkParserGenerator = class {
819
786
  const { pretty, indent } = prettyIndent(opts.prettify);
820
787
  const coreFormat = format === "pojo" ? "json" : format;
821
788
  const out = wasm.info(type, coreFormat, opts.bit ?? "", pretty, indent, false, false, true, "");
822
- this.throwOnError(out);
823
789
  if (format === "pojo") return JSON.parse(out);
824
790
  return out;
825
791
  }
@@ -851,8 +817,7 @@ var BitmarkParserGenerator = class {
851
817
  const isHtmlInput = opts.inputFormat === "html" || opts.inputFormat === void 0 && typeof input === "string" && /<table[\s>]/iu.test(inStr);
852
818
  if (!isHtmlInput) {
853
819
  const { source, isBitmark } = this.resolveInput(input, void 0);
854
- const out2 = wasm.convert(source, isBitmark ? "bitmark" : "json", "html", "optimized", false, 2, 1, false, false);
855
- this.throwOnError(out2);
820
+ const out2 = wasm.convert(source, isBitmark ? "bitmark" : "json", "html", "optimized", false, 2, 1, false, false, "", "");
856
821
  return this.deliver(extractHtmlTables(out2).join("\n"), opts.outputFile, opts.fileOptions);
857
822
  }
858
823
  const bitType = opts.tableFormat === "table" ? "table" : "table-extended";
@@ -861,13 +826,11 @@ var BitmarkParserGenerator = class {
861
826
  const { pretty, indent } = prettyIndent(jsonOptions.prettify);
862
827
  const spaces = spacesAround(bitmarkOptions.spacesAroundValues);
863
828
  if (outputFormat === "html") {
864
- const out2 = wasm.convert(wrapped, "html", "html", "optimized", false, 2, 1, false, false);
865
- this.throwOnError(out2);
829
+ const out2 = wasm.convert(wrapped, "html", "html", "optimized", false, 2, 1, false, false, "", "");
866
830
  return this.deliver(extractHtmlTables(out2).join("\n"), opts.outputFile, opts.fileOptions);
867
831
  }
868
832
  const coreFormat = outputFormat === "ast" ? "json" : outputFormat;
869
- const out = wasm.convert(wrapped, "html", coreFormat, "optimized", pretty, indent, spaces, false, false);
870
- this.throwOnError(out);
833
+ const out = wasm.convert(wrapped, "html", coreFormat, "optimized", pretty, indent, spaces, false, false, "", "");
871
834
  if (outputFormat === "bitmark" || outputFormat === "text") {
872
835
  return this.deliver(out, opts.outputFile, opts.fileOptions);
873
836
  }
@@ -891,8 +854,7 @@ var BitmarkParserGenerator = class {
891
854
  const opts = Object.assign({}, options);
892
855
  const wasm = getLegacyWasm();
893
856
  const { source, isBitmark } = this.resolveInput(input, opts.inputFormat);
894
- const out = wasm.convert(source, isBitmark ? "bitmark" : "json", "json", "optimized", false, 2, 1, true, false);
895
- this.throwOnError(out);
857
+ const out = wasm.convert(source, isBitmark ? "bitmark" : "json", "json", "optimized", false, 2, 1, true, false, "", "");
896
858
  return makePseudoAst(JSON.parse(out));
897
859
  }
898
860
  /**
@@ -944,11 +906,9 @@ var BitmarkParserGenerator = class {
944
906
  }
945
907
  if (ast !== void 0) {
946
908
  const out2 = isBitmarkText ? wasm.convert_text(JSON.stringify(ast), "astToText", "bitmark++", textLocation) : wasm.convert_text(JSON.stringify(ast), "astToPlainText", "plainText", textLocation);
947
- this.throwOnError(out2);
948
909
  return this.deliver(out2, opts.outputFile, opts.fileOptions);
949
910
  }
950
911
  const out = wasm.convert_text(inStr, "textToAst", isBitmarkText ? "bitmark++" : "plainText", "body");
951
- this.throwOnError(out);
952
912
  const { pretty, indent } = prettyIndent(jsonOptions.prettify);
953
913
  const outStr = pretty ? wasm.prettify(out, indent) : out;
954
914
  if (opts.outputFile) return this.deliver(outStr, opts.outputFile, opts.fileOptions);
@@ -966,7 +926,6 @@ var BitmarkParserGenerator = class {
966
926
  "bitmark++",
967
927
  "body"
968
928
  );
969
- this.throwOnError(out);
970
929
  return out;
971
930
  }
972
931
  /**
@@ -981,7 +940,6 @@ var BitmarkParserGenerator = class {
981
940
  if (inputFormat === "bitmarkText") {
982
941
  const ast = wasm.convert_text(String(input), "textToAst", "bitmark++", "body");
983
942
  const out2 = wasm.convert_text(ast, "astToPlainText", "bitmark++", "body");
984
- this.throwOnError(out2);
985
943
  return out2;
986
944
  }
987
945
  if (typeof input === "string" && this.parseTextAstString(input) === void 0 && !wasm.is_bitmark(input)) {
@@ -998,8 +956,7 @@ var BitmarkParserGenerator = class {
998
956
  }
999
957
  }
1000
958
  const { source, isBitmark } = this.resolveInput(input, void 0);
1001
- const out = wasm.convert(source, isBitmark ? "bitmark" : "json", "text", "optimized", false, 2, 1, false, false);
1002
- this.throwOnError(out);
959
+ const out = wasm.convert(source, isBitmark ? "bitmark" : "json", "text", "optimized", false, 2, 1, false, false, "", "");
1003
960
  return out;
1004
961
  }
1005
962
  /** If `s` parses as a TextAst JSON array, return it. */
@@ -1103,12 +1060,6 @@ var BitmarkParserGenerator = class {
1103
1060
  encoding: fileOptions?.encoding ?? "utf8"
1104
1061
  });
1105
1062
  }
1106
- /** The core reports failures as `error:`-prefixed strings; bpg throws. */
1107
- throwOnError(out) {
1108
- if (out.startsWith("error:")) {
1109
- throw new Error(out.slice("error:".length).trim());
1110
- }
1111
- }
1112
1063
  };
1113
1064
  function extractHtmlTables(html) {
1114
1065
  const tables = [];
@@ -1145,7 +1096,6 @@ function extractHtmlTables(html) {
1145
1096
  }
1146
1097
  function bitmarkTextParse(text) {
1147
1098
  const out = getLegacyWasm().convert_text(text, "textToAst", "bitmark++", "body");
1148
- if (out.startsWith("error:")) throw new Error(out.slice("error:".length).trim());
1149
1099
  return JSON.parse(out);
1150
1100
  }
1151
1101
 
@@ -1475,11 +1425,6 @@ function patchEntry(path2, op, value) {
1475
1425
  var ALL_LANGUAGES = "all";
1476
1426
 
1477
1427
  // src/core/api.ts
1478
- function lex(input, options = {}) {
1479
- const wasm = getActive();
1480
- if (typeof wasm.lex !== "function") throw new UnsupportedFeatureError("lex", variant());
1481
- return wasm.lex(input, options.stage ?? "lex");
1482
- }
1483
1428
  function bitmarkToObjects(input, options = {}) {
1484
1429
  const out = getActive().convert(
1485
1430
  input,
@@ -1490,9 +1435,10 @@ function bitmarkToObjects(input, options = {}) {
1490
1435
  2,
1491
1436
  1,
1492
1437
  options.includeUnknownProperties ?? false,
1493
- false
1438
+ false,
1439
+ "",
1440
+ ""
1494
1441
  );
1495
- if (out.startsWith("error:")) throw new Error(out.slice("error:".length).trim());
1496
1442
  return JSON.parse(out);
1497
1443
  }
1498
1444
  function objectsToBitmark(bits, options = {}) {
@@ -1508,9 +1454,10 @@ function objectsToBitmark(bits, options = {}) {
1508
1454
  2,
1509
1455
  1,
1510
1456
  false,
1511
- false
1457
+ false,
1458
+ "",
1459
+ ""
1512
1460
  );
1513
- if (out.startsWith("error:")) throw new Error(out.slice("error:".length).trim());
1514
1461
  return out;
1515
1462
  }
1516
1463
  function convert(input, options = {}) {
@@ -1523,7 +1470,19 @@ function convert(input, options = {}) {
1523
1470
  options.indent ?? 2,
1524
1471
  options.spacesAroundValues ?? 1,
1525
1472
  options.includeUnknownProperties ?? false,
1526
- options.mappingReport ?? false
1473
+ options.mappingReport ?? false,
1474
+ options.positionEncoding ?? "",
1475
+ options.tokensLayout ?? ""
1476
+ );
1477
+ }
1478
+ function semanticTokens(input, options = {}) {
1479
+ return JSON.parse(
1480
+ convert(input, {
1481
+ inputFormat: "bitmark",
1482
+ outputFormat: "semantic-tokens",
1483
+ positionEncoding: options.positionEncoding,
1484
+ tokensLayout: options.tokensLayout
1485
+ })
1527
1486
  );
1528
1487
  }
1529
1488
  function canonicalize(input, options = {}) {
@@ -1537,9 +1496,7 @@ function canonicalize(input, options = {}) {
1537
1496
  );
1538
1497
  }
1539
1498
  function countBits(input) {
1540
- const out = getActive().count_bits(input);
1541
- if (out.startsWith("error:")) throw new Error(out);
1542
- return Number.parseInt(out, 10);
1499
+ return Number.parseInt(getActive().count_bits(input), 10);
1543
1500
  }
1544
1501
  function splitBits(input) {
1545
1502
  return JSON.parse(getActive().split_bits(input));
@@ -1575,7 +1532,6 @@ function diff(a, b, options = {}) {
1575
1532
  options.bboxTolerance ?? 8,
1576
1533
  options.spacesAroundValues ?? 0
1577
1534
  );
1578
- if (out.startsWith("error:")) throw new Error(out.slice("error:".length).trim());
1579
1535
  return out;
1580
1536
  }
1581
1537
  function diffBits(a, b, options = {}) {
@@ -1601,7 +1557,11 @@ function info(options = {}) {
1601
1557
  const onlyDeprecated = options.onlyDeprecated ?? false;
1602
1558
  const full = options.full ?? false;
1603
1559
  const language = typeof options.language === "string" ? options.language : options.language?.join(",") ?? "";
1604
- return getActive().info(
1560
+ const wasm = getActive();
1561
+ if (format !== "json" && typeof wasm.info_text_supported !== "function") {
1562
+ throw new UnsupportedFeatureError('info (format: "text")', variant());
1563
+ }
1564
+ return wasm.info(
1605
1565
  infoType,
1606
1566
  format,
1607
1567
  bit,
@@ -1614,8 +1574,11 @@ function info(options = {}) {
1614
1574
  );
1615
1575
  }
1616
1576
  function register(options) {
1617
- const result = getActive().register(options.type, options.data);
1618
- if (result) throw new RegisterError(result.replace(/^error:\s*/, ""));
1577
+ try {
1578
+ getActive().register(options.type, options.data);
1579
+ } catch (e) {
1580
+ throw new RegisterError(e instanceof Error ? e.message : String(e));
1581
+ }
1619
1582
  rememberRegistration(options.type, options.data);
1620
1583
  }
1621
1584
 
@@ -1665,10 +1628,10 @@ function transformParallel(input, options = {}) {
1665
1628
  init,
1666
1629
  initSync,
1667
1630
  legacy,
1668
- lex,
1669
1631
  objectsToBitmark,
1670
1632
  patchEntry,
1671
1633
  register,
1634
+ semanticTokens,
1672
1635
  splitBits,
1673
1636
  transform,
1674
1637
  transformParallel,