Failing to build parser with Rhino with PEG.js version 0.7.0

140 views
Skip to first unread message

Leonardo Gomes

unread,
Jul 29, 2012, 6:51:05 AM7/29/12
to PEG.js: Parser Generator for JavaScript
Hello,

I've been building parsers using a Maven Plugin (i.e. calling PEG.js from Java code) successfully using PEG.js version 0.6.1, but now while trying to upgrade to the new version, it's failing with the error message: 

sun.org.mozilla.javascript.internal.EcmaError: SyntaxError: missing ; before statement (<Unknown source>#3213(eval)#1) in <Unknown source>#3213(eval) at line number 1

While debugging I've noticed that the parser that comes out of the PEG.buildParser() function call looks syntactically wrong, whereas of course it doesn't happen while calling it from a browser.

Here is how I'm calling it:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine jsEngine = manager.getEngineByName("JavaScript");
jsEngine.eval(new InputStreamReader(this.getClass().getResourceAsStream(PEGJS_LIB)));
Invocable invok = (Invocable) jsEngine;
Object peg = jsEngine.get("PEG");
invok.invokeMethod(peg, "buildParser", grammarSource);

Where, "grammarSource", in my test-case, is the basic grammar used as an example in the website, and PEGJS_LIB = peg-0.7.0.js.

Any ideas are welcome! I've run out of them here...


Cheers,

Leo.

David Majda

unread,
Jul 29, 2012, 11:03:46 AM7/29/12
to leonardo...@gmail.com, PEG.js: Parser Generator for JavaScript
Hi Leonardo!

2012/7/29 Leonardo Gomes <leonardo...@gmail.com>:
> I've been building parsers using a Maven Plugin (i.e. calling PEG.js from
> Java code) successfully using PEG.js version 0.6.1, but now while trying to
> upgrade to the new version, it's failing with the error message:
>
>> sun.org.mozilla.javascript.internal.EcmaError: SyntaxError: missing ;
>> before statement (<Unknown source>#3213(eval)#1) in <Unknown
>> source>#3213(eval) at line number 1

Rhino is not a supported platform anymore, meaning you are on your own
here. But if you (or someone else) debugs this and provides a fix
which won't break PEG.js on any of the supported platforms or have
some other negative consequences, I'll accept it.

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

Leonardo Gomes

unread,
Aug 1, 2012, 5:18:02 PM8/1/12
to PEG.js: Parser Generator for JavaScript
Ops, sent the previous email directly by mistake. Sending it again, now to the discussion list :)

On Wed, Aug 1, 2012 at 11:17 PM, Leonardo Gomes <leonardo...@gmail.com> wrote:
Thanks for your quick answer!

I found a workaround by using Selenium's Firefox WebDriver: http://seleniumhq.org/docs/03_webdriver.html

public void test() throws IOException {
    FirefoxDriver exec = new FirefoxDriver();
    exec.get("http://pegjs.majda.cz/online");
    String grammar = IOUtils.toString(this.getClass().getResourceAsStream("myGrammar.pegjs"));
    System.out.println( exec.executeScript("return PEG.buildParser(arguments[0])", grammar));
}

The thing is that you need an actual Firefox installed to run that :(

Do you see any other alternative to generating a parser headlessly, other than using Node.js?

Thanks again,

Leonardo.

David Majda

unread,
Sep 16, 2012, 12:44:03 PM9/16/12
to leonardo...@gmail.com, PEG.js: Parser Generator for JavaScript
> On Wed, Aug 1, 2012 at 11:17 PM, Leonardo Gomes <leonardo...@gmail.com>
> wrote:
>>
>> Do you see any other alternative to generating a parser headlessly, other
>> than using Node.js?

No.

Gavin Hogan

unread,
Oct 5, 2012, 8:18:31 AM10/5/12
to pe...@googlegroups.com, leonardo...@gmail.com
I tracked down the cause for this issue.  'PEG.compiler.emitter.Codie.pushRaw.unindent()' line 3681 destroys the templates.  I have not fully determined how important this function is for the parser, but it seems like a 'nice to have'.  I replace the body of the function locally and Rhino worked as expected.  



function unindent(code, level, unindentFirst) {
      return code
}

I know you are not interested in supporting Rhino, but I thought I should share this on this thread so others might be able to solve the problem as needed.


Thanks for Peg.JS - It is really great.

Gavin

Reply all
Reply to author
Forward
0 new messages