在scala中解析案例语句

时间:2018-08-16 04:11:58

标签: regex scala parser-combinators

在scala中解析案例语句

CASE WHEN col1 <> 0 AND col2 <> 0 THEN 'COL1 & COL2 IS NOT ZERO' ELSE 'COL1 & COL2 IS ZERO'

这里的挑战是给出所有可能出现case语句的场景,例如它可以包含在一个函数中。同样,case语句/函数等也可以放入另一个必须处理的case语句中。

1 个答案:

答案 0 :(得分:2)

此问题可以使用Scala解析器组合器解决

首先定义映射体验所需的类

sealed trait Exp {
  def asStr: String

  override def toString: String = asStr
}
case class OperationExp(a: Exp, op: String, b: Exp, c: Option[String]) extends Exp { override def asStr = s"$a $op $b ${c.getOrElse("")}" }
case class CaseConditions(conditionValue: List[(String, String)] , elseValue: String, asAlias: Option[Exp]) extends Exp {
  override def asStr = "CASE " + conditionValue.map(c => s"WHEN ${c._1} THEN ${c._2}").mkString(" ") + s" ELSE ${elseValue} END ${asAlias.getOrElse("")}"
}

现在的解决方案

case class OperationExp(a: Exp, op: String, b: Exp, c: Option[String]) extends Exp { override def asStr = s"$a $op $b ${c.getOrElse("")}" }

case class CaseConditions(conditionValue: List[(String, String)] , elseValue: String, asAlias: Option[Exp]) extends Exp {
  override def asStr = "CASE " + conditionValue.map(c => s"WHEN ${c._1} THEN ${c._2}").mkString(" ") + s" ELSE ${elseValue} END ${asAlias.getOrElse("")}"
}
val identifiers: Parser[String]     = "[a-zA-Z0-9_~\\|,'\\-\\+:.()]+".r
      val operatorTokens: Parser[String]  = "[<>=!]+".r | ("IS NOT" | "IN" | "IS")
  val conditionJoiner: Parser[String] = ( "AND" | "OR" )

  val excludeKeywords = List("CASE","WHEN", "THEN", "ELSE", "END")


  val identifierWithoutCaseKw: Parser[Exp] = Parser(input =>
    identifiers(input).filterWithError(
      !excludeKeywords.contains(_),
      reservedWord => s"$reservedWord encountered",
      input
    )
  ) ^^ StrExp

  val anyStrExp: Parser[Exp]     = "[^()]*".r  ^^ StrExp




  val funcIdentifier: Parser[Exp] = name ~ ("(" ~> (caseConditionExpresionParser | funcIdentifier | anyStrExp) <~ ")") ^^ {case func ~ param => FunCallExp(func, Seq(param))}

  val identifierOrFunctions =  funcIdentifier | identifierWithoutCaseKw

  val conditionParser: Parser[String] =
    identifierOrFunctions ~ operatorTokens ~ identifierOrFunctions ~ opt(conditionJoiner) ^^ {
      case a ~ op ~ b ~ c => s"$a $op $b ${c.getOrElse("")}"
    }

  def caseConditionExpresionParser: Parser[CaseConditions]  = "CASE" ~ rep1("WHEN" ~ rep(conditionParser) ~ "THEN" ~ rep(identifierWithoutCaseKw)) ~ "ELSE" ~ rep(identifierWithoutCaseKw) ~ "END" ~ opt("AS" ~> identifierWithoutCaseKw)^^ {
    case "CASE" ~ conditionValuePair ~ "ELSE" ~ falseValue ~ "END" ~ asName =>
      CaseConditions(
        conditionValuePair.map(cv => (
          cv._1._1._2.mkString(" "),
          parsePipes(cv._2.mkString(" ")).isRight match {
            case true => parsePipes(cv._2.mkString(" ")).right.get
            case _    => cv._2.mkString(" ")
          }
        )),
        parsePipes(falseValue.mkString("")).isRight match {
          case true => parsePipes(falseValue.mkString(" ")).right.get
          case _    => falseValue.mkString("")
        }, asName)
  }
//this parser can be used to get the results
  val caseExpression = caseConditionExpresionParser | funcIdentifier

def parsePipes(input: String): Either[Seq[ParsingError], String] = {
    parse(caseExpression, input) match {
      case Success(parsed, _) => Right(parsed.asStr)
      case Failure(msg, next) => Left(Seq(ParsingError(s"Failed to parse $pipedStr: $msg, next: ${next.source}.")))
      case Error(msg, next)   => Left(Seq(ParsingError(s"Error in $pipedStr parse: $msg, next: ${next.source}.")))
    }
  }