wavebeem / bread-n-butter

Parser combinators for TypeScript and JavaScript
https://bread-n-butter.wavebeem.com/
MIT License
35 stars 6 forks source link

Add many() / sepBy() method with min and max count #14

Closed seanchas116 closed 3 years ago

seanchas116 commented 3 years ago

Hello, I'm using bread-n-butter for parsing CSS values, and I'd like to have min/max support for repeating parsers. (for example: box-shadow may contain length between 2 and 4 times)

Thus I added new many and sepBy methods that supports min and max parameters.

(I told by @minamorl about this library and I'm very confortable with it because it's simple and works well with TypeScript!)

I'm not sure this API design is good, many0/many1/sepBy0/sepBy1 might be removed or many could be renamed to repeat, like in Parsimmon.

wavebeem commented 3 years ago

Hi @seanchas116!

I really like this PR :) The code is short and easy to read.

I like the name .repeat better than .many too! (Parsimmon doesn't use .repeat, by the way)

bnb.text("x").repeat(); // 0+ times
bnb.text("x").repeat(1); // 1+ times
bnb.text("x").repeat(1, 10); // 1-10 times
bnb.text("x").repeat(10, 10); // 10 times

bnb.text("x").sepBy(bnb.text(",")); // 0+ times
bnb.text("x").sepBy(bnb.text(","), 1); // 1+ times
bnb.text("x").sepBy(bnb.text(","), 1, 10); // 1-10 times
bnb.text("x").sepBy(bnb.text(","), 10, 10); // 10 times

You could use .repeat(n) to mean .repeat(n, n), but I don't like .repeat(n, Infinity). I think the way you designed it is better.

I like the idea of removing many0, many1, sepBy0, sepBy also.

If you make those changes, I can update docs/api.md and docs/tutorial.md, then do a release. I can do it later this week :)

ありがとうございました🙇‍♀️

seanchas116 commented 3 years ago

Thank you for replying! I made the changes and fixed the examples.

Parsimmon doesn't use .repeat, by the way

I mistook .repeat for .times 😵 (but I still like the name .repeat)