LaurentRosenfeld / Perl-6-Miscellaneous

2 stars 5 forks source link

Re: URL-normalization #1

Closed jaldhar closed 5 years ago

jaldhar commented 5 years ago

My solution missed the challenge deadline but I did complete it in the end and blogged about it. So if you would like to take a look,

https://www.braincells.com/perl/2019/08/perl_weekly_challenge_week_21.html

I used the approach of creating a grammar as some other participants did but I think there could be a lot of room for improvement so your comments would be most welcome.

LaurentRosenfeld commented 5 years ago

OK, Jaldhar, thank you for informing me, I'll look at your blog and will update my post accordingly.

LaurentRosenfeld commented 5 years ago

Hi Jaldhar,

I am not sure to understand the problem you encountered, but, in general, when you want to insert a modified component into your AST, it is usually best to do it in the action corresponding to the rule level immediately above.

For example, if you take the JSON grammar described in by P6 book (which can be found for free on this Github repository, in LaTeX and PDF formats), some of the rules are as follows:

rule object    { '{' \s* <pairlist> '}' \s* }
rule pairlist  {  <pair> * % \, }
rule pair      {  <string>':' <value> }

Now look at the corresponding actions:

method object($/) {
    make $<pairlist>.made.hash.item;
}
method pairlist($/) {
    make $<pair>».made.flat;
}
method pair($/) {
    make $<string>.made => $<value>.made;
}

As you can see, an object is notably composed of a pairlist. And the code makes pairlist in the object method. Similarly, a pairlist is made of possibly several pairs, and the pairs are made in the pairlist method. And string is made in the pair method.

I'm not saying that you must follow this guideline (I just don't know whether this is actually the best way), but that's definitely what worked best for me.

In your case, if you want to do something to PctEncoded, it is probably a good idea to try do it at the level immediately above, i.e. in the UserinfoChar action method.

(The alternative is to insert the actions within the grammar, but that tends get messy for anything larger than a very simple grammar.)

As a side note, you have:

token HexOctet { <[ 0..9 A..F a..f ]> ** 2 }

Please note that Perl 6 has an xdigit predefined character class.

I hope this helps.

(Note: I'll update the blog a bit later.)

Best regards, Laurent.

LaurentRosenfeld commented 5 years ago

Oops, closed the issue by mistake. Now reopened.