Dear Marc, We may have version-specific issues, but I can't be certain at
the moment because you changed the first two lines of your Grammar.

#old:
    rule  TOP {<line>* %% \n  }
    token line { <col>* %% ',' }

#new:
  rule  TOP {<line>*  }
  token line { <col>* %% ',' \n }

If I change back to the #old lines above, and use `.subst` to correct
double-quoted strings, it all works:

grammar CSV {
    rule  TOP {<line>* %% \n  }
    token line { <col>* %% ',' }
    #rule  TOP {<line>*  }
    #token line { <col>* %% ',' \n }
    proto token col {*}
    token col:sym<bare>   { <-[\n",]>* }
    token col:sym<quoted> {
        '"' ~ '"'
        [<( [ '""' | <-[\n"]> ]* )>]
    }
}

class CSV_as_table {
    method TOP ($/)  { make $/<line>.map: *.made }
    method line ($/) { make $/<col>.map: *.made }
    method col:sym<bare>   ($/) { make ~$/ }
    #method col:sym<quoted> ($_) { .make: ~S:g/'""'/"/ }
    method col:sym<quoted> ($/) { make $/.subst(/'""'/, '"', :global) }
}

.raku.say for CSV.parse(
    '162,1,2,"Watt, Mrs. James (Elizabeth ""Bessie"" Inglis
Milne)",female,40,0,0,C.A. 33595,15.75,,S,
    162,1,2,"Watt, Mrs. James (Elizabeth ""Bessie"" Inglis
Milne)",female,40,0,0,C.A. 33595,15.75,,S,',
    actions => CSV_as_table.new,
).made;

OUTPUT:

("162", "1", "2", "Watt, Mrs. James (Elizabeth \"Bessie\" Inglis Milne)",
"female", "40", "0", "0", "C.A. 33595", "15.75", "", "S", "").Seq
("162", "1", "2", "Watt, Mrs. James (Elizabeth \"Bessie\" Inglis Milne)",
"female", "40", "0", "0", "C.A. 33595", "15.75", "", "S", "").Seq

Best, Bill.

PS It doesn't appear that your code handles embedded newlines, but this has
been interesting nevertheless. Here's a related link to a one-liner that
uses Raku's Text::CSV module:

https://unix.stackexchange.com/a/670358/227738

On Sat, Nov 20, 2021 at 9:12 PM Marc Chantreux <e...@phear.org> wrote:

> > On Sat, Nov 20, 2021 at 9:03 PM Marc Chantreux <e...@phear.org> wrote:
> > > > > method col:sym<quoted> ($/) { .make ~S:g/'""'/"/ }
> > > > That's not working for me. I'm on Moar (2021.06).
> > > works for me with:
> > > method col:sym<quoted> ($_) { .make: ~S:g/'""'/"/ }
>
> > Yeah, you got it wrong the first time.  To explicate, the key difference
> > is differently named parameters: ($/) vs ($_).
>
> Ohh ... sorry i didn't spot the problem because the relation between $_
> and the use of .make was too obvious to me. thank you for noticing!
>
> regards
> marc
>

Reply via email to