com-lihaoyi / fastparse

Writing Fast Parsers Fast in Scala
https://com-lihaoyi.github.io/fastparse
MIT License
1.09k stars 164 forks source link

Add an upper boundary to rep #27

Closed nrinaudo closed 8 years ago

nrinaudo commented 9 years ago

It's currently possible to specify a minimum number of repetitions for repeated sequences. It'd be great to also have a maximum number of repetitions.

My use case is parsing HTTP grammar, where for example language tags are defined as:

language-tag  = primary-tag *( "-" subtag )
primary-tag   = 1*8ALPHA
subtag        = 1*8ALPHA

I haven't yet found a sane way to express that with fastparse, but maybe I'm missing something?

lihaoyi commented 9 years ago

This should be really easy to add to https://github.com/lihaoyi/fastparse/blob/master/fastparse/shared/src/main/scala/fastparse/parsers/Combinators.scala#L333-L358 if anyone wants to pick this up. We even already keep count of how many iterations we've had, so it's a simple matter of bailing out early if we hit the target