@futpib/parser 1.0.0 → 1.0.1

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 (171) hide show
  1. package/build/apk.d.ts +29 -3
  2. package/build/apkParser.d.ts +15 -2
  3. package/build/apkParser.js +70 -41
  4. package/build/apkParser.test.js +2 -2
  5. package/build/apkUnparser.d.ts +4 -0
  6. package/build/apkUnparser.js +90 -0
  7. package/build/apkUnparser.test.d.ts +1 -0
  8. package/build/apkUnparser.test.js +26 -0
  9. package/build/arbitraryFileSystemEntry.js +1 -1
  10. package/build/arbitraryZip.d.ts +1 -1
  11. package/build/arbitraryZip.js +13 -19
  12. package/build/arbitraryZipPermissions.d.ts +1 -8
  13. package/build/arbitraryZipPermissions.js +1 -16
  14. package/build/arbitraryZipStream.d.ts +1 -1
  15. package/build/arbitraryZipStream.js +3 -3
  16. package/build/arrayParser.d.ts +1 -1
  17. package/build/arrayParser.js +2 -2
  18. package/build/arrayParser.test.js +2 -2
  19. package/build/arrayUnparser.d.ts +2 -0
  20. package/build/arrayUnparser.js +8 -0
  21. package/build/bsonParser.test.js +2 -2
  22. package/build/customInvariant.d.ts +4 -0
  23. package/build/customInvariant.js +11 -0
  24. package/build/debugLogParser.d.ts +1 -1
  25. package/build/debugLogParser.js +1 -1
  26. package/build/elementParser.d.ts +2 -2
  27. package/build/elementParser.js +1 -1
  28. package/build/endOfInputParser.d.ts +2 -2
  29. package/build/exactElementParser.d.ts +1 -1
  30. package/build/exactSequenceParser.js +1 -1
  31. package/build/index.d.ts +5 -2
  32. package/build/index.js +3 -0
  33. package/build/inputReader.d.ts +3 -3
  34. package/build/inputReader.js +6 -6
  35. package/build/inputReader.test.js +7 -7
  36. package/build/javaKeyStore.d.ts +1 -0
  37. package/build/javaKeyStore.js +1 -0
  38. package/build/javaKeyStoreParser.d.ts +2 -0
  39. package/build/javaKeyStoreParser.js +67 -0
  40. package/build/javaKeyStoreParser.test.d.ts +1 -0
  41. package/build/javaKeyStoreParser.test.js +16 -0
  42. package/build/javaKeystoreParser.d.ts +2 -0
  43. package/build/javaKeystoreParser.js +7 -0
  44. package/build/jsonParser.js +3 -2
  45. package/build/jsonParser.test.js +2 -2
  46. package/build/listParser.d.ts +1 -1
  47. package/build/listParser.js +5 -5
  48. package/build/negativeLookahead.d.ts +1 -1
  49. package/build/negativeLookahead.js +16 -18
  50. package/build/negativeLookaheadParser.d.ts +2 -0
  51. package/build/negativeLookaheadParser.js +18 -0
  52. package/build/optionalParser.d.ts +1 -1
  53. package/build/optionalParser.js +2 -2
  54. package/build/parser.d.ts +3 -3
  55. package/build/parser.js +3 -3
  56. package/build/parser.test.js +16 -16
  57. package/build/parserAccessorParser.d.ts +1 -1
  58. package/build/parserConsumedSequenceParser.d.ts +2 -0
  59. package/build/parserConsumedSequenceParser.js +17 -0
  60. package/build/parserContext.d.ts +6 -6
  61. package/build/parserContext.js +15 -14
  62. package/build/parserContext.test.js +7 -7
  63. package/build/parserCreatorCompose.d.ts +3 -3
  64. package/build/parserCreatorCompose.js +2 -2
  65. package/build/parserImplementationInvariant.d.ts +1 -1
  66. package/build/parserImplementationInvariant.js +2 -2
  67. package/build/parserInputCompanion.d.ts +20 -0
  68. package/build/parserInputCompanion.js +30 -0
  69. package/build/parserInvariant.d.ts +1 -1
  70. package/build/parserInvariant.js +1 -1
  71. package/build/quantifierParser.d.ts +2 -0
  72. package/build/quantifierParser.js +17 -0
  73. package/build/sequenceBuffer.d.ts +3 -3
  74. package/build/sequenceBuffer.js +6 -6
  75. package/build/sequenceBuffer.test.js +2 -2
  76. package/build/sequenceUnparser.d.ts +2 -0
  77. package/build/sequenceUnparser.js +6 -0
  78. package/build/sliceBoundedParser.d.ts +1 -1
  79. package/build/sliceBoundedParser.js +1 -1
  80. package/build/sliceBoundedParser.test.js +2 -2
  81. package/build/terminatedArrayParser.d.ts +1 -1
  82. package/build/terminatedArrayParser.js +3 -3
  83. package/build/uint8Array.d.ts +1 -0
  84. package/build/uint8Array.js +7 -0
  85. package/build/unparser.d.ts +8 -0
  86. package/build/unparser.js +104 -0
  87. package/build/unparser.test.d.ts +1 -0
  88. package/build/unparser.test.js +150 -0
  89. package/build/unparserContext.d.ts +31 -0
  90. package/build/unparserContext.js +74 -0
  91. package/build/unparserError.d.ts +9 -0
  92. package/build/unparserError.js +9 -0
  93. package/build/unparserImplementationInvariant.d.ts +2 -0
  94. package/build/unparserImplementationInvariant.js +5 -0
  95. package/build/unparserInputCompanion.d.ts +15 -0
  96. package/build/unparserInputCompanion.js +13 -0
  97. package/build/unparserOutputCompanion.d.ts +15 -0
  98. package/build/unparserOutputCompanion.js +13 -0
  99. package/build/zip.d.ts +9 -17
  100. package/build/zipParser.d.ts +13 -10
  101. package/build/zipParser.js +48 -60
  102. package/build/zipParser.test.js +2 -7
  103. package/build/zipUnparser.d.ts +5 -0
  104. package/build/zipUnparser.js +171 -0
  105. package/build/zipUnparser.test.d.ts +1 -0
  106. package/build/zipUnparser.test.js +80 -0
  107. package/package.json +4 -2
  108. package/src/apk.ts +35 -3
  109. package/src/apkParser.test.ts +2 -2
  110. package/src/apkParser.test.ts.md +114 -111
  111. package/src/apkParser.test.ts.snap +0 -0
  112. package/src/apkParser.ts +150 -85
  113. package/src/apkUnparser.test.ts +37 -0
  114. package/src/apkUnparser.ts +120 -0
  115. package/src/arbitraryFileSystemEntry.ts +2 -4
  116. package/src/arbitraryZip.ts +20 -27
  117. package/src/arbitraryZipPermissions.ts +0 -25
  118. package/src/arbitraryZipStream.ts +4 -4
  119. package/src/arrayParser.test.ts +3 -3
  120. package/src/arrayParser.ts +3 -2
  121. package/src/arrayUnparser.ts +13 -0
  122. package/src/bsonParser.test.ts +2 -2
  123. package/src/bsonParser.ts +3 -3
  124. package/src/{parserInvariant.ts → customInvariant.ts} +1 -1
  125. package/src/debugLogParser.ts +1 -1
  126. package/src/elementParser.ts +3 -3
  127. package/src/endOfInputParser.ts +4 -4
  128. package/src/exactElementParser.ts +1 -1
  129. package/src/exactSequenceParser.ts +2 -2
  130. package/src/index.ts +15 -2
  131. package/src/inputReader.test.ts +7 -7
  132. package/src/inputReader.ts +5 -5
  133. package/src/javaKeyStore.ts +0 -0
  134. package/src/javaKeyStoreParser.test.ts +23 -0
  135. package/src/javaKeyStoreParser.test.ts.md +103 -0
  136. package/src/javaKeyStoreParser.test.ts.snap +0 -0
  137. package/src/javaKeyStoreParser.ts +136 -0
  138. package/src/jsonParser.test.ts +2 -2
  139. package/src/jsonParser.ts +13 -12
  140. package/src/listParser.ts +6 -6
  141. package/src/negativeLookaheadParser.ts +24 -0
  142. package/src/optionalParser.ts +3 -3
  143. package/src/parser.test.ts +19 -17
  144. package/src/parser.ts +7 -7
  145. package/src/parserAccessorParser.ts +1 -1
  146. package/src/parserConsumedSequenceParser.ts +20 -0
  147. package/src/parserContext.test.ts +7 -7
  148. package/src/parserContext.ts +18 -14
  149. package/src/parserCreatorCompose.ts +6 -6
  150. package/src/parserImplementationInvariant.ts +2 -2
  151. package/src/{inputCompanion.ts → parserInputCompanion.ts} +10 -6
  152. package/src/quantifierParser.ts +25 -0
  153. package/src/sequenceBuffer.test.ts +2 -2
  154. package/src/sequenceBuffer.ts +5 -5
  155. package/src/sequenceUnparser.ts +9 -0
  156. package/src/sliceBoundedParser.test.ts +2 -2
  157. package/src/sliceBoundedParser.ts +2 -2
  158. package/src/terminatedArrayParser.ts +3 -3
  159. package/src/uint8Array.ts +10 -0
  160. package/src/unparser.test.ts +221 -0
  161. package/src/unparser.ts +209 -0
  162. package/src/unparserContext.ts +127 -0
  163. package/src/unparserError.ts +12 -0
  164. package/src/unparserImplementationInvariant.ts +6 -0
  165. package/src/unparserOutputCompanion.ts +24 -0
  166. package/src/zip.ts +10 -22
  167. package/src/zipParser.test.ts +2 -8
  168. package/src/zipParser.ts +147 -129
  169. package/src/zipUnparser.test.ts +119 -0
  170. package/src/zipUnparser.ts +239 -0
  171. package/src/negativeLookahead.ts +0 -26
