Alexey Romanov
Alexey Romanov

Reputation: 170839

How to handle case-insensitive keywords with Scala parser combinators

Some languages, like SQL, have case-insensitive keywords (and/or identifiers). When using TokenParsers, how can I handle this case insensitivity? One option is to generate all possible case combinations of keywords, but this performs quite badly if there are many keywords (in my case it somehow led to stack overflows in whitespace!) and won't work for the identifiers in any case.

[Self-answering in hope someone else won't end up spending a day on this.]

Upvotes: 2

Views: 168

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170839

This can be done by overriding processIdent (assumes keywords are given in upper case, as normal for SQL):

override protected def processIdent(name: String) = {
  val upperCased = name.toUpperCase
  if (reserved contains upperCased) Keyword(upperCased) else Identifier(upperCased)
}

Upvotes: 2

Related Questions