From 19a4c0363181f8790131f95a29358aed3eb73435 Mon Sep 17 00:00:00 2001 From: Bart van der Schoor Date: Tue, 1 Apr 2014 20:58:33 +0200 Subject: [PATCH] added definitions for parsimmon --- README.md | 1 + parsimmon/parsimmon-tests.ts | 116 ++++++++++++++++++++++++++++ parsimmon/parsimmon.d.ts | 146 +++++++++++++++++++++++++++++++++++ 3 files changed, 263 insertions(+) create mode 100644 parsimmon/parsimmon-tests.ts create mode 100644 parsimmon/parsimmon.d.ts diff --git a/README.md b/README.md index 30c81dd14c..35bd97dc7c 100755 --- a/README.md +++ b/README.md @@ -219,6 +219,7 @@ List of Definitions * [Numeral.js](https://github.com/adamwdraper/Numeral-js) (by [Vincent Bortone](https://github.com/vbortone/)) * [OpenLayers](https://github.com/openlayers/openlayers) (by [Ilya Bolkhovsky](https://github.com/bolhovsky/)) * [Parallel.js](https://github.com/adambom/parallel.js) (by [Josh Baldwin](https://github.com/jbaldwin)) +* [Parsimmon](https://github.com/jayferd/parsimmon) (by [Bart van der Schoor](https://github.com/Bartvds)) * [PDF.js](https://github.com/mozilla/pdf.js) (by [Josh Baldwin](https://github.com/jbaldwin)) * [Persona](http://www.mozilla.org/en-US/persona) (by [James Frasca](https://github.com/Nycto)) * [PhantomJS](http://phantomjs.org) (by [Jed Hunsaker](https://github.com/jedhunsaker)) diff --git a/parsimmon/parsimmon-tests.ts b/parsimmon/parsimmon-tests.ts new file mode 100644 index 0000000000..16d1510b5d --- /dev/null +++ b/parsimmon/parsimmon-tests.ts @@ -0,0 +1,116 @@ +/// + +import P = require('parsimmon'); +import Parser = P.Parser; +import Mark = P.Mark; + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +class Foo { + bar: Bar; +} + +class Bar { + foo: Foo; +} + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +var str: string; +var num: number; +var regex: RegExp; + +var foo: Foo; +var bar: Bar; + +var strArr: string[]; +var fooArr: Foo[]; +var barArr: Bar[]; + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +var strPar: Parser; +var numPar: Parser; +var voidPar: Parser; +var anyPar: Parser; + +var fooPar: Parser; +var barPar: Parser; + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +var anyArrPar: Parser; + +var fooArrPar: Parser; +var barArrPar: Parser; + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +var fooMarkPar: Parser>; + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +foo = fooPar.parse(str); + +fooPar = fooPar.or(fooPar); +anyPar = fooPar.or(barPar); + +barPar = fooPar.then((f) => { + foo = f; + return barPar; +}); +barPar = fooPar.then(barPar); + +barPar = fooPar.map((f) => { + foo = f; + return bar; +}); + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +fooPar = fooPar.skip(barPar); + +barPar = barPar = fooPar.result(bar); + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +fooArrPar = fooPar.many(); +fooArrPar = fooPar.times(num); +fooArrPar = fooPar.times(num, num); +fooArrPar = fooPar.atMost(num); +fooArrPar = fooPar.atLeast(num); + +fooMarkPar = fooPar.mark(); + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +strPar = P.string(str); +strPar = P.regex(regex); + +fooPar = P.succeed(foo); + +fooArrPar = P.seq(fooPar, fooPar); +anyArrPar = P.seq(barPar, fooPar, numPar); + +fooPar = P.lazy(() => { + return fooPar; +}); + +voidPar = P.fail(str); +fooPar = P.fail(str); + +// -- -- -- -- -- -- -- -- -- -- -- -- -- + +strPar = P.letter; +strPar = P.letters; + +strPar = P.digit; +strPar = P.digits; + +strPar = P.whitespace; +strPar = P.optWhitespace; + +strPar = P.any; +strPar = P.all; +strPar = P.eof; +numPar = P.index; \ No newline at end of file diff --git a/parsimmon/parsimmon.d.ts b/parsimmon/parsimmon.d.ts new file mode 100644 index 0000000000..435ea285a9 --- /dev/null +++ b/parsimmon/parsimmon.d.ts @@ -0,0 +1,146 @@ +// Type definitions for Parsimmon 0.3.0 +// Project: https://github.com/jayferd/parsimmon +// Definitions by: Bart van der Schoor +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +// TODO convert to generics + +declare module 'parsimmon' { + module Parsimmon { + + export interface Mark { + start: number; + end: number; + value: T; + } + + export interface Parser { + /* + parse the string + */ + parse(input: string): T; + /* + 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. + */ + 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; + /* + yields an object with start, value, and end keys, where value is the original value yielded by the parser, and start and end are the indices in the stream that contain the parsed text. + */ + mark(): 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; + + /* + 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; + + /* + 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; +}