@futpib/parser 1.0.0 → 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} +7 -7
- package/build/androidPackageUnparser.d.ts +4 -0
- package/build/androidPackageUnparser.js +90 -0
- package/build/androidPackageUnparser.test.js +26 -0
- package/build/arbitrarilySlicedAsyncInterable.d.ts +3 -1
- package/build/arbitrarilySlicedAsyncInterable.js +3 -3
- 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 +4 -4
- package/build/arrayUnparser.d.ts +2 -0
- package/build/arrayUnparser.js +8 -0
- package/build/backsmali.d.ts +1 -0
- package/build/backsmali.js +22 -0
- package/build/bsonParser.js +6 -2
- package/build/bsonParser.test.js +2 -2
- package/build/{parserInvariant.d.ts → customInvariant.d.ts} +1 -1
- package/build/{parserInvariant.js → customInvariant.js} +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.d.ts +1 -1
- package/build/debugLogParser.js +15 -4
- package/build/disjunctionParser.d.ts +2 -1
- package/build/disjunctionParser.js +4 -2
- package/build/elementParser.d.ts +2 -2
- package/build/elementParser.js +1 -1
- 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/endOfInputParser.d.ts +2 -2
- package/build/exactElementParser.d.ts +1 -1
- 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/index.d.ts +5 -2
- package/build/index.js +3 -0
- package/build/inputReader.d.ts +14 -3
- package/build/inputReader.js +43 -6
- package/build/inputReader.test.js +172 -7
- 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.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/jsonParser.d.ts +2 -0
- package/build/jsonParser.js +13 -15
- package/build/jsonParser.test.js +2 -2
- package/build/leb128Parser.d.ts +7 -0
- package/build/leb128Parser.js +82 -0
- package/build/leb128Parser.test.d.ts +1 -0
- package/build/leb128Parser.test.js +107 -0
- package/build/listParser.d.ts +1 -1
- package/build/listParser.js +5 -5
- package/build/lookaheadParser.d.ts +2 -0
- package/build/lookaheadParser.js +14 -0
- package/build/{negativeLookahead.d.ts → negativeLookaheadParser.d.ts} +1 -1
- package/build/negativeLookaheadParser.js +24 -0
- package/build/negativeLookaheadParser.test.d.ts +1 -0
- package/build/negativeLookaheadParser.test.js +30 -0
- package/build/nonEmptyArrayParser.d.ts +2 -0
- package/build/nonEmptyArrayParser.js +32 -0
- package/build/nonEmptyArrayParser.test.d.ts +1 -0
- package/build/nonEmptyArrayParser.test.js +16 -0
- package/build/optionalParser.d.ts +1 -1
- package/build/optionalParser.js +2 -2
- package/build/parser.d.ts +13 -4
- package/build/parser.js +67 -32
- package/build/parser.test.js +94 -27
- package/build/parserAccessorParser.d.ts +1 -1
- package/build/parserAccessorParser.js +9 -1
- package/build/parserConsumedSequenceParser.d.ts +2 -0
- package/build/parserConsumedSequenceParser.js +22 -0
- package/build/parserContext.d.ts +20 -6
- package/build/parserContext.js +70 -40
- package/build/parserContext.test.js +34 -7
- package/build/parserCreatorCompose.d.ts +4 -3
- package/build/parserCreatorCompose.js +9 -3
- package/build/parserError.d.ts +6 -0
- package/build/parserError.js +6 -6
- package/build/parserImplementationInvariant.d.ts +1 -1
- package/build/parserImplementationInvariant.js +2 -2
- package/build/parserInputCompanion.d.ts +35 -0
- package/build/parserInputCompanion.js +68 -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/quantifierParser.d.ts +2 -0
- package/build/quantifierParser.js +17 -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 +13 -3
- package/build/sequenceBuffer.js +60 -8
- package/build/sequenceBuffer.test.js +59 -2
- 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/sequenceUnparser.d.ts +2 -0
- package/build/sequenceUnparser.js +6 -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 +2 -2
- package/build/sliceBoundedParser.js +8 -3
- package/build/sliceBoundedParser.test.js +37 -3
- 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 +4 -2
- package/build/terminatedArrayParser.js +82 -5
- 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/uint8Array.d.ts +1 -0
- package/build/uint8Array.js +7 -0
- 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/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/unparserOutputCompanion.d.ts +15 -0
- package/build/unparserOutputCompanion.js +13 -0
- package/build/zip.d.ts +9 -17
- package/build/zipParser.d.ts +20 -12
- package/build/zipParser.js +83 -71
- package/build/zipParser.test.js +2 -7
- package/build/zipUnparser.d.ts +8 -0
- package/build/zipUnparser.js +200 -0
- package/build/zipUnparser.test.d.ts +1 -0
- package/build/zipUnparser.test.js +80 -0
- package/package.json +26 -8
- 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} +7 -8
- package/src/androidPackageParser.test.ts.md +271 -0
- 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/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 +4 -4
- package/src/arrayParser.ts +3 -2
- package/src/arrayUnparser.ts +13 -0
- package/src/backsmali.ts +30 -0
- package/src/bsonParser.test.ts +2 -2
- package/src/bsonParser.ts +16 -5
- package/src/{parserInvariant.ts → customInvariant.ts} +2 -2
- 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 +20 -4
- package/src/disjunctionParser.ts +10 -5
- package/src/elementParser.ts +3 -3
- 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/endOfInputParser.ts +4 -4
- package/src/exactElementParser.ts +18 -12
- package/src/exactSequenceParser.ts +24 -3
- 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/index.ts +15 -2
- package/src/inputReader.test.ts +216 -7
- package/src/inputReader.ts +80 -5
- package/src/inputReaderState.ts +33 -0
- package/src/inspect.ts +9 -0
- package/src/javaKeyStore.ts +0 -0
- package/src/javaKeyStoreParser.test.ts +22 -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 +23 -34
- package/src/leb128Parser.test.ts +171 -0
- package/src/leb128Parser.ts +125 -0
- package/src/listParser.ts +6 -6
- package/src/lookaheadParser.ts +19 -0
- package/src/negativeLookaheadParser.test.ts +53 -0
- package/src/negativeLookaheadParser.ts +36 -0
- package/src/nonEmptyArrayParser.test.ts +20 -0
- package/src/nonEmptyArrayParser.ts +44 -0
- package/src/optionalParser.ts +4 -3
- package/src/parser.test.ts +148 -27
- package/src/parser.test.ts.md +21 -21
- package/src/parser.test.ts.snap +0 -0
- package/src/parser.ts +153 -49
- package/src/parserAccessorParser.ts +12 -2
- package/src/parserConsumedSequenceParser.ts +29 -0
- package/src/parserContext.test.ts +38 -7
- package/src/parserContext.ts +127 -51
- package/src/parserCreatorCompose.ts +25 -7
- package/src/parserError.ts +9 -6
- package/src/parserImplementationInvariant.ts +2 -2
- package/src/parserInputCompanion.ts +102 -0
- package/src/promiseCompose.ts +17 -3
- package/src/promiseSettled.ts +6 -0
- package/src/quantifierParser.ts +25 -0
- package/src/separatedArrayParser.test.ts +34 -0
- package/src/separatedArrayParser.ts +55 -0
- package/src/sequenceBuffer.test.ts +72 -2
- package/src/sequenceBuffer.ts +93 -7
- package/src/sequenceTerminatedSequenceParser.test.ts +60 -0
- package/src/sequenceTerminatedSequenceParser.ts +62 -0
- package/src/sequenceUnparser.ts +9 -0
- package/src/skipParser.ts +7 -5
- package/src/skipToParser.ts +16 -0
- package/src/sliceBoundedParser.test.ts +45 -3
- package/src/sliceBoundedParser.ts +21 -3
- 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 +111 -6
- package/src/toAsyncIterable.ts +7 -0
- package/src/toAsyncIterator.ts +48 -0
- package/src/tupleParser.ts +8 -5
- package/src/uint8Array.ts +10 -0
- package/src/unionParser.test.ts +79 -0
- package/src/unionParser.ts +44 -16
- 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 +223 -146
- package/src/zipUnparser.test.ts +119 -0
- package/src/zipUnparser.ts +300 -0
- package/build/apk.d.ts +0 -13
- package/build/apkParser.d.ts +0 -3
- package/build/apkParser.js +0 -135
- 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/jsonParser2.d.ts +0 -3
- package/build/jsonParser2.js +0 -52
- package/build/jsonParser2.test.js +0 -22
- package/build/negativeLookahead.js +0 -20
- 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/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/zipEntry.d.ts +0 -28
- package/build/zipFile.d.ts +0 -32
- package/build/zipFileEntry.d.ts +0 -6
- package/src/apk.ts +0 -16
- package/src/apkParser.test.ts.md +0 -268
- package/src/apkParser.test.ts.snap +0 -0
- package/src/apkParser.ts +0 -327
- package/src/inputCompanion.ts +0 -43
- package/src/invariantDefined.ts +0 -6
- package/src/invariantIdentity.ts +0 -8
- package/src/negativeLookahead.ts +0 -26
- /package/build/{apk.js → androidPackage.js} +0 -0
- /package/build/{apkParser.test.d.ts → androidPackageParser.test.d.ts} +0 -0
- /package/build/{arbitraryDosPermissions.d.ts → androidPackageUnparser.test.d.ts} +0 -0
- /package/build/{arbitraryDosPermissions.js → dalvikExecutableParser.test.d.ts} +0 -0
- /package/build/{inputChunkBuffer.test.d.ts → dalvikExecutableParserAgainstSmaliParser.test.d.ts} +0 -0
- /package/build/{jsonParser2.test.d.ts → elementTerminatedArrayParser.test.d.ts} +0 -0
- /package/build/{parserParsingInvariant.d.ts → elementTerminatedSequenceArrayParser.test.d.ts} +0 -0
- /package/build/{parserParsingInvariant.js → elementTerminatedSequenceParser.test.d.ts} +0 -0
- /package/build/{zipEntry.js → fixedLengthSequenceParser.test.d.ts} +0 -0
- /package/build/{zipFile.js → javaKeyStore.d.ts} +0 -0
- /package/build/{zipFileEntry.js → javaKeyStore.js} +0 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { getParserName, setParserName, type Parser } from './parser.js';
|
|
2
|
+
import { ParserParsingFailedError } from './parserError.js';
|
|
3
|
+
|
|
4
|
+
export const createNegativeLookaheadParser = <Sequence>(
|
|
5
|
+
childParser: Parser<unknown, Sequence>,
|
|
6
|
+
): Parser<void, Sequence> => {
|
|
7
|
+
const negativeLookaheadParser: Parser<void, Sequence> = async parserContext => {
|
|
8
|
+
const childParserContext = parserContext.lookahead();
|
|
9
|
+
|
|
10
|
+
let childParserSuccess: boolean;
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
await childParser(childParserContext);
|
|
14
|
+
|
|
15
|
+
childParserSuccess = true;
|
|
16
|
+
} catch (error) {
|
|
17
|
+
if (!(error instanceof ParserParsingFailedError)) {
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
childParserSuccess = false;
|
|
22
|
+
} finally {
|
|
23
|
+
childParserContext.dispose();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
parserContext.invariant(
|
|
27
|
+
!childParserSuccess,
|
|
28
|
+
'Negative lookahead assertion failed for child parser %s.',
|
|
29
|
+
getParserName(childParser),
|
|
30
|
+
);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
setParserName(negativeLookaheadParser, `(?!${getParserName(childParser)})`);
|
|
34
|
+
|
|
35
|
+
return negativeLookaheadParser;
|
|
36
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import test from 'ava';
|
|
2
|
+
import { createNonEmptyArrayParser } from './nonEmptyArrayParser.js';
|
|
3
|
+
import { type Parser, runParser } from './parser.js';
|
|
4
|
+
import { stringParserInputCompanion } from './parserInputCompanion.js';
|
|
5
|
+
import { createExactElementParser } from './exactElementParser.js';
|
|
6
|
+
|
|
7
|
+
test('empty array does not match', async t => {
|
|
8
|
+
const parser: Parser<string[], string> = createNonEmptyArrayParser(createExactElementParser('0'));
|
|
9
|
+
|
|
10
|
+
await t.throwsAsync(() => runParser(parser, '', stringParserInputCompanion), {
|
|
11
|
+
message: /Expected .* to match at least once/,
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test('non-empty array matches', async t => {
|
|
16
|
+
const parser: Parser<string[], string> = createNonEmptyArrayParser(createExactElementParser('0'));
|
|
17
|
+
const result = await runParser(parser, '0', stringParserInputCompanion);
|
|
18
|
+
|
|
19
|
+
t.deepEqual(result, ['0']);
|
|
20
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { getParserName, type Parser, setParserName } from './parser.js';
|
|
2
|
+
import { ParserParsingFailedError } from './parserError.js';
|
|
3
|
+
|
|
4
|
+
export const createNonEmptyArrayParser = <ElementOutput, Sequence>(
|
|
5
|
+
elementParser: Parser<ElementOutput, Sequence>,
|
|
6
|
+
): Parser<ElementOutput[], Sequence> => {
|
|
7
|
+
const nonEmptyArrayParser: Parser<ElementOutput[], Sequence> = async parserContext => {
|
|
8
|
+
const elements: ElementOutput[] = [];
|
|
9
|
+
|
|
10
|
+
while (true) {
|
|
11
|
+
const elementParserContext = parserContext.lookahead();
|
|
12
|
+
const initialPosition = elementParserContext.position;
|
|
13
|
+
try {
|
|
14
|
+
const element = await elementParser(elementParserContext);
|
|
15
|
+
if (elementParserContext.position === initialPosition) {
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
elements.push(element);
|
|
20
|
+
elementParserContext.unlookahead();
|
|
21
|
+
} catch (error) {
|
|
22
|
+
if (error instanceof ParserParsingFailedError) {
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
throw error;
|
|
27
|
+
} finally {
|
|
28
|
+
elementParserContext.dispose();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
parserContext.invariant(
|
|
33
|
+
elements.length > 0,
|
|
34
|
+
'Expected elementParser (%s) to match at least once',
|
|
35
|
+
getParserName(elementParser, 'anonymousNonEmptyArrayChild'),
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
return elements;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
setParserName(nonEmptyArrayParser, getParserName(elementParser, 'anonymousNonEmptyArrayChild') + '+');
|
|
42
|
+
|
|
43
|
+
return nonEmptyArrayParser;
|
|
44
|
+
};
|
package/src/optionalParser.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { getParserName, Parser, setParserName } from
|
|
2
|
-
import { ParserParsingFailedError } from
|
|
1
|
+
import { getParserName, type Parser, setParserName } from './parser.js';
|
|
2
|
+
import { ParserParsingFailedError } from './parserError.js';
|
|
3
3
|
|
|
4
4
|
export const createOptionalParser = <Output, Sequence>(
|
|
5
5
|
childParser: Parser<Output, Sequence>,
|
|
6
6
|
): Parser<undefined | Output, Sequence> => {
|
|
7
7
|
const optionalParser: Parser<undefined | Output, Sequence> = async parserContext => {
|
|
8
8
|
const childParserContext = parserContext.lookahead();
|
|
9
|
+
|
|
9
10
|
try {
|
|
10
11
|
const value = await childParser(childParserContext);
|
|
11
12
|
childParserContext.unlookahead();
|
|
@@ -24,4 +25,4 @@ export const createOptionalParser = <Output, Sequence>(
|
|
|
24
25
|
setParserName(optionalParser, getParserName(childParser, 'anonymousOptionalChild') + '?');
|
|
25
26
|
|
|
26
27
|
return optionalParser;
|
|
27
|
-
}
|
|
28
|
+
};
|
package/src/parser.test.ts
CHANGED
|
@@ -1,34 +1,44 @@
|
|
|
1
1
|
import test from 'ava';
|
|
2
|
+
import invariant from 'invariant';
|
|
2
3
|
import { createUnionParser } from './unionParser.js';
|
|
3
|
-
import { Parser, runParser } from './parser.js';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { type Parser, runParser, runParserWithRemainingInput } from './parser.js';
|
|
5
|
+
import { stringParserInputCompanion, uint8ArrayParserInputCompanion } from './parserInputCompanion.js';
|
|
6
|
+
import {
|
|
7
|
+
ParserError,
|
|
8
|
+
ParserParsingInvariantError,
|
|
9
|
+
ParserParsingJoinAllError,
|
|
10
|
+
ParserParsingJoinDeepestError,
|
|
11
|
+
ParserParsingJoinError,
|
|
12
|
+
ParserParsingJoinFurthestError,
|
|
13
|
+
ParserParsingJoinNoneError,
|
|
14
|
+
ParserUnexpectedRemainingInputError,
|
|
15
|
+
} from './parserError.js';
|
|
6
16
|
import { createTupleParser } from './tupleParser.js';
|
|
7
17
|
import { promiseCompose } from './promiseCompose.js';
|
|
8
18
|
import { createDisjunctionParser } from './disjunctionParser.js';
|
|
9
|
-
import
|
|
10
|
-
import { createExactSequenceParser } from './exactSequenceParser.js';
|
|
19
|
+
import { createExactSequenceNaiveParser } from './exactSequenceParser.js';
|
|
11
20
|
import { createArrayParser } from './arrayParser.js';
|
|
12
21
|
import { createElementParser } from './elementParser.js';
|
|
22
|
+
import { toAsyncIterable } from './toAsyncIterable.js';
|
|
13
23
|
|
|
14
24
|
const aUnionParser = createUnionParser<string, string>([
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
25
|
+
createExactSequenceNaiveParser('1'),
|
|
26
|
+
createExactSequenceNaiveParser('aa'),
|
|
27
|
+
createExactSequenceNaiveParser('AAA'),
|
|
18
28
|
]);
|
|
19
29
|
|
|
20
30
|
const abDisjunctionParser = createDisjunctionParser<string, string>([
|
|
21
31
|
aUnionParser,
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
32
|
+
createExactSequenceNaiveParser('2'),
|
|
33
|
+
createExactSequenceNaiveParser('bb'),
|
|
34
|
+
createExactSequenceNaiveParser('BBB'),
|
|
25
35
|
]);
|
|
26
36
|
|
|
27
37
|
const abcUnionParser = createUnionParser<string, string>([
|
|
28
38
|
abDisjunctionParser,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
39
|
+
createExactSequenceNaiveParser('final_3'),
|
|
40
|
+
createExactSequenceNaiveParser('final_cc'),
|
|
41
|
+
createExactSequenceNaiveParser('final_CCC'),
|
|
32
42
|
]);
|
|
33
43
|
|
|
34
44
|
const sampleParser = promiseCompose(
|
|
@@ -44,6 +54,16 @@ async function * asyncIteratorFromString(string: string) {
|
|
|
44
54
|
yield string;
|
|
45
55
|
}
|
|
46
56
|
|
|
57
|
+
async function stringFromAsyncIterable(asyncIterable: AsyncIterable<string>) {
|
|
58
|
+
let string = '';
|
|
59
|
+
|
|
60
|
+
for await (const chunk of asyncIterable) {
|
|
61
|
+
string += chunk;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return string;
|
|
65
|
+
}
|
|
66
|
+
|
|
47
67
|
function sortChildErrors(error: ParserParsingJoinNoneError) {
|
|
48
68
|
error.childErrors.sort((a, b) => a.message.localeCompare(b.message));
|
|
49
69
|
|
|
@@ -55,14 +75,14 @@ function sortChildErrors(error: ParserParsingJoinNoneError) {
|
|
|
55
75
|
}
|
|
56
76
|
|
|
57
77
|
function removeStackLocations(errorStack: string) {
|
|
58
|
-
return errorStack.
|
|
78
|
+
return errorStack.replaceAll(/((at [^\n]+)[\s\n]+)+(at [^\n]+)/g, 'at [LOCATIONS]');
|
|
59
79
|
}
|
|
60
80
|
|
|
61
81
|
test('errorJoinMode: none', async t => {
|
|
62
|
-
const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'),
|
|
82
|
+
const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'), stringParserInputCompanion, {
|
|
63
83
|
errorJoinMode: 'none',
|
|
64
84
|
}), {
|
|
65
|
-
instanceOf: ParserParsingJoinNoneError
|
|
85
|
+
instanceOf: ParserParsingJoinNoneError,
|
|
66
86
|
});
|
|
67
87
|
|
|
68
88
|
t.is(error.position, 12);
|
|
@@ -72,10 +92,10 @@ test('errorJoinMode: none', async t => {
|
|
|
72
92
|
});
|
|
73
93
|
|
|
74
94
|
test('errorJoinMode: all', async t => {
|
|
75
|
-
const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'),
|
|
95
|
+
const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'), stringParserInputCompanion, {
|
|
76
96
|
errorJoinMode: 'all',
|
|
77
97
|
}), {
|
|
78
|
-
instanceOf: ParserParsingJoinAllError
|
|
98
|
+
instanceOf: ParserParsingJoinAllError,
|
|
79
99
|
});
|
|
80
100
|
|
|
81
101
|
sortChildErrors(error);
|
|
@@ -107,7 +127,7 @@ test('errorJoinMode: all', async t => {
|
|
|
107
127
|
});
|
|
108
128
|
|
|
109
129
|
test('errorJoinMode: deepest', async t => {
|
|
110
|
-
const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'),
|
|
130
|
+
const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'), stringParserInputCompanion, {
|
|
111
131
|
errorJoinMode: 'deepest',
|
|
112
132
|
}), {
|
|
113
133
|
instanceOf: ParserParsingJoinDeepestError,
|
|
@@ -142,7 +162,7 @@ test('errorJoinMode: deepest', async t => {
|
|
|
142
162
|
});
|
|
143
163
|
|
|
144
164
|
test('errorJoinMode: furthest', async t => {
|
|
145
|
-
const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'),
|
|
165
|
+
const error = await t.throwsAsync(runParser(sampleParser, asyncIteratorFromString('1bbfinal_CC!'), stringParserInputCompanion, {
|
|
146
166
|
errorJoinMode: 'furthest',
|
|
147
167
|
}), {
|
|
148
168
|
instanceOf: ParserParsingJoinFurthestError,
|
|
@@ -162,15 +182,116 @@ test('errorJoinMode: furthest', async t => {
|
|
|
162
182
|
t.is(error1.position, 12);
|
|
163
183
|
});
|
|
164
184
|
|
|
165
|
-
test('throws on
|
|
185
|
+
test('throws on parserInputCompanion type mismatch', async t => {
|
|
166
186
|
const anythingParser: Parser<any, any> = createArrayParser(createElementParser());
|
|
167
187
|
|
|
168
|
-
await runParser(anythingParser, asyncIteratorFromString('anything'),
|
|
169
|
-
await runParser(anythingParser, 'anything',
|
|
170
|
-
await runParser(anythingParser, Buffer.from('anything'),
|
|
171
|
-
await runParser(anythingParser, new Uint8Array([ 1, 2, 3 ]),
|
|
188
|
+
await runParser(anythingParser, asyncIteratorFromString('anything'), stringParserInputCompanion);
|
|
189
|
+
await runParser(anythingParser, 'anything', stringParserInputCompanion);
|
|
190
|
+
await runParser(anythingParser, Buffer.from('anything'), uint8ArrayParserInputCompanion);
|
|
191
|
+
await runParser(anythingParser, new Uint8Array([ 1, 2, 3 ]), uint8ArrayParserInputCompanion);
|
|
172
192
|
|
|
173
|
-
await t.throwsAsync(runParser(anythingParser, asyncIteratorFromString('anything'),
|
|
193
|
+
await t.throwsAsync(runParser(anythingParser, asyncIteratorFromString('anything'), uint8ArrayParserInputCompanion), {
|
|
174
194
|
message: /input companion/,
|
|
175
195
|
});
|
|
176
196
|
});
|
|
197
|
+
|
|
198
|
+
test('thrown error has input reader state', async t => {
|
|
199
|
+
const error = await t.throwsAsync(
|
|
200
|
+
runParser(
|
|
201
|
+
createTupleParser([
|
|
202
|
+
createExactSequenceNaiveParser('foo'),
|
|
203
|
+
createExactSequenceNaiveParser('bar'),
|
|
204
|
+
]),
|
|
205
|
+
(async function * () {
|
|
206
|
+
yield 'foo';
|
|
207
|
+
yield 'qux';
|
|
208
|
+
yield 'bar';
|
|
209
|
+
})(),
|
|
210
|
+
stringParserInputCompanion,
|
|
211
|
+
),
|
|
212
|
+
{
|
|
213
|
+
instanceOf: ParserError,
|
|
214
|
+
},
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
t.is(error.position, 4);
|
|
218
|
+
|
|
219
|
+
invariant(error.inputReaderSate, 'error.inputReaderSate');
|
|
220
|
+
|
|
221
|
+
const {
|
|
222
|
+
consumedBufferedSequences,
|
|
223
|
+
unconsumedBufferedSequences,
|
|
224
|
+
unbufferedSequences,
|
|
225
|
+
position,
|
|
226
|
+
...inputReaderStateRest
|
|
227
|
+
} = error.inputReaderSate;
|
|
228
|
+
|
|
229
|
+
t.is(position, 4);
|
|
230
|
+
t.deepEqual(inputReaderStateRest, {});
|
|
231
|
+
|
|
232
|
+
const unbufferedSequencesArray = [];
|
|
233
|
+
|
|
234
|
+
if (unbufferedSequences) {
|
|
235
|
+
for await (const sequence of toAsyncIterable(unbufferedSequences)) {
|
|
236
|
+
unbufferedSequencesArray.push(sequence);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
t.deepEqual(consumedBufferedSequences, [ 'q' ]);
|
|
241
|
+
t.deepEqual(unconsumedBufferedSequences, [ 'ux' ]);
|
|
242
|
+
t.deepEqual(unbufferedSequencesArray, [ 'bar' ]);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test('runParser throws with remaining input', async t => {
|
|
246
|
+
const parser: Parser<string, string> = createExactSequenceNaiveParser('foo');
|
|
247
|
+
const error = await t.throwsAsync(() => runParser(parser, 'foobar', stringParserInputCompanion), {
|
|
248
|
+
instanceOf: ParserUnexpectedRemainingInputError,
|
|
249
|
+
message: /remaining input/,
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
t.is(error.position, 3);
|
|
253
|
+
|
|
254
|
+
const {
|
|
255
|
+
position,
|
|
256
|
+
consumedBufferedSequences,
|
|
257
|
+
unconsumedBufferedSequences,
|
|
258
|
+
unbufferedSequences,
|
|
259
|
+
...inputReaderStateRest
|
|
260
|
+
} = error.inputReaderSate!;
|
|
261
|
+
|
|
262
|
+
t.deepEqual(inputReaderStateRest, {});
|
|
263
|
+
t.is(position, 3);
|
|
264
|
+
t.deepEqual(consumedBufferedSequences, [ 'foo' ]);
|
|
265
|
+
t.deepEqual(unconsumedBufferedSequences, [ 'bar' ]);
|
|
266
|
+
t.truthy(unbufferedSequences);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
test('runParser does not throw without remaining input', async t => {
|
|
270
|
+
const parser: Parser<string, string> = createExactSequenceNaiveParser('foo');
|
|
271
|
+
const output = await runParser(parser, 'foo', stringParserInputCompanion);
|
|
272
|
+
|
|
273
|
+
t.deepEqual(output, 'foo');
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test('runParserWithRemainingInput with remaining input', async t => {
|
|
277
|
+
const parser: Parser<string, string> = createExactSequenceNaiveParser('foo');
|
|
278
|
+
const {
|
|
279
|
+
output,
|
|
280
|
+
remainingInput,
|
|
281
|
+
position,
|
|
282
|
+
...resultRest
|
|
283
|
+
} = await runParserWithRemainingInput(parser, 'foobar', stringParserInputCompanion);
|
|
284
|
+
|
|
285
|
+
t.deepEqual(resultRest, {});
|
|
286
|
+
t.deepEqual(output, 'foo');
|
|
287
|
+
t.deepEqual(await stringFromAsyncIterable(remainingInput!), 'bar');
|
|
288
|
+
t.is(position, 3);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
test('runParserWithRemainingInput without remaining input', async t => {
|
|
292
|
+
const parser: Parser<string, string> = createExactSequenceNaiveParser('foo');
|
|
293
|
+
const { output, remainingInput } = await runParserWithRemainingInput(parser, 'foo', stringParserInputCompanion);
|
|
294
|
+
|
|
295
|
+
t.deepEqual(output, 'foo');
|
|
296
|
+
t.is(remainingInput, undefined);
|
|
297
|
+
});
|
package/src/parser.test.ts.md
CHANGED
|
@@ -8,32 +8,32 @@ Generated by [AVA](https://avajs.dev).
|
|
|
8
8
|
|
|
9
9
|
> Snapshot 1
|
|
10
10
|
|
|
11
|
-
`ParserParsingJoinNoneError:
|
|
11
|
+
`ParserParsingJoinNoneError: No union child parser succeeded.␊
|
|
12
12
|
at [LOCATIONS]`
|
|
13
13
|
|
|
14
14
|
## errorJoinMode: all
|
|
15
15
|
|
|
16
16
|
> Snapshot 1
|
|
17
17
|
|
|
18
|
-
`ParserParsingJoinAllError:
|
|
18
|
+
`ParserParsingJoinAllError: No union child parser succeeded.␊
|
|
19
19
|
Child error stacks, indented:␊
|
|
20
|
-
ParserParsingInvariantError:
|
|
21
|
-
ParserParsingInvariantError:
|
|
22
|
-
ParserParsingJoinAllError:
|
|
20
|
+
ParserParsingInvariantError: Expected "3", got "C", at [LOCATIONS]␊
|
|
21
|
+
ParserParsingInvariantError: Expected "c", got "C", at [LOCATIONS]␊
|
|
22
|
+
ParserParsingJoinAllError: No disjunction child parser succeeded.␊
|
|
23
23
|
Child error stacks, indented:␊
|
|
24
|
-
ParserParsingJoinAllError:
|
|
24
|
+
ParserParsingJoinAllError: No union child parser succeeded.␊
|
|
25
25
|
Child error stacks, indented:␊
|
|
26
|
-
ParserParsingInvariantError:
|
|
27
|
-
ParserParsingInvariantError:
|
|
28
|
-
ParserParsingInvariantError:
|
|
26
|
+
ParserParsingInvariantError: Expected "1", got "f", at [LOCATIONS]␊
|
|
27
|
+
ParserParsingInvariantError: Expected "a", got "f", at [LOCATIONS]␊
|
|
28
|
+
ParserParsingInvariantError: Expected "A", got "f", at [LOCATIONS]␊
|
|
29
29
|
End of child error stacks␊
|
|
30
30
|
at [LOCATIONS]␊
|
|
31
|
-
ParserParsingInvariantError:
|
|
32
|
-
ParserParsingInvariantError:
|
|
33
|
-
ParserParsingInvariantError:
|
|
31
|
+
ParserParsingInvariantError: Expected "2", got "f", at [LOCATIONS]␊
|
|
32
|
+
ParserParsingInvariantError: Expected "b", got "f", at [LOCATIONS]␊
|
|
33
|
+
ParserParsingInvariantError: Expected "B", got "f", at [LOCATIONS]␊
|
|
34
34
|
End of child error stacks␊
|
|
35
35
|
at [LOCATIONS]␊
|
|
36
|
-
ParserParsingInvariantError:
|
|
36
|
+
ParserParsingInvariantError: Expected "C", got "!", at [LOCATIONS]␊
|
|
37
37
|
End of child error stacks␊
|
|
38
38
|
at [LOCATIONS]`
|
|
39
39
|
|
|
@@ -41,15 +41,15 @@ Generated by [AVA](https://avajs.dev).
|
|
|
41
41
|
|
|
42
42
|
> Snapshot 1
|
|
43
43
|
|
|
44
|
-
`ParserParsingJoinDeepestError:
|
|
44
|
+
`ParserParsingJoinDeepestError: No union child parser succeeded.␊
|
|
45
45
|
Deepest child error stacks, indented:␊
|
|
46
|
-
ParserParsingJoinDeepestError:
|
|
46
|
+
ParserParsingJoinDeepestError: No disjunction child parser succeeded.␊
|
|
47
47
|
Deepest child error stacks, indented:␊
|
|
48
|
-
ParserParsingJoinDeepestError:
|
|
48
|
+
ParserParsingJoinDeepestError: No union child parser succeeded.␊
|
|
49
49
|
Deepest child error stacks, indented:␊
|
|
50
|
-
ParserParsingInvariantError:
|
|
51
|
-
ParserParsingInvariantError:
|
|
52
|
-
ParserParsingInvariantError:
|
|
50
|
+
ParserParsingInvariantError: Expected "1", got "f", at [LOCATIONS]␊
|
|
51
|
+
ParserParsingInvariantError: Expected "a", got "f", at [LOCATIONS]␊
|
|
52
|
+
ParserParsingInvariantError: Expected "A", got "f", at [LOCATIONS]␊
|
|
53
53
|
End of deepest child error stacks␊
|
|
54
54
|
at [LOCATIONS]␊
|
|
55
55
|
End of deepest child error stacks␊
|
|
@@ -61,8 +61,8 @@ Generated by [AVA](https://avajs.dev).
|
|
|
61
61
|
|
|
62
62
|
> Snapshot 1
|
|
63
63
|
|
|
64
|
-
`ParserParsingJoinFurthestError:
|
|
64
|
+
`ParserParsingJoinFurthestError: No union child parser succeeded.␊
|
|
65
65
|
Furthest child error stacks, indented:␊
|
|
66
|
-
ParserParsingInvariantError:
|
|
66
|
+
ParserParsingInvariantError: Expected "C", got "!", at [LOCATIONS]␊
|
|
67
67
|
End of furthest child error stacks␊
|
|
68
68
|
at [LOCATIONS]`
|
package/src/parser.test.ts.snap
CHANGED
|
Binary file
|