Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Generated parser throws error for escaped quotes on Node.js

Generated parser throws error for escaped quotes on Node.js


I am using PEG.js to create a parser which includes parsing strings.
The strings containing any kind of character are wrapped by quotes " and may contain escaped quotes \".
So far I have the following rule:

    = ["] string:(( '\\"' {return '"';} / [^"])*) ["]
        {return string.join('');}

It works in the PEG.js Online Version and produces "abc\"def" for the given input "abc\"def".

The parser generated for Node.js version 0.6.21 with PEG.js version 0.7.0 is executed the following way

var result = parser.parse('"abc\"def"');

and produces the following error:

{ name: 'SyntaxError',
  expected: [],
  found: 'd',
  message: 'Expected end of input but "d" found.',
  offset: 5,
  line: 1,
  column: 6 }

However, using \\" instead of \" succeeds with the expected output.

var result = parser.parse('"abc\\"def"'); // parses correctly

Is there an explanation or workaround for this problem? In particular, it is not possible for me to double escape all quotes in the expected input of the parser.

Problem courtesy of: MKroehnert


The string literal in this statement...

var result = parser.parse('"abc\"def"');

... actually doesn't contain a backslash. In JavaScript this sequence of symbols - \" - is parsed as a single one - " - no matter what quotation marks are used to delimit a string - double or single ones. JS doesn't interpolate variable and expressions in strings, and there is basically no difference between those.

This string - '"abc\\"def"' - however, has a backslash: it's encoded by that \\ sequence. Note that it's not necessary to use another backslash to escape the double quote itself (as delimiters are single quotation marks). But you'd have to, if "\"abc\\\"def\"" form were used.

Solution courtesy of: raina77ow


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Generated parser throws error for escaped quotes on Node.js


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription