From 5da11b9a1fa97fb472af1d94ef69ceb340303878 Mon Sep 17 00:00:00 2001 From: wmrowan Date: Thu, 8 Dec 2016 13:04:55 -0800 Subject: [PATCH] Update Parsimmon typings (#13146) --- parsimmon/parsimmon-tests.ts | 16 +++++++-- parsimmon/parsimmon.d.ts | 67 ++++++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 6 deletions(-) diff --git a/parsimmon/parsimmon-tests.ts b/parsimmon/parsimmon-tests.ts index 0cd201cd44..8f87227740 100644 --- a/parsimmon/parsimmon-tests.ts +++ b/parsimmon/parsimmon-tests.ts @@ -46,6 +46,7 @@ var barPar: Parser; var anyArrPar: Parser; +var strArrPar: Parser; var fooArrPar: Parser; var barArrPar: Parser; @@ -68,7 +69,16 @@ index = fooResult.index; // -- -- -- -- -- -- -- -- -- -- -- -- -- +fooResult = P.makeSuccess(0, foo); +fooResult = P.makeFailure(0, ''); + +fooPar = P((input: string, i: number) => P.makeSuccess(0, foo)); +fooPar = P.Parser((input: string, i: number) => P.makeSuccess(0, foo)); + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + fooResult = fooPar.parse(str); +foo = fooPar.tryParse(str); fooPar = fooPar.or(fooPar); anyPar = fooPar.or(barPar); @@ -171,9 +181,9 @@ numPar = P.seqMap(P.digit, P.digits, (a: string, b: string) => 42); strPar = P.seqMap(P.digit, P.digits, P.letter, (a: string, b: string, c: string) => 'foo'); strPar = P.seqMap(P.digit, P.digits, P.letter, P.letters.map(Number), (a: string, b: string, c: string, d: number) => 'foo'); -strPar = P.sepBy(P.string('foo'), P.string('bar')); -strPar = P.sepBy1(P.string('foo'), P.string('bar')); +strArrPar = P.sepBy(P.string('foo'), P.string('bar')); +strArrPar = P.sepBy1(P.string('foo'), P.string('bar')); strPar = P.test((a: string) => false); -strPar = P.takeWhile((a: string) => true); \ No newline at end of file +strPar = P.takeWhile((a: string) => true); diff --git a/parsimmon/parsimmon.d.ts b/parsimmon/parsimmon.d.ts index fa356a799c..22bfa1283b 100644 --- a/parsimmon/parsimmon.d.ts +++ b/parsimmon/parsimmon.d.ts @@ -1,4 +1,4 @@ -// Type definitions for Parsimmon 0.9.2 +// Type definitions for Parsimmon 1.0.0 // Project: https://github.com/jneen/parsimmon // Definitions by: Bart van der Schoor , Mizunashi Mana , Boris Cherny , Benny van Reeven // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -6,6 +6,42 @@ // TODO convert to generics declare module 'parsimmon' { + + /** + * **NOTE:** You probably will never need to use this function. Most parsing + * can be accomplished using `Parsimmon.regexp` and combination with + * `Parsimmon.seq` and `Parsimmon.alt`. + * + * You can add a primitive parser (similar to the included ones) by using + * `Parsimmon(fn)`. This is an example of how to create a parser that matches + * any character except the one provided: + * + * ```javascript + * function notChar(char) { + * return Parsimmon(function(input, i) { + * if (input.charAt(i) !== char) { + * return Parsimmon.makeSuccess(i + 1, input.charAt(i)); + * } + * return Parsimmon.makeFailure(i, 'anything different than "' + char + '"'); + * }); + * } + * ``` + * + * This parser can then be used and composed the same way all the existing + * ones are used and composed, for example: + * + * ```javascript + * var parser = + * Parsimmon.seq( + * Parsimmon.string('a'), + * notChar('b').times(5) + * ); + * parser.parse('accccc'); + * //=> {status: true, value: ['a', ['c', 'c', 'c', 'c', 'c']]} + * ``` + */ + function Parsimmon(fn: (input: string, i: number) => Parsimmon.Result): Parsimmon.Parser; + namespace Parsimmon { export type StreamType = string; @@ -37,6 +73,12 @@ declare module 'parsimmon' { * parse the string */ parse(input: string): Result; + /** + * Like parser.parse(input) but either returns the parsed value or throws + * an error on failure. The error object contains additional properties + * about the error. + */ + tryParse(input: string): T; /** * returns a new parser which tries parser, and if it fails uses otherParser. */ @@ -101,6 +143,25 @@ declare module 'parsimmon' { desc(description: string): Parser; } + /** + * Alias of `Parsimmon(fn)` for backwards compatibility. + */ + export function Parser(fn: (input: string, i: number) => Parsimmon.Result): Parser; + + /** + * To be used inside of Parsimmon(fn). Generates an object describing how + * far the successful parse went (index), and what value it created doing + * so. See documentation for Parsimmon(fn). + */ + export function makeSuccess(index: number, value: T): Result; + + /** + * To be used inside of Parsimmon(fn). Generates an object describing how + * far the unsuccessful parse went (index), and what kind of syntax it + * expected to see (expectation). See documentation for Parsimmon(fn). + */ + export function makeFailure(furthest: number, expectation: string): Result; + /** * Returns true if obj is a Parsimmon parser, otherwise false. */ @@ -193,12 +254,12 @@ declare module 'parsimmon' { /** * Accepts two parsers, and expects zero or more matches for content, separated by separator, yielding an array. */ - export function sepBy(content: Parser, separator: Parser): Parser + export function sepBy(content: Parser, separator: Parser): Parser /** * This is the same as Parsimmon.sepBy, but matches the content parser at least once. */ - export function sepBy1(content: Parser, separator: Parser): Parser + export function sepBy1(content: Parser, separator: Parser): Parser /** * accepts a function that returns a parser, which is evaluated the first time the parser is used.