[parsimmon] Add tests for 1.10.0 changes

This commit is contained in:
Jonathan
2018-06-18 22:31:03 +01:00
parent 9f1a0e5500
commit 2df1d871a5
2 changed files with 96 additions and 1 deletions
+6 -1
View File
@@ -182,6 +182,11 @@ declare namespace Parsimmon {
* Note: parser.tie() is usually used after Parsimmon.seq(...parsers) or parser.many().
*/
tie(): Parser<string>;
/**
* 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<string>;
/**
* 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<U>(result: U): U;
of<U>(result: U): Parser<U>;
}
/**
+90
View File
@@ -69,6 +69,7 @@ let fooReply: Reply<Foo>;
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<number> = P.seq(barPar, fooPar, numPar).map(([a, b, c]: [Bar, Foo, number]) => 42);
interface SeqObj {
first: number;
second: string;
third: Foo;
}
const seqObjPar: Parser<SeqObj> = P.seqObj<SeqObj>(
['first', numPar],
barPar,
fooArrPar,
['third', fooPar],
['second', strPar]);
fooPar = P.custom<Foo>((success, failure) => (stream, i) => { str = stream; num = i; return success(num, foo); });
fooPar = P.custom<Foo>((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<never>
// -- -- -- -- -- -- -- -- -- -- -- -- --
const bytePar: Parser<number> = P.byte(3);
const byteParMany: Parser<number[]> = P.bitSeq([1, 2, 5, 1]);
interface ByteSeqObj {
first: number;
second: number;
third: number;
}
const byteParObj: Parser<ByteSeqObj> = P.bitSeqObj([
['first', 3],
6,
['second', 8],
7,
['third', 9],
]);
const byteParObjErr: Parser<ByteSeqObj> = 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<undefined | string> = 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 extends string>(name: Name) {
let node: P.Parser<P.Node<'identifier', string>> = P.letters.node('identifier');
node = P.letters.thru(makeNode('identifier'));
// -- -- -- -- -- -- -- -- -- -- -- -- --
// Fantasy Land support
fooPar = fooPar.empty(); // $ExpectType Parser<never>
// 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;