-
Notifications
You must be signed in to change notification settings - Fork 2
/
csv_parser.rb
41 lines (33 loc) · 882 Bytes
/
csv_parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
require 'parsby'
# This is based on:
#
# RFC 4180: Common Format and MIME Type for Comma-Separated Values (CSV) Files
module Parsby::Example
module CsvParser
include Parsby::Combinators
extend self
def parse(io)
csv.parse io
end
define_combinator :csv do
many(record) < eof
end
define_combinator :record do
sep_by(lit(","), cell) < (eol | eof)
end
define_combinator :cell do
quoted_cell | non_quoted_cell
end
define_combinator :quoted_cell do
non_quote = join(many(any_char.that_fail(lit('"'))))
inner = sep_by(lit('""'), non_quote).fmap {|r| r.join '"' }
lit('"') > inner < lit('"')
end
define_combinator :non_quoted_cell do
join(many(any_char.that_fail(lit(",") | lit("\"") | eol)))
end
define_combinator :eol do
lit("\r\n") | lit("\n")
end
end
end