Add fallback and simplify or

This commit is contained in:
Andy Hanson
2016-12-15 13:39:51 -08:00
parent 26460b45c5
commit b16be42e6d
2 changed files with 13 additions and 7 deletions

15
parsimmon/index.d.ts vendored
View File

@@ -83,8 +83,7 @@ declare namespace Parsimmon {
/**
* returns a new parser which tries parser, and if it fails uses otherParser.
*/
or(otherParser: Parser<T>): Parser<T>;
or<U>(otherParser: Parser<U>): Parser<any>;
or<U>(otherParser: Parser<U>): Parser<T | U>;
/**
* 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
@@ -106,14 +105,18 @@ declare namespace Parsimmon {
* transforms the output of parser with the given function.
*/
map<U>(call: (result: T) => U): Parser<U>;
/**
* expects otherParser after parser, but preserves the yield value of parser.
*/
skip<U>(otherParser: Parser<U>): Parser<T>;
/**
* returns a new parser with the same behavior, but which yields aResult.
*/
result<U>(aResult: U): Parser<U>;
/**
* returns a new parser that returns the fallback value if the first parser failed.
*/
fallback<U>(fallbackValue: U): Parser<T | U>;
/**
* expects otherParser after parser, but preserves the yield value of parser.
*/
skip<U>(otherParser: Parser<U>): Parser<T>;
/**
* expects parser zero or more times, and yields an array of the results.
*/

View File

@@ -38,6 +38,7 @@ var indexPar: Parser<Index>;
var fooPar: Parser<Foo>;
var barPar: Parser<Bar>;
var fooOrBarPar: Parser<Foo | Bar>;
// -- -- -- -- -- -- -- -- -- -- -- -- --
@@ -84,7 +85,7 @@ fooResult = fooPar.parse(str);
foo = fooPar.tryParse(str);
fooPar = fooPar.or(fooPar);
anyPar = fooPar.or(barPar);
fooOrBarPar = fooPar.or(barPar);
barPar = fooPar.chain((f) => {
foo = f;
@@ -108,6 +109,8 @@ fooPar = fooPar.skip(barPar);
barPar = barPar = fooPar.result(bar);
fooOrBarPar = fooPar.fallback(bar);
// -- -- -- -- -- -- -- -- -- -- -- -- --
fooArrPar = fooPar.many();