Hey all,
Firstly, I want to thank everyone for all their responses. It helped
greatly.
I wanted to share what I ended up with that seems to be working. (below)
I'm open to suggestions on how to improve this, tidy things up a bit, etc.
Running on the below grammar yields the following what to me seems perfect:
% raku ./test2.raku < test.data
{objectKey => {a => 1234, anotherObjectKey => {b => "45934"}, b => 5345,
newobjectKey => {a => 1534, b => "asdf"}}}
Thanks all for taking the time to respond.
~Paul
---- data ----
objectKey:
{
a = 1234;
b = 5345;
newobjectKey:
{
a = 1534;
b = "asdf";
}
anotherObjectKey:
{
b = "45934";
}
}
---- end data ----
----- logic -----
grammar Test {
rule TOP { <object> }
rule object { <objectKey> <objectBody> }
rule objectKey { <cstr> ':' }
rule objectBody { '{' ~ '}' <item>+ }
rule pair { <cstr> '=' <value> ';' }
token cstr { <alpha>+ }
token number { <[0..9]>+ }
token string { '"' ~ '"' <-["]>+ }
proto token item { * };
token item:sym<object> { <object> }
token item:sym<pair> { <pair> }
proto token value { * };
token value:sym<number> { <number> }
token value:sym<string> { <string> }
}
class TestActions {
method TOP($/) { make $<object>.made; }
method object($/) { make $<objectKey>.made => $<objectBody>.made; }
method objectBody($/) { make $<item>>>.made.hash.item; }
method pair($/) { make $<cstr>.made => $<value>.made; }
method objectKey($/) { make $<cstr>.made; }
method cstr($/) { make ~$/; }
method string($/) { make ~$/; }
method number($/) { make ~$/; }
method item:sym<pair>($/) { make $<pair>.made; }
method item:sym<object>($/) { make $<object>.made; }
method value:sym<number>($/) { make ~$/; }
method value:sym<string>($/) { make ~$/; }
}
----- end logic -----
On Sun, Dec 26, 2021 at 1:01 AM Paul Procacci <[email protected]> wrote:
> Hey all,
>
> Twas the night of Christmas, when all through the house, not a creature
> was stirring except Paul w/ his mouse.
>
> Hope everyone had a Merry Christmas and takes likings to corny opening
> statements. ;)
>
> I was writing a little something tonight using Grammars and ran into
> something that I can't seem to wrap my head around. I'm hoping someone
> could explain in detail.
>
> Given the following data:
> ---- data -----
> objectKey:
> {
> a = "bi";
> b = "hi";
> }
> ---- end data -----
>
>
> .... and the following logic partially taken from JSON::Tiny:
>
> ---- code ----
> grammar myTest {
> token TOP { \s* <object> \s* }
> rule object { <objectKey> '{' <pairlist> '}' }
> # rule object { <objectKey> '{' ~ '}' <pairlist> }
> rule objectKey { <cstr> ':' }
> rule pairlist { <pair> * % \; }
> rule pair { <cstr> '=' <value> }
> token cstr { <alpha>+ }
> token value { '"' ~ '"' <alpha>* }
> }
>
> class myTestActions {
> method TOP($/) {
> make $<pairlist>.made.hash.item;
> }
>
> method object($/) {
> say 'hello';
> }
>
> method objectKey($/) {
> make $<cstr>.made;
> }l
> method pairlist($/) {
> make $<pair>>>.made.flat;
> }
>
> method pair($/) {
> make $<cstr>.made => $<value>.made;
> }
>
> method cstr($/) { make ~$/ }
> method value($/) { make ~$/ }
> }
> ---- code ----
>
>
> ... it'd be my hopes that this would match. However, It's not matching on
> 'object' and I can't seem to figure out why.
>
> Adding Grammar::Tracer yields the following:
>
> TOP
> | object
> | | objectKey
> | | | cstr
> | | | * MATCH "objectKey"
> | | * MATCH "objectKey:\n"
> | | pairlist
> | | | pair
> | | | | cstr
> | | | | * MATCH "a"
> | | | | value
> | | | | * MATCH "\"bi\""
> | | | * MATCH "a = \"bi\""
> | | | pair
> | | | | cstr
> | | | | * MATCH "b"
> | | | | value
> | | | | * MATCH "\"hi\""
> | | | * MATCH "b = \"hi\""
> | | | pair
> | | | | cstr
> | | | | * FAIL
> | | | * FAIL
> | | * MATCH "a = \"bi\";\n\tb = \"hi\""
> | * FAIL
> * FAIL
>
> What exactly am I doing wrong? Does '{' ~ '}' not work as I expect here?
> Appreciate any insight.
>
> Thanks,
> Paul
> --
> __________________
>
> :(){ :|:& };:
>
--
__________________
:(){ :|:& };: