@futpib/parser 1.0.1 → 1.0.2
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/allSettledStream.js +32 -14
- package/build/allSettledStream.test.js +32 -0
- package/build/androidPackage.d.ts +39 -0
- package/build/androidPackageParser.d.ts +17 -0
- package/build/androidPackageParser.js +183 -0
- package/build/{apkParser.test.js → androidPackageParser.test.js} +6 -6
- package/build/androidPackageUnparser.d.ts +4 -0
- package/build/{apkUnparser.js → androidPackageUnparser.js} +23 -23
- package/build/androidPackageUnparser.test.js +26 -0
- package/build/arbitrarilySlicedAsyncInterable.d.ts +3 -1
- package/build/arbitrarilySlicedAsyncInterable.js +3 -3
- package/build/arrayParser.test.js +3 -3
- package/build/backsmali.d.ts +1 -0
- package/build/backsmali.js +22 -0
- package/build/bsonParser.js +6 -2
- package/build/customInvariant.d.ts +1 -1
- package/build/dalvikBytecodeParser/formatParsers.d.ts +97 -0
- package/build/dalvikBytecodeParser/formatParsers.js +169 -0
- package/build/dalvikBytecodeParser.d.ts +107 -0
- package/build/dalvikBytecodeParser.js +836 -0
- package/build/dalvikExecutable.d.ts +158 -0
- package/build/dalvikExecutable.js +20 -0
- package/build/dalvikExecutableParser/stringSyntaxParser.d.ts +4 -0
- package/build/dalvikExecutableParser/stringSyntaxParser.js +76 -0
- package/build/dalvikExecutableParser/typeParsers.d.ts +10 -0
- package/build/dalvikExecutableParser/typeParsers.js +34 -0
- package/build/dalvikExecutableParser/typedNumbers.d.ts +90 -0
- package/build/dalvikExecutableParser/typedNumbers.js +19 -0
- package/build/dalvikExecutableParser.d.ts +5 -0
- package/build/dalvikExecutableParser.js +1439 -0
- package/build/dalvikExecutableParser.test.js +70 -0
- package/build/dalvikExecutableParserAgainstSmaliParser.test.js +298 -0
- package/build/debugLogInputParser.d.ts +4 -0
- package/build/debugLogInputParser.js +16 -0
- package/build/debugLogParser.js +14 -3
- package/build/disjunctionParser.d.ts +2 -1
- package/build/disjunctionParser.js +4 -2
- package/build/elementTerminatedArrayParser.d.ts +3 -0
- package/build/elementTerminatedArrayParser.js +18 -0
- package/build/elementTerminatedArrayParser.test.js +52 -0
- package/build/elementTerminatedSequenceArrayParser.d.ts +3 -0
- package/build/elementTerminatedSequenceArrayParser.js +32 -0
- package/build/elementTerminatedSequenceArrayParser.test.js +34 -0
- package/build/elementTerminatedSequenceParser.d.ts +3 -0
- package/build/elementTerminatedSequenceParser.js +27 -0
- package/build/elementTerminatedSequenceParser.test.js +34 -0
- package/build/exactElementParser.js +10 -5
- package/build/exactSequenceParser.d.ts +2 -1
- package/build/exactSequenceParser.js +12 -2
- package/build/fetchCid.d.ts +1 -0
- package/build/fetchCid.js +107 -0
- package/build/fixedLengthSequenceParser.d.ts +1 -0
- package/build/fixedLengthSequenceParser.js +18 -1
- package/build/fixedLengthSequenceParser.test.js +41 -0
- package/build/hasExecutable.d.ts +1 -0
- package/build/hasExecutable.js +8 -0
- package/build/highResolutionTimer.d.ts +16 -0
- package/build/highResolutionTimer.js +42 -0
- package/build/inputReader.d.ts +11 -0
- package/build/inputReader.js +37 -0
- package/build/inputReader.test.js +165 -0
- package/build/inputReaderState.d.ts +10 -0
- package/build/inputReaderState.js +16 -0
- package/build/inspect.d.ts +1 -0
- package/build/inspect.js +7 -0
- package/build/javaKeyStoreParser.test.js +2 -2
- package/build/jsonParser.d.ts +2 -0
- package/build/jsonParser.js +11 -14
- package/build/leb128Parser.d.ts +7 -0
- package/build/leb128Parser.js +82 -0
- package/build/leb128Parser.test.js +107 -0
- package/build/lookaheadParser.d.ts +2 -0
- package/build/lookaheadParser.js +14 -0
- package/build/negativeLookaheadParser.js +21 -15
- package/build/negativeLookaheadParser.test.js +30 -0
- package/build/nonEmptyArrayParser.d.ts +2 -0
- package/build/nonEmptyArrayParser.js +32 -0
- package/build/nonEmptyArrayParser.test.js +16 -0
- package/build/parser.d.ts +10 -1
- package/build/parser.js +66 -31
- package/build/parser.test.js +79 -12
- package/build/parserAccessorParser.js +9 -1
- package/build/parserConsumedSequenceParser.js +20 -15
- package/build/parserContext.d.ts +14 -0
- package/build/parserContext.js +56 -27
- package/build/parserContext.test.js +27 -0
- package/build/parserCreatorCompose.d.ts +1 -0
- package/build/parserCreatorCompose.js +8 -2
- package/build/parserError.d.ts +6 -0
- package/build/parserError.js +6 -6
- package/build/parserInputCompanion.d.ts +15 -0
- package/build/parserInputCompanion.js +38 -0
- package/build/promiseCompose.d.ts +1 -1
- package/build/promiseCompose.js +12 -1
- package/build/promiseSettled.d.ts +1 -0
- package/build/promiseSettled.js +4 -0
- package/build/separatedArrayParser.d.ts +2 -0
- package/build/separatedArrayParser.js +39 -0
- package/build/separatedArrayParser.test.d.ts +1 -0
- package/build/separatedArrayParser.test.js +21 -0
- package/build/sequenceBuffer.d.ts +10 -0
- package/build/sequenceBuffer.js +54 -2
- package/build/sequenceBuffer.test.js +57 -0
- package/build/sequenceTerminatedSequenceParser.d.ts +5 -0
- package/build/sequenceTerminatedSequenceParser.js +32 -0
- package/build/sequenceTerminatedSequenceParser.test.d.ts +1 -0
- package/build/sequenceTerminatedSequenceParser.test.js +37 -0
- package/build/skipParser.d.ts +1 -1
- package/build/skipParser.js +4 -2
- package/build/skipToParser.d.ts +2 -0
- package/build/skipToParser.js +11 -0
- package/build/sliceBoundedParser.d.ts +1 -1
- package/build/sliceBoundedParser.js +7 -2
- package/build/sliceBoundedParser.test.js +35 -1
- package/build/smali.d.ts +1 -0
- package/build/smali.js +17 -0
- package/build/smaliParser.d.ts +12 -0
- package/build/smaliParser.js +656 -0
- package/build/smaliParser.test.d.ts +1 -0
- package/build/smaliParser.test.js +115 -0
- package/build/terminatedArrayParser.d.ts +3 -1
- package/build/terminatedArrayParser.js +79 -2
- package/build/terminatedArrayParser.test.d.ts +1 -0
- package/build/terminatedArrayParser.test.js +131 -0
- package/build/toAsyncIterable.d.ts +1 -0
- package/build/toAsyncIterable.js +7 -0
- package/build/toAsyncIterator.d.ts +1 -0
- package/build/toAsyncIterator.js +33 -0
- package/build/tupleParser.d.ts +4 -0
- package/build/tupleParser.js +1 -5
- package/build/unionParser.d.ts +2 -1
- package/build/unionParser.js +27 -12
- package/build/unionParser.test.d.ts +1 -0
- package/build/unionParser.test.js +60 -0
- package/build/zipParser.d.ts +7 -2
- package/build/zipParser.js +36 -12
- package/build/zipUnparser.d.ts +4 -1
- package/build/zipUnparser.js +55 -26
- package/build/zipUnparser.test.js +14 -14
- package/package.json +23 -7
- package/src/allSettledStream.test.ts +40 -0
- package/src/allSettledStream.ts +47 -15
- package/src/androidPackage.ts +48 -0
- package/src/{apkParser.test.ts → androidPackageParser.test.ts} +6 -7
- package/src/{apkParser.test.ts.md → androidPackageParser.test.ts.md} +4 -4
- package/src/androidPackageParser.test.ts.snap +0 -0
- package/src/androidPackageParser.ts +440 -0
- package/src/androidPackageUnparser.test.ts +36 -0
- package/src/androidPackageUnparser.ts +120 -0
- package/src/arbitrarilySlicedAsyncInterable.ts +7 -2
- package/src/arrayParser.test.ts +3 -3
- package/src/backsmali.ts +30 -0
- package/src/bsonParser.ts +13 -2
- package/src/customInvariant.ts +1 -1
- package/src/dalvikBytecodeParser/formatParsers.ts +421 -0
- package/src/dalvikBytecodeParser.ts +2074 -0
- package/src/dalvikExecutable.ts +220 -0
- package/src/dalvikExecutableParser/stringSyntaxParser.ts +145 -0
- package/src/dalvikExecutableParser/typeParsers.ts +65 -0
- package/src/dalvikExecutableParser/typedNumbers.ts +57 -0
- package/src/dalvikExecutableParser.test.ts +89 -0
- package/src/dalvikExecutableParser.test.ts.md +634 -0
- package/src/dalvikExecutableParser.test.ts.snap +0 -0
- package/src/dalvikExecutableParser.ts +2768 -0
- package/src/dalvikExecutableParserAgainstSmaliParser.test.ts +346 -0
- package/src/debugLogInputParser.ts +28 -0
- package/src/debugLogParser.ts +19 -3
- package/src/disjunctionParser.ts +10 -5
- package/src/elementTerminatedArrayParser.test.ts +99 -0
- package/src/elementTerminatedArrayParser.ts +31 -0
- package/src/elementTerminatedSequenceArrayParser.test.ts +54 -0
- package/src/elementTerminatedSequenceArrayParser.ts +52 -0
- package/src/elementTerminatedSequenceParser.test.ts +54 -0
- package/src/elementTerminatedSequenceParser.ts +43 -0
- package/src/exactElementParser.ts +17 -11
- package/src/exactSequenceParser.ts +23 -2
- package/src/fetchCid.ts +125 -0
- package/src/fixedLengthSequenceParser.test.ts +77 -0
- package/src/fixedLengthSequenceParser.ts +28 -1
- package/src/hasExecutable.ts +11 -0
- package/src/highResolutionTimer.ts +49 -0
- package/src/inputReader.test.ts +209 -0
- package/src/inputReader.ts +75 -0
- package/src/inputReaderState.ts +33 -0
- package/src/inspect.ts +9 -0
- package/src/javaKeyStoreParser.test.ts +2 -3
- package/src/jsonParser.ts +13 -25
- package/src/leb128Parser.test.ts +171 -0
- package/src/leb128Parser.ts +125 -0
- package/src/lookaheadParser.ts +19 -0
- package/src/negativeLookaheadParser.test.ts +53 -0
- package/src/negativeLookaheadParser.ts +26 -14
- package/src/nonEmptyArrayParser.test.ts +20 -0
- package/src/nonEmptyArrayParser.ts +44 -0
- package/src/optionalParser.ts +1 -0
- package/src/parser.test.ts +131 -12
- package/src/parser.test.ts.md +21 -21
- package/src/parser.test.ts.snap +0 -0
- package/src/parser.ts +149 -45
- package/src/parserAccessorParser.ts +12 -2
- package/src/parserConsumedSequenceParser.ts +25 -16
- package/src/parserContext.test.ts +31 -0
- package/src/parserContext.ts +109 -37
- package/src/parserCreatorCompose.ts +20 -2
- package/src/parserError.ts +9 -6
- package/src/parserInputCompanion.ts +55 -0
- package/src/promiseCompose.ts +17 -3
- package/src/promiseSettled.ts +6 -0
- package/src/separatedArrayParser.test.ts +34 -0
- package/src/separatedArrayParser.ts +55 -0
- package/src/sequenceBuffer.test.ts +70 -0
- package/src/sequenceBuffer.ts +88 -2
- package/src/sequenceTerminatedSequenceParser.test.ts +60 -0
- package/src/sequenceTerminatedSequenceParser.ts +62 -0
- package/src/skipParser.ts +7 -5
- package/src/skipToParser.ts +16 -0
- package/src/sliceBoundedParser.test.ts +43 -1
- package/src/sliceBoundedParser.ts +19 -1
- package/src/smali.ts +24 -0
- package/src/smaliParser.test.ts +132 -0
- package/src/smaliParser.test.ts.md +2320 -0
- package/src/smaliParser.test.ts.snap +0 -0
- package/src/smaliParser.ts +1166 -0
- package/src/terminatedArrayParser.test.ts +258 -0
- package/src/terminatedArrayParser.ts +108 -3
- package/src/toAsyncIterable.ts +7 -0
- package/src/toAsyncIterator.ts +48 -0
- package/src/tupleParser.ts +8 -5
- package/src/unionParser.test.ts +79 -0
- package/src/unionParser.ts +44 -16
- package/src/zipParser.ts +77 -18
- package/src/zipUnparser.test.ts +18 -18
- package/src/zipUnparser.ts +88 -27
- package/build/apk.d.ts +0 -39
- package/build/apkParser.d.ts +0 -16
- package/build/apkParser.js +0 -164
- package/build/apkUnparser.d.ts +0 -4
- package/build/apkUnparser.test.js +0 -26
- package/build/arbitraryDosDate.d.ts +0 -2
- package/build/arbitraryDosDate.js +0 -8
- package/build/arbitraryZipEntry.d.ts +0 -3
- package/build/arbitraryZipEntry.js +0 -26
- package/build/createDisjunctionParser.d.ts +0 -2
- package/build/createDisjunctionParser.js +0 -47
- package/build/createExactParser.d.ts +0 -2
- package/build/createExactParser.js +0 -12
- package/build/createSequentialUnionParser.d.ts +0 -2
- package/build/createSequentialUnionParser.js +0 -69
- package/build/fixedLengthChunkParser.d.ts +0 -2
- package/build/fixedLengthChunkParser.js +0 -12
- package/build/fixedLengthParser.d.ts +0 -2
- package/build/fixedLengthParser.js +0 -12
- package/build/inputChunkBuffer.d.ts +0 -15
- package/build/inputChunkBuffer.js +0 -40
- package/build/inputChunkBuffer.test.js +0 -34
- package/build/inputCompanion.d.ts +0 -18
- package/build/inputCompanion.js +0 -28
- package/build/invariantDefined.d.ts +0 -1
- package/build/invariantDefined.js +0 -5
- package/build/invariantIdentity.d.ts +0 -3
- package/build/invariantIdentity.js +0 -5
- package/build/javaKeystoreParser.d.ts +0 -2
- package/build/javaKeystoreParser.js +0 -7
- package/build/jsonParser2.d.ts +0 -3
- package/build/jsonParser2.js +0 -52
- package/build/jsonParser2.test.js +0 -22
- package/build/negativeLookahead.d.ts +0 -2
- package/build/negativeLookahead.js +0 -18
- package/build/parserCompose.d.ts +0 -3
- package/build/parserCompose.js +0 -7
- package/build/parserImplementationInvariantInvariant.d.ts +0 -3
- package/build/parserImplementationInvariantInvariant.js +0 -15
- package/build/parserInvariant.d.ts +0 -4
- package/build/parserInvariant.js +0 -11
- package/build/promiseFish.d.ts +0 -1
- package/build/promiseFish.js +0 -3
- package/build/sequenceParser.d.ts +0 -3
- package/build/sequenceParser.js +0 -10
- package/build/terminatedSequenceParser.d.ts +0 -2
- package/build/terminatedSequenceParser.js +0 -24
- package/build/unparserInputCompanion.d.ts +0 -15
- package/build/unparserInputCompanion.js +0 -13
- package/build/zipEntry.d.ts +0 -28
- package/build/zipFile.d.ts +0 -32
- package/build/zipFileEntry.d.ts +0 -6
- package/src/apk.ts +0 -48
- package/src/apkParser.test.ts.snap +0 -0
- package/src/apkParser.ts +0 -392
- package/src/apkUnparser.test.ts +0 -37
- package/src/apkUnparser.ts +0 -120
- package/src/invariantDefined.ts +0 -6
- package/src/invariantIdentity.ts +0 -8
- /package/build/{apk.js → androidPackage.js} +0 -0
- /package/build/{apkParser.test.d.ts → androidPackageParser.test.d.ts} +0 -0
- /package/build/{apkUnparser.test.d.ts → androidPackageUnparser.test.d.ts} +0 -0
- /package/build/{arbitraryDosPermissions.d.ts → dalvikExecutableParser.test.d.ts} +0 -0
- /package/build/{arbitraryDosPermissions.js → dalvikExecutableParserAgainstSmaliParser.test.d.ts} +0 -0
- /package/build/{inputChunkBuffer.test.d.ts → elementTerminatedArrayParser.test.d.ts} +0 -0
- /package/build/{jsonParser2.test.d.ts → elementTerminatedSequenceArrayParser.test.d.ts} +0 -0
- /package/build/{parserParsingInvariant.d.ts → elementTerminatedSequenceParser.test.d.ts} +0 -0
- /package/build/{parserParsingInvariant.js → fixedLengthSequenceParser.test.d.ts} +0 -0
- /package/build/{zipEntry.js → leb128Parser.test.d.ts} +0 -0
- /package/build/{zipFile.js → negativeLookaheadParser.test.d.ts} +0 -0
- /package/build/{zipFileEntry.js → nonEmptyArrayParser.test.d.ts} +0 -0
package/build/parser.test.js
CHANGED
|
@@ -1,31 +1,32 @@
|
|
|
1
1
|
import test from 'ava';
|
|
2
2
|
import invariant from 'invariant';
|
|
3
3
|
import { createUnionParser } from './unionParser.js';
|
|
4
|
-
import { runParser } from './parser.js';
|
|
4
|
+
import { runParser, runParserWithRemainingInput } from './parser.js';
|
|
5
5
|
import { stringParserInputCompanion, uint8ArrayParserInputCompanion } from './parserInputCompanion.js';
|
|
6
|
-
import { ParserParsingInvariantError, ParserParsingJoinAllError, ParserParsingJoinDeepestError, ParserParsingJoinError, ParserParsingJoinFurthestError, ParserParsingJoinNoneError, } from './parserError.js';
|
|
6
|
+
import { ParserError, ParserParsingInvariantError, ParserParsingJoinAllError, ParserParsingJoinDeepestError, ParserParsingJoinError, ParserParsingJoinFurthestError, ParserParsingJoinNoneError, ParserUnexpectedRemainingInputError, } from './parserError.js';
|
|
7
7
|
import { createTupleParser } from './tupleParser.js';
|
|
8
8
|
import { promiseCompose } from './promiseCompose.js';
|
|
9
9
|
import { createDisjunctionParser } from './disjunctionParser.js';
|
|
10
|
-
import {
|
|
10
|
+
import { createExactSequenceNaiveParser } from './exactSequenceParser.js';
|
|
11
11
|
import { createArrayParser } from './arrayParser.js';
|
|
12
12
|
import { createElementParser } from './elementParser.js';
|
|
13
|
+
import { toAsyncIterable } from './toAsyncIterable.js';
|
|
13
14
|
const aUnionParser = createUnionParser([
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
createExactSequenceNaiveParser('1'),
|
|
16
|
+
createExactSequenceNaiveParser('aa'),
|
|
17
|
+
createExactSequenceNaiveParser('AAA'),
|
|
17
18
|
]);
|
|
18
19
|
const abDisjunctionParser = createDisjunctionParser([
|
|
19
20
|
aUnionParser,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
createExactSequenceNaiveParser('2'),
|
|
22
|
+
createExactSequenceNaiveParser('bb'),
|
|
23
|
+
createExactSequenceNaiveParser('BBB'),
|
|
23
24
|
]);
|
|
24
25
|
const abcUnionParser = createUnionParser([
|
|
25
26
|
abDisjunctionParser,
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
createExactSequenceNaiveParser('final_3'),
|
|
28
|
+
createExactSequenceNaiveParser('final_cc'),
|
|
29
|
+
createExactSequenceNaiveParser('final_CCC'),
|
|
29
30
|
]);
|
|
30
31
|
const sampleParser = promiseCompose(createTupleParser([
|
|
31
32
|
aUnionParser,
|
|
@@ -35,6 +36,13 @@ const sampleParser = promiseCompose(createTupleParser([
|
|
|
35
36
|
async function* asyncIteratorFromString(string) {
|
|
36
37
|
yield string;
|
|
37
38
|
}
|
|
39
|
+
async function stringFromAsyncIterable(asyncIterable) {
|
|
40
|
+
let string = '';
|
|
41
|
+
for await (const chunk of asyncIterable) {
|
|
42
|
+
string += chunk;
|
|
43
|
+
}
|
|
44
|
+
return string;
|
|
45
|
+
}
|
|
38
46
|
function sortChildErrors(error) {
|
|
39
47
|
error.childErrors.sort((a, b) => a.message.localeCompare(b.message));
|
|
40
48
|
for (const childError of error.childErrors) {
|
|
@@ -124,3 +132,62 @@ test('throws on parserInputCompanion type mismatch', async (t) => {
|
|
|
124
132
|
message: /input companion/,
|
|
125
133
|
});
|
|
126
134
|
});
|
|
135
|
+
test('thrown error has input reader state', async (t) => {
|
|
136
|
+
const error = await t.throwsAsync(runParser(createTupleParser([
|
|
137
|
+
createExactSequenceNaiveParser('foo'),
|
|
138
|
+
createExactSequenceNaiveParser('bar'),
|
|
139
|
+
]), (async function* () {
|
|
140
|
+
yield 'foo';
|
|
141
|
+
yield 'qux';
|
|
142
|
+
yield 'bar';
|
|
143
|
+
})(), stringParserInputCompanion), {
|
|
144
|
+
instanceOf: ParserError,
|
|
145
|
+
});
|
|
146
|
+
t.is(error.position, 4);
|
|
147
|
+
invariant(error.inputReaderSate, 'error.inputReaderSate');
|
|
148
|
+
const { consumedBufferedSequences, unconsumedBufferedSequences, unbufferedSequences, position, ...inputReaderStateRest } = error.inputReaderSate;
|
|
149
|
+
t.is(position, 4);
|
|
150
|
+
t.deepEqual(inputReaderStateRest, {});
|
|
151
|
+
const unbufferedSequencesArray = [];
|
|
152
|
+
if (unbufferedSequences) {
|
|
153
|
+
for await (const sequence of toAsyncIterable(unbufferedSequences)) {
|
|
154
|
+
unbufferedSequencesArray.push(sequence);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
t.deepEqual(consumedBufferedSequences, ['q']);
|
|
158
|
+
t.deepEqual(unconsumedBufferedSequences, ['ux']);
|
|
159
|
+
t.deepEqual(unbufferedSequencesArray, ['bar']);
|
|
160
|
+
});
|
|
161
|
+
test('runParser throws with remaining input', async (t) => {
|
|
162
|
+
const parser = createExactSequenceNaiveParser('foo');
|
|
163
|
+
const error = await t.throwsAsync(() => runParser(parser, 'foobar', stringParserInputCompanion), {
|
|
164
|
+
instanceOf: ParserUnexpectedRemainingInputError,
|
|
165
|
+
message: /remaining input/,
|
|
166
|
+
});
|
|
167
|
+
t.is(error.position, 3);
|
|
168
|
+
const { position, consumedBufferedSequences, unconsumedBufferedSequences, unbufferedSequences, ...inputReaderStateRest } = error.inputReaderSate;
|
|
169
|
+
t.deepEqual(inputReaderStateRest, {});
|
|
170
|
+
t.is(position, 3);
|
|
171
|
+
t.deepEqual(consumedBufferedSequences, ['foo']);
|
|
172
|
+
t.deepEqual(unconsumedBufferedSequences, ['bar']);
|
|
173
|
+
t.truthy(unbufferedSequences);
|
|
174
|
+
});
|
|
175
|
+
test('runParser does not throw without remaining input', async (t) => {
|
|
176
|
+
const parser = createExactSequenceNaiveParser('foo');
|
|
177
|
+
const output = await runParser(parser, 'foo', stringParserInputCompanion);
|
|
178
|
+
t.deepEqual(output, 'foo');
|
|
179
|
+
});
|
|
180
|
+
test('runParserWithRemainingInput with remaining input', async (t) => {
|
|
181
|
+
const parser = createExactSequenceNaiveParser('foo');
|
|
182
|
+
const { output, remainingInput, position, ...resultRest } = await runParserWithRemainingInput(parser, 'foobar', stringParserInputCompanion);
|
|
183
|
+
t.deepEqual(resultRest, {});
|
|
184
|
+
t.deepEqual(output, 'foo');
|
|
185
|
+
t.deepEqual(await stringFromAsyncIterable(remainingInput), 'bar');
|
|
186
|
+
t.is(position, 3);
|
|
187
|
+
});
|
|
188
|
+
test('runParserWithRemainingInput without remaining input', async (t) => {
|
|
189
|
+
const parser = createExactSequenceNaiveParser('foo');
|
|
190
|
+
const { output, remainingInput } = await runParserWithRemainingInput(parser, 'foo', stringParserInputCompanion);
|
|
191
|
+
t.deepEqual(output, 'foo');
|
|
192
|
+
t.is(remainingInput, undefined);
|
|
193
|
+
});
|
|
@@ -1 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import { getParserName, setParserName } from './parser.js';
|
|
2
|
+
export const createParserAccessorParser = (parserAccessor) => {
|
|
3
|
+
const parserAccessorParser = async (parserContext) => {
|
|
4
|
+
const parser = parserAccessor();
|
|
5
|
+
setParserName(parserAccessorParser, `parserAccessorParser(${getParserName(parser, 'anonymousParserAccessor')})`);
|
|
6
|
+
return parser(parserContext);
|
|
7
|
+
};
|
|
8
|
+
return parserAccessorParser;
|
|
9
|
+
};
|
|
@@ -1,17 +1,22 @@
|
|
|
1
1
|
import { createFixedLengthSequenceParser } from "./fixedLengthSequenceParser.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
2
|
+
import { getParserName, setParserName } from "./parser.js";
|
|
3
|
+
export const createParserConsumedSequenceParser = (childParser) => {
|
|
4
|
+
const parserConsumedSequenceParser = async (parserContext) => {
|
|
5
|
+
const initialPosition = parserContext.position;
|
|
6
|
+
const childParserContext = parserContext.lookahead();
|
|
7
|
+
let value;
|
|
8
|
+
let consumedLength;
|
|
9
|
+
try {
|
|
10
|
+
value = await childParser(childParserContext);
|
|
11
|
+
consumedLength = childParserContext.position - initialPosition;
|
|
12
|
+
}
|
|
13
|
+
finally {
|
|
14
|
+
childParserContext.dispose();
|
|
15
|
+
}
|
|
16
|
+
const consumedSequenceParser = createFixedLengthSequenceParser(consumedLength);
|
|
17
|
+
const consumedSequence = await consumedSequenceParser(parserContext);
|
|
18
|
+
return [value, consumedSequence];
|
|
19
|
+
};
|
|
20
|
+
setParserName(parserConsumedSequenceParser, `parserConsumedSequenceParser(${getParserName(childParser, 'anonymousParserConsumedSequenceParserChild')})`);
|
|
21
|
+
return parserConsumedSequenceParser;
|
|
17
22
|
};
|
package/build/parserContext.d.ts
CHANGED
|
@@ -10,12 +10,19 @@ type LookaheadOptions = {
|
|
|
10
10
|
type ParserContextOptions<Sequence, Element> = RunParserOptions<unknown, Sequence, Element> & LookaheadOptions;
|
|
11
11
|
export type ParserContext<Sequence, Element> = {
|
|
12
12
|
from(elements: Element[]): Sequence;
|
|
13
|
+
concat(sequences: Sequence[]): Sequence;
|
|
13
14
|
length(sequence: Sequence): number;
|
|
14
15
|
at(sequence: Sequence, index: number): Element | undefined;
|
|
16
|
+
subsequence(sequence: Sequence, start: number, end: number): Sequence;
|
|
17
|
+
indexOf(sequence: Sequence, element: Element, fromIndex?: number): number;
|
|
18
|
+
indexOfSubsequence(sequence: Sequence, subsequence: Sequence, fromIndex?: number): number;
|
|
19
|
+
equals(sequenceA: Sequence, sequenceB: Sequence): boolean;
|
|
15
20
|
get position(): number;
|
|
16
21
|
peek(offset: number): Promise<Element | undefined>;
|
|
22
|
+
peekSequence(start: number, end: number): Promise<Sequence | undefined>;
|
|
17
23
|
skip(offset: number): void;
|
|
18
24
|
read(offset: number): Promise<Element>;
|
|
25
|
+
readSequence(start: number, end: number): Promise<Sequence>;
|
|
19
26
|
lookahead(options?: LookaheadOptions): ParserContext<Sequence, Element>;
|
|
20
27
|
unlookahead(): void;
|
|
21
28
|
dispose(): void;
|
|
@@ -33,12 +40,19 @@ export declare class ParserContextImplementation<Sequence, Element> implements P
|
|
|
33
40
|
constructor(_parserInputCompanion: ParserInputCompanion<Sequence, Element>, _inputReader: InputReader<Sequence, Element>, _parentParserContext: ParserContextImplementation<Sequence, Element> | undefined, _options: ParserContextOptions<Sequence, Element>);
|
|
34
41
|
get [Symbol.toStringTag](): string;
|
|
35
42
|
from(elements: Element[]): Sequence;
|
|
43
|
+
concat(sequences: Sequence[]): Sequence;
|
|
36
44
|
length(sequence: Sequence): number;
|
|
37
45
|
at(sequence: Sequence, index: number): Element | undefined;
|
|
46
|
+
subsequence(sequence: Sequence, start: number, end: number): Sequence;
|
|
47
|
+
indexOf(sequence: Sequence, element: Element, fromIndex?: number): number;
|
|
48
|
+
indexOfSubsequence(sequence: Sequence, subsequence: Sequence, fromIndex?: number): number;
|
|
49
|
+
equals(sequenceA: Sequence, sequenceB: Sequence): boolean;
|
|
38
50
|
get position(): number;
|
|
39
51
|
peek(offset: number): Promise<Element | undefined>;
|
|
52
|
+
peekSequence(start: number, end: number): Promise<Sequence | undefined>;
|
|
40
53
|
skip(offset: number): void;
|
|
41
54
|
read(offset: number): Promise<Element>;
|
|
55
|
+
readSequence(start: number, end: number): Promise<Sequence>;
|
|
42
56
|
lookahead(options?: LookaheadOptions): ParserContext<Sequence, Element>;
|
|
43
57
|
unlookahead(): void;
|
|
44
58
|
dispose(): void;
|
package/build/parserContext.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* eslint-disable prefer-arrow-callback */
|
|
2
|
-
import invariant from 'invariant';
|
|
3
2
|
import { ParserUnexpectedEndOfInputError, ParserParsingInvariantError, ParserParsingJoinNoneError, ParserParsingJoinAllError, ParserParsingJoinDeepestError, ParserParsingJoinFurthestError, } from './parserError.js';
|
|
4
3
|
import { customInvariant } from './customInvariant.js';
|
|
4
|
+
import { parserImplementationInvariant } from './parserImplementationInvariant.js';
|
|
5
5
|
let parserContextId = 0;
|
|
6
6
|
export class ParserContextImplementation {
|
|
7
7
|
_parserInputCompanion;
|
|
@@ -33,12 +33,27 @@ export class ParserContextImplementation {
|
|
|
33
33
|
from(elements) {
|
|
34
34
|
return this._parserInputCompanion.from(elements);
|
|
35
35
|
}
|
|
36
|
+
concat(sequences) {
|
|
37
|
+
return this._parserInputCompanion.concat(sequences);
|
|
38
|
+
}
|
|
36
39
|
length(sequence) {
|
|
37
40
|
return this._parserInputCompanion.length(sequence);
|
|
38
41
|
}
|
|
39
42
|
at(sequence, index) {
|
|
40
43
|
return this._parserInputCompanion.at(sequence, index);
|
|
41
44
|
}
|
|
45
|
+
subsequence(sequence, start, end) {
|
|
46
|
+
return this._parserInputCompanion.subsequence(sequence, start, end);
|
|
47
|
+
}
|
|
48
|
+
indexOf(sequence, element, fromIndex) {
|
|
49
|
+
return this._parserInputCompanion.indexOf(sequence, element, fromIndex);
|
|
50
|
+
}
|
|
51
|
+
indexOfSubsequence(sequence, subsequence, fromIndex) {
|
|
52
|
+
return this._parserInputCompanion.indexOfSubsequence(sequence, subsequence, fromIndex);
|
|
53
|
+
}
|
|
54
|
+
equals(sequenceA, sequenceB) {
|
|
55
|
+
return this._parserInputCompanion.equals(sequenceA, sequenceB);
|
|
56
|
+
}
|
|
42
57
|
get position() {
|
|
43
58
|
return this._inputReader.position;
|
|
44
59
|
}
|
|
@@ -49,6 +64,17 @@ export class ParserContextImplementation {
|
|
|
49
64
|
}
|
|
50
65
|
return this._inputReader.peek(offset);
|
|
51
66
|
}
|
|
67
|
+
async peekSequence(start, end) {
|
|
68
|
+
if (this._options.sliceEnd !== undefined
|
|
69
|
+
&& (this.position + end - 1) >= this._options.sliceEnd) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
parserImplementationInvariant(start >= 0, 'start (%s) >= 0', start);
|
|
73
|
+
parserImplementationInvariant(end >= start, 'end (%s) >= start (%s)', start, end);
|
|
74
|
+
parserImplementationInvariant(Number.isSafeInteger(start), 'start (%d) is not a safe integer', start);
|
|
75
|
+
parserImplementationInvariant(Number.isSafeInteger(end), 'end (%d) is not a safe integer', end);
|
|
76
|
+
return this._inputReader.peekSequence(start, end);
|
|
77
|
+
}
|
|
52
78
|
skip(offset) {
|
|
53
79
|
this._inputReader.skip(offset);
|
|
54
80
|
}
|
|
@@ -60,13 +86,19 @@ export class ParserContextImplementation {
|
|
|
60
86
|
this.skip(offset + 1);
|
|
61
87
|
return element;
|
|
62
88
|
}
|
|
89
|
+
async readSequence(start, end) {
|
|
90
|
+
const sequence = await this.peekSequence(start, end);
|
|
91
|
+
if (sequence === undefined) {
|
|
92
|
+
throw new ParserUnexpectedEndOfInputError('', this._depth, this.position);
|
|
93
|
+
}
|
|
94
|
+
this.skip(end);
|
|
95
|
+
return sequence;
|
|
96
|
+
}
|
|
63
97
|
lookahead(options = {}) {
|
|
64
98
|
const lookaheadInputReader = this._inputReader.lookahead();
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
invariant(this.position === lookaheadInputReader.position, 'lookahead this.position (%s) === lookaheadInputReader.position (%s)', this.position, lookaheadInputReader.position);
|
|
69
|
-
const lookaheadParserContext = new ParserContextImplementation(this._parserInputCompanion, lookaheadInputReader, this, {
|
|
99
|
+
parserImplementationInvariant(this.position === lookaheadInputReader.position, 'lookahead this.position (%s) === lookaheadInputReader.position (%s)', this.position, lookaheadInputReader.position);
|
|
100
|
+
const ParserContext = this._options.parserContextClass ?? ParserContextImplementation;
|
|
101
|
+
const lookaheadParserContext = new ParserContext(this._parserInputCompanion, lookaheadInputReader, this, {
|
|
70
102
|
...this._options,
|
|
71
103
|
...options,
|
|
72
104
|
debugName: [
|
|
@@ -78,41 +110,38 @@ export class ParserContextImplementation {
|
|
|
78
110
|
options.debugName,
|
|
79
111
|
].join(''),
|
|
80
112
|
});
|
|
81
|
-
|
|
82
|
-
debugger;
|
|
83
|
-
}
|
|
84
|
-
invariant(this.position === lookaheadParserContext.position, 'lookahead this.position (%s) === lookaheadParserContext.position (%s)', this.position, lookaheadParserContext.position);
|
|
113
|
+
parserImplementationInvariant(this.position === lookaheadParserContext.position, 'lookahead this.position (%s) === lookaheadParserContext.position (%s)', this.position, lookaheadParserContext.position);
|
|
85
114
|
return lookaheadParserContext;
|
|
86
115
|
}
|
|
87
116
|
unlookahead() {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
||
|
|
117
|
+
const parentParserContext = parserImplementationInvariant(this._parentParserContext, 'this._parentParserContext !== undefined');
|
|
118
|
+
parserImplementationInvariant((parentParserContext._exclusiveChildParserContext === undefined
|
|
119
|
+
|| parentParserContext._exclusiveChildParserContext === this), [
|
|
91
120
|
'Parent\'s exclusive child must be undefined or this',
|
|
92
121
|
'this: %s',
|
|
93
122
|
'parent: %s',
|
|
94
123
|
'parent.exclusiveChild: %s',
|
|
95
|
-
].join('\n'), this.toString(),
|
|
96
|
-
|
|
97
|
-
const offset = this._inputReader.position -
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
this._inputReader =
|
|
101
|
-
|
|
124
|
+
].join('\n'), this.toString(), parentParserContext.toString(), parentParserContext._exclusiveChildParserContext?.toString());
|
|
125
|
+
parserImplementationInvariant(parentParserContext.position <= this.position, 'unlookahead this._parentParserContext.position (%s) <= this.position (%s)', parentParserContext.position, this.position);
|
|
126
|
+
const offset = this._inputReader.position - parentParserContext._inputReader.position;
|
|
127
|
+
parentParserContext.skip(offset);
|
|
128
|
+
parserImplementationInvariant(parentParserContext.position === this.position, 'unlookahead this._parentParserContext.position (%s) === this.position (%s)', parentParserContext.position, this.position);
|
|
129
|
+
this._inputReader = parentParserContext._inputReader;
|
|
130
|
+
parentParserContext._exclusiveChildParserContext = this;
|
|
102
131
|
if (this._exclusiveChildParserContext) {
|
|
103
132
|
this._exclusiveChildParserContext.unlookahead();
|
|
104
133
|
}
|
|
105
134
|
}
|
|
106
135
|
dispose() {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
||
|
|
136
|
+
const parentParserContext = parserImplementationInvariant(this._parentParserContext, 'this._parentParserContext !== undefined');
|
|
137
|
+
parserImplementationInvariant((parentParserContext._exclusiveChildParserContext === undefined
|
|
138
|
+
|| parentParserContext._exclusiveChildParserContext === this), [
|
|
110
139
|
'Parent\'s exclusive child must be undefined or this',
|
|
111
140
|
'this: %s',
|
|
112
141
|
'parent: %s',
|
|
113
142
|
'parent.exclusiveChild: %s',
|
|
114
|
-
].join('\n'), this.toString(),
|
|
115
|
-
|
|
143
|
+
].join('\n'), this.toString(), parentParserContext.toString(), parentParserContext._exclusiveChildParserContext?.toString());
|
|
144
|
+
parentParserContext._exclusiveChildParserContext = undefined;
|
|
116
145
|
this._parentParserContext = undefined;
|
|
117
146
|
}
|
|
118
147
|
invariant(value, format, ...formatArguments) {
|
|
@@ -122,7 +151,7 @@ export class ParserContextImplementation {
|
|
|
122
151
|
}, value, format, ...formatArguments);
|
|
123
152
|
}
|
|
124
153
|
invariantJoin(value, childErrors, format, ...formatArguments) {
|
|
125
|
-
|
|
154
|
+
parserImplementationInvariant(childErrors.length > 0, 'childErrors.length > 0');
|
|
126
155
|
const errorJoinMode = this._options.errorJoinMode ?? 'none';
|
|
127
156
|
const parserContext = this;
|
|
128
157
|
if (errorJoinMode === 'none') {
|
|
@@ -189,6 +218,6 @@ export class ParserContextImplementation {
|
|
|
189
218
|
return new ParserParsingJoinAllError(message, parserContext._depth, parserContext.position, childErrors);
|
|
190
219
|
}, value, format, ...formatArguments);
|
|
191
220
|
}
|
|
192
|
-
|
|
221
|
+
return parserImplementationInvariant(false, 'Unsupported errorJoinMode: %s', errorJoinMode);
|
|
193
222
|
}
|
|
194
223
|
}
|
|
@@ -25,6 +25,26 @@ test('parserContext.read', async (t) => {
|
|
|
25
25
|
instanceOf: ParserUnexpectedEndOfInputError,
|
|
26
26
|
});
|
|
27
27
|
});
|
|
28
|
+
test('parserContext.readSequence', async (t) => {
|
|
29
|
+
const parserContext = new ParserContextImplementation(stringParserInputCompanion, new InputReaderImplementation(stringParserInputCompanion, (async function* () {
|
|
30
|
+
yield '';
|
|
31
|
+
yield 'abc';
|
|
32
|
+
yield 'def';
|
|
33
|
+
yield '';
|
|
34
|
+
yield 'gh';
|
|
35
|
+
})()), ...commonParserContextArguments);
|
|
36
|
+
t.is(await parserContext.readSequence(0, 0), '');
|
|
37
|
+
t.is(await parserContext.readSequence(0, 1), 'a');
|
|
38
|
+
t.is(await parserContext.readSequence(1, 4), 'cde');
|
|
39
|
+
t.is(await parserContext.readSequence(0, 1), 'f');
|
|
40
|
+
t.is(await parserContext.readSequence(0, 0), '');
|
|
41
|
+
t.is(await parserContext.readSequence(0, 1), 'g');
|
|
42
|
+
t.is(await parserContext.readSequence(1, 1), '');
|
|
43
|
+
t.is(await parserContext.readSequence(0, 0), '');
|
|
44
|
+
await t.throwsAsync(async () => parserContext.readSequence(0, 1), {
|
|
45
|
+
instanceOf: ParserUnexpectedEndOfInputError,
|
|
46
|
+
});
|
|
47
|
+
});
|
|
28
48
|
test('parserContext.lookahead', async (t) => {
|
|
29
49
|
const parserContext = new ParserContextImplementation(stringParserInputCompanion, new InputReaderImplementation(stringParserInputCompanion, (async function* () {
|
|
30
50
|
yield* 'abcdefgh';
|
|
@@ -35,11 +55,15 @@ test('parserContext.lookahead', async (t) => {
|
|
|
35
55
|
sliceEnd: 3,
|
|
36
56
|
});
|
|
37
57
|
t.is(await lookaheadContext3.peek(2), 'c');
|
|
58
|
+
t.is(await lookaheadContext3.peekSequence(2, 3), 'c');
|
|
38
59
|
t.is(await lookaheadContext3.peek(3), undefined);
|
|
60
|
+
t.is(await lookaheadContext3.peekSequence(3, 4), undefined);
|
|
39
61
|
t.is(await lookaheadContext3.read(0), 'a');
|
|
40
62
|
t.is(await lookaheadContext3.read(0), 'b');
|
|
41
63
|
t.is(await lookaheadContext3.read(0), 'c');
|
|
42
64
|
t.is(await lookaheadContext3.peek(0), undefined);
|
|
65
|
+
t.is(await lookaheadContext3.peekSequence(0, 1), undefined);
|
|
66
|
+
t.is(await lookaheadContext3.peekSequence(0, 0), '');
|
|
43
67
|
t.is(await parserContext.peek(0), 'a');
|
|
44
68
|
t.is(await lookaheadContext1.peek(0), 'a');
|
|
45
69
|
t.is(await lookaheadContext2.peek(0), 'a');
|
|
@@ -52,6 +76,9 @@ test('parserContext.lookahead', async (t) => {
|
|
|
52
76
|
t.is(lookaheadContext1.position, 1);
|
|
53
77
|
t.is(lookaheadContext2.position, 0);
|
|
54
78
|
t.is(lookaheadContext3.position, 3);
|
|
79
|
+
lookaheadContext3.skip(1);
|
|
80
|
+
t.is(await lookaheadContext3.peekSequence(0, 0), undefined);
|
|
81
|
+
t.is(lookaheadContext3.position, 4);
|
|
55
82
|
});
|
|
56
83
|
test('parserContext.unlookahead', async (t) => {
|
|
57
84
|
const parserContext = new ParserContextImplementation(stringParserInputCompanion, new InputReaderImplementation(stringParserInputCompanion, (async function* () {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { type Parser } from './parser.js';
|
|
2
2
|
import { type DeriveSequenceElement } from './sequence.js';
|
|
3
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>;
|
|
4
|
+
export declare function parserCreatorComposeMem<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,10 +1,16 @@
|
|
|
1
|
+
import mem from 'mem';
|
|
1
2
|
export function parserCreatorCompose(f1, f2) {
|
|
2
|
-
|
|
3
|
+
const parserCreatorComposedOuter = (...arguments_) => {
|
|
3
4
|
const parserA = f1(...arguments_);
|
|
4
|
-
|
|
5
|
+
const parserCreatorComposedInner = async (parserContext) => {
|
|
5
6
|
const outputA = await parserA(parserContext);
|
|
6
7
|
const parserB = f2(outputA);
|
|
7
8
|
return parserB(parserContext);
|
|
8
9
|
};
|
|
10
|
+
return parserCreatorComposedInner;
|
|
9
11
|
};
|
|
12
|
+
return parserCreatorComposedOuter;
|
|
13
|
+
}
|
|
14
|
+
export function parserCreatorComposeMem(f1, f2) {
|
|
15
|
+
return parserCreatorCompose(mem(f1), mem(f2));
|
|
10
16
|
}
|
package/build/parserError.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import { InputReaderState } from "./inputReaderState.js";
|
|
1
2
|
export declare class ParserError extends Error {
|
|
2
3
|
name: string;
|
|
4
|
+
position: undefined | number;
|
|
5
|
+
inputReaderSate: undefined | InputReaderState<unknown>;
|
|
3
6
|
}
|
|
4
7
|
export declare class ParserImplementationError extends ParserError {
|
|
5
8
|
name: string;
|
|
@@ -41,3 +44,6 @@ export declare class ParserParsingInvariantError extends ParserParsingFailedErro
|
|
|
41
44
|
export declare class ParserUnexpectedEndOfInputError extends ParserParsingFailedError {
|
|
42
45
|
name: string;
|
|
43
46
|
}
|
|
47
|
+
export declare class ParserUnexpectedRemainingInputError extends ParserParsingFailedError {
|
|
48
|
+
name: string;
|
|
49
|
+
}
|
package/build/parserError.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export class ParserError extends Error {
|
|
2
2
|
name = 'ParserError';
|
|
3
|
+
position = undefined;
|
|
4
|
+
inputReaderSate = undefined;
|
|
3
5
|
}
|
|
4
6
|
export class ParserImplementationError extends ParserError {
|
|
5
7
|
name = 'ParserImplementationError';
|
|
@@ -12,12 +14,7 @@ export class ParserParsingFailedError extends ParserError {
|
|
|
12
14
|
position;
|
|
13
15
|
name = 'ParserParsingFailedError';
|
|
14
16
|
constructor(message, depth, position) {
|
|
15
|
-
super(
|
|
16
|
-
'(position: ',
|
|
17
|
-
position,
|
|
18
|
-
') ',
|
|
19
|
-
message,
|
|
20
|
-
].join(''));
|
|
17
|
+
super(message);
|
|
21
18
|
this.depth = depth;
|
|
22
19
|
this.position = position;
|
|
23
20
|
}
|
|
@@ -59,3 +56,6 @@ export class ParserParsingInvariantError extends ParserParsingFailedError {
|
|
|
59
56
|
export class ParserUnexpectedEndOfInputError extends ParserParsingFailedError {
|
|
60
57
|
name = 'ParserUnexpectedEndOfInputError';
|
|
61
58
|
}
|
|
59
|
+
export class ParserUnexpectedRemainingInputError extends ParserParsingFailedError {
|
|
60
|
+
name = 'ParserUnexpectedRemainingInputError';
|
|
61
|
+
}
|
|
@@ -1,20 +1,35 @@
|
|
|
1
1
|
export type ParserInputCompanion<Sequence, Element> = {
|
|
2
2
|
is(value: unknown): value is Sequence;
|
|
3
3
|
from(elements: Element[]): Sequence;
|
|
4
|
+
concat(sequences: Sequence[]): Sequence;
|
|
4
5
|
length(sequence: Sequence): number;
|
|
5
6
|
at(sequence: Sequence, index: number): Element | undefined;
|
|
7
|
+
subsequence(sequence: Sequence, start: number, end: number): Sequence;
|
|
8
|
+
indexOf(sequence: Sequence, element: Element, fromIndex?: number): number;
|
|
9
|
+
indexOfSubsequence(sequence: Sequence, subsequence: Sequence, fromIndex?: number): number;
|
|
10
|
+
equals(sequenceA: Sequence, sequenceB: Sequence): boolean;
|
|
6
11
|
};
|
|
7
12
|
export declare class StringParserInputCompanion implements ParserInputCompanion<string, string> {
|
|
8
13
|
is(value: unknown): value is string;
|
|
9
14
|
from(elements: string[]): string;
|
|
15
|
+
concat(sequences: string[]): string;
|
|
10
16
|
length(sequence: string): number;
|
|
11
17
|
at(sequence: string, index: number): string | undefined;
|
|
18
|
+
subsequence(sequence: string, start: number, end: number): string;
|
|
19
|
+
indexOf(sequence: string, element: string, fromIndex?: number): number;
|
|
20
|
+
indexOfSubsequence(sequence: string, subsequence: string, fromIndex?: number): number;
|
|
21
|
+
equals(sequenceA: string, sequenceB: string): boolean;
|
|
12
22
|
}
|
|
13
23
|
export declare const stringParserInputCompanion: StringParserInputCompanion;
|
|
14
24
|
export declare class Uint8ArrayParserInputCompanion implements ParserInputCompanion<Uint8Array, number> {
|
|
15
25
|
is(value: unknown): value is Uint8Array;
|
|
16
26
|
from(elements: number[]): Uint8Array;
|
|
27
|
+
concat(sequences: Uint8Array[]): Uint8Array;
|
|
17
28
|
length(sequence: Uint8Array): number;
|
|
18
29
|
at(sequence: Uint8Array, index: number): number | undefined;
|
|
30
|
+
subsequence(sequence: Uint8Array, start: number, end: number): Uint8Array;
|
|
31
|
+
indexOf(sequence: Uint8Array, element: number, fromIndex?: number): number;
|
|
32
|
+
indexOfSubsequence(sequence: Uint8Array, subsequence: Uint8Array, fromIndex?: number): number;
|
|
33
|
+
equals(sequenceA: Uint8Array, sequenceB: Uint8Array): boolean;
|
|
19
34
|
}
|
|
20
35
|
export declare const uint8ArrayParserInputCompanion: Uint8ArrayParserInputCompanion;
|
|
@@ -5,12 +5,27 @@ export class StringParserInputCompanion {
|
|
|
5
5
|
from(elements) {
|
|
6
6
|
return elements.join('');
|
|
7
7
|
}
|
|
8
|
+
concat(sequences) {
|
|
9
|
+
return sequences.join('');
|
|
10
|
+
}
|
|
8
11
|
length(sequence) {
|
|
9
12
|
return sequence.length;
|
|
10
13
|
}
|
|
11
14
|
at(sequence, index) {
|
|
12
15
|
return sequence.at(index);
|
|
13
16
|
}
|
|
17
|
+
subsequence(sequence, start, end) {
|
|
18
|
+
return sequence.slice(start, end);
|
|
19
|
+
}
|
|
20
|
+
indexOf(sequence, element, fromIndex) {
|
|
21
|
+
return sequence.indexOf(element, fromIndex);
|
|
22
|
+
}
|
|
23
|
+
indexOfSubsequence(sequence, subsequence, fromIndex) {
|
|
24
|
+
return sequence.indexOf(subsequence, fromIndex);
|
|
25
|
+
}
|
|
26
|
+
equals(sequenceA, sequenceB) {
|
|
27
|
+
return sequenceA === sequenceB;
|
|
28
|
+
}
|
|
14
29
|
}
|
|
15
30
|
export const stringParserInputCompanion = new StringParserInputCompanion();
|
|
16
31
|
export class Uint8ArrayParserInputCompanion {
|
|
@@ -20,11 +35,34 @@ export class Uint8ArrayParserInputCompanion {
|
|
|
20
35
|
from(elements) {
|
|
21
36
|
return new Uint8Array(elements);
|
|
22
37
|
}
|
|
38
|
+
concat(sequences) {
|
|
39
|
+
return new Uint8Array(Buffer.concat(sequences));
|
|
40
|
+
}
|
|
23
41
|
length(sequence) {
|
|
24
42
|
return sequence.length;
|
|
25
43
|
}
|
|
26
44
|
at(sequence, index) {
|
|
27
45
|
return sequence.at(index);
|
|
28
46
|
}
|
|
47
|
+
subsequence(sequence, start, end) {
|
|
48
|
+
return sequence.subarray(start, end);
|
|
49
|
+
}
|
|
50
|
+
indexOf(sequence, element, fromIndex) {
|
|
51
|
+
return sequence.indexOf(element, fromIndex);
|
|
52
|
+
}
|
|
53
|
+
indexOfSubsequence(sequence, subsequence, fromIndex) {
|
|
54
|
+
return Buffer.from(sequence).indexOf(Buffer.from(subsequence), fromIndex);
|
|
55
|
+
}
|
|
56
|
+
equals(sequenceA, sequenceB) {
|
|
57
|
+
if (sequenceA.length !== sequenceB.length) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
for (let index = 0; index < sequenceA.length; index++) {
|
|
61
|
+
if (sequenceA[index] !== sequenceB[index]) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
29
67
|
}
|
|
30
68
|
export const uint8ArrayParserInputCompanion = new Uint8ArrayParserInputCompanion();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function promiseCompose<A, B, C>(f1: (a: A) => Promise<B>, f2: (b: B) => C | Promise<C>): (a: A) => Promise<C>;
|
|
1
|
+
export declare function promiseCompose<A, B, C>(f1: (a: A) => B | Promise<B>, f2: (b: B) => C | Promise<C>): (a: A) => C | Promise<C>;
|
package/build/promiseCompose.js
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
1
|
export function promiseCompose(f1, f2) {
|
|
2
|
-
|
|
2
|
+
function promiseComposed(a) {
|
|
3
|
+
const bOrBPromise = f1(a);
|
|
4
|
+
if (bOrBPromise instanceof Promise) {
|
|
5
|
+
return bOrBPromise.then(f2);
|
|
6
|
+
}
|
|
7
|
+
return f2(bOrBPromise);
|
|
8
|
+
}
|
|
9
|
+
;
|
|
10
|
+
Object.defineProperty(promiseComposed, 'name', {
|
|
11
|
+
value: `promiseCompose(${f1.name}, ${f2.name})`,
|
|
12
|
+
});
|
|
13
|
+
return promiseComposed;
|
|
3
14
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function promiseSettled<T>(promise: Promise<T>): Promise<PromiseSettledResult<T>>;
|