Search code examples
javaparsingdsljparsec

Parsing text in parens using JParsec


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.


Solution

  • 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.