Sathish
Sathish

Reputation: 93

Scala Parser Combinator Replace Patterns

I got a below program, I can parse the pattern like convert(a.ACCOUNT_ID, string) to the expression, but I want to replace this pattern with CAST(a.ACCOUNT_ID AS VARCHAR). I can do parse the result expression and replace the strings with the one above but there are expressions like this hence I don't want to do that way.. Is there any way that I can do a pattern replace? Like if I find a pattern as convert(a.ACCOUNT_ID, string) then replace it with CAST(a.ACCOUNT_ID AS VARCHAR)

import scala.util.parsing.combinator._
import scala.util.parsing.combinator.lexical._
import scala.util.parsing.combinator.syntactical._
import scala.util.parsing.combinator.token._
import scala.util.parsing.input.CharSequenceReader


trait QParser  extends RegexParsers with JavaTokenParsers  {
  def knownFunction: Parser[Any] = ident ~ "(" ~ ident ~ ("." ~ ident <~ "," ~ ident ~ ")")

  def parse(inputString: String): Any = synchronized {
    phrase(knownFunction)(new CharSequenceReader(inputString)) match {
      case Success(result, _) => result
      case Failure(msg,_) => throw new DataTypeException(msg)
      case Error(msg,_) => throw new DataTypeException(msg)
    }
  }

 class DataTypeException(message: String) extends Exception(message)

}

object Parser extends QParser {
  def main(args: Array[String]) {
     println(parse("convert(a.ACCOUNT_ID, string)"));
  }
}

Output: (((convert~()~a)~(.~ACCOUNT_ID))

Upvotes: 0

Views: 328

Answers (1)

Peter Neyens
Peter Neyens

Reputation: 9820

I am not exactly sure what you mean with "there are expressions like this hence I don't want to do that way", but you can transform the result of your parser function using the ^^ operator.

A transformation function for your parser could be :

def knownFunction: Parser[String] = 
  ident ~ "(" ~ ident ~ "." ~ ident ~ "," ~ ident ~ ")" ^^ {
    case func ~ "(" ~ obj ~ "." ~ value ~ "," ~ castType ~ ")" =>
      val sqlFunc = Map("convert" -> "CAST")
      val sqlType = Map("string" -> "VARCHAR")
      s"${sqlFunc(func)}($obj.$value AS ${sqlType(castType)})"
  }

Using this updated function, the output of your application would be :

CAST(a.ACCOUNT_ID AS VARCHAR)

More information about the Scala Combinator Parsing can be found in a chapter of Programming in Scala, 1ed.

Upvotes: 2

Related Questions