Repetition

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Repetition

semperos

First post - Thanks, Mathias for an awesome project.

My only previous PEG experience is with Ruby's Parslet library. I'm trying to understand the "right" way to capture repetition in Parboiled.

In Parslet, I can write something like rule(:foo) { match('\d').repeat(1,2) }, which will match a number at least once, but not more than two times. In the Java Parboiled example for the TimeParser, I understand how the OneOrTwoDigits rule works, with sub-rules that capture only a certain number of matches and ordering them appropriately. For simple repetition needs, this is fine, but if I have multiple Rules that have different "minimum" and "maximum" repetition constraints, it seems like there might be a more "elegant" way to capture the repetition.

Thoughts?

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Repetition

mathias
Administrator
The "repeat" from the Ruby Parslet lib you mention is not a standard part of PEGs as such, it's a helper method provided by Parslet to easy rule construction.
Currently there is no direct counterpart in parboiled that offers the same functionality.

The Scala side of parboiled has a rule constructor called "nTimes" that does something similar: concatenating n calls to a rule into one sequence. You can achieve something similar yourself in Java without much effort. If you want the full "n to m" matches functionality your rule constructor would have to be somewhat more elaborate:
first concatenate the rule n times and then add an "Optional(rule, Optional(rule, Optional(rule, ...)))" construct with a depth of n-m.

The reason that something like this is not yet implemented is simply that the need for it is apparently quite rare in real-world applications.
However, if you'd like to contribute such a rule construction helper I'd be happy to integrate it.

Cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 21.10.2011, at 15:26, semperos [via parboiled users] wrote:

> First post - Thanks, Mathias for an awesome project.
>
> My only previous PEG experience is with Ruby's Parslet library. I'm trying to understand the "right" way to capture repetition in Parboiled.
>
> In Parslet, I can write something like rule(:foo) { match('\d').repeat(1,2) }, which will match a number at least once, but not more than two times. In the Java Parboiled example for the TimeParser, I understand how the OneOrTwoDigits rule works, with sub-rules that capture only a certain number of matches and ordering them appropriately. For simple repetition needs, this is fine, but if I have multiple Rules that have different "minimum" and "maximum" repetition constraints, it seems like there might be a more "elegant" way to capture the repetition.
>
> Thoughts?
>
>
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/Repetition-tp3440822p3440822.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Repetition

semperos
Thanks for the quick response!

Once I noticed the absence of explicit repetition in parboiled, I realized/remembered that it wasn't part of the "PEG spec." I've only used it as I learn how to construct PEG's, working on tasks that could easily be handled by regexes anyway.

Thanks for the explanation and reference to the Scala nTimes piece. If I find I need it outside toy PEG parsers, I'll take the time to contribute something back on the Java side.

-Daniel
Loading...