Re: fixing the documentation
Hello, > The best would be if you propose a PR or open an issue at > https://github.com/Raku/doc. Any help with the documentation would > most certainly be appreciated as people working on the docs project > are overloaded. Sorry I was late on this because I wasn't sure how to revamp the whole thing. so i just simplified the 5to6 page this way: https://github.com/Raku/doc/commit/cd32380ab4e1c5ad2017d60a31def9189b54c80f Simple changes are better anyway. thanks. marc
can't make from a S/// ?
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 {* %% \n } token line { * %% ',' } proto token col {*} token col:sym { <-[\n",]>* } token col:sym { '"' ~ '"' [ <( [ '""' | <-[\n"]> ]* )> ] } } class CSV_as_table { method TOP ($/) { make $/.map: *.made } method line ($/) { make $/.map: *.made } method col:sym ($/) { make ~$/ } method col:sym ($/) { make ~$/ } # method col:sym ($/) { 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 ($/) { make S:g/'""'/'"'/ with ~$/ } instead of method col:sym ($/) { make ~$/ } I get this error message: Cannot assign to a readonly variable or a value in method col:sym at whole.raku line 17 in regex col:sym 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 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
Re: can't make from a S/// ?
On Fri, Nov 19, 2021 at 1:19 AM Marc Chantreux wrote: > > method col:sym ($/) { make S:g/'""'/'"'/ with ~$/ } > > Cannot assign to a readonly variable or a value > > why isn't it working? 1. Even though `S` politely leaves "the current topic" (`$_`) alone, it still generates a match object, and then binds that to "the current match object" (`$/`). 2. In your method's signature you include `$/` as a parameter. Like all such parameters, it defaults to being a read-only one. See also: * https://stackoverflow.com/questions/40970080/ perl6-rakudo-2016-11-match-tries-to-assign-to-read-only-variable-why-not-in-201 * https://github.com/rakudo/rakudo/issues/1235 > how to fix it I'm not saying the following is how to fix it. But I'll show code that works for your example. The key thing is that, if you're going to use `S///`, you need to deal with the fact it will overwrite `$/`. Note that the answer is NOT to make the `$/` parameter writable using `is copy` or `is rw`. The first argument that is passed to a method in an actions class is the match object that's just been matched. And you need that so you can hang some data off of it using `make`. You do not want it clobbered! Here's what I suggest you consider: method col:sym ($_) { .make: S:g/'""'/"/ } What I've done here is to switch from `$/` to `$_`. This: * Frees up `$/`. So the `S///` can now clobber it. * Means I have to write `.make:` instead of `make`. I could have changed the `with ~$/` to `with ~$_` but it's already the topic (and gets coerced to a string). > am I right when i feel there is a way to do this > substitution inside the grammar As I've shown, yes. But it draws you into the `$/` dance. > is it a good idea? First, in a comment in the issue I linked above, jnthn writes: > using subst [ or `S///`] inside of an action method would imply re-parsing, which one should think twice about anyway. Second, I've no idea where the `$/` that `S///` clobbers comes from. If we could confirm what I wrote is safe, then perhaps it's one appropriate idiomatic response to the issue. If not, the following clumsy solution might be the way to go: method col:sym ($_) { my $/; .make: S:g/'""'/"/ } -- love, raiph
Re: fixing the documentation
Thanks a lot. Cheers El vie, 19 nov 2021 a las 0:08, Marc Chantreux () escribió: > Hello, > > > The best would be if you propose a PR or open an issue at > > https://github.com/Raku/doc. Any help with the documentation would > > most certainly be appreciated as people working on the docs project > > are overloaded. > > Sorry I was late on this because I wasn't sure how to revamp the whole > thing. so i just simplified the 5to6 page this way: > > https://github.com/Raku/doc/commit/cd32380ab4e1c5ad2017d60a31def9189b54c80f > > Simple changes are better anyway. > > thanks. > marc > -- JJ