Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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.


In reply to Re: Quoted Text rule for Parse::RecDescent? by ikegami
in thread Quoted Text rule for Parse::RecDescent? by suaveant

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

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

      The best computer themed movie is:











      Results (298 votes), past polls