jsontestingrakuregexp-grammars

Perl6: Convert Match object to JSON-serializable Hash


I am currently gettin' my hands dirty on some Perl6. Specifically I am trying to write a Fortran parser based on grammars (the Fortran::Grammar module)

For testing purposes, I would like to have the possiblity to convert a Match object into a JSON-serializable Hash.

Googling / official Perl6 documentation didn't help. My apologies if I overlooked something.

My attempts so far:


Solution

  • Here's an action class method from one of my Perl 6 projects, which does what you describe.

    It does almost the same as what Christoph posted, but is written more verbosely (and I've added copious amounts of comments to make it easier to understand):

    #| Fallback action method that produces a Hash tree from named captures.
    method FALLBACK ($name, $/) {
    
        # Unless an embedded { } block in the grammar already called make()...
        unless $/.made.defined {
    
            # If the Match has named captures, produce a hash with one entry
            # per capture:
            if $/.hash -> %captures {
                make hash do for %captures.kv -> $k, $v {
    
                    # The key of the hash entry is the capture's name.
                    $k => $v ~~ Array 
    
                        # If the capture was repeated by a quantifier, the
                        # value becomes a list of what each repetition of the
                        # sub-rule produced:
                        ?? $v.map(*.made).cache 
    
                        # If the capture wasn't quantified, the value becomes
                        # what the sub-rule produced:
                        !! $v.made
                }
            }
    
            # If the Match has no named captures, produce the string it matched:
            else { make ~$/ }
        }
    }
    

    Notes: