Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Avoid escaping literal string twice

Fix failure in
    ("(" | ")")("(")
inside an instance of RegexParsers.
The problem is the implementation of the `|` operator in RegexParsers.

When running the above code, "(" is escaped twice. Printing the above
parser results in:

/(\\\()|(\))/

and using a debugger confirms the result.

With this fix, the existing regular expression is not escaped again.
Note that escapeRegex escapes a normal string to produce a regex,
unlike the name may suggest.

A complete example reproducing the issue is available at:
https://gist.github.com/Blaisorblade/4743188
  • Loading branch information...
commit 71a1e3b74dc73c89044a14eaa97e5092689f31e7 1 parent dca624d
@Blaisorblade Blaisorblade authored
Showing with 2 additions and 2 deletions.
  1. +2 −2 src/main/scala/com/codecommit/gll/RegexParsers.scala
View
4 src/main/scala/com/codecommit/gll/RegexParsers.scala
@@ -51,9 +51,9 @@ trait RegexParsers extends Parsers {
class RichRegexParser(left: RegexParser) extends RichParser(left) {
- def |(right: Regex) = new RegexParser(new Regex("(" + escapeRegex(left.regex.toString) + ")|(" + escapeRegex(right.regex.toString) + ")"))
+ def |(right: Regex) = new RegexParser(new Regex("(" + left.regex.toString + ")|(" + escapeRegex(right.regex.toString) + ")"))
- def |(right: String) = new RegexParser(new Regex("(" + escapeRegex(left.regex.toString) + ")|(" + escapeRegex(right) + ")"))
+ def |(right: String) = new RegexParser(new Regex("(" + left.regex.toString + ")|(" + escapeRegex(right) + ")"))
}
case class RegexParser(val regex: Regex) extends TerminalParser[String] {
Please sign in to comment.
Something went wrong with that request. Please try again.