@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.
- package/build/apk.d.ts +29 -3
- package/build/apkParser.d.ts +15 -2
- package/build/apkParser.js +70 -41
- package/build/apkParser.test.js +2 -2
- package/build/apkUnparser.d.ts +4 -0
- package/build/apkUnparser.js +90 -0
- package/build/apkUnparser.test.d.ts +1 -0
- package/build/apkUnparser.test.js +26 -0
- package/build/arbitraryFileSystemEntry.js +1 -1
- package/build/arbitraryZip.d.ts +1 -1
- package/build/arbitraryZip.js +13 -19
- package/build/arbitraryZipPermissions.d.ts +1 -8
- package/build/arbitraryZipPermissions.js +1 -16
- package/build/arbitraryZipStream.d.ts +1 -1
- package/build/arbitraryZipStream.js +3 -3
- package/build/arrayParser.d.ts +1 -1
- package/build/arrayParser.js +2 -2
- package/build/arrayParser.test.js +2 -2
- package/build/arrayUnparser.d.ts +2 -0
- package/build/arrayUnparser.js +8 -0
- package/build/bsonParser.test.js +2 -2
- package/build/customInvariant.d.ts +4 -0
- package/build/customInvariant.js +11 -0
- package/build/debugLogParser.d.ts +1 -1
- package/build/debugLogParser.js +1 -1
- package/build/elementParser.d.ts +2 -2
- package/build/elementParser.js +1 -1
- package/build/endOfInputParser.d.ts +2 -2
- package/build/exactElementParser.d.ts +1 -1
- package/build/exactSequenceParser.js +1 -1
- package/build/index.d.ts +5 -2
- package/build/index.js +3 -0
- package/build/inputReader.d.ts +3 -3
- package/build/inputReader.js +6 -6
- package/build/inputReader.test.js +7 -7
- package/build/javaKeyStore.d.ts +1 -0
- package/build/javaKeyStore.js +1 -0
- package/build/javaKeyStoreParser.d.ts +2 -0
- package/build/javaKeyStoreParser.js +67 -0
- package/build/javaKeyStoreParser.test.d.ts +1 -0
- package/build/javaKeyStoreParser.test.js +16 -0
- package/build/javaKeystoreParser.d.ts +2 -0
- package/build/javaKeystoreParser.js +7 -0
- package/build/jsonParser.js +3 -2
- package/build/jsonParser.test.js +2 -2
- package/build/listParser.d.ts +1 -1
- package/build/listParser.js +5 -5
- package/build/negativeLookahead.d.ts +1 -1
- package/build/negativeLookahead.js +16 -18
- package/build/negativeLookaheadParser.d.ts +2 -0
- package/build/negativeLookaheadParser.js +18 -0
- package/build/optionalParser.d.ts +1 -1
- package/build/optionalParser.js +2 -2
- package/build/parser.d.ts +3 -3
- package/build/parser.js +3 -3
- package/build/parser.test.js +16 -16
- package/build/parserAccessorParser.d.ts +1 -1
- package/build/parserConsumedSequenceParser.d.ts +2 -0
- package/build/parserConsumedSequenceParser.js +17 -0
- package/build/parserContext.d.ts +6 -6
- package/build/parserContext.js +15 -14
- package/build/parserContext.test.js +7 -7
- package/build/parserCreatorCompose.d.ts +3 -3
- package/build/parserCreatorCompose.js +2 -2
- package/build/parserImplementationInvariant.d.ts +1 -1
- package/build/parserImplementationInvariant.js +2 -2
- package/build/parserInputCompanion.d.ts +20 -0
- package/build/parserInputCompanion.js +30 -0
- package/build/parserInvariant.d.ts +1 -1
- package/build/parserInvariant.js +1 -1
- package/build/quantifierParser.d.ts +2 -0
- package/build/quantifierParser.js +17 -0
- package/build/sequenceBuffer.d.ts +3 -3
- package/build/sequenceBuffer.js +6 -6
- package/build/sequenceBuffer.test.js +2 -2
- package/build/sequenceUnparser.d.ts +2 -0
- package/build/sequenceUnparser.js +6 -0
- package/build/sliceBoundedParser.d.ts +1 -1
- package/build/sliceBoundedParser.js +1 -1
- package/build/sliceBoundedParser.test.js +2 -2
- package/build/terminatedArrayParser.d.ts +1 -1
- package/build/terminatedArrayParser.js +3 -3
- package/build/uint8Array.d.ts +1 -0
- package/build/uint8Array.js +7 -0
- package/build/unparser.d.ts +8 -0
- package/build/unparser.js +104 -0
- package/build/unparser.test.d.ts +1 -0
- package/build/unparser.test.js +150 -0
- package/build/unparserContext.d.ts +31 -0
- package/build/unparserContext.js +74 -0
- package/build/unparserError.d.ts +9 -0
- package/build/unparserError.js +9 -0
- package/build/unparserImplementationInvariant.d.ts +2 -0
- package/build/unparserImplementationInvariant.js +5 -0
- package/build/unparserInputCompanion.d.ts +15 -0
- package/build/unparserInputCompanion.js +13 -0
- package/build/unparserOutputCompanion.d.ts +15 -0
- package/build/unparserOutputCompanion.js +13 -0
- package/build/zip.d.ts +9 -17
- package/build/zipParser.d.ts +13 -10
- package/build/zipParser.js +48 -60
- package/build/zipParser.test.js +2 -7
- package/build/zipUnparser.d.ts +5 -0
- package/build/zipUnparser.js +171 -0
- package/build/zipUnparser.test.d.ts +1 -0
- package/build/zipUnparser.test.js +80 -0
- package/package.json +4 -2
- package/src/apk.ts +35 -3
- package/src/apkParser.test.ts +2 -2
- package/src/apkParser.test.ts.md +114 -111
- package/src/apkParser.test.ts.snap +0 -0
- package/src/apkParser.ts +150 -85
- package/src/apkUnparser.test.ts +37 -0
- package/src/apkUnparser.ts +120 -0
- package/src/arbitraryFileSystemEntry.ts +2 -4
- package/src/arbitraryZip.ts +20 -27
- package/src/arbitraryZipPermissions.ts +0 -25
- package/src/arbitraryZipStream.ts +4 -4
- package/src/arrayParser.test.ts +3 -3
- package/src/arrayParser.ts +3 -2
- package/src/arrayUnparser.ts +13 -0
- package/src/bsonParser.test.ts +2 -2
- package/src/bsonParser.ts +3 -3
- package/src/{parserInvariant.ts → customInvariant.ts} +1 -1
- package/src/debugLogParser.ts +1 -1
- package/src/elementParser.ts +3 -3
- package/src/endOfInputParser.ts +4 -4
- package/src/exactElementParser.ts +1 -1
- package/src/exactSequenceParser.ts +2 -2
- package/src/index.ts +15 -2
- package/src/inputReader.test.ts +7 -7
- package/src/inputReader.ts +5 -5
- package/src/javaKeyStore.ts +0 -0
- package/src/javaKeyStoreParser.test.ts +23 -0
- package/src/javaKeyStoreParser.test.ts.md +103 -0
- package/src/javaKeyStoreParser.test.ts.snap +0 -0
- package/src/javaKeyStoreParser.ts +136 -0
- package/src/jsonParser.test.ts +2 -2
- package/src/jsonParser.ts +13 -12
- package/src/listParser.ts +6 -6
- package/src/negativeLookaheadParser.ts +24 -0
- package/src/optionalParser.ts +3 -3
- package/src/parser.test.ts +19 -17
- package/src/parser.ts +7 -7
- package/src/parserAccessorParser.ts +1 -1
- package/src/parserConsumedSequenceParser.ts +20 -0
- package/src/parserContext.test.ts +7 -7
- package/src/parserContext.ts +18 -14
- package/src/parserCreatorCompose.ts +6 -6
- package/src/parserImplementationInvariant.ts +2 -2
- package/src/{inputCompanion.ts → parserInputCompanion.ts} +10 -6
- package/src/quantifierParser.ts +25 -0
- package/src/sequenceBuffer.test.ts +2 -2
- package/src/sequenceBuffer.ts +5 -5
- package/src/sequenceUnparser.ts +9 -0
- package/src/sliceBoundedParser.test.ts +2 -2
- package/src/sliceBoundedParser.ts +2 -2
- package/src/terminatedArrayParser.ts +3 -3
- package/src/uint8Array.ts +10 -0
- package/src/unparser.test.ts +221 -0
- package/src/unparser.ts +209 -0
- package/src/unparserContext.ts +127 -0
- package/src/unparserError.ts +12 -0
- package/src/unparserImplementationInvariant.ts +6 -0
- package/src/unparserOutputCompanion.ts +24 -0
- package/src/zip.ts +10 -22
- package/src/zipParser.test.ts +2 -8
- package/src/zipParser.ts +147 -129
- package/src/zipUnparser.test.ts +119 -0
- package/src/zipUnparser.ts +239 -0
- package/src/negativeLookahead.ts +0 -26
package/build/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { Parser, runParser, setParserName, getParserName, } from './parser.js';
|
|
2
|
-
export { ParserContext, } from './parserContext.js';
|
|
1
|
+
export { type Parser, runParser, setParserName, getParserName, } from './parser.js';
|
|
2
|
+
export type { ParserContext, } from './parserContext.js';
|
|
3
3
|
export { createTupleParser, } from './tupleParser.js';
|
|
4
4
|
export { createExactSequenceParser, } from './exactSequenceParser.js';
|
|
5
5
|
export { createFixedLengthSequenceParser, } from './fixedLengthSequenceParser.js';
|
|
@@ -16,3 +16,6 @@ export { createSkipParser, } from './skipParser.js';
|
|
|
16
16
|
export { createEndOfInputParser, } from './endOfInputParser.js';
|
|
17
17
|
export { createListParser, } from './listParser.js';
|
|
18
18
|
export { createDebugLogParser, } from './debugLogParser.js';
|
|
19
|
+
export { type Unparser, runUnparser, } from './unparser.js';
|
|
20
|
+
export { createArrayUnparser, } from './arrayUnparser.js';
|
|
21
|
+
export { createSequenceUnparser, } from './sequenceUnparser.js';
|
package/build/index.js
CHANGED
|
@@ -15,3 +15,6 @@ export { createSkipParser, } from './skipParser.js';
|
|
|
15
15
|
export { createEndOfInputParser, } from './endOfInputParser.js';
|
|
16
16
|
export { createListParser, } from './listParser.js';
|
|
17
17
|
export { createDebugLogParser, } from './debugLogParser.js';
|
|
18
|
+
export { runUnparser, } from './unparser.js';
|
|
19
|
+
export { createArrayUnparser, } from './arrayUnparser.js';
|
|
20
|
+
export { createSequenceUnparser, } from './sequenceUnparser.js';
|
package/build/inputReader.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type ParserInputCompanion } from './parserInputCompanion.js';
|
|
2
2
|
export type InputReader<Sequence, Element> = {
|
|
3
3
|
get position(): number;
|
|
4
4
|
peek(offset: number): Promise<Element | undefined>;
|
|
@@ -6,14 +6,14 @@ export type InputReader<Sequence, Element> = {
|
|
|
6
6
|
lookahead(): InputReader<Sequence, Element>;
|
|
7
7
|
};
|
|
8
8
|
export declare class InputReaderImplementation<Sequence, Element> implements InputReader<Sequence, Element> {
|
|
9
|
-
private readonly
|
|
9
|
+
private readonly _parserInputCompanion;
|
|
10
10
|
private readonly _inputAsyncIterator;
|
|
11
11
|
private readonly _id;
|
|
12
12
|
private _position;
|
|
13
13
|
private _uncommitedSkipOffset;
|
|
14
14
|
private readonly _promiseMutex;
|
|
15
15
|
private readonly _sequenceBuffer;
|
|
16
|
-
constructor(
|
|
16
|
+
constructor(_parserInputCompanion: ParserInputCompanion<Sequence, Element>, _inputAsyncIterator: AsyncIterator<Sequence>);
|
|
17
17
|
get [Symbol.toStringTag](): string;
|
|
18
18
|
get position(): number;
|
|
19
19
|
peek(offset: number): Promise<Element | undefined>;
|
package/build/inputReader.js
CHANGED
|
@@ -4,17 +4,17 @@ import { SequenceBufferImplementation } from './sequenceBuffer.js';
|
|
|
4
4
|
import { parserImplementationInvariant } from './parserImplementationInvariant.js';
|
|
5
5
|
let inputReaderId = 0;
|
|
6
6
|
export class InputReaderImplementation {
|
|
7
|
-
|
|
7
|
+
_parserInputCompanion;
|
|
8
8
|
_inputAsyncIterator;
|
|
9
9
|
_id = inputReaderId++;
|
|
10
10
|
_position = 0;
|
|
11
11
|
_uncommitedSkipOffset = 0;
|
|
12
12
|
_promiseMutex = new PromiseMutex();
|
|
13
13
|
_sequenceBuffer;
|
|
14
|
-
constructor(
|
|
15
|
-
this.
|
|
14
|
+
constructor(_parserInputCompanion, _inputAsyncIterator) {
|
|
15
|
+
this._parserInputCompanion = _parserInputCompanion;
|
|
16
16
|
this._inputAsyncIterator = _inputAsyncIterator;
|
|
17
|
-
this._sequenceBuffer = new SequenceBufferImplementation(this.
|
|
17
|
+
this._sequenceBuffer = new SequenceBufferImplementation(this._parserInputCompanion);
|
|
18
18
|
}
|
|
19
19
|
get [Symbol.toStringTag]() {
|
|
20
20
|
return [
|
|
@@ -43,11 +43,11 @@ export class InputReaderImplementation {
|
|
|
43
43
|
if (inputIteratorResult.done) {
|
|
44
44
|
return undefined;
|
|
45
45
|
}
|
|
46
|
-
parserImplementationInvariant(this.
|
|
46
|
+
parserImplementationInvariant(this._parserInputCompanion.is(inputIteratorResult.value), [
|
|
47
47
|
'Input iterator result value (%s) is of unexpected type.',
|
|
48
48
|
'Expected a sequence (a chunk of input) recognized by the input companion (%s).',
|
|
49
49
|
'You may have provided a wrong input companion for the input.',
|
|
50
|
-
], inputIteratorResult.value, this.
|
|
50
|
+
], inputIteratorResult.value, this._parserInputCompanion.constructor.name);
|
|
51
51
|
this._sequenceBuffer.push(inputIteratorResult.value);
|
|
52
52
|
}
|
|
53
53
|
});
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import test from 'ava';
|
|
2
2
|
import { InputReaderImplementation } from './inputReader.js';
|
|
3
|
-
import {
|
|
3
|
+
import { stringParserInputCompanion } from './parserInputCompanion.js';
|
|
4
4
|
import { ParserImplementationError } from './parserError.js';
|
|
5
5
|
test('inputReader', async (t) => {
|
|
6
|
-
const inputReader = new InputReaderImplementation(
|
|
6
|
+
const inputReader = new InputReaderImplementation(stringParserInputCompanion, (async function* () {
|
|
7
7
|
yield '';
|
|
8
8
|
yield 'abc';
|
|
9
9
|
yield 'def';
|
|
@@ -32,7 +32,7 @@ test('inputReader', async (t) => {
|
|
|
32
32
|
t.is(await inputReader.peek(0), undefined);
|
|
33
33
|
});
|
|
34
34
|
test('inputReader.peek concurrent', async (t) => {
|
|
35
|
-
const inputReader = new InputReaderImplementation(
|
|
35
|
+
const inputReader = new InputReaderImplementation(stringParserInputCompanion, (async function* () {
|
|
36
36
|
yield* 'abcdefgh';
|
|
37
37
|
})());
|
|
38
38
|
const peeks = await Promise.all([
|
|
@@ -51,7 +51,7 @@ test('inputReader.peek concurrent', async (t) => {
|
|
|
51
51
|
t.deepEqual(peeks, ['a', 'a', 'a', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']);
|
|
52
52
|
});
|
|
53
53
|
test('inputReader skip while peeking', async (t) => {
|
|
54
|
-
const inputReader = new InputReaderImplementation(
|
|
54
|
+
const inputReader = new InputReaderImplementation(stringParserInputCompanion, (async function* () {
|
|
55
55
|
yield* 'abcdefgh';
|
|
56
56
|
})());
|
|
57
57
|
const peekPromise = inputReader.peek(0);
|
|
@@ -61,7 +61,7 @@ test('inputReader skip while peeking', async (t) => {
|
|
|
61
61
|
});
|
|
62
62
|
test('inputReader.lookahead', async (t) => {
|
|
63
63
|
let read = 0;
|
|
64
|
-
const inputReader = new InputReaderImplementation(
|
|
64
|
+
const inputReader = new InputReaderImplementation(stringParserInputCompanion, (async function* () {
|
|
65
65
|
for (const character of 'abcdefgh') {
|
|
66
66
|
read++;
|
|
67
67
|
yield character;
|
|
@@ -129,7 +129,7 @@ test('inputReader.lookahead', async (t) => {
|
|
|
129
129
|
});
|
|
130
130
|
test('inputReader.lookahead concurrent', async (t) => {
|
|
131
131
|
let read = 0;
|
|
132
|
-
const inputReader = new InputReaderImplementation(
|
|
132
|
+
const inputReader = new InputReaderImplementation(stringParserInputCompanion, (async function* () {
|
|
133
133
|
for (const character of 'abcdefgh') {
|
|
134
134
|
read++;
|
|
135
135
|
yield character;
|
|
@@ -154,7 +154,7 @@ test('inputReader.lookahead concurrent', async (t) => {
|
|
|
154
154
|
});
|
|
155
155
|
test('inputReader.lookahead skip position', async (t) => {
|
|
156
156
|
let read = 0;
|
|
157
|
-
const inputReader = new InputReaderImplementation(
|
|
157
|
+
const inputReader = new InputReaderImplementation(stringParserInputCompanion, (async function* () {
|
|
158
158
|
for (const character of 'abcdefgh') {
|
|
159
159
|
read++;
|
|
160
160
|
yield character;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { createExactSequenceParser } from './exactSequenceParser.js';
|
|
2
|
+
import { createFixedLengthSequenceParser } from './fixedLengthSequenceParser.js';
|
|
3
|
+
import { setParserName } from './parser.js';
|
|
4
|
+
import { parserCreatorCompose } from './parserCreatorCompose.js';
|
|
5
|
+
import { promiseCompose } from './promiseCompose.js';
|
|
6
|
+
import { createQuantifierParser } from './quantifierParser.js';
|
|
7
|
+
import { createTupleParser } from './tupleParser.js';
|
|
8
|
+
import { createUnionParser } from './unionParser.js';
|
|
9
|
+
// https://github.com/openjdk/jdk/blob/c517ffba7d9388e75b5d7bba77e565e71c0a7d76/src/java.base/share/classes/com/sun/crypto/provider/JceKeyStore.java#L512-L553
|
|
10
|
+
const uint64BEParser = promiseCompose(createFixedLengthSequenceParser(8), array => Buffer.from(array).readBigUInt64BE());
|
|
11
|
+
setParserName(uint64BEParser, 'uint64BEParser');
|
|
12
|
+
const uint32BEParser = promiseCompose(createFixedLengthSequenceParser(4), array => Buffer.from(array).readUInt32BE());
|
|
13
|
+
setParserName(uint32BEParser, 'uint32BEParser');
|
|
14
|
+
const uint16BEParser = promiseCompose(createFixedLengthSequenceParser(2), array => Buffer.from(array).readUInt16BE());
|
|
15
|
+
setParserName(uint16BEParser, 'uint16BEParser');
|
|
16
|
+
const createUint32BECountPrefixedParser = (elementParser) => parserCreatorCompose(() => uint32BEParser, count => createQuantifierParser(elementParser, count))();
|
|
17
|
+
const javaModifiedUTF8StringParser = promiseCompose(parserCreatorCompose(() => uint16BEParser, length => createFixedLengthSequenceParser(length))(), uint8Array => Buffer.from(uint8Array).toString('utf8'));
|
|
18
|
+
const uint32LengthPrefixedUint8ArrayParser = parserCreatorCompose(() => uint32BEParser, length => createFixedLengthSequenceParser(length))();
|
|
19
|
+
const javaKeyStoreImplementationParser = promiseCompose(createUnionParser([
|
|
20
|
+
createExactSequenceParser(Buffer.from('feedfeed', 'hex')),
|
|
21
|
+
createExactSequenceParser(Buffer.from('cececece', 'hex')),
|
|
22
|
+
]), uint8Array => uint8Array[0] === 0xFE ? 'JKS' : 'JCEKS');
|
|
23
|
+
const javaKeyStorePrivateKeyEntryPrivateKeyParser = uint32LengthPrefixedUint8ArrayParser;
|
|
24
|
+
const javaKeyStorePrivateKeyEntryCertificateParser = promiseCompose(createTupleParser([
|
|
25
|
+
javaModifiedUTF8StringParser,
|
|
26
|
+
uint32LengthPrefixedUint8ArrayParser,
|
|
27
|
+
]), ([type, certificate]) => ({ type, certificate }));
|
|
28
|
+
const javaKeyStorePrivateKeyEntryCertificateChainParser = createUint32BECountPrefixedParser(javaKeyStorePrivateKeyEntryCertificateParser);
|
|
29
|
+
const javaKeyStorePrivateKeyEntryParser = promiseCompose(createTupleParser([
|
|
30
|
+
createExactSequenceParser(Buffer.from('00000001', 'hex')),
|
|
31
|
+
javaModifiedUTF8StringParser,
|
|
32
|
+
uint64BEParser,
|
|
33
|
+
javaKeyStorePrivateKeyEntryPrivateKeyParser,
|
|
34
|
+
javaKeyStorePrivateKeyEntryCertificateChainParser,
|
|
35
|
+
]), ([_tag, alias, creationDate, privateKey, certificateChain,]) => ({
|
|
36
|
+
type: 'privateKey',
|
|
37
|
+
alias,
|
|
38
|
+
creationDate: new Date(Number(creationDate)),
|
|
39
|
+
privateKey,
|
|
40
|
+
certificateChain,
|
|
41
|
+
}));
|
|
42
|
+
const javaKeyStoreTrustedCertEntryParser = createTupleParser([
|
|
43
|
+
createExactSequenceParser(Buffer.from('00000002', 'hex')),
|
|
44
|
+
async (parserContext) => parserContext.invariant(false, 'Not implemented'),
|
|
45
|
+
]);
|
|
46
|
+
const javaKeyStoreSecretKeyEntryParser = createTupleParser([
|
|
47
|
+
createExactSequenceParser(Buffer.from('00000003', 'hex')),
|
|
48
|
+
async (parserContext) => parserContext.invariant(false, 'Not implemented'),
|
|
49
|
+
]);
|
|
50
|
+
const javaKeyStoreEntryParser = createUnionParser([
|
|
51
|
+
javaKeyStorePrivateKeyEntryParser,
|
|
52
|
+
javaKeyStoreTrustedCertEntryParser,
|
|
53
|
+
javaKeyStoreSecretKeyEntryParser,
|
|
54
|
+
]);
|
|
55
|
+
const javaKeyStoreEntriesParser = createUint32BECountPrefixedParser(javaKeyStoreEntryParser);
|
|
56
|
+
const javaKeyStoreParser_ = createTupleParser([
|
|
57
|
+
javaKeyStoreImplementationParser,
|
|
58
|
+
uint32BEParser,
|
|
59
|
+
javaKeyStoreEntriesParser,
|
|
60
|
+
createFixedLengthSequenceParser(20),
|
|
61
|
+
]);
|
|
62
|
+
export const javaKeyStoreParser = promiseCompose(javaKeyStoreParser_, ([implementation, version, entries, hash,]) => ({
|
|
63
|
+
implementation,
|
|
64
|
+
version,
|
|
65
|
+
entries,
|
|
66
|
+
hash,
|
|
67
|
+
}));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import test from 'ava';
|
|
2
|
+
import { uint8ArrayParserInputCompanion } from './parserInputCompanion.js';
|
|
3
|
+
import { runParser } from './parser.js';
|
|
4
|
+
import { javaKeyStoreParser } from './javaKeyStoreParser.js';
|
|
5
|
+
for (const javaKeyStoreCid of [
|
|
6
|
+
'bafkreig6k53b6p7bdvfjxc5mcb4qv3mffqls5ymqerxkqd6ih2xy5cs3n4',
|
|
7
|
+
]) {
|
|
8
|
+
test('javaKeyStore ' + javaKeyStoreCid, async (t) => {
|
|
9
|
+
const javaKeyStoreResponse = await fetch('https://ipfs.io/ipfs/' + javaKeyStoreCid);
|
|
10
|
+
const javaKeyStoreStream = javaKeyStoreResponse.body;
|
|
11
|
+
const actual = await runParser(javaKeyStoreParser, javaKeyStoreStream, uint8ArrayParserInputCompanion, {
|
|
12
|
+
errorJoinMode: 'all',
|
|
13
|
+
});
|
|
14
|
+
t.snapshot(actual);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { createArrayParser } from "./arrayParser.js";
|
|
2
|
+
import { createElementParser } from "./elementParser.js";
|
|
3
|
+
import { createTupleParser } from "./tupleParser.js";
|
|
4
|
+
export const javaKeyStoreParser = createTupleParser([
|
|
5
|
+
createArrayParser(createElementParser()),
|
|
6
|
+
createArrayParser(createElementParser()),
|
|
7
|
+
]);
|
package/build/jsonParser.js
CHANGED
|
@@ -51,7 +51,7 @@ const jsonStringParser = promiseCompose(createTupleParser([
|
|
|
51
51
|
createExactSequenceParser('"'),
|
|
52
52
|
promiseCompose(createTerminatedArrayParser(jsonStringCharacterParser, createExactSequenceParser('"')), ([characters]) => characters.join('')),
|
|
53
53
|
]), ([, string]) => string);
|
|
54
|
-
const jsonNumberParser = parserCreatorCompose(() => createArrayParser(parserCreatorCompose(() => elementParser,
|
|
54
|
+
const jsonNumberParser = parserCreatorCompose(() => createArrayParser(parserCreatorCompose(() => elementParser, character => async (parserContext) => {
|
|
55
55
|
parserContext.invariant((character === '-'
|
|
56
56
|
|| (character >= '0' && character <= '9')
|
|
57
57
|
|| character === '.'
|
|
@@ -59,7 +59,7 @@ const jsonNumberParser = parserCreatorCompose(() => createArrayParser(parserCrea
|
|
|
59
59
|
|| character === 'E'
|
|
60
60
|
|| character === '+'), 'Expected "-", "0" to "9", ".", "e", "E", "+", got "%s"', character);
|
|
61
61
|
return character;
|
|
62
|
-
})()),
|
|
62
|
+
})()), characters => async (parserContext) => {
|
|
63
63
|
parserContext.invariant(characters.length > 0, 'Expected at least one character');
|
|
64
64
|
return Number(characters.join(''));
|
|
65
65
|
})();
|
|
@@ -67,6 +67,7 @@ const jsonTrueParser = promiseCompose(createExactSequenceParser('true'), () => t
|
|
|
67
67
|
setParserName(jsonTrueParser, 'jsonTrueParser');
|
|
68
68
|
const jsonFalseParser = promiseCompose(createExactSequenceParser('false'), () => false);
|
|
69
69
|
setParserName(jsonFalseParser, 'jsonFalseParser');
|
|
70
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
70
71
|
const jsonNullParser = promiseCompose(createExactSequenceParser('null'), () => null);
|
|
71
72
|
setParserName(jsonNullParser, 'jsonNullParser');
|
|
72
73
|
const jsonPrimitiveParser = createUnionParser([
|
package/build/jsonParser.test.js
CHANGED
|
@@ -2,12 +2,12 @@ import { testProp, fc } from '@fast-check/ava';
|
|
|
2
2
|
import { jsonValueParser } from './jsonParser.js';
|
|
3
3
|
import { runParser } from './parser.js';
|
|
4
4
|
import { arbitrarilySlicedAsyncIterator } from './arbitrarilySlicedAsyncInterator.js';
|
|
5
|
-
import {
|
|
5
|
+
import { stringParserInputCompanion } from './parserInputCompanion.js';
|
|
6
6
|
testProp('json', [
|
|
7
7
|
arbitrarilySlicedAsyncIterator(fc.tuple(fc.json(), fc.nat())
|
|
8
8
|
.map(([jsonString, indent]) => JSON.stringify(JSON.parse(jsonString), null, indent))),
|
|
9
9
|
], async (t, [jsonString, jsonStringChunkIterator]) => {
|
|
10
|
-
const actual = await runParser(jsonValueParser, jsonStringChunkIterator,
|
|
10
|
+
const actual = await runParser(jsonValueParser, jsonStringChunkIterator, stringParserInputCompanion, {
|
|
11
11
|
errorJoinMode: 'none',
|
|
12
12
|
});
|
|
13
13
|
const expected = JSON.parse(jsonString);
|
package/build/listParser.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Parser } from
|
|
1
|
+
import { type Parser } from './parser.js';
|
|
2
2
|
export declare const createListParser: <ElementOutput, Sequence>(elementParser: Parser<ElementOutput, Sequence>) => Parser<ElementOutput[], Sequence>;
|
package/build/listParser.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { createNegativeLookaheadParser } from
|
|
2
|
-
import { createParserAccessorParser } from
|
|
3
|
-
import { promiseCompose } from
|
|
4
|
-
import { createTupleParser } from
|
|
5
|
-
import { createUnionParser } from
|
|
1
|
+
import { createNegativeLookaheadParser } from './negativeLookaheadParser.js';
|
|
2
|
+
import { createParserAccessorParser } from './parserAccessorParser.js';
|
|
3
|
+
import { promiseCompose } from './promiseCompose.js';
|
|
4
|
+
import { createTupleParser } from './tupleParser.js';
|
|
5
|
+
import { createUnionParser } from './unionParser.js';
|
|
6
6
|
class Nil {
|
|
7
7
|
}
|
|
8
8
|
class Cons {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Parser } from
|
|
1
|
+
import { type Parser } from './parser.js';
|
|
2
2
|
export declare const createNegativeLookaheadParser: <Sequence>(childParser: Parser<unknown, Sequence>) => Parser<void, Sequence>;
|
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
import { getParserName } from
|
|
2
|
-
import { ParserParsingFailedError } from
|
|
3
|
-
export const createNegativeLookaheadParser = (childParser) => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { getParserName } from './parser.js';
|
|
2
|
+
import { ParserParsingFailedError } from './parserError.js';
|
|
3
|
+
export const createNegativeLookaheadParser = (childParser) => async (parserContext) => {
|
|
4
|
+
const childParserContext = parserContext.lookahead();
|
|
5
|
+
try {
|
|
6
|
+
await childParser(childParserContext);
|
|
7
|
+
parserContext.invariant(false, 'Negative lookahead assertion failed for child parser %s.', getParserName(childParser));
|
|
8
|
+
}
|
|
9
|
+
catch (error) {
|
|
10
|
+
if (error instanceof ParserParsingFailedError) {
|
|
11
|
+
return;
|
|
9
12
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
finally {
|
|
17
|
-
childParserContext.dispose();
|
|
18
|
-
}
|
|
19
|
-
};
|
|
13
|
+
throw error;
|
|
14
|
+
}
|
|
15
|
+
finally {
|
|
16
|
+
childParserContext.dispose();
|
|
17
|
+
}
|
|
20
18
|
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { getParserName } from './parser.js';
|
|
2
|
+
import { ParserParsingFailedError } from './parserError.js';
|
|
3
|
+
export const createNegativeLookaheadParser = (childParser) => async (parserContext) => {
|
|
4
|
+
const childParserContext = parserContext.lookahead();
|
|
5
|
+
try {
|
|
6
|
+
await childParser(childParserContext);
|
|
7
|
+
parserContext.invariant(false, 'Negative lookahead assertion failed for child parser %s.', getParserName(childParser));
|
|
8
|
+
}
|
|
9
|
+
catch (error) {
|
|
10
|
+
if (error instanceof ParserParsingFailedError) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
throw error;
|
|
14
|
+
}
|
|
15
|
+
finally {
|
|
16
|
+
childParserContext.dispose();
|
|
17
|
+
}
|
|
18
|
+
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Parser } from
|
|
1
|
+
import { type Parser } from './parser.js';
|
|
2
2
|
export declare const createOptionalParser: <Output, Sequence>(childParser: Parser<Output, Sequence>) => Parser<undefined | Output, Sequence>;
|
package/build/optionalParser.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { getParserName, setParserName } from
|
|
2
|
-
import { ParserParsingFailedError } from
|
|
1
|
+
import { getParserName, setParserName } from './parser.js';
|
|
2
|
+
import { ParserParsingFailedError } from './parserError.js';
|
|
3
3
|
export const createOptionalParser = (childParser) => {
|
|
4
4
|
const optionalParser = async (parserContext) => {
|
|
5
5
|
const childParserContext = parserContext.lookahead();
|
package/build/parser.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type ParserInputCompanion } from './parserInputCompanion.js';
|
|
2
2
|
import { type ParserContext } from './parserContext.js';
|
|
3
|
-
import { DeriveSequenceElement } from './sequence.js';
|
|
3
|
+
import { type DeriveSequenceElement } from './sequence.js';
|
|
4
4
|
export type Parser<Output, Sequence, Element = DeriveSequenceElement<Sequence>> = (parserContext: ParserContext<Sequence, Element>) => Promise<Output>;
|
|
5
5
|
export declare function getParserName(parser: Parser<any, any, any>, default_?: string): string;
|
|
6
6
|
export declare function setParserName<Output, Sequence, Element = DeriveSequenceElement<Sequence>>(parser: Parser<Output, Sequence, Element>, name: string): Parser<Output, Sequence, Element>;
|
|
7
7
|
export type RunParserOptions<Output, Sequence, Element = DeriveSequenceElement<Sequence>> = {
|
|
8
8
|
errorJoinMode?: 'none' | 'deepest' | 'furthest' | 'all';
|
|
9
9
|
};
|
|
10
|
-
export declare function runParser<Output, Sequence, Element = DeriveSequenceElement<Sequence>>(parser: Parser<Output, Sequence, Element>, input: AsyncIterator<Sequence> | AsyncIterable<Sequence> | Iterable<Sequence> | Sequence,
|
|
10
|
+
export declare function runParser<Output, Sequence, Element = DeriveSequenceElement<Sequence>>(parser: Parser<Output, Sequence, Element>, input: AsyncIterator<Sequence> | AsyncIterable<Sequence> | Iterable<Sequence> | Sequence, parserInputCompanion: ParserInputCompanion<Sequence, Element>, options?: RunParserOptions<Output, Sequence, Element>): Promise<Output>;
|
package/build/parser.js
CHANGED
|
@@ -42,10 +42,10 @@ function toAsyncIterator(value) {
|
|
|
42
42
|
}
|
|
43
43
|
invariant(false, 'Value must be an async iterator, async iterable, iterable or iterator got %s.', value);
|
|
44
44
|
}
|
|
45
|
-
export async function runParser(parser, input,
|
|
45
|
+
export async function runParser(parser, input, parserInputCompanion, options = {}) {
|
|
46
46
|
const inputAsyncIterator = toAsyncIterator(input);
|
|
47
|
-
const inputReader = new InputReaderImplementation(
|
|
48
|
-
const parserContext = new ParserContextImplementation(
|
|
47
|
+
const inputReader = new InputReaderImplementation(parserInputCompanion, inputAsyncIterator);
|
|
48
|
+
const parserContext = new ParserContextImplementation(parserInputCompanion, inputReader, undefined, {
|
|
49
49
|
...options,
|
|
50
50
|
debugName: 'root',
|
|
51
51
|
});
|
package/build/parser.test.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import test from 'ava';
|
|
2
|
+
import invariant from 'invariant';
|
|
2
3
|
import { createUnionParser } from './unionParser.js';
|
|
3
4
|
import { runParser } from './parser.js';
|
|
4
|
-
import {
|
|
5
|
-
import { ParserParsingInvariantError, ParserParsingJoinAllError, ParserParsingJoinDeepestError, ParserParsingJoinError, ParserParsingJoinFurthestError, ParserParsingJoinNoneError } from './parserError.js';
|
|
5
|
+
import { stringParserInputCompanion, uint8ArrayParserInputCompanion } from './parserInputCompanion.js';
|
|
6
|
+
import { ParserParsingInvariantError, ParserParsingJoinAllError, ParserParsingJoinDeepestError, ParserParsingJoinError, ParserParsingJoinFurthestError, ParserParsingJoinNoneError, } from './parserError.js';
|
|
6
7
|
import { createTupleParser } from './tupleParser.js';
|
|
7
8
|
import { promiseCompose } from './promiseCompose.js';
|
|
8
9
|
import { createDisjunctionParser } from './disjunctionParser.js';
|
|
9
|
-
import invariant from 'invariant';
|
|
10
10
|
import { createExactSequenceParser } from './exactSequenceParser.js';
|
|
11
11
|
import { createArrayParser } from './arrayParser.js';
|
|
12
12
|
import { createElementParser } from './elementParser.js';
|
|
@@ -44,23 +44,23 @@ function sortChildErrors(error) {
|
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
function removeStackLocations(errorStack) {
|
|
47
|
-
return errorStack.
|
|
47
|
+
return errorStack.replaceAll(/((at [^\n]+)[\s\n]+)+(at [^\n]+)/g, 'at [LOCATIONS]');
|
|
48
48
|
}
|
|
49
49
|
test('errorJoinMode: none', async (t) => {
|
|
50
|
-
const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'),
|
|
50
|
+
const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'), stringParserInputCompanion, {
|
|
51
51
|
errorJoinMode: 'none',
|
|
52
52
|
}), {
|
|
53
|
-
instanceOf: ParserParsingJoinNoneError
|
|
53
|
+
instanceOf: ParserParsingJoinNoneError,
|
|
54
54
|
});
|
|
55
55
|
t.is(error.position, 12);
|
|
56
56
|
t.deepEqual(error.childErrors, []);
|
|
57
57
|
t.snapshot(removeStackLocations(error.stack));
|
|
58
58
|
});
|
|
59
59
|
test('errorJoinMode: all', async (t) => {
|
|
60
|
-
const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'),
|
|
60
|
+
const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'), stringParserInputCompanion, {
|
|
61
61
|
errorJoinMode: 'all',
|
|
62
62
|
}), {
|
|
63
|
-
instanceOf: ParserParsingJoinAllError
|
|
63
|
+
instanceOf: ParserParsingJoinAllError,
|
|
64
64
|
});
|
|
65
65
|
sortChildErrors(error);
|
|
66
66
|
t.snapshot(removeStackLocations(error.stack));
|
|
@@ -79,7 +79,7 @@ test('errorJoinMode: all', async (t) => {
|
|
|
79
79
|
t.is(error3.position, 4, 'error3.position');
|
|
80
80
|
});
|
|
81
81
|
test('errorJoinMode: deepest', async (t) => {
|
|
82
|
-
const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'),
|
|
82
|
+
const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'), stringParserInputCompanion, {
|
|
83
83
|
errorJoinMode: 'deepest',
|
|
84
84
|
}), {
|
|
85
85
|
instanceOf: ParserParsingJoinDeepestError,
|
|
@@ -101,7 +101,7 @@ test('errorJoinMode: deepest', async (t) => {
|
|
|
101
101
|
t.is(error3.position, 4);
|
|
102
102
|
});
|
|
103
103
|
test('errorJoinMode: furthest', async (t) => {
|
|
104
|
-
const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'),
|
|
104
|
+
const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'), stringParserInputCompanion, {
|
|
105
105
|
errorJoinMode: 'furthest',
|
|
106
106
|
}), {
|
|
107
107
|
instanceOf: ParserParsingJoinFurthestError,
|
|
@@ -114,13 +114,13 @@ test('errorJoinMode: furthest', async (t) => {
|
|
|
114
114
|
invariant(error1 instanceof ParserParsingInvariantError, 'error1 instanceof ParserParsingInvariantError');
|
|
115
115
|
t.is(error1.position, 12);
|
|
116
116
|
});
|
|
117
|
-
test('throws on
|
|
117
|
+
test('throws on parserInputCompanion type mismatch', async (t) => {
|
|
118
118
|
const anythingParser = createArrayParser(createElementParser());
|
|
119
|
-
await runParser(anythingParser, asyncIteratorFromString('anything'),
|
|
120
|
-
await runParser(anythingParser, 'anything',
|
|
121
|
-
await runParser(anythingParser, Buffer.from('anything'),
|
|
122
|
-
await runParser(anythingParser, new Uint8Array([1, 2, 3]),
|
|
123
|
-
await t.throwsAsync(runParser(anythingParser, asyncIteratorFromString('anything'),
|
|
119
|
+
await runParser(anythingParser, asyncIteratorFromString('anything'), stringParserInputCompanion);
|
|
120
|
+
await runParser(anythingParser, 'anything', stringParserInputCompanion);
|
|
121
|
+
await runParser(anythingParser, Buffer.from('anything'), uint8ArrayParserInputCompanion);
|
|
122
|
+
await runParser(anythingParser, new Uint8Array([1, 2, 3]), uint8ArrayParserInputCompanion);
|
|
123
|
+
await t.throwsAsync(runParser(anythingParser, asyncIteratorFromString('anything'), uint8ArrayParserInputCompanion), {
|
|
124
124
|
message: /input companion/,
|
|
125
125
|
});
|
|
126
126
|
});
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Parser } from
|
|
1
|
+
import { type Parser } from './parser.js';
|
|
2
2
|
export declare const createParserAccessorParser: <Output, Sequence>(parserAccessor: () => Parser<Output, Sequence>) => Parser<Output, Sequence>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createFixedLengthSequenceParser } from "./fixedLengthSequenceParser.js";
|
|
2
|
+
export const createParserConsumedSequenceParser = (childParser) => async (parserContext) => {
|
|
3
|
+
const initialPosition = parserContext.position;
|
|
4
|
+
const childParserContext = parserContext.lookahead();
|
|
5
|
+
let value;
|
|
6
|
+
let consumedLength;
|
|
7
|
+
try {
|
|
8
|
+
value = await childParser(childParserContext);
|
|
9
|
+
consumedLength = childParserContext.position - initialPosition;
|
|
10
|
+
}
|
|
11
|
+
finally {
|
|
12
|
+
childParserContext.dispose();
|
|
13
|
+
}
|
|
14
|
+
const consumedSequenceParser = createFixedLengthSequenceParser(consumedLength);
|
|
15
|
+
const consumedSequence = await consumedSequenceParser(parserContext);
|
|
16
|
+
return [value, consumedSequence];
|
|
17
|
+
};
|
package/build/parserContext.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type ParserInputCompanion } from './parserInputCompanion.js';
|
|
2
2
|
import { type InputReader } from './inputReader.js';
|
|
3
|
-
import { ParserParsingFailedError } from './parserError.js';
|
|
4
|
-
import { RunParserOptions } from './parser.js';
|
|
5
|
-
import { Falsy, ValueOrAccessor } from './
|
|
3
|
+
import { type ParserParsingFailedError } from './parserError.js';
|
|
4
|
+
import { type RunParserOptions } from './parser.js';
|
|
5
|
+
import { type Falsy, type ValueOrAccessor } from './customInvariant.js';
|
|
6
6
|
type LookaheadOptions = {
|
|
7
7
|
debugName?: string;
|
|
8
8
|
sliceEnd?: number;
|
|
@@ -23,14 +23,14 @@ export type ParserContext<Sequence, Element> = {
|
|
|
23
23
|
invariantJoin<T>(value: T, childErrors: ParserParsingFailedError[], format: ValueOrAccessor<string | string[]>, ...formatArguments: any[]): Exclude<T, Falsy>;
|
|
24
24
|
};
|
|
25
25
|
export declare class ParserContextImplementation<Sequence, Element> implements ParserContext<Sequence, Element> {
|
|
26
|
-
private readonly
|
|
26
|
+
private readonly _parserInputCompanion;
|
|
27
27
|
private _inputReader;
|
|
28
28
|
private _parentParserContext;
|
|
29
29
|
private readonly _options;
|
|
30
30
|
private readonly _id;
|
|
31
31
|
private readonly _depth;
|
|
32
32
|
private _exclusiveChildParserContext;
|
|
33
|
-
constructor(
|
|
33
|
+
constructor(_parserInputCompanion: ParserInputCompanion<Sequence, Element>, _inputReader: InputReader<Sequence, Element>, _parentParserContext: ParserContextImplementation<Sequence, Element> | undefined, _options: ParserContextOptions<Sequence, Element>);
|
|
34
34
|
get [Symbol.toStringTag](): string;
|
|
35
35
|
from(elements: Element[]): Sequence;
|
|
36
36
|
length(sequence: Sequence): number;
|