I think/hope that Perl6 will have adequate introspection. But you'd have to ensure that a match-object contains all the information needed to fully constrain the generated output. Consider
rule foo:w { $a := (\d+) = $b := (\w+) [ , | and ] $c := (\w+) }; dump( rule=>"foo"; a=>7, b=>"hello", c=>"world" );
After substituting the values into the captures we then are left with:
\s+ 7 \s+ hello [ \s* , \s* | \s+ and \s+ ] world
So the dumper now needs to make two sets of decisions: what whitespace to substitute for the \ss; and whether to use a comma or the word "and" as the final separator. The whitespace issue could be defined as a standard pretty-print policy. But in the absence of any further hints, the dump function can probably do no better than choose an arbitrary (e.g. always first) member of an uncaptured alternation

So yes, I definitely think it could be done; but we may want a mechanism to set the policy for the unconstrained parts of a rule. A simple set of rules, such as "generate as little text as possible"; and "choose first option on alternations" would probably be enough for simple expressions. But what about look-ahead assertions; or embedded code: these things would be much harder to define simple rules for. Perhaps our dump function would require anything non-trivial to be captured.

An XML grammar would probably be a nice thing to use to play with the idea: more complex than your pack/unpack (BTW: I like that idea); but much simpler than, say, a full Perl6 grammar. --Dave


In reply to Re: Using Perl6 patterns/grammar definition for <I>output</I>? by dpuu
in thread Using Perl6 patterns/grammar definition for 'output'? by John M. Dlugosz

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.