@bcts/dcbor-pattern 1.0.0-alpha.12 → 1.0.0-alpha.14

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.
package/dist/index.cjs CHANGED
@@ -1,5 +1,6 @@
1
1
  let _bcts_dcbor = require("@bcts/dcbor");
2
2
  let _bcts_known_values = require("@bcts/known-values");
3
+ let _bcts_dcbor_parse = require("@bcts/dcbor-parse");
3
4
  let _bcts_components = require("@bcts/components");
4
5
 
5
6
  //#region src/error.ts
@@ -76,8 +77,6 @@ const errorToString = (error) => {
76
77
  case "InvalidDigestPattern": return `Invalid digest pattern: ${error.message} at ${error.span.start}..${error.span.end}`;
77
78
  case "UnterminatedDigestQuoted": return `Unterminated digest quoted pattern at ${error.span.start}..${error.span.end}`;
78
79
  case "UnterminatedDateQuoted": return `Unterminated date quoted pattern at ${error.span.start}..${error.span.end}`;
79
- case "InvalidDigest": return `Invalid digest at ${error.span.start}..${error.span.end}`;
80
- case "InvalidDate": return `Invalid date at ${error.span.start}..${error.span.end}`;
81
80
  case "Unknown": return "Unknown error";
82
81
  }
83
82
  };
@@ -187,14 +186,6 @@ const adjustSpan = (error, offset) => {
187
186
  type: "UnterminatedDateQuoted",
188
187
  span: span(offset + error.span.start, offset + error.span.end)
189
188
  };
190
- case "InvalidDigest": return {
191
- type: "InvalidDigest",
192
- span: span(offset + error.span.start, offset + error.span.end)
193
- };
194
- case "InvalidDate": return {
195
- type: "InvalidDate",
196
- span: span(offset + error.span.start, offset + error.span.end)
197
- };
198
189
  case "EmptyInput":
199
190
  case "UnexpectedEndOfInput":
200
191
  case "Unknown": return error;
@@ -761,7 +752,10 @@ const truncateWithEllipsis = (s, maxLength) => {
761
752
  const formatCborElement = (cbor$3, format, maxLength) => {
762
753
  let diagnostic;
763
754
  if (format === PathElementFormat.DiagnosticSummary) diagnostic = (0, _bcts_dcbor.summary)(cbor$3);
764
- else diagnostic = (0, _bcts_dcbor.diagnosticOpt)(cbor$3, { flat: true });
755
+ else diagnostic = (0, _bcts_dcbor.diagnosticOpt)(cbor$3, {
756
+ flat: true,
757
+ summarize: true
758
+ });
765
759
  return truncateWithEllipsis(diagnostic, maxLength);
766
760
  };
767
761
  /**
@@ -1383,7 +1377,7 @@ const datePatternDisplay = (pattern) => {
1383
1377
  //#endregion
1384
1378
  //#region src/pattern/value/digest-pattern.ts
1385
1379
  /** CBOR tag for digest (BCR-2021-002) */
1386
- const DIGEST_TAG = 40001n;
1380
+ const DIGEST_TAG = 40001;
1387
1381
  /** Expected size of a SHA-256 digest */
1388
1382
  const DIGEST_SIZE = 32;
1389
1383
  /**
@@ -1419,7 +1413,8 @@ const digestPatternBinaryRegex = (pattern) => ({
1419
1413
  */
1420
1414
  const extractDigestBytes = (haystack) => {
1421
1415
  if (!(0, _bcts_dcbor.isTagged)(haystack)) return;
1422
- if ((0, _bcts_dcbor.tagValue)(haystack) !== DIGEST_TAG) return;
1416
+ const tag = (0, _bcts_dcbor.tagValue)(haystack);
1417
+ if (tag === void 0 || Number(tag) !== DIGEST_TAG) return;
1423
1418
  const content = (0, _bcts_dcbor.tagContent)(haystack);
1424
1419
  if (content === void 0) return;
1425
1420
  const bytes = (0, _bcts_dcbor.asBytes)(content);
@@ -2311,6 +2306,13 @@ const taggedPatternWithRegex = (regex, pattern) => ({
2311
2306
  pattern
2312
2307
  });
2313
2308
  /**
2309
+ * Compare two tag values, handling both number and bigint types.
2310
+ */
2311
+ const tagsEqual = (a, b) => {
2312
+ if (a === void 0) return false;
2313
+ return Number(a) === Number(b);
2314
+ };
2315
+ /**
2314
2316
  * Tests if a CBOR value matches this tagged pattern.
2315
2317
  */
2316
2318
  const taggedPatternMatches = (pattern, haystack) => {
@@ -2320,7 +2322,7 @@ const taggedPatternMatches = (pattern, haystack) => {
2320
2322
  if (content === void 0) return false;
2321
2323
  switch (pattern.variant) {
2322
2324
  case "Any": return true;
2323
- case "Tag": return tag === pattern.tag.value && matchPattern(pattern.pattern, content);
2325
+ case "Tag": return tagsEqual(tag, pattern.tag.value) && matchPattern(pattern.pattern, content);
2324
2326
  case "Name": return String(tag) === pattern.name && matchPattern(pattern.pattern, content);
2325
2327
  case "Regex": {
2326
2328
  const tagName = String(tag);
@@ -2347,7 +2349,7 @@ const taggedPatternPathsWithCaptures = (pattern, haystack) => {
2347
2349
  switch (pattern.variant) {
2348
2350
  case "Any": return [[[haystack]], /* @__PURE__ */ new Map()];
2349
2351
  case "Tag": {
2350
- if (tag !== pattern.tag.value) return [[], /* @__PURE__ */ new Map()];
2352
+ if (!tagsEqual(tag, pattern.tag.value)) return [[], /* @__PURE__ */ new Map()];
2351
2353
  const innerResult = getPatternPathsWithCapturesDirect(pattern.pattern, content);
2352
2354
  if (innerResult.paths.length === 0) return [[], innerResult.captures];
2353
2355
  const taggedPaths = innerResult.paths.map((contentPath) => {
@@ -2987,13 +2989,8 @@ const axisChildren = (axis, cbor$3) => {
2987
2989
  }
2988
2990
  case "MapValue": {
2989
2991
  if (!(0, _bcts_dcbor.isMap)(cbor$3)) return [];
2990
- const keys = (0, _bcts_dcbor.mapKeys)(cbor$3);
2991
- if (keys === void 0 || keys === null) return [];
2992
- const values = [];
2993
- for (const key of keys) {
2994
- const value = (0, _bcts_dcbor.mapValue)(cbor$3, key);
2995
- if (value !== void 0 && value !== null) values.push(value);
2996
- }
2992
+ const values = (0, _bcts_dcbor.mapValues)(cbor$3);
2993
+ if (values === void 0 || values === null) return [];
2997
2994
  return values;
2998
2995
  }
2999
2996
  case "TaggedContent": {
@@ -3216,7 +3213,8 @@ const runThread = (prog, start, out) => {
3216
3213
  case "Search": {
3217
3214
  const innerPattern = prog.literals[instr.patternIndex];
3218
3215
  const result = searchPatternPathsWithCaptures(searchPattern(innerPattern), th.cbor);
3219
- for (const searchPath of result.paths) {
3216
+ const reversedPaths = [...result.paths].reverse();
3217
+ for (const searchPath of reversedPaths) {
3220
3218
  const newThread = {
3221
3219
  pc: th.pc + 1,
3222
3220
  cbor: th.cbor,
@@ -4954,7 +4952,7 @@ var Lexer = class Lexer {
4954
4952
  });
4955
4953
  }
4956
4954
  /**
4957
- * Parse a number literal.
4955
+ * Parse a number literal using dcbor-parse for consistency with dCBOR.
4958
4956
  */
4959
4957
  parseNumber(start) {
4960
4958
  const numStart = this.#position;
@@ -4982,8 +4980,18 @@ var Lexer = class Lexer {
4982
4980
  });
4983
4981
  while (isDigit(this.peek() ?? "")) this.advance();
4984
4982
  }
4985
- const numStr = this.#input.slice(numStart, this.#position);
4986
- const value = parseFloat(numStr);
4983
+ const parseResult = (0, _bcts_dcbor_parse.parseDcborItemPartial)(this.#input.slice(numStart, this.#position));
4984
+ if (!parseResult.ok) return Err({
4985
+ type: "InvalidNumberFormat",
4986
+ span: this.spanFrom(start)
4987
+ });
4988
+ const [cbor$3] = parseResult.value;
4989
+ const numValue = cbor$3.asNumber();
4990
+ if (numValue === void 0) return Err({
4991
+ type: "InvalidNumberFormat",
4992
+ span: this.spanFrom(start)
4993
+ });
4994
+ const value = typeof numValue === "bigint" ? Number(numValue) : numValue;
4987
4995
  if (!isFinite(value)) return Err({
4988
4996
  type: "InvalidNumberFormat",
4989
4997
  span: this.spanFrom(start)
@@ -5888,7 +5896,8 @@ const parsePrimary = (lexer) => {
5888
5896
  });
5889
5897
  } catch {
5890
5898
  return Err({
5891
- type: "InvalidDigest",
5899
+ type: "InvalidDigestPattern",
5900
+ message: `Invalid digest hex: ${token.value}`,
5892
5901
  span: spanned.span
5893
5902
  });
5894
5903
  }
@@ -5905,7 +5914,7 @@ const parsePrimary = (lexer) => {
5905
5914
  });
5906
5915
  } catch {
5907
5916
  return Err({
5908
- type: "InvalidDate",
5917
+ type: "InvalidDateFormat",
5909
5918
  span: spanned.span
5910
5919
  });
5911
5920
  }