🤖 Merge PR #49906 parsimmon: sepBy1 should return a non-empty array by @Nathan-Fenner

This commit is contained in:
Nathan Fenner
2020-12-05 18:57:31 -08:00
committed by GitHub
parent 4cdb090326
commit 2540e799bc
2 changed files with 9 additions and 3 deletions

View File

@@ -250,9 +250,9 @@ declare namespace Parsimmon {
/**
* Equivalent to Parsimmon.sepBy(parser, separator).
*
* Expects one or more matches for parser, separated by the parser separator, yielding an array.
* Expects one or more matches for parser, separated by the parser separator, yielding a non-empty array.
*/
sepBy1<U>(separator: Parser<U>): Parser<T[]>;
sepBy1<U>(separator: Parser<U>): Parser<[T, ...T[]]>;
/**
* Equivalent to Parsimmon.of(result).
*/
@@ -472,7 +472,7 @@ declare namespace Parsimmon {
/**
* This is the same as Parsimmon.sepBy, but matches the content parser at least once.
*/
function sepBy1<T, U>(content: Parser<T>, separator: Parser<U>): Parser<T[]>;
function sepBy1<T, U>(content: Parser<T>, separator: Parser<U>): Parser<[T, ...T[]]>;
/**
* accepts a function that returns a parser, which is evaluated the first time the parser is used.

View File

@@ -39,7 +39,9 @@ let fooOrBarPar: Parser<Foo | Bar>;
// -- -- -- -- -- -- -- -- -- -- -- -- --
let strArrPar: Parser<string[]>;
let str1ArrPar: Parser<[string, ...string[]]>;
let fooArrPar: Parser<Foo[]>;
let foo1ArrPar: Parser<[Foo, ...Foo[]]>;
// -- -- -- -- -- -- -- -- -- -- -- -- --
@@ -284,6 +286,9 @@ strPar = P.seqMap(
strArrPar = P.sepBy(P.string('foo'), P.string('bar'));
strArrPar = P.sepBy1(P.string('foo'), P.string('bar'));
str1ArrPar = P.sepBy1(P.string('foo'), P.string('bar'));
function flattenMultiple(first: string, ...rest: string[]): number { return rest.length; }
numPar = str1ArrPar.map(arr => flattenMultiple(...arr));
strPar = P.test((a: string) => false);
@@ -324,6 +329,7 @@ numPar = P.digit
fooArrPar = fooPar.sepBy(barPar);
fooArrPar = fooPar.sepBy1(barPar);
foo1ArrPar = fooPar.sepBy1(barPar);
fooPar = barPar.of(foo);