hello rakoons,

I want to be able to parse this:

CSV.parse(
    '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.say;

I wrote this simple Grammar and action

grammar CSV {
    rule  TOP {<line>* %% \n  }
    token line { <col>* %% ',' }
    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 ~$/ }
    # method col:sym<quoted> ($/) { make S:g/'""'/'"'/ with ~$/ }
}

And it *almost* work. the thing I try to do now is to replace the "" in
quoted strings by a simple ".

when I use

    method col:sym<quoted> ($/) { make S:g/'""'/'"'/ with ~$/ }

instead of

    method col:sym<quoted> ($/) { make ~$/ }

I get this error message:

    Cannot assign to a readonly variable or a value
      in method col:sym<quoted> at whole.raku line 17
      in regex col:sym<quoted> at whole.raku line 7
      in regex col at whole.raku line 5
      in regex line at whole.raku line 4
      in regex TOP at whole.raku line 3
      in block <unit> at whole.raku line 30

I just don't understand why so i come with 2 questions
(or 4):

* how to fix it (and why isn't it working) ?
* am I right when i feel there is a way to do this substitution inside
  the grammar (and is it a good idea) ?

regards,
marc

Reply via email to