perlregexp-grammars

Using perl's Regexp::Grammars, how do I make a capture dependent on $MATCH?


I've got a token like such:

<delim2=((?{ $MATCH{delim} }))>

and what I want to happen is for delim2 to capture and be set to the value of delim. When I run this, delim2 is set, but the capture is never done. I think this is an error in my reasoning: I'm trying to chain this form:

<ALIAS= ( PATTERN )>     Match pattern, save match in $MATCH{ALIAS}

and this form: (?{ MATCH{delim} }) into something like this

<ALIAS= ( (?{MATCH{delim}) )>     Matches the value of $MATCH{delim} save to $MATCH{delim2}

but this simply doesn't seem valid. I can verify my original token works <delim2=((?{ die $MATCH{delim} }))> will die with the value, and, if I hard code it, I get the right capture and everything works <delim2=(')>? So how do I go about achieving sane results, while having a dynamic pattern?


Solution

  • (?{ $MATCH{delim} }) doesn't assert that $MATCH{delim} appears here in the input; only that it's a true value. Regexp::Grammars should have a "named-backreference" construct like perl \k<NAME> but it doesn't (and you can't use \k<NAME> because Regexp::Grammars stores its results somewhere entirely different).

    You could do something like

    (??{ quotemeta $MATCH{delim} })<delim2=(?{ $MATCH{delim} })>
    

    which is horrible but seems to work in testing. Or you could give in and go to Parse::RecDescent which has better support for this kind of thing. Or you could start hacking on R::G.