with scala-parser-combinators, want try parse postfix string(end). previous parser cought end contents. how fix it? (just changing "[^z]".r not answer.)
val input = "aaabbbzzz" parseall(body, input) // java.lang.illegalstateexception: `zzz' expected end of source found def body: parser[list[string]] = content.+ <~ end def content: parser[string] = repchar // more complex (repchar | foo | bar) def repchar: parser[string] = ".{3}".r // if change "[^z]{3}", no flexibility. def end: parser[string] = "zzz" i want try followings.
"""(.*)(?=zzz)""".r.into(str => ...check content.+ or not... <~ end) - search strings until
endstring. - then parse them parser.
another way fix use not combinator. need check parsing not end value.
the trick not doesn't consume input, if not(end) succeeds (meaning end failed) didn't reach stopping condition can parse 3 characters content parser made end parser failing.
as non-greedy approach linked in comments, fail input has characters after "zzz" in input (such "aaabbbzzzzzz" example).
but may sufficient use case. give try with:
def body: parser[list[string]] = rep(not(end) ~> content) <~ end in fact kind of takeuntil parser, because parses content repeatedly until you're able parse end.
Comments
Post a Comment