sirthias/parboiled — Issue #36: Another utility function: adapt `normal* (special normal*)*` to Parboiled
Author: fge
Created: 2011-12-28 16:11:29+00:00
State: open

Yet another musing... I use regexes a lot, and find myself to use this pattern very often. There are a lot of situations where this pattern is used. For instance, to recognize double quoted strings, one would define:

* `normal` as `[^"]`,
* `special` as `\\.`,

and the full regex is `^"[^"]*(\\.[^"]*)*"$`.

This pattern can be effectively used in Parboiled as well (for matching multiline comments, or even quoted strings as above), so I coded a utility function for that, with the options of altering the quantifiers so that they can become `+` on each element:

```java
/**
* Adaptation of the {@code normal* (special normal*)*} regex pattern
*
*

The three booleans allow for the first {@code normal},
* the inside {@code normal} or the group to be empty,
* respectively. If disallowed, the {@code *} quantifier (ie,
* {@link BaseParser#ZeroOrMore(Object)} is turned into a {@code +}
* quantifier (ie, {@link BaseParser#OneOrMore(Object)}.

*
* @param normal the normal rule
* @param special the special rule
* @param allowFirstEmpty allow first normal to be empty
* @param allowInsideEmpty allow insider normal to be empty
* @param allowGroupEmpty allow group to be empty
* @return the matching rule
*/
Rule NSN(final Rule normal, final Rule special,
final boolean allowFirstEmpty, final boolean allowInsideEmpty,
final boolean allowGroupEmpty)
{
final Rule firstNormal = allowFirstEmpty
? ZeroOrMore(normal) : OneOrMore(normal);
final Rule insiderNormal = allowInsideEmpty
? ZeroOrMore(normal) : OneOrMore(normal);
final Rule group = allowGroupEmpty
? ZeroOrMore(Sequence(special, insiderNormal))
: OneOrMore(Sequence(special, insiderNormal));

return Sequence(firstNormal, group);
}
```

Are there chances that it makes it into parboiled?