Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

I was able to solve the issue in the end.

It's reasonable, that the parser won't backtrack within the regex, so I thought I should rewrite the AnyChar.rep part as a recursive rule, like this:

val before: P[Any] = P(AnyChar | (AnyChar ~ before)) 

But that wasn't enough, fastparse still don't seem to backtrack.

I stumbled upon this question about parsing ambiguous grammarthis question about parsing ambiguous grammar. So I tried using GLL combinators instead of Fastparse, and that made it work.

object TestParser1 extends Parsers with RegexParsers { lazy val before: Parser[String] = (".".r | (".".r ~ before)) ^^ { case a ~ b => a.toString + b.toString case x => x.toString } lazy val content: Parser[String] = "xxx" lazy val after: Parser[String] = "b+".r lazy val all: Parser[String] = before ~ content ~ after ^^ { case (b, c, a) => s"$b $c $a" } def test() = { val r = all("aaaaxxxbbb") r.toList foreach println } } 

I was able to solve the issue in the end.

It's reasonable, that the parser won't backtrack within the regex, so I thought I should rewrite the AnyChar.rep part as a recursive rule, like this:

val before: P[Any] = P(AnyChar | (AnyChar ~ before)) 

But that wasn't enough, fastparse still don't seem to backtrack.

I stumbled upon this question about parsing ambiguous grammar. So I tried using GLL combinators instead of Fastparse, and that made it work.

object TestParser1 extends Parsers with RegexParsers { lazy val before: Parser[String] = (".".r | (".".r ~ before)) ^^ { case a ~ b => a.toString + b.toString case x => x.toString } lazy val content: Parser[String] = "xxx" lazy val after: Parser[String] = "b+".r lazy val all: Parser[String] = before ~ content ~ after ^^ { case (b, c, a) => s"$b $c $a" } def test() = { val r = all("aaaaxxxbbb") r.toList foreach println } } 

I was able to solve the issue in the end.

It's reasonable, that the parser won't backtrack within the regex, so I thought I should rewrite the AnyChar.rep part as a recursive rule, like this:

val before: P[Any] = P(AnyChar | (AnyChar ~ before)) 

But that wasn't enough, fastparse still don't seem to backtrack.

I stumbled upon this question about parsing ambiguous grammar. So I tried using GLL combinators instead of Fastparse, and that made it work.

object TestParser1 extends Parsers with RegexParsers { lazy val before: Parser[String] = (".".r | (".".r ~ before)) ^^ { case a ~ b => a.toString + b.toString case x => x.toString } lazy val content: Parser[String] = "xxx" lazy val after: Parser[String] = "b+".r lazy val all: Parser[String] = before ~ content ~ after ^^ { case (b, c, a) => s"$b $c $a" } def test() = { val r = all("aaaaxxxbbb") r.toList foreach println } } 
changed Any to String
Source Link

I was able to solve the issue in the end.

It's reasonable, that the parser won't backtrack within the regex, so I thought I should rewrite the AnyChar.rep part as a recursive rule, like this:

val before: P[Any] = P(AnyChar | (AnyChar ~ before)) 

But that wasn't enough, fastparse still don't seem to backtrack.

I stumbled upon this question about parsing ambiguous grammar. So I tried using GLL combinators instead of Fastparse, and that made it work.

object TestParser1 extends Parsers with RegexParsers { lazy val before: Parser[Any]Parser[String] = (".".r | (".".r ~ before)) ^^ { case a ~ b => a.toString + b.toString case x => x.toString } lazy val content: Parser[Any]Parser[String] = "xxx" lazy val after: Parser[Any]Parser[String] = "b+".r lazy val all: Parser[Any]Parser[String] = before ~ content ~ after ^^ { case (b, c, a) => s"$b $c $a" } def test() = { val r = all("aaaaxxxbbb") r.toList foreach println } } 

I was able to solve the issue in the end.

It's reasonable, that the parser won't backtrack within the regex, so I thought I should rewrite the AnyChar.rep part as a recursive rule, like this:

val before: P[Any] = P(AnyChar | (AnyChar ~ before)) 

But that wasn't enough, fastparse still don't seem to backtrack.

I stumbled upon this question about parsing ambiguous grammar. So I tried using GLL combinators instead of Fastparse, and that made it work.

object TestParser1 extends Parsers with RegexParsers { lazy val before: Parser[Any] = (".".r | (".".r ~ before)) ^^ { case a ~ b => a.toString + b.toString case x => x.toString } lazy val content: Parser[Any] = "xxx" lazy val after: Parser[Any] = "b+".r lazy val all: Parser[Any] = before ~ content ~ after def test() = { val r = all("aaaaxxxbbb") r.toList foreach println } } 

I was able to solve the issue in the end.

It's reasonable, that the parser won't backtrack within the regex, so I thought I should rewrite the AnyChar.rep part as a recursive rule, like this:

val before: P[Any] = P(AnyChar | (AnyChar ~ before)) 

But that wasn't enough, fastparse still don't seem to backtrack.

I stumbled upon this question about parsing ambiguous grammar. So I tried using GLL combinators instead of Fastparse, and that made it work.

object TestParser1 extends Parsers with RegexParsers { lazy val before: Parser[String] = (".".r | (".".r ~ before)) ^^ { case a ~ b => a.toString + b.toString case x => x.toString } lazy val content: Parser[String] = "xxx" lazy val after: Parser[String] = "b+".r lazy val all: Parser[String] = before ~ content ~ after ^^ { case (b, c, a) => s"$b $c $a" } def test() = { val r = all("aaaaxxxbbb") r.toList foreach println } } 
Source Link

I was able to solve the issue in the end.

It's reasonable, that the parser won't backtrack within the regex, so I thought I should rewrite the AnyChar.rep part as a recursive rule, like this:

val before: P[Any] = P(AnyChar | (AnyChar ~ before)) 

But that wasn't enough, fastparse still don't seem to backtrack.

I stumbled upon this question about parsing ambiguous grammar. So I tried using GLL combinators instead of Fastparse, and that made it work.

object TestParser1 extends Parsers with RegexParsers { lazy val before: Parser[Any] = (".".r | (".".r ~ before)) ^^ { case a ~ b => a.toString + b.toString case x => x.toString } lazy val content: Parser[Any] = "xxx" lazy val after: Parser[Any] = "b+".r lazy val all: Parser[Any] = before ~ content ~ after def test() = { val r = all("aaaaxxxbbb") r.toList foreach println } }