develooper Front page | perl.perl6.users | Postings from December 2021

Grammar Help

Thread Next
From:
Paul Procacci
Date:
December 26, 2021 06:01
Subject:
Grammar Help
Message ID:
CAFbbPui1zp0WpuZE8GNvoxAAr3kYCP+5pv2Ri832vrN02fK3Zw@mail.gmail.com
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
-- 
__________________

:(){ :|:& };:

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About