Skip to content

Instantly share code, notes, and snippets.

@b2gills

b2gills/csv.p6 Secret

Created November 3, 2017 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b2gills/6ef4b37353fb10a99a955fabd843c210 to your computer and use it in GitHub Desktop.
Save b2gills/6ef4b37353fb10a99a955fabd843c210 to your computer and use it in GitHub Desktop.
#! /usr/bin/env perl6
use v6.c;
grammar CSV {
token TOP {
<line>* %% "\n"
}
token line {
^^
<element>* %% 「,」
$$
}
token element {
[
|| <string>
|| <-[,\n]>
]*
}
token string {
「"」 ~ 「"」
[
|| 「\"」+
|| <-["]>
]*
}
}
class Export {
method TOP ($/) {
make @<line>».made.List
}
method line ($/) {
make @<element>».made.List
}
method element ($/) {
make( $<string> ?? $<string>[0].made !! $/.trim )
}
method string ($/) {
make $/.substr(1,*-1).subst:
:global,
「\"」, #"
'"'
}
}
my \result = CSV.parse: slurp, :actions(Export);
for result<line> -> $line {
put $line, ':';
for $line.made -> $element {
say " [$element]"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment