Skip to content

Instantly share code, notes, and snippets.

@toshikazuhorii
Created October 12, 2011 11:51
Show Gist options
  • Save toshikazuhorii/1281026 to your computer and use it in GitHub Desktop.
Save toshikazuhorii/1281026 to your computer and use it in GitHub Desktop.
object CsvParser3 extends RegexParsers {
def eol = '\n'
def cell = "\"" ~> "[^\"]*".r <~ "\""
def row = repsep(cell, ",") <~ eol
def headerRow = row ^^ { cells => new HeaderRow(cells) }
def dataRow = ( rep( ("\"" ~> "[^\"]*".r <~ "\",") | ("[^,\n]*".r <~ ",") ) ~ ( ("\"" ~> "[^\"]*".r <~ "\"\n") | ("[^,\n]*".r <~ "\n") ) ) ^^ { cells => new DataRow(cells._1 ::: List(cells._2)) }
def all = headerRow ~ rep(dataRow) ^^ { res => res._1 :: res._2 }
def parse(input: String): ParseResult[List[Row]] = parseAll(all, input)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment