-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcsv_gram.pl
130 lines (117 loc) · 3.81 KB
/
csv_gram.pl
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use v6;
class Text::CSV {
has Str $.quote_char is rw = '"';
has Str $.escape_char is rw = '"';
has Str $.sep_char is rw = ',';
has Str $.eol is rw; # = ($*IN.newline),
has Bool $.always_quote is rw;
has Bool $.quote_space is rw = True;
has Bool $.quote_null is rw = True;
has Bool $.quote_binary is rw = True;
has Bool $.binary is rw;
has Bool $.keep_meta_info is rw;
has Bool $.allow_loose_quotes is rw;
has Bool $.allow_loose_escapes is rw;
has Bool $.allow_whitespace is rw;
has Bool $.blank_is_undef is rw;
has Bool $.empty_is_undef is rw;
has Bool $.verbatim is rw;
has Bool $.auto_diag is rw;
class CSV_Actions {
method fields($/){
make $<field>>><text>>>.Str;
}
method line($/){
make $<fields>.ast;
}
}
method compose {
my $q = $!quote_space;
my $s = $!sep_char;
my $e = $!escape_char;
my $l = $!eol;
grammar {
token lines { <line>* }
token line { <fields> <lineend> }
token fields { <field>* % <separator> }
token field { <quote> : $<text>=<quotedvalue> <quote> | $<text>=<value> }
token value { [ <-separator> & <-quote> & <-lineend> ] * }
token quotedvalue { <-quote> * }
token separator { "$s" }
token quote { "$q" }
token escape { "$e" }
token lineend { $l }
}
}
has $!gram = self.compose();#.new;
has $!ast;
method parse(Str:D $line){
#say nqp::objectid($!gram);
$!ast = $!gram.parse($line, :rule<fields>, :actions(CSV_Actions)).ast;
}
method getline(){
$!ast;
}
}
sub MAIN(
Str :$quote_char = '"',
Str :$escape_char = '"',
Str :$sep_char = ',',
Str :$eol, # = ($*IN.newline),
Bool :$always_quote,
Bool :$quote_space = True,
Bool :$quote_null = True,
Bool :$quote_binary = True,
Bool :$binary,
Bool :$keep_meta_info,
Bool :$allow_loose_quotes,
Bool :$allow_loose_escapes,
Bool :$allow_whitespace,
Bool :$blank_is_undef,
Bool :$empty_is_undef,
Bool :$verbatim,
Bool :$auto_diag,
) {
my $csv_parser = Text::CSV.new
## :$quote_char
## :$escape_char
## :$sep_char
## :$eol
## :$always_quote
## :$quote_space
## :$quote_null
## :$quote_binary
## :$binary
## :$keep_meta_info
## :$allow_loose_quotes
## :$allow_loose_escapes
## :$allow_whitespace
## :$blank_is_undef
## :$empty_is_undef
## :$verbatim
## :$auto_diag
;
$csv_parser.parse(q/ab,cde,"q",/);
say $csv_parser.getline().perl;
my $csv_parser2 = Text::CSV.new :sep_char<e> ;
$csv_parser2.parse(q/ab,cde"q"e/);
say $csv_parser2.getline().perl;
$csv_parser.parse(q/ab,cde,"q",/);
say $csv_parser.getline().perl;
my $csv_parser3 = Text::CSV.new :sep_char<,> ;
$csv_parser2.parse(q/ab,cde"q"e/);
say $csv_parser2.getline().perl;
#$csv_parser.parse(q/ab,cde"q"eaa"aaarghh/);
#$csv_parser.parse(q/ab,cde"q"e"aaarghh/);
## $csv_parser.sep_char=',';
my $sum = 0;
for lines() :eager {
$csv_parser.parse($_);
my $r = $csv_parser.getline();
# say $r.perl;
# say +$r;
$sum += +$r;
#last;
}
say $sum;
}