At the time I was asking, seems like there was no way to do that. However, a github fork-and-pull later, there is: reluctantBetween()
.
Big thanks to @abailly on the fast response.
سؤال
I'm writing a parser for a DSL that uses the syntax (nodeHead: nodeBody)
. The problem is that nodeBody
may contain parens, at some cases.
The between
operator of JParsec should have been a good solution, yet the following code fails:
public void testSample() {
Parser<Pair<String,String>> sut = Parsers.tuple(Scanners.IDENTIFIER.followedBy(Scanners.among(":")),
Scanners.ANY_CHAR.many().source()
).between(Scanners.among("("), Scanners.among(")"));
sut.parse("(hello:world)");
}
It does not fail when I change ANY_CHAR
to IDENTIFIER
, so I assume the issue here is that the second parser in the tuple is too greedy. Alternatively, can I make JParsec apply the between parsers before it applies the body?
Any ideas are very much appriciated.
المحلول
At the time I was asking, seems like there was no way to do that. However, a github fork-and-pull later, there is: reluctantBetween()
.
Big thanks to @abailly on the fast response.
نصائح أخرى
If the syntax rule is that the last character will always be ")", you could probably do:
static <T> Parser<T> reluctantBetween(
Parser<?> begin, Parser<T> parser, Parser<?> end) {
Parser<?> terminator = end.followedBy(eof());
return between(begin, terminator.not().next(parser).many(), terminator);
}