pegjs: Curly braces in JavaScript code must be balanced

I am writing something that compiles to JSON. Simple code:

start
  = a

a
  = x:(.*) {return x.join("{")}

doesn’t work.

workaround is:

  = x:(.*) {return x.join(String.fromCharCode(123))}

(or 125 for other brace)

About this issue

  • Original URL
  • State: open
  • Created 12 years ago
  • Comments: 16 (9 by maintainers)

Most upvoted comments

@futagoza Isn’t that a regression? I might have forgotten, but I remember PEG.js metagrammar included whole ES5 grammar at one time for the purpose of proper brace balancing.

In any case, it’s pretty easy to put a grammar that matches braces, single-line comments, multi-line comments, regexps (as /-delimited thing without any unescaped / inside), and three types of string per any of ES standards, and reads the rest of code as a bunch of uninterpreted chars. In this way PEG.js might avoid problems with brace balancing without incurring costs of supporting the ever extending ES grammar (unless it’s extended with new types of tokens that can contain braces, of course, which is quite a rare occasion).