Permalink
Browse files

= Improve CsvParser example, closes #108

  • Loading branch information...
sirthias committed Feb 18, 2015
1 parent be254e1 commit c85faceb62c3ad5235da14c65eac5f91cc9f5120
@@ -54,17 +54,17 @@ class CsvParser(val input: ParserInput, headerPresent: Boolean, fieldDelimiter:
def header = rule { record }
def record = rule { oneOrMore(field).separatedBy(fieldDelimiter ~ OWS) ~> Record }
def record = rule { oneOrMore(field).separatedBy(fieldDelimiter) ~> Record }
def field = rule { `quoted-field` | `unquoted-field` }
def `quoted-field` = rule {
'"' ~ clearSB() ~ zeroOrMore((QTEXTDATA | '"' ~ '"') ~ appendSB()) ~ '"' ~ OWS ~ push(sb.toString)
OWS ~ '"' ~ clearSB() ~ zeroOrMore((QTEXTDATA | "\"\"") ~ appendSB()) ~ '"' ~ OWS ~ push(sb.toString)
}
def `unquoted-field` = rule { capture(zeroOrMore(TEXTDATA)) }
def NL = rule { optional('\r') ~ '\n' ~ OWS }
def NL = rule { optional('\r') ~ '\n' }
def OWS = rule { zeroOrMore(' ') }
}
@@ -27,15 +27,15 @@ class CsvParserSpec extends Specification {
""""first_name","last_name","company_name","address","city","county","state","zip","phone1","phone2","email","web"
|"James","Butt", "Benton, John B Jr","6649 N Blue Gum St","New Orleans","Orleans","LA",70116,"504-621-8927","504-845-1427","jbutt@gmail.com","http://www.bentonjohnbjr.com"
|"Josephine","Darakjy","Chanay, Jeffrey A Esq","4 B Blue Ridge Blvd","Brighton","Livingston","MI",48116,"810-292-9388","810-374-9840","josephine_darakjy@darakjy.org","http://www.chanayjeffreyaesq.com"
|Art,"Venere","Chemel, James L Cpa","8 W Cerritos Ave #54","Bridgeport","Gloucester","NJ","08014","856-636-8749","856-264-4130","art@venere.org","http://www.chemeljameslcpa.com"
|Art,"Venere","Chemel, James L Cpa","8 W Cerritos Ave #54","Bridgeport","Gloucester","NJ",08014 ,"856-636-8749","856-264-4130","art@venere.org","http://www.chemeljameslcpa.com"
|"Lenna","Paprocki","Feltz ""Printing"" Service", 639 Main St,"Anchorage","Anchorage","AK",99501,"907-385-4412","907-921-2010","lpaprocki@hotmail.com","http://www.feltzprintingservice.com"
""".stripMargin
|""".stripMargin
} === file(
record("first_name","last_name","company_name","address","city","county","state","zip","phone1","phone2","email","web"),
record("James","Butt", "Benton, John B Jr","6649 N Blue Gum St","New Orleans","Orleans","LA","70116","504-621-8927","504-845-1427","jbutt@gmail.com","http://www.bentonjohnbjr.com"),
record("Josephine","Darakjy","Chanay, Jeffrey A Esq","4 B Blue Ridge Blvd","Brighton","Livingston","MI","48116","810-292-9388","810-374-9840","josephine_darakjy@darakjy.org","http://www.chanayjeffreyaesq.com"),
record("Art","Venere","Chemel, James L Cpa","8 W Cerritos Ave #54","Bridgeport","Gloucester","NJ","08014","856-636-8749","856-264-4130","art@venere.org","http://www.chemeljameslcpa.com"),
record("Lenna","Paprocki","Feltz \"Printing\" Service","639 Main St","Anchorage","Anchorage","AK","99501","907-385-4412","907-921-2010","lpaprocki@hotmail.com","http://www.feltzprintingservice.com"))
record("Art","Venere","Chemel, James L Cpa","8 W Cerritos Ave #54","Bridgeport","Gloucester","NJ","08014 ","856-636-8749","856-264-4130","art@venere.org","http://www.chemeljameslcpa.com"),
record("Lenna","Paprocki","Feltz \"Printing\" Service"," 639 Main St","Anchorage","Anchorage","AK","99501","907-385-4412","907-921-2010","lpaprocki@hotmail.com","http://www.feltzprintingservice.com"))
}
}

0 comments on commit c85face

Please sign in to comment.