| 1 |
(use parser.peg) |
| 2 |
(use gauche.charconv) |
| 3 |
(use srfi.13) |
| 4 |
(use srfi.42) |
| 5 |
|
| 6 |
(define data |
| 7 |
(let1 s (call-with-input-file "data/13tokyo.csv" |
| 8 |
port->string |
| 9 |
:encoding 'utf8) |
| 10 |
(string-concatenate (make-list 10 s)))) |
| 11 |
|
| 12 |
(define csv-parser |
| 13 |
(let () |
| 14 |
(define ws ($skip-many ($one-of #[ \t]))) |
| 15 |
(define comma ($seq ws ($char #\,) ws)) |
| 16 |
(define dquote ($char #\")) |
| 17 |
(define double-dquote ($do [($string "\"\"")] ($return #\"))) |
| 18 |
(define quoted-body ($many ($or ($one-of #[^\"]) double-dquote))) |
| 19 |
(define quoted ($between dquote quoted-body dquote)) |
| 20 |
(define unquoted ($sep-by ($many1 ($one-of #[^ \t\r\n,])) |
| 21 |
($many_ ($one-of #[ \t])))) |
| 22 |
(define field ($or quoted unquoted)) |
| 23 |
(define record ($sep-by ($->rope field) comma 1)) |
| 24 |
($sep-by record newline))) |
| 25 |
|
| 26 |
;(profiler-start) |
| 27 |
(time (peg-parse-string csv-parser data)) |
| 28 |
;(profiler-stop) |
| 29 |
|
| 30 |
;(profiler-show) |
| 31 |
|
| 32 |
#| |
| 33 |
(add-load-path ".") |
| 34 |
(use peg-orig) |
| 35 |
|
| 36 |
(define csv-parser |
| 37 |
(let* ((spaces ($many ($one-of #[ \t]))) |
| 38 |
(comma ($seq spaces ($char #\,) spaces)) |
| 39 |
(dquote ($char #\")) |
| 40 |
(double-dquote ($do (($string "\"\"")) ($return #\"))) |
| 41 |
(quoted-body ($many ($or ($one-of #[^\"]) double-dquote))) |
| 42 |
(quoted ($between dquote quoted-body dquote)) |
| 43 |
(unquoted ($alternate ($one-of #[^ \t\n,]) spaces)) |
| 44 |
(field ($or quoted unquoted)) |
| 45 |
(record ($sep-by ($->rope field) comma 1))) |
| 46 |
($sep-by record newline))) |
| 47 |
|
| 48 |
(time (parse-string csv-parser data)) |
| 49 |
|# |
| 50 |
|
| 51 |
;;;============================================================ |
| 52 |
(use text.csv) |
| 53 |
(define reader (make-csv-reader #\,)) |
| 54 |
|
| 55 |
(time (port->list reader (open-input-string data))) |