@@ -1,17 +1,18 @@
1
+ /* eslint-disable prefer-arrow-callback */
1
2
  import invariant from 'invariant';
2
- import { ParserUnexpectedEndOfInputError, ParserParsingInvariantError, ParserParsingJoinNoneError, ParserParsingJoinAllError, ParserParsingJoinDeepestError, ParserParsingJoinFurthestError } from './parserError.js';
3
- import { parserInvariant } from './parserInvariant.js';
3
+ import { ParserUnexpectedEndOfInputError, ParserParsingInvariantError, ParserParsingJoinNoneError, ParserParsingJoinAllError, ParserParsingJoinDeepestError, ParserParsingJoinFurthestError, } from './parserError.js';
4
+ import { customInvariant } from './customInvariant.js';
4
5
  let parserContextId = 0;
5
6
  export class ParserContextImplementation {
6
- _inputCompanion;
7
+ _parserInputCompanion;
7
8
  _inputReader;
8
9
  _parentParserContext;
9
10
  _options;
10
11
  _id = parserContextId++;
11
12
  _depth;
12
13
  _exclusiveChildParserContext = undefined;
13
- constructor(_inputCompanion, _inputReader, _parentParserContext = undefined, _options) {
14
- this._inputCompanion = _inputCompanion;
14
+ constructor(_parserInputCompanion, _inputReader, _parentParserContext = undefined, _options) {
15
+ this._parserInputCompanion = _parserInputCompanion;
15
16
  this._inputReader = _inputReader;
16
17
  this._parentParserContext = _parentParserContext;
17
18
  this._options = _options;
@@ -30,13 +31,13 @@ export class ParserContextImplementation {
30
31
  ].join(' ');
31
32
  }
32
33
  from(elements) {
33
- return this._inputCompanion.from(elements);
34
+ return this._parserInputCompanion.from(elements);
34
35
  }
35
36
  length(sequence) {
36
- return this._inputCompanion.length(sequence);
37
+ return this._parserInputCompanion.length(sequence);
37
38
  }
38
39
  at(sequence, index) {
39
- return this._inputCompanion.at(sequence, index);
40
+ return this._parserInputCompanion.at(sequence, index);
40
41
  }
41
42
  get position() {
42
43
  return this._inputReader.position;
@@ -65,7 +66,7 @@ export class ParserContextImplementation {
65
66
  debugger;
66
67
  }
67
68
  invariant(this.position === lookaheadInputReader.position, 'lookahead this.position (%s) === lookaheadInputReader.position (%s)', this.position, lookaheadInputReader.position);
68
- const lookaheadParserContext = new ParserContextImplementation(this._inputCompanion, lookaheadInputReader, this, {
69
+ const lookaheadParserContext = new ParserContextImplementation(this._parserInputCompanion, lookaheadInputReader, this, {
69
70
  ...this._options,
70
71
  ...options,
71
72
  debugName: [
@@ -116,7 +117,7 @@ export class ParserContextImplementation {
116
117
  }
117
118
  invariant(value, format, ...formatArguments) {
118
119
  const parserContext = this;
119
- return parserInvariant(function (message) {
120
+ return customInvariant(function (message) {
120
121
  return new ParserParsingInvariantError(message, parserContext._depth, parserContext.position);
121
122
  }, value, format, ...formatArguments);
122
123
  }
@@ -125,12 +126,12 @@ export class ParserContextImplementation {
125
126
  const errorJoinMode = this._options.errorJoinMode ?? 'none';
126
127
  const parserContext = this;
127
128
  if (errorJoinMode === 'none') {
128
- return parserInvariant(function (message) {
129
+ return customInvariant(function (message) {
129
130
  return new ParserParsingJoinNoneError(message, parserContext._depth, parserContext.position);
130
131
  }, value, format, ...formatArguments);
131
132
  }
132
133
  if (errorJoinMode === 'furthest') {
133
- return parserInvariant(function (message) {
134
+ return customInvariant(function (message) {
134
135
  let furthestPosition = 0;
135
136
  let furthestChildErrors = [];
136
137
  for (const childError of childErrors) {
@@ -154,7 +155,7 @@ export class ParserContextImplementation {
154
155
  }, value, format, ...formatArguments);
155
156
  }
156
157
  if (errorJoinMode === 'deepest') {
157
- return parserInvariant(function (message) {
158
+ return customInvariant(function (message) {
158
159
  let deepestDepth = 0;
159
160
  let deepestChildErrors = [];
160
161
  for (const childError of childErrors) {
@@ -178,7 +179,7 @@ export class ParserContextImplementation {
178
179
  }, value, format, ...formatArguments);
179
180
  }
180
181
  if (errorJoinMode === 'all') {
181
- return parserInvariant(function (message) {
182
+ return customInvariant(function (message) {
182
183
  message += [
183
184
  '',
184
185
  'Child error stacks, indented:',
@@ -1,6 +1,6 @@
1
1
  import test from 'ava';
2
2
  import { ParserContextImplementation } from './parserContext.js';
3
- import { stringInputCompanion } from './inputCompanion.js';
3
+ import { stringParserInputCompanion } from './parserInputCompanion.js';
4
4
  import { InputReaderImplementation } from './inputReader.js';
5
5
  import { ParserUnexpectedEndOfInputError } from './parserError.js';
6
6
  const commonParserContextArguments = [
@@ -11,7 +11,7 @@ const commonParserContextArguments = [
11
11
  },
12
12
  ];
13
13
  test('parserContext.read', async (t) => {
14
- const parserContext = new ParserContextImplementation(stringInputCompanion, new InputReaderImplementation(stringInputCompanion, (async function* () {
14
+ const parserContext = new ParserContextImplementation(stringParserInputCompanion, new InputReaderImplementation(stringParserInputCompanion, (async function* () {
15
15
  yield '';
16
16
  yield 'abc';
17
17
  yield 'def';
@@ -26,7 +26,7 @@ test('parserContext.read', async (t) => {
26
26
  });
27
27
  });
28
28
  test('parserContext.lookahead', async (t) => {
29
- const parserContext = new ParserContextImplementation(stringInputCompanion, new InputReaderImplementation(stringInputCompanion, (async function* () {
29
+ const parserContext = new ParserContextImplementation(stringParserInputCompanion, new InputReaderImplementation(stringParserInputCompanion, (async function* () {
30
30
  yield* 'abcdefgh';
31
31
  })()), ...commonParserContextArguments);
32
32
  const lookaheadContext1 = parserContext.lookahead();
@@ -54,7 +54,7 @@ test('parserContext.lookahead', async (t) => {
54
54
  t.is(lookaheadContext3.position, 3);
55
55
  });
56
56
  test('parserContext.unlookahead', async (t) => {
57
- const parserContext = new ParserContextImplementation(stringInputCompanion, new InputReaderImplementation(stringInputCompanion, (async function* () {
57
+ const parserContext = new ParserContextImplementation(stringParserInputCompanion, new InputReaderImplementation(stringParserInputCompanion, (async function* () {
58
58
  yield* 'abcdefgh';
59
59
  })()), ...commonParserContextArguments);
60
60
  const lookaheadContext = parserContext.lookahead();
@@ -68,7 +68,7 @@ test('parserContext.unlookahead', async (t) => {
68
68
  t.is(await lookaheadContext.read(0), 'c');
69
69
  });
70
70
  test('parserContext.unlookahead while peeking', async (t) => {
71
- const parserContext = new ParserContextImplementation(stringInputCompanion, new InputReaderImplementation(stringInputCompanion, (async function* () {
71
+ const parserContext = new ParserContextImplementation(stringParserInputCompanion, new InputReaderImplementation(stringParserInputCompanion, (async function* () {
72
72
  yield* 'abcdefgh';
73
73
  })()), ...commonParserContextArguments);
74
74
  const lookaheadContext = parserContext.lookahead();
@@ -79,7 +79,7 @@ test('parserContext.unlookahead while peeking', async (t) => {
79
79
  t.is(await peekPromise, 'c');
80
80
  });
81
81
  test('parserContext deep unlookahead normal order', async (t) => {
82
- const parserContext = new ParserContextImplementation(stringInputCompanion, new InputReaderImplementation(stringInputCompanion, (async function* () {
82
+ const parserContext = new ParserContextImplementation(stringParserInputCompanion, new InputReaderImplementation(stringParserInputCompanion, (async function* () {
83
83
  yield* 'abcdefgh';
84
84
  })()), ...commonParserContextArguments);
85
85
  const child = parserContext.lookahead();
@@ -92,7 +92,7 @@ test('parserContext deep unlookahead normal order', async (t) => {
92
92
  t.is(await parserContext.read(0), 'd');
93
93
  });
94
94
  test('parserContext deep unlookahead weird order', async (t) => {
95
- const parserContext = new ParserContextImplementation(stringInputCompanion, new InputReaderImplementation(stringInputCompanion, (async function* () {
95
+ const parserContext = new ParserContextImplementation(stringParserInputCompanion, new InputReaderImplementation(stringParserInputCompanion, (async function* () {
96
96
  yield* 'abcdefgh';
97
97
  })()), ...commonParserContextArguments);
98
98
  const child = parserContext.lookahead({
@@ -1,3 +1,3 @@
1
- import { Parser } from "./parser.js";
2
- import { DeriveSequenceElement } from "./sequence.js";
3
- export declare function parserCreatorCompose<Arguments extends unknown[], OutputA, OutputB, Sequence, Element = DeriveSequenceElement<Sequence>>(f1: (...args: Arguments) => Parser<OutputA, Sequence, Element>, f2: (outputA: OutputA) => Parser<OutputB, Sequence, Element>): (...args: Arguments) => Parser<OutputB, Sequence, Element>;
1
+ import { type Parser } from './parser.js';
2
+ import { type DeriveSequenceElement } from './sequence.js';
3
+ export declare function parserCreatorCompose<Arguments extends unknown[], OutputA, OutputB, Sequence, Element = DeriveSequenceElement<Sequence>>(f1: (...arguments_: Arguments) => Parser<OutputA, Sequence, Element>, f2: (outputA: OutputA) => Parser<OutputB, Sequence, Element>): (...arguments_: Arguments) => Parser<OutputB, Sequence, Element>;
@@ -1,6 +1,6 @@
1
1
  export function parserCreatorCompose(f1, f2) {
2
- return (...args) => {
3
- const parserA = f1(...args);
2
+ return (...arguments_) => {
3
+ const parserA = f1(...arguments_);
4
4
  return async (parserContext) => {
5
5
  const outputA = await parserA(parserContext);
6
6
  const parserB = f2(outputA);
@@ -1,2 +1,2 @@
1
- import { type Falsy, type ValueOrAccessor } from './parserInvariant.js';
1
+ import { type Falsy, type ValueOrAccessor } from './customInvariant.js';
2
2
  export declare function parserImplementationInvariant<T>(value: T, format: ValueOrAccessor<string | string[]>, ...formatArguments: any[]): Exclude<T, Falsy>;
@@ -1,5 +1,5 @@
1
1
  import { ParserImplementationInvariantError } from './parserError.js';
2
- import { parserInvariant } from './parserInvariant.js';
2
+ import { customInvariant } from './customInvariant.js';
3
3
  export function parserImplementationInvariant(value, format, ...formatArguments) {
4
- return parserInvariant(ParserImplementationInvariantError, value, format, ...formatArguments);
4
+ return customInvariant(ParserImplementationInvariantError, value, format, ...formatArguments);
5
5
  }
@@ -0,0 +1,20 @@
1
+ export type ParserInputCompanion<Sequence, Element> = {
2
+ is(value: unknown): value is Sequence;
3
+ from(elements: Element[]): Sequence;
4
+ length(sequence: Sequence): number;
5
+ at(sequence: Sequence, index: number): Element | undefined;
6
+ };
7
+ export declare class StringParserInputCompanion implements ParserInputCompanion<string, string> {
8
+ is(value: unknown): value is string;
9
+ from(elements: string[]): string;
10
+ length(sequence: string): number;
11
+ at(sequence: string, index: number): string | undefined;
12
+ }
13
+ export declare const stringParserInputCompanion: StringParserInputCompanion;
14
+ export declare class Uint8ArrayParserInputCompanion implements ParserInputCompanion<Uint8Array, number> {
15
+ is(value: unknown): value is Uint8Array;
16
+ from(elements: number[]): Uint8Array;
17
+ length(sequence: Uint8Array): number;
18
+ at(sequence: Uint8Array, index: number): number | undefined;
19
+ }
20
+ export declare const uint8ArrayParserInputCompanion: Uint8ArrayParserInputCompanion;
@@ -0,0 +1,30 @@
1
+ export class StringParserInputCompanion {
2
+ is(value) {
3
+ return typeof value === 'string';
4
+ }
5
+ from(elements) {
6
+ return elements.join('');
7
+ }
8
+ length(sequence) {
9
+ return sequence.length;
10
+ }
11
+ at(sequence, index) {
12
+ return sequence.at(index);
13
+ }
14
+ }
15
+ export const stringParserInputCompanion = new StringParserInputCompanion();
16
+ export class Uint8ArrayParserInputCompanion {
17
+ is(value) {
18
+ return value instanceof Uint8Array;
19
+ }
20
+ from(elements) {
21
+ return new Uint8Array(elements);
22
+ }
23
+ length(sequence) {
24
+ return sequence.length;
25
+ }
26
+ at(sequence, index) {
27
+ return sequence.at(index);
28
+ }
29
+ }
30
+ export const uint8ArrayParserInputCompanion = new Uint8ArrayParserInputCompanion();
@@ -1,4 +1,4 @@
1
1
  import { type Constructor } from 'type-fest';
2
2
  export type Falsy = '' | 0 | false | undefined;
3
3
  export type ValueOrAccessor<T> = T | (() => T);
4
- export declare function parserInvariant<T>(ErrorConstructor: Constructor<Error, [message: string]> | ((message: string) => Error), value: T, formatOrFormatLines: ValueOrAccessor<string | string[]>, ...formatArguments: any[]): Exclude<T, Falsy>;
4
+ export declare function customInvariant<T>(ErrorConstructor: Constructor<Error, [message: string]> | ((message: string) => Error), value: T, formatOrFormatLines: ValueOrAccessor<string | string[]>, ...formatArguments: any[]): Exclude<T, Falsy>;
@@ -1,4 +1,4 @@
1
- export function parserInvariant(ErrorConstructor, value, formatOrFormatLines, ...formatArguments) {
1
+ export function customInvariant(ErrorConstructor, value, formatOrFormatLines, ...formatArguments) {
2
2
  if (value) {
3
3
  return value;
4
4
  }
@@ -0,0 +1,2 @@
1
+ import { type Parser } from './parser.js';
2
+ export declare const createQuantifierParser: <Output, Sequence>(childParser: Parser<Output, Sequence>, count: number) => Parser<Output[], Sequence>;
@@ -0,0 +1,17 @@
1
+ import { getParserName, setParserName } from './parser.js';
2
+ export const createQuantifierParser = (childParser, count) => {
3
+ const quantifierParser = async (parserContext) => {
4
+ const elements = [];
5
+ for (let index = 0; index < count; index++) {
6
+ elements.push(await childParser(parserContext));
7
+ }
8
+ return elements;
9
+ };
10
+ setParserName(quantifierParser, [
11
+ getParserName(childParser, 'anonymousQuantifierChild'),
12
+ '{',
13
+ count,
14
+ '}',
15
+ ].join(''));
16
+ return quantifierParser;
17
+ };
@@ -1,14 +1,14 @@
1
- import { type InputCompanion } from './inputCompanion.js';
1
+ import { type ParserInputCompanion } from './parserInputCompanion.js';
2
2
  export type SequenceBuffer<Sequence, Element> = {
3
3
  push(sequence: Sequence): void;
4
4
  peek(offset: number): Element | undefined;
5
5
  skip(offset: number): void;
6
6
  };
7
7
  export declare class SequenceBufferImplementation<Sequence, Element> implements SequenceBuffer<Sequence, Element> {
8
- private readonly _inputCompanion;
8
+ private readonly _parserInputCompanion;
9
9
  private readonly _sequences;
10
10
  private _indexInFirstSequence;
11
- constructor(_inputCompanion: InputCompanion<Sequence, Element>);
11
+ constructor(_parserInputCompanion: ParserInputCompanion<Sequence, Element>);
12
12
  push(sequence: Sequence): void;
13
13
  peek(offset: number): Element | undefined;
14
14
  skip(offset: number): void;
@@ -1,16 +1,16 @@
1
1
  import invariant from 'invariant';
2
2
  export class SequenceBufferImplementation {
3
- _inputCompanion;
3
+ _parserInputCompanion;
4
4
  _sequences = [];
5
5
  _indexInFirstSequence = 0;
6
- constructor(_inputCompanion) {
7
- this._inputCompanion = _inputCompanion;
6
+ constructor(_parserInputCompanion) {
7
+ this._parserInputCompanion = _parserInputCompanion;
8
8
  }
9
9
  push(sequence) {
10
10
  this._sequences.push(sequence);
11
11
  while (this._sequences.length > 0) {
12
12
  const firstSequence = this._sequences[0];
13
- const firstSequenceLength = this._inputCompanion.length(firstSequence);
13
+ const firstSequenceLength = this._parserInputCompanion.length(firstSequence);
14
14
  if (firstSequenceLength === 0) {
15
15
  this._sequences.shift();
16
16
  continue;
@@ -26,9 +26,9 @@ export class SequenceBufferImplementation {
26
26
  invariant(offset >= 0, 'Offset must be non-negative.');
27
27
  let index = this._indexInFirstSequence + offset;
28
28
  for (const sequence of this._sequences) {
29
- const sequenceLength = this._inputCompanion.length(sequence);
29
+ const sequenceLength = this._parserInputCompanion.length(sequence);
30
30
  if (index < sequenceLength) {
31
- return this._inputCompanion.at(sequence, index);
31
+ return this._parserInputCompanion.at(sequence, index);
32
32
  }
33
33
  index -= sequenceLength;
34
34
  }
@@ -1,8 +1,8 @@
1
1
  import test from 'ava';
2
2
  import { SequenceBufferImplementation } from './sequenceBuffer.js';
3
- import { stringInputCompanion } from './inputCompanion.js';
3
+ import { stringParserInputCompanion } from './parserInputCompanion.js';
4
4
  test('sequenceBuffer', t => {
5
- const sequenceBuffer = new SequenceBufferImplementation(stringInputCompanion);
5
+ const sequenceBuffer = new SequenceBufferImplementation(stringParserInputCompanion);
6
6
  t.is(sequenceBuffer.peek(0), undefined);
7
7
  sequenceBuffer.push('');
8
8
  t.is(sequenceBuffer.peek(0), undefined);
@@ -0,0 +1,2 @@
1
+ import { Unparser } from "./unparser.js";
2
+ export declare const createSequenceUnparser: <Sequence>() => Unparser<Sequence, Sequence>;
@@ -0,0 +1,6 @@
1
+ export const createSequenceUnparser = () => {
2
+ const sequenceUnparser = async function* (input) {
3
+ yield input;
4
+ };
5
+ return sequenceUnparser;
6
+ };
@@ -1,2 +1,2 @@
1
- import { Parser } from "./parser.js";
1
+ import { type Parser } from './parser.js';
2
2
  export declare const createSliceBoundedParser: <Output, Sequence>(childParser: Parser<Output, Sequence>, sliceEnd: number) => Parser<Output, Sequence>;
@@ -1,4 +1,4 @@
1
- import { getParserName, setParserName } from "./parser.js";
1
+ import { getParserName, setParserName } from './parser.js';
2
2
  export const createSliceBoundedParser = (childParser, sliceEnd) => {
3
3
  const sliceBoundedParser = async (parserContext) => {
4
4
  const childParserContext = parserContext.lookahead({
@@ -4,7 +4,7 @@ import { createElementParser } from './elementParser.js';
4
4
  import { createTupleParser } from './tupleParser.js';
5
5
  import { createSliceBoundedParser } from './sliceBoundedParser.js';
6
6
  import { runParser } from './parser.js';
7
- import { stringInputCompanion } from './inputCompanion.js';
7
+ import { stringParserInputCompanion } from './parserInputCompanion.js';
8
8
  const anythingParser = createArrayParser(createElementParser());
9
9
  test('sliceBoundedParser', async (t) => {
10
10
  const parser = createTupleParser([
@@ -12,7 +12,7 @@ test('sliceBoundedParser', async (t) => {
12
12
  createSliceBoundedParser(anythingParser, 2),
13
13
  createElementParser(),
14
14
  ]);
15
- const result = await runParser(parser, 'abba', stringInputCompanion);
15
+ const result = await runParser(parser, 'abba', stringParserInputCompanion);
16
16
  t.deepEqual(result, [
17
17
  'a',
18
18
  [
@@ -1,2 +1,2 @@
1
- import { Parser } from "./parser.js";
1
+ import { type Parser } from './parser.js';
2
2
  export declare const createTerminatedArrayParser: <ElementOutput, TerminatorOutput, Sequence>(elementParser: Parser<ElementOutput, Sequence>, terminatorParser: Parser<unknown, Sequence>) => Parser<[ElementOutput[], TerminatorOutput], Sequence>;
@@ -1,6 +1,6 @@
1
- import { getParserName, setParserName } from "./parser.js";
2
- import { promiseCompose } from "./promiseCompose.js";
3
- import { createUnionParser } from "./unionParser.js";
1
+ import { getParserName, setParserName } from './parser.js';
2
+ import { promiseCompose } from './promiseCompose.js';
3
+ import { createUnionParser } from './unionParser.js';
4
4
  class Terminated {
5
5
  value;
6
6
  constructor(value) {
@@ -0,0 +1 @@
1
+ export declare function uint8ArrayAsyncIterableToUint8Array(uint8ArrayAsyncIterable: AsyncIterable<Uint8Array> | Iterable<Uint8Array>): Promise<Uint8Array>;
@@ -0,0 +1,7 @@
1
+ export async function uint8ArrayAsyncIterableToUint8Array(uint8ArrayAsyncIterable) {
2
+ const chunks = [];
3
+ for await (const chunk of uint8ArrayAsyncIterable) {
4
+ chunks.push(chunk);
5
+ }
6
+ return Buffer.concat(chunks);
7
+ }
@@ -0,0 +1,8 @@
1
+ import { DeriveSequenceElement } from "./sequence.js";
2
+ import { UnparserContext, WriteEarlier, WriteLater } from "./unparserContext.js";
3
+ import { UnparserOutputCompanion } from "./unparserOutputCompanion.js";
4
+ type UnparserIterableValue<Sequence, Element> = Sequence | Element | WriteLater<Sequence, Element> | WriteEarlier<Sequence, Element>;
5
+ export type UnparserResult<Sequence, Element> = AsyncIterable<UnparserIterableValue<Sequence, Element>>;
6
+ export type Unparser<Input, Sequence, Element = DeriveSequenceElement<Sequence>> = (input: Input, unparserContext: UnparserContext<Sequence, Element>) => UnparserResult<Sequence, Element>;
7
+ export declare function runUnparser<Input, Sequence, Element = DeriveSequenceElement<Sequence>>(unparser: Unparser<Input, Sequence, Element>, input: Input, unparserOutputCompanion: UnparserOutputCompanion<Sequence, Element>): AsyncIterable<Sequence>;
8
+ export {};
@@ -0,0 +1,104 @@
1
+ import invariant from "invariant";
2
+ import { UnparserContextImplementation, WriteEarlier, WriteLater } from "./unparserContext.js";
3
+ import { unparserImplementationInvariant } from "./unparserImplementationInvariant.js";
4
+ async function* onYield(asyncIterable, onYield) {
5
+ for await (const value of asyncIterable) {
6
+ onYield(value);
7
+ yield value;
8
+ }
9
+ }
10
+ async function* elementsToSequences(values, unparserOutputCompanion) {
11
+ let elements = [];
12
+ for await (const value of values) {
13
+ if (value instanceof WriteLater
14
+ || value instanceof WriteEarlier) {
15
+ if (elements.length > 0) {
16
+ const sequence = unparserOutputCompanion.from(elements);
17
+ yield sequence;
18
+ elements = [];
19
+ }
20
+ yield value;
21
+ }
22
+ else if (unparserOutputCompanion.is(value)) {
23
+ if (unparserOutputCompanion.length(value) === 0) {
24
+ continue;
25
+ }
26
+ if (elements.length > 0) {
27
+ const sequence = unparserOutputCompanion.from(elements);
28
+ yield sequence;
29
+ elements = [];
30
+ }
31
+ yield value;
32
+ }
33
+ else {
34
+ elements.push(value);
35
+ }
36
+ }
37
+ if (elements.length > 0) {
38
+ const sequence = unparserOutputCompanion.from(elements);
39
+ yield sequence;
40
+ }
41
+ }
42
+ function wrapUnparserResult(unparserResult, unparserContext, unparserOutputCompanion) {
43
+ return onYield(elementsToSequences(unparserResult, unparserOutputCompanion), value => unparserContext.handleYield(value));
44
+ }
45
+ export async function* runUnparser(unparser, input, unparserOutputCompanion) {
46
+ const unparserContext = new UnparserContextImplementation(unparserOutputCompanion);
47
+ const values = unparser(input, unparserContext);
48
+ const valuesWithoutElements = wrapUnparserResult(values, unparserContext, unparserOutputCompanion);
49
+ const outputQueue = [];
50
+ const iteratorStack = [
51
+ [
52
+ valuesWithoutElements[Symbol.asyncIterator](),
53
+ value => outputQueue.push(value),
54
+ () => { },
55
+ ],
56
+ ];
57
+ while (true) {
58
+ while (outputQueue.length > 0
59
+ && !(outputQueue[0] instanceof WriteLater)) {
60
+ yield outputQueue.shift();
61
+ }
62
+ if (iteratorStack.length === 0) {
63
+ break;
64
+ }
65
+ const [iterator, pushOutput, finishOutput] = iteratorStack[0];
66
+ const iteratorResult = await iterator.next();
67
+ if (iteratorResult.done) {
68
+ finishOutput();
69
+ iteratorStack.shift();
70
+ continue;
71
+ }
72
+ const { value } = iteratorResult;
73
+ if (value instanceof WriteEarlier) {
74
+ const { writeLater, unparserResult, unparserContext } = value;
75
+ invariant(unparserContext instanceof UnparserContextImplementation, 'UnparserContext not an instance of UnparserContextImplementation.');
76
+ function getOutputQueueWriteLaterIndex() {
77
+ const outputQueueWriteLaterIndex = outputQueue.indexOf(writeLater);
78
+ unparserImplementationInvariant(outputQueueWriteLaterIndex !== -1, [
79
+ 'WriteLater has already been written or was not yielded yet.',
80
+ 'WriteLater stack: %s',
81
+ 'End of WriteLater stack.',
82
+ ], writeLater.stack);
83
+ return outputQueueWriteLaterIndex;
84
+ }
85
+ const unparserResultWithoutElements = wrapUnparserResult(unparserResult, unparserContext, unparserOutputCompanion);
86
+ const newPushOutput = value => {
87
+ const outputQueueWriteLaterIndex = getOutputQueueWriteLaterIndex();
88
+ outputQueue.splice(outputQueueWriteLaterIndex, 0, value);
89
+ };
90
+ const newFinishOutput = () => {
91
+ const outputQueueWriteLaterIndex = getOutputQueueWriteLaterIndex();
92
+ const [removedValue] = outputQueue.splice(outputQueueWriteLaterIndex, 1);
93
+ invariant(removedValue === writeLater, 'WriteLater was not removed from the output queue.');
94
+ };
95
+ iteratorStack.unshift([
96
+ unparserResultWithoutElements[Symbol.asyncIterator](),
97
+ newPushOutput,
98
+ newFinishOutput,
99
+ ]);
100
+ continue;
101
+ }
102
+ pushOutput(value);
103
+ }
104
+ }
@@ -0,0 +1 @@
1
+ export {};