| 1 |
(use gauche.vm.profiler) |
| 2 |
(use parser.peg) |
| 3 |
(use text.csv) |
| 4 |
|
| 5 |
(define data |
| 6 |
(let1 d (call-with-input-file "data/13tokyo.csv" |
| 7 |
port->string |
| 8 |
:encoding 'utf8) |
| 9 |
(string-append d d d d d))) |
| 10 |
|
| 11 |
(define csv-parser |
| 12 |
(let* ((spaces ($many ($one-of #[ \t]))) |
| 13 |
(skip-spaces ($skip-many ($one-of #[ \t]))) |
| 14 |
(comma ($seq skip-spaces ($char #\,) skip-spaces)) |
| 15 |
(dquote ($char #\")) |
| 16 |
(double-dquote ($do (($string "\"\"")) ($return #\"))) |
| 17 |
(quoted-body ($many ($or ($one-of #[^\"]) double-dquote))) |
| 18 |
(quoted ($between dquote quoted-body dquote)) |
| 19 |
(unquoted ($sep-by ($one-of #[^ \t\n,]) spaces)) |
| 20 |
(field ($or quoted unquoted)) |
| 21 |
(record ($sep-by ($->rope field) comma 1))) |
| 22 |
($sep-by record newline))) |
| 23 |
|
| 24 |
(profiler-start) |
| 25 |
(peg-parse-string csv-parser data) |
| 26 |
(profiler-stop) |
| 27 |
(profiler-show) |
| 28 |
(profiler-reset) |
| 29 |
|
| 30 |
(define reader (make-csv-reader #\,)) |
| 31 |
|
| 32 |
(profiler-start) |
| 33 |
(call-with-input-string data (^p (generator-for-each identity (cut reader p)))) |
| 34 |
(profiler-stop) |
| 35 |
(profiler-show) |