我正在尝试将包含由分隔符分隔的字符串的单行解析为这些字符串的序列。它应该能够在字符串中包含任何字符,如果字段包含分隔符,则需要在其周围加上双引号。为了在这样的字段中使用双引号,双引号由 . 转义。
我的语法如下所示:
class CsvParser(val input: ParserInput, val delimiter: String = ",") extends Parser {
def line: Rule1[Seq[String]] = rule {record ~ EOI}
def record = rule(oneOrMore(field).separatedBy(delimiter))
def QUOTE = "\""
def ESCAPED_QUOTE = "\\\""
def DELIMITER_QUOTE = delimiter+"\""
def WS = " \t".replace(delimiter, "")
def field = rule{whiteSpace ~ ((QUOTE ~ escapedField ~ QUOTE) | unquotedField) ~ whiteSpace}
def escapedField = rule { capture(zeroOrMore(noneOf(QUOTE) | ESCAPED_QUOTE)) ~> (_.replace(ESCAPED_QUOTE, QUOTE)) }
def unquotedField = rule { capture(zeroOrMore(noneOf(DELIMITER_QUOTE))) }
def whiteSpace = rule(zeroOrMore(anyOf(WS)))
}
当我打电话给"quote\"key",1,2
我时Invalid input 'k', expected whiteSpace, ',' or 'EOI' (line 1, column 9)
我究竟做错了什么?我将如何调试这个?(作为一个额外的问题:我将如何扩展语法以允许分隔符成为多个字符,例如##
?)
谢谢!