Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Quoted Text rule for Parse::RecDescent?

by ikegami (Pope)
on Aug 23, 2005 at 17:18 UTC ( #485962=note: print w/replies, xml ) Need Help??


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

Keep in mind the last item in the production is the one that determines the returned value of the entire production.

Also keep in mind that extract_quotelike behaves different in list context compared to scalar context.

 

The following will do the trick:

{ # Place this block at the top of the grammar, before any rules. use Text::Balanced qw( extract_quotelike ); } string: { [ $item[0], scalar extract_quotelike($text) ] }

Of course, that leaves the quotes in. If you want to remove the quotes, the following will work:

{ # Place this block at the top of the grammar, before any rules. sub dequote_double { local $_ = (@_ ? $_[0] : $_); $_ = substr($_, 1, -1); s/\\(.)/$1/sg; } # Like in Perl. # Unlike bash. sub dequote_single { local $_ = (@_ ? $_[0] : $_); $_ = substr($_, 1, -1); s/\\(['\\])/$1/sg; } } string : /"(?:[^\\"]|\\.)*"/ {[ $item[0], dequote_double($item[1]) ]} | /'(?:[^\\']|\\.)*'/ {[ $item[0], dequote_single($item[1]) ]}

Fortunately and unfortunately, it doesn't parse `, q, qq, s, tr, y and here-docs like extract-quotelike. If you want it to parse any or all of those, reply to this node and I'll provide.

 

By the way, don't use $_ without localizing *_ or at least $_ first. You risk clobbering something in the caller. (for/foreach localize this for you, but not while.)

Update: I pretty much rewrote this node a few times, trying to make my thoughts coherent.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://485962]
help
Chatterbox?
[erix]: that's a good one. I must remember that :P
[stevieb]: I really dislike how perlbrew doesn't list your version of perl if a newer minor version bump has happened. eg: I use 5.24.1, but 5.24.2 is available, hiding my 5.24.1. I may look at this, as I made it display correctly in berrybrew in cases like this
[stevieb]: choroba just gleaned your post about Module::Starter. I use it too, pretty much for every dist I write

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2017-08-18 20:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (310 votes). Check out past polls.

    Notices?