Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case insensitive Scala parser-combinator

I'm trying to create a language, and there are some parts of it that I want to be case insensitive. I'm sure this is something easy, but I haven't been able to find it.

Edit: Re-reading makes me ashamed of this question. Here is a failing test that explains what I mean.

like image 491
Andres Avatar asked May 21 '11 08:05

Andres


1 Answers

Use a regular expression instead of a literal.

lazy val caseSensitiveKeyword: Parser[String] = "casesensitive"
lazy val caseInsensitiveKeyWord: Parser[String] = """(?i)\Qcaseinsensitive\E""".r

(See the docs for java.util.Pattern for info on the regex syntax used.)

If you're doing this frequently you could pimp String to simplify the syntax:

class MyRichString(str: String) {
  def ignoreCase: Parser[String] = ("""(?i)\Q""" + str + """\E""").r
}

implicit def pimpString(str: String): MyRichString = new MyRichString(str)

lazy val caseInsensitiveKeyword = "caseinsensitive".ignoreCase
like image 191
David Winslow Avatar answered Nov 13 '22 13:11

David Winslow