문제

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);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top