Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Quoted Text rule for Parse::RecDescent?

by halley (Prior)
on Aug 23, 2005 at 15:14 UTC ( #485935=note: print w/ replies, xml ) Need Help??


in reply to Quoted Text rule for Parse::RecDescent?

If you don't want anything but ' and " strings, then just fail the rule if $item[...][1] ne "'" and $item[...][1] ne '"'.

In my application, I allow qr// and ' and ", but not others like qq//, qx//, s/// or tr///. In qr//, I scan for any use of (?{...}) and fail for those. I just bring it up to show that you can choose your rules pretty flexibly.

(Expanded answer.)

As for "how to use the code blocks in a production," just realize that there's no real difference between the parsing parts and the code parts: if you get through all of the subrules in the production, then the whole production succeeds and passes whatever is in $return up to the calling rule. The code blocks in YACC are very distinct, but code blocks in P::RD are more like embedded (?{}) code in a regexp. (That was my revelation thanks to another monk a few weeks ago.)

So, do whatever you want in a code block. The items so far are in @item and %item. You can assign whatever you want to assign into $return, including undef to fail the rule.

string: <perl_quotelike> { if ($item[1][0] =~ /^(qx|s|tr|y)$/ or # no perl commands $item[1][1] eq '`' or # no backticks $item[1][2] =~ /\(\?\??\{/) # no qr'' with code { $return = undef } else { $return = eval "" . join('', @{$item[1]}) } (defined $return) }

--
[ e d @ h a l l e y . c c ]


Comment on Re: Quoted Text rule for Parse::RecDescent?
Select or Download Code
Replies are listed 'Best First'.
Re^2: Quoted Text rule for Parse::RecDescent?
by suaveant (Parson) on Aug 26, 2005 at 18:06 UTC
    ahhh... that helps. Though it seems a little more efficient to check
    if(!$item[1][0] && $item[1][1] eq '"' || $item[1][1] eq "'")) {

    Thanks!

                    - Ant
                    - Some of my best work - (1 2 3)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://485935]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2015-07-08 01:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls