简体   繁体   English

在Scala中使用解析器组合器时,如何将任意数量的项目转换为对象?

[英]How do I convert an arbitrary number of items to objects when using parser combinators in Scala?

I have been playing with Scala's combinators and parsers, and have a question that may be too elementary (apologies if it is). 我一直在与Scala的组合器和解析器一起玩,并且有一个问题可能太简单了(如果是的话,很抱歉)。 I have written it out in this code to make the question easy to understand and my question is at the end. 我已经在这段代码中写了出来,以使问题易于理解,而我的问题就在结尾。

import scala.util.parsing.combinator._
// First, I create a case class
case class Human(fname: String, lname: String, age: Int)
// Now, I create a parser object
object SimpleParser extends RegexParsers {
 def fname: Parser[String] = """[A-Za-z]+""".r ^^ {_.toString}
 def lname: Parser[String] = """[A-Za-z]+""".r ^^ {_.toString}
 def age: Parser[Int] = """[1-9][0-9]{0,2}""".r ^^ {_.toInt}
 def person: Parser[Human] = fname ~ lname ~ age ^^ {case f ~ l ~ a => Human(f, l, a)}
// Now, I need to read a list of these, not just one. 
// How do I do this? Here is what I tried, but can't figure out what goes inside 
// the curly braces to the right of ^^
// def group: Parser[List[Human]] = rep(person) ^^ {}
// def group: Parser[List[Human]] = (person)+ ^^ {}
}

// Here is what I did to test things:
val s1 = "Bilbo Baggins 123"
val r = SimpleParser.parseAll(SimpleParser.person, s1)
println("First Name: " + r.get.fname)
println("Last Name: " + r.get.lname)
println("Age: " + r.get.age) 
// So that worked well; I could read these things into an object and examine the object,
// and can do things with the object now.
// But how do I read either of these into, say, a List[Human] or some collection?
val s2 = "Bilbo Baggins 123 Frodo Baggins 40 John Doe 22"
val s3 = "Bilbo Baggins 123; Frodo Baggins 40; John Doe 22"

If there is something very obvious I missed please let me know. 如果有什么很明显的我想念的地方,请告诉我。 Thanks! 谢谢!

You were very close. 你很亲近 For the space-separated version, rep is all you need: 对于以空格分隔的版本, rep是您所需要的:

lazy val people: Parser[List[Human]] = rep(person)

And for the version with semicolons, you can use repsep : 对于带有分号的版本,可以使用repsep

lazy val peopleWithSemicolons: Parser[List[Human]] = repsep(person, ";")

Note that in both cases rep* returns the result you want—there's no need to map over the result with ^^ . 请注意,在两种情况下rep*返回所需的结果-无需使用^^映射结果。 This is also the case for fname and lname , where the regular expression will be implicitly converted into a Parser[String] , which means that mapping _.toString doesn't actually change anything. 对于fnamelname也是这种情况,其中正则表达式将隐式转换为Parser[String] ,这意味着_.toString映射_.toString不会更改任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM