使用Scala组合器解析创建对象的实例

时间:2020-05-26 19:11:42

标签: scala parsing parser-combinators

我有这个解析器:

import scala.util.parsing.combinator.JavaTokenParsers

class RequestMappingParser extends JavaTokenParsers {
  def requestMapping: Parser[Any] = "@RequestMapping(" ~ commaDelimitedSeq ~ ")"
  def commaDelimitedSeq: Parser[Any] = repsep(keyValue, ",")
  def keyValue: Parser[Any] = key ~ "=" ~ value
  def key: Parser[Any] = "value" | "method"
  def value: Parser[Any] = """[^)]*""".r
}

我有这个简单的课程:

class MethodRequestMapping(val value: String, val method: String)

我的解析器可以解析此字符串:

"@RequestMapping(value = \"/ex/foos\", method = RequestMethod.GET)"

,并返回结果Parser[Any]。我希望它返回Parser[MethodRequestMapping]的结果。

我该怎么做?我知道我必须做这样的事情:

def requestMapping: Parser[MethodRequestMapping] = "@RequestMapping(" ~ commaDelimitedSeq ~ ")" ^^ {
  // Some sort of pattern matching and creation of MethodRequestMapping
  // ???
}

但是,发生了什么?还是最好以完全不同的方式来做?

1 个答案:

答案 0 :(得分:2)

首先,将您使用的组合词的一半创建为Parser[String],并且是类型注释将它们组合成Parser[Any](由于协方差而被允许,但在这种情况下完全没有用) )。

所以您实际上有类似的东西:

class RequestMappingParser extends JavaTokenParsers {
  def requestMapping: Parser[String ~ List[String ~ String ~ String] ~ String] = "@RequestMapping(" ~ commaDelimitedSeq ~ ")"
  def commaDelimitedSeq: Parser[List[String ~ String ~ String]] = repsep(keyValue, ",")
  def keyValue: Parser[String ~ String ~ String] = key ~ "=" ~ value
  def key: Parser[String] = "value" | "method"
  def value: Parser[String] = """[^)]*""".r
}

然后我们可以使用map^^来调整类型-在这之前很难,因为我们在那里有Any,但是如果我们不放弃类型,我们将能够轻松进行图案匹配

class RequestMappingParser extends JavaTokenParsers {
  // result of this parser will be ethier "value" or "method"
  def key: Parser[String] = "value" | "method"
  def value: Parser[String] = """[^),]*""".r // you had an error here, you were missing "," to separate items
  // here we'll map String ~ String ~ String to tuple - we'll drop unused "=" in the process
  def keyValue: Parser[(String, String)] = (key ~ "=" ~ value).map {
    case k ~ _ ~ v => k -> v
  }
  // repsep wil return List[(String, String)], which we'll map to Map
  def commaDelimitedSeq: Parser[Map[String, String]] = repsep(keyValue, ",").map(_.toMap)
  // this would give us String ~ Map[String, String] ~ String, but we don't need
  // these String constants. Also we can map things into out final result
  def requestMapping: Parser[MethodRequestMapping] = ("@RequestMapping(" ~ commaDelimitedSeq ~ ")").map {
    case _ ~ map ~ _ =>
      new MethodRequestMapping(value = map("value"), method = map("method"))
  }
}
val parser = new RequestMappingParser()
val parsed = "@RequestMapping(value = \"/ex/foos\", method = RequestMethod.GET)"
val result = parser.parse(parser.requestMapping, parsed).get
// result.value == "\"/ex/foos\""
// result.method == "RequestMethod.GET"

也就是说-不鼓励使用此解析器组合库,主要是因为速度较慢。对于生产,建议使用FastParse之类的东西,它具有更快的实现和更多的功能。