From 06646ec23d8c4c8f84402655690069200fa309fe Mon Sep 17 00:00:00 2001 From: karak Date: Thu, 9 Nov 2017 18:05:53 +0900 Subject: [PATCH] Fixed custom parsers feature. The return type of actions, so called "Reply", should be different from that of parsers. --- types/parsimmon/index.d.ts | 32 +++++++++++++++++++++++------- types/parsimmon/parsimmon-tests.ts | 7 ++++--- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/types/parsimmon/index.d.ts b/types/parsimmon/index.d.ts index 6bc93ae84a..dfbae05e23 100644 --- a/types/parsimmon/index.d.ts +++ b/types/parsimmon/index.d.ts @@ -41,7 +41,7 @@ * //=> {status: true, value: ['a', ['c', 'c', 'c', 'c', 'c']]} * ``` */ -declare function Parsimmon(fn: (input: string, i: number) => Parsimmon.Result): Parsimmon.Parser; +declare function Parsimmon(fn: (input: string, i: number) => Parsimmon.Reply): Parsimmon.Parser; declare namespace Parsimmon { type StreamType = string; @@ -169,21 +169,21 @@ declare namespace Parsimmon { /** * Alias of `Parsimmon(fn)` for backwards compatibility. */ - function Parser(fn: (input: string, i: number) => Parsimmon.Result): Parser; + function Parser(fn: (input: string, i: number) => Parsimmon.Reply): 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). */ - function makeSuccess(index: number, value: T): Success; + function makeSuccess(index: number, value: T): SuccessReply; /** * 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). */ - function makeFailure(furthest: number, expectation: string): Failure; + function makeFailure(furthest: number, expectation: string): FailureReply; /** * Returns true if obj is a Parsimmon parser, otherwise false. @@ -276,9 +276,27 @@ declare namespace Parsimmon { p1: Parser, p2: Parser, p3: Parser, p4: Parser, p5: Parser, p6: Parser, p7: Parser, p8: Parser, cb: (a1: T, a2: U, a3: V, a4: W, a5: X, a6: Y, a7: Z, a8: A) => B): Parser; - type SuccessFunctionType = (index: number, result: U) => Result; - type FailureFunctionType = (index: number, msg: string) => Result; - type ParseFunctionType = (stream: StreamType, index: number) => Result; + interface SuccessReply { + status: true; + index: number; + value: T; + furthest: -1; + expected: string[]; + } + + interface FailureReply { + status: false; + index: -1; + value: null; + furthest: number; + expected: string[]; + } + + type Reply = SuccessReply | FailureReply; + + type SuccessFunctionType = (index: number, result: U) => Reply; + type FailureFunctionType = (index: number, msg: string) => Reply; + type ParseFunctionType = (stream: StreamType, index: number) => Reply; /** * allows to add custom primitive parsers. */ diff --git a/types/parsimmon/parsimmon-tests.ts b/types/parsimmon/parsimmon-tests.ts index 4c8780f5fc..f89d2fda2f 100644 --- a/types/parsimmon/parsimmon-tests.ts +++ b/types/parsimmon/parsimmon-tests.ts @@ -1,5 +1,5 @@ import P = require('parsimmon'); -import { Parser, Mark, Result, Index } from "parsimmon"; +import { Parser, Mark, Result, Index, Reply } from "parsimmon"; // -- -- -- -- -- -- -- -- -- -- -- -- -- @@ -65,9 +65,10 @@ if (fooResult.status === true) { } // -- -- -- -- -- -- -- -- -- -- -- -- -- +let fooReply: Reply; -fooResult = P.makeSuccess(0, foo); -fooResult = P.makeFailure(0, ''); +fooReply = P.makeSuccess(0, foo); +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));