This channel is intended for people just starting with the Raku Programming Language (raku.org). Logs are available at irclogs.raku.org/raku-beginner/live.html
Set by lizmat on 8 June 2022.
rje I started learning Raku around the first week of April.  This week I wrote my first Grammar, but I am still not sure why it works the way it does. 20:59
The nice thing is, the Grammar and Actions total about 14 lines.  Is there a place here where I can paste in code? 21:01
Maybe there's a Raku channel on Discord. 21:02
Rob Eaglestone 鷲 石 Ah and here it is. 21:04
So here's what I did. Constructive criticism welcome. grammar GTXGrammar { rule TOP { \s* <context>+ } rule context { <key> '{' <pair>+ '}' } rule pair { <key> <value> } token key { <[\w-]>+ } token value { <-[\n]>+ } } class GTXActions { method TOP ($/) { make $<context>>>.made } method context ($/) { make $<key>.made =>
$<pair>>>.made.flat.hash } method pair ($/) { make $<key>.made => $<value>.made } method key ($/) { make ~$/ } method value ($/) { make ~$/ } }
So this parses a file as a two-layer hash, where leaf values are read as strings. 21:05
For example cow { radius 15 center -5,16 } default { order virus wave } virus { preserve-by-allegiances ImDd kill-hexes 1805 1905 2105 2109 2309 2405 2406 2407 2409 2411 2412 2413 2424 2425 2506 2512 2515 2523 2615 2713 2715 2716 2717 2721 2807 2810 2812 2813 2820 2905 2906 2907 2908 2910 2913 2918 3005 3008 3011 3013 3015 3016 3110 3111 3112 3113 3213 3115 3116 } 21:06
location { y -1 x -2 }
p6steve Grammar::Tracer is your friend 22:20
TOP | context | | key | | * MATCH "cow" | | pair | | | key | | | * MATCH "radius" | | | value | | | * MATCH "15" | | * MATCH "radius 15\n " | | pair | | | key | | | * MATCH "center" | | | value | | | * MATCH "-5,16" | | * MATCH "center -5,16\n" | | pair | | | key | | | * FAIL | | * FAIL | * MATCH "cow \{\n radius 15\n center -5,16\n}\n" 22:21
afaict this is a very strong first cut ... and yes, raku Grammars are killer 22:22
it seems a bit odd that <[\w-]> is not built in (this is a critique of raku) 22:26
there appears to be a bit of loose whitespace after the match 22:29
say $match.made[0]<cow><center>; #-5,16 23:00
my $match = GTXGrammar.parse($txt, actions => GTXActions.new); my %h = $match.made; say %h.keys; say %h<location>; 23:05
(default cow location virus) {x => -2, y => -1} 23:06
works for me ;-)