Hello dear monks,

I am trying to work my way through Perl grammars and, more specifically, actions objects.

I have written this grammar for a subset of JSON (as a shorter example of my larger problem):

grammar My-Grammar { token TOP { \s* <object> \s* } rule object { '{' \s* <pairlist> '}' \s* } rule pairlist { <pair> * % \, } rule pair { <string>':' <value> } token string { \" <[ \w \s \- ' ]>+ \" } token number { [ \d+ [ \. \d+ ]? ] | [ \. \d+ ] } token value { <object> | <string> | <number> | true | false | null } }
Used with the following JSON subset string and parsing call:
my $source-string = q/{ "firstName": "John", "lastName": "Smith", "isAlive": true, "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100" }, "spouse": null }/; my $match = My-Grammar.parse($source-string); say ~$match if $match; # say $match.made if $match;
I get the following stringified match object:
{ "firstName": "John", "lastName": "Smith", "isAlive": true, "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100" }, "spouse": null }
So parsing the string with the grammar seems to work correctly.

I have a problem when I try to add an actions object.

This is about the best actions class I could come up with so far:

class My-actions { method TOP($/) { make $/.values.[0].made; }; method object ($/) { make $<pairlist>.made.hash.item; } method pairlist($/) { make $<pair>>>.made.flat; } method pair($/) { make $<string>.made => $<value>.made ; } method string($/) { make ~$/; } method number($/) { make +$/.Str; } method value($/) { given ~$/ { when "true" {make Bool::True;} when "false" {make Bool::False;} when "null" {make Any;} default { make ~$/;} } } }
With this actions class, I get the following AST:
{"address" => { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100" }, "age" => 25, "firstName" => "John", "isAlive" => True, "lastName" + => "Smith", "spouse" => (Any)}
Except for the address component, the AST seems correct.

The address component is not transformed into AST nodes obviously because of this line:

default { make ~$/;}
in the value method of the actions class, which just stringifies the address sub-object. I have put that line so far as a temporary workaround just to get the overall shebang sort of working, but I have no idea how to get further. Specifically, I have tried many things, but I just can't figure out how to identify that the value is itself (recursively) an object and, even if I knew how to identify that, I still probably wouldn't know how to process it to produce a proper AST subtree for it.

Would a nice monk around here be kind enough to enlighten me on this last part?


In reply to Perl 6 grammars - setting the actions object by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.