parse quoted string containing escaped quote

1,518 views
Skip to first unread message

jan zimmek

unread,
Feb 17, 2012, 3:06:03 PM2/17/12
to PEG.js: Parser Generator for JavaScript
hello,

i just found pegjs and started to test it, but run into trouble when
trying to parse a quoted string which contains escaped quote
characters.

this is my actual grammar:

// ---- START ----

start
= "{" (quoted_word)* "}"

quoted_word
= ' '? '"' word:word '"' { return word; }

word
= word:['a-z]+ { return word.join(''); }

// ---- END ----


but i want it to parse the following:


parser.parse('{"abc\"xyz"}');


the error following error occur:

{ name: 'SyntaxError',
message: 'Expected " ", "\\"" or "}" but "x" found.',
line: 1,
column: 7 }


any help would be really appreciated.

regards
jan

Subbu Allamaraju

unread,
Apr 9, 2012, 11:52:34 AM4/9/12
to pe...@googlegroups.com
I came across a similar issue in supporting escaped quotes in quoted strings. 

start = StringLiteral
StringLiteral = parts:(('"' DoubleString '"') / ("'" SingleString "'")) {
    return '"' + parts[1] + '"';
}
SingleString = c:([\u0020-\u0026]/[\u0028-\u005B]/[\u005D-\uFFFF]/[\\"])* {
    return c.join('');
}
DoubleString = c:([\u0020-\u0021]/[\u0023-\u005B]/[\u005D-\uFFFF]/[\\']/EscapedQuote)* {
    return c.join('');
}
EscapedQuote = [\u005c\u0022]

This grammar is unable to parse any strings.

What is the best way to support escaped strings?

Thanks for any direction.
Subbu

Juan Felipe Garcia Catalan

unread,
Apr 15, 2012, 5:16:58 PM4/15/12
to pe...@googlegroups.com
This seems to work for me, just handles double quotes, but should be easy to extend for singles:

str
    = "\"" str:(!unscapedquote anycharacter)* last:unscapedquote {
var r = "";
for (var c in str) {
   r += str[c][1];
}
return r + last; 
    }

unscapedquote
    = last:[^\\] "\"" {return last;}

anycharacter 
  = .

Subbu Allamaraju

unread,
Apr 16, 2012, 1:37:58 AM4/16/12
to pe...@googlegroups.com
Good idea. Thanks.

David Majda

unread,
Apr 19, 2012, 12:59:05 PM4/19/12
to pe...@googlegroups.com
2012/2/17 jan zimmek <jzi...@googlemail.com>:

> i just found pegjs and started to test it, but run into trouble when
> trying to parse a quoted string which contains escaped quote
> characters.

2012/4/9 Subbu Allamaraju <su...@subbu.org>:


> I came across a similar issue in supporting escaped quotes in quoted
> strings.

Have a look how this is handled e.g. in the JSON example grammar [1]
(look at the "string" rule and other rules it references).

[1] https://github.com/dmajda/pegjs/blob/master/examples/json.pegjs

--
David Majda
Entropy fighter
http://majda.cz/

Subbu Allamaraju

unread,
Apr 20, 2012, 2:08:13 PM4/20/12
to pe...@googlegroups.com
That works. Thanks.
Reply all
Reply to author
Forward
0 new messages