diff --git a/types/parsimmon/index.d.ts b/types/parsimmon/index.d.ts index 6919296cf8..8975c596a6 100644 --- a/types/parsimmon/index.d.ts +++ b/types/parsimmon/index.d.ts @@ -182,6 +182,11 @@ declare namespace Parsimmon { * Note: parser.tie() is usually used after Parsimmon.seq(...parsers) or parser.many(). */ tie(): Parser; + /** + * When called on a parser yielding an array of strings, yields all their strings + * concatenated with the separator. Asserts that its input is actually an array of strings. + */ + tieWith(join: string): Parser; /** * expects parser zero or more times, and yields an array of the results. */ @@ -244,7 +249,7 @@ declare namespace Parsimmon { /** * Equivalent to Parsimmon.of(result). */ - of(result: U): U; + of(result: U): Parser; } /** diff --git a/types/parsimmon/parsimmon-tests.ts b/types/parsimmon/parsimmon-tests.ts index dbcf3d0753..0f2d296c5a 100644 --- a/types/parsimmon/parsimmon-tests.ts +++ b/types/parsimmon/parsimmon-tests.ts @@ -69,6 +69,7 @@ let fooReply: Reply; fooReply = P.makeSuccess(0, foo); fooReply = P.makeFailure(0, ''); +fooReply = P.makeFailure(0, ['', '']); fooPar = P((input: string, i: number) => P.makeSuccess(0, foo)); fooPar = P.Parser((input: string, i: number) => P.makeSuccess(0, foo)); @@ -97,10 +98,27 @@ barPar = fooPar.map((f) => { return bar; }); +strPar = P.string(str); + +strPar = strPar.contramap((f) => { + f; // $ExpectType string + return f.toUpperCase(); +}); + +barPar = strPar.promap((f) => { + f; // $ExpectType string + return 3; +}, (f) => { + f; // $ExpectType number + return bar; +}); + // -- -- -- -- -- -- -- -- -- -- -- -- -- fooPar = fooPar.skip(barPar); +fooPar = fooPar.wrap(barPar, strPar); + barPar = barPar = fooPar.result(bar); fooOrBarPar = fooPar.fallback(bar); @@ -116,6 +134,7 @@ fooArrPar = fooPar.atLeast(num); fooMarkPar = fooPar.mark(); fooPar = fooPar.desc(str); +fooPar = fooPar.desc([str, str]); // -- -- -- -- -- -- -- -- -- -- -- -- -- @@ -128,6 +147,19 @@ fooArrPar = P.seq(fooPar, fooPar); const par: Parser<[Bar, Foo, number]> = P.seq(barPar, fooPar, numPar); const par2: Parser = P.seq(barPar, fooPar, numPar).map(([a, b, c]: [Bar, Foo, number]) => 42); +interface SeqObj { + first: number; + second: string; + third: Foo; +} + +const seqObjPar: Parser = P.seqObj( + ['first', numPar], + barPar, + fooArrPar, + ['third', fooPar], + ['second', strPar]); + fooPar = P.custom((success, failure) => (stream, i) => { str = stream; num = i; return success(num, foo); }); fooPar = P.custom((success, failure) => (stream, i) => failure(num, str)); @@ -141,6 +173,36 @@ fooPar = P.lazy(() => { voidPar = P.fail(str); fooPar = P.fail(str); +fooPar = P.empty(); // $ExpectType Parser + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +const bytePar: Parser = P.byte(3); + +const byteParMany: Parser = P.bitSeq([1, 2, 5, 1]); + +interface ByteSeqObj { + first: number; + second: number; + third: number; +} + +const byteParObj: Parser = P.bitSeqObj([ + ['first', 3], + 6, + ['second', 8], + 7, + ['third', 9], +]); + +const byteParObjErr: Parser = P.bitSeqObj([ // $ExpectError + ['first', 3], + 6, + ['second', 8], + 7, + /* missing 'third' key */ +]); + // -- -- -- -- -- -- -- -- -- -- -- -- -- strPar = P.letter; @@ -152,6 +214,12 @@ strPar = P.digits; strPar = P.whitespace; strPar = P.optWhitespace; +strPar = P.cr; +strPar = P.lf; +strPar = P.crlf; +strPar = P.newline; +const voidOrStrPar: Parser = P.end; + strPar = P.any; strPar = P.all; voidPar = P.eof; @@ -166,6 +234,8 @@ bool = P.isParser(42); strPar = P.oneOf('a'); strPar = P.noneOf('a'); +strPar = P.range('a', 'z'); + strPar = P.regex(/foo/); strPar = P.regex(/foo/, 3); strPar = P.regexp(/bar/); @@ -176,6 +246,10 @@ emptyStrPar = P.lookahead(str); emptyStrPar = P.lookahead(/foo/); emptyStrPar = P.lookahead(fooPar); +strPar = strPar.tie(); + +strPar = strPar.tieWith(""); + fooPar = P.of(foo); str = P.formatError('foo', strPar.parse('bar')); @@ -214,6 +288,22 @@ function makeNode(name: Name) { let node: P.Parser> = P.letters.node('identifier'); node = P.letters.thru(makeNode('identifier')); +// -- -- -- -- -- -- -- -- -- -- -- -- -- +// Fantasy Land support + +fooPar = fooPar.empty(); // $ExpectType Parser + +// example taken from the documentation for the #ap method +numPar = P.digit + .ap(P.digit + .map(s => (t: string) => + Number(s) + Number(t))); + +fooArrPar = fooPar.sepBy(barPar); +fooArrPar = fooPar.sepBy1(barPar); + +fooPar = barPar.of(foo); + // -- -- -- -- -- -- -- -- -- -- -- -- -- let language: Language;