// Type definitions for Parsimmon 0.5.0 // Project: https://github.com/jneen/parsimmon // Definitions by: Bart van der Schoor , Mizunashi Mana // Definitions: https://github.com/borisyankov/DefinitelyTyped // TODO convert to generics declare module 'parsimmon' { module Parsimmon { export type StreamType = string; export interface Mark { start: number; end: number; value: T; } export interface Result { status: boolean; value?: T; expected?: string; index?: number; } export interface Parser { /* parse the string */ parse(input: string): Result; /* returns a new parser which tries parser, and if it fails uses otherParser. */ or(otherParser: Parser): Parser; or(otherParser: Parser): Parser; /* returns a new parser which tries parser, and on success calls the given function with the result of the parse, which is expected to return another parser, which will be tried next */ chain(next: (result: T) => Parser): Parser; /* returns a new parser which tries parser, and on success calls the given function with the result of the parse, which is expected to return another parser. */ then(call: (result: T) => Parser): Parser; /* expects anotherParser to follow parser, and yields the result of anotherParser. NB: the result of parser here is ignored. */ then(anotherParser: Parser): Parser; /* transforms the output of parser with the given function. */ map(call: (result: T) => U): Parser; /* expects otherParser after parser, but preserves the yield value of parser. */ skip(otherParser: Parser): Parser; /* returns a new parser with the same behavior, but which yields aResult. */ result(aResult: U): Parser; /* expects parser zero or more times, and yields an array of the results. */ many(): Parser; /* expects parser exactly n times, and yields an array of the results. */ times(n: number): Parser; /* expects parser between min and max times, and yields an array of the results. */ times(min: number, max: number): Parser; /* expects parser at most n times. Yields an array of the results. */ atMost(n: number): Parser; /* expects parser at least n times. Yields an array of the results. */ atLeast(n: number): Parser; /* returns a new parser whose failure message is the passed description. */ mark(): Parser>; desc(description: string): Parser; } /* is a parser that expects to find "my-string", and will yield the same. */ export function string(mystring: string): Parser; /* is a parser that expects the stream to match the given regex. */ export function regex(myregex: RegExp): Parser; /* is a parser that doesn't consume any of the string, and yields result. */ export function succeed(result: U): Parser; /* accepts a variable number of parsers that it expects to find in order, yielding an array of the results. */ export function seq(...parsers: Parser[]): Parser; export function seq(...parsers: Parser[]): Parser; export type SuccessFunctionType = (index: number, result: U) => Result; export type FailureFunctionType = (index: number, msg: string) => Result; export type ParseFunctionType = (stream: StreamType, index: number) => Result; /* allows to add custom primitive parsers. */ export function custom(parsingFunction: (success: SuccessFunctionType, failure: FailureFunctionType) => ParseFunctionType): Parser; /* accepts a variable number of parsers, and yields the value of the first one that succeeds, backtracking in between. */ export function alt(...parsers: Parser[]): Parser; export function alt(...parsers: Parser[]): Parser; /* accepts a function that returns a parser, which is evaluated the first time the parser is used. This is useful for referencing parsers that haven't yet been defined. */ export function lazy(f: () => Parser): Parser; export function lazy(description: string, f: () => Parser): Parser; /* fail paring with a message */ export function fail(message: string): Parser; export function fail(message: string): Parser; /* is equivalent to Parsimmon.regex(/[a-z]/i) */ export var letter: Parser; /* is equivalent to Parsimmon.regex(/[a-z]*`/i) */ export var letters: Parser; /* is equivalent to Parsimmon.regex(/[0-9]/) */ export var digit: Parser; /* is equivalent to Parsimmon.regex(/[0-9]*`/) */ export var digits: Parser; /* is equivalent to Parsimmon.regex(/\s+/) */ export var whitespace: Parser; /* is equivalent to Parsimmon.regex(/\s*`/) */ export var optWhitespace: Parser; /* consumes and yields the next character of the stream. */ export var any: Parser; /* consumes and yields the entire remainder of the stream. */ export var all: Parser; /* expects the end of the stream. */ export var eof: Parser; /* is a parser that yields the current index of the parse. */ export var index: Parser; } export = Parsimmon; }