title: With pyparsing, how do you parse a quoted string that ends with a backslash
body: <p>I'm trying to use <a href="http://pyparsing.wikispaces.com/" rel="nofollow">pyparsing</a> to parse quoted strings under the following conditions:</p>
  
  <ul>
  <li>The quoted string might contain internal quotes.</li>
  <li>I want to use backslashes to escape internal quotes.</li>
  <li>The quoted string might end with a backslash.</li>
  </ul>
  
  <p>I'm struggling to define a successful parser. Also, I'm starting to wonder whether the regular expression used by pyparsing for quoted strings of this kind is correct (see my alternative regular expression below).</p>
  
  <p>Am I using pyparsing incorrectly (most likely) or is there a bug in pyparsing?</p>
  
  <p>Here's a script that demonstrates the problem (<strong>Note: ignore this script; please focus instead on the Update below.</strong>):</p>
  
  <pre><code>import pyparsing as pp
  import re
  
  # A single-quoted string having:
  #   - Internal escaped quote.
  #   - A backslash as the last character before the final quote.
  txt = r"'ab\'cd\'"
  
  # Parse with pyparsing.
  # Does not work as expected: grabs only first 3 characters.
  parser = pp.QuotedString(quoteChar = "'", escChar = '\\', escQuote = '\\')
  toks   = parser.parseString(txt)
  print
  print 'txt:    ', txt
  print 'pattern:', parser.pattern
  print 'toks:   ', toks
  
  # Parse with a regex just like the pyparsing pattern, but with
  # the last two groups flipped -- which seems more correct to me.
  # This works.
  rgx = re.compile(r"\'(?:[^'\n\r\\]|(?:\\.)|(?:\\))*\'")
  print
  print rgx.search(txt).group(0)
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>txt:     'ab\'cd\'
  pattern: \'(?:[^'\n\r\\]|(?:\\)|(?:\\.))*\'
  toks:    ["ab'"]
  
  'ab\'cd\'
  </code></pre>
  
  <h2>Update</h2>
  
  <p>Thanks for the replies. I suspect that I've confused things by framing my question badly, so let me try again.</p>
  
  <p>Let's say we are trying to parse a language that uses quoting rules generally like Python's. We want users to be able to define strings that can include internal quotes (protected by backslashes) and we want those strings to be able to end with a backslash. Here's an example file in our language. Note that the file would also parse as valid Python syntax, and if we printed <code>foo</code> (in Python), the output would be the literal value: <code>ab'cd\</code></p>
  
  <pre><code># demo.txt
  foo = 'ab\'cd\\'
  </code></pre>
  
  <p>My goal is to use pyparsing to parse such a language. Is there a way to do it? The question above is basically where I ended up after several failed attempts. Below is my initial attempt. It fails because there are two backslashes at the end, rather than just one.</p>
  
  <pre><code>with open('demo.txt') as fh:
      txt = fh.read().split()[-1].strip()
  
  parser = pp.QuotedString(quoteChar = "'", escChar = '\\')
  toks   = parser.parseString(txt)
  print
  print 'txt:    ', txt
  print 'pattern:', parser.pattern
  print 'toks:   ', toks             # ["ab'cd\\\\"]
  </code></pre>
  
  <p>I guess the problem is that <code>QuotedString</code> treats the backslash only as a quote-escape whereas Python treats a backslash as a more general-purpose escape.</p>
  
  <p>Is there a simple way to do this that I'm overlooking? One workaround that occurs to me is to use <code>.setParseAction(...)</code> to handle the double-backslashes after the fact -- perhaps like this, which seems to work:</p>
  
  <pre><code>qHandler = lambda s,l,t: [ t[0].replace('\\\\', '\\') ]
  parser = pp.QuotedString(quoteChar = "'", escChar = '\\').setParseAction(qHandler)
  </code></pre>