10

I'm looking for implementation of Java source code parser written in JavaScript language. Do you know any?

Ilya Lakhin
  • 1,904
  • 1
  • 16
  • 31
  • 2
    For what purpose? Interpreting the code and running it? Colorizing it in HTML? Mapping it into a JSON object? – Jeff May 13 '11 at 14:57
  • 2
    What exactly are you trying to do? – gen_Eric May 13 '11 at 14:58
  • @Jeff In the result I would like to have some kind of Abstract Syntax Tree. – Ilya Lakhin May 13 '11 at 15:03
  • @Rocket I'm interested in it for educational purpose. First of all I want to estimate modern JavaScript engines abilities in parsing language with strict type system. – Ilya Lakhin May 13 '11 at 15:10
  • 1
    I don't think I've seen one. I've seen syntax highlighters, but never parsers. – gen_Eric May 13 '11 at 15:11
  • @Eliah: What has a "strict type system" got to do with *parsing*? You might wish to parse, as one step, and and then do name and type resolution on the resulting AST. You're not going to do N&T *during* parsing. ... And, why is the capability of JavaScript wrt N&T interesting? – Ira Baxter Sep 08 '15 at 08:17

4 Answers4

11

Have a look at ANTLR which can have Javascript as a target, with the Java 1.5 grammar at http://www.antlr.org/grammar/1152141644268/Java.g

Edit: link stopped working - try https://github.com/antlr/grammars-v4/blob/master/java/Java.g4 :)

Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
  • Hm... Good idea! Thank you, Chris. But, I'm not sure that the performance of such parser would be enough for parsing large amounts of source codes. – Ilya Lakhin May 13 '11 at 15:38
  • 1
    ok. This is not the solution which I expected, but currently it is only solution which solves this problem. – Ilya Lakhin May 13 '11 at 16:47
  • @Eliah: "... not sure ... large amounts of source codes " ANTLR produces pretty efficient parsers in general. (My personal bet would be for it, rather than against it.) I suspect the only way to actually find out is produce such a parser and try it. And if you are concerned, why not do this using Java or C++? – Ira Baxter Sep 08 '15 at 08:19
5

Here is Java 1.7 parser http://mazko.github.io/jsjavaparser/ using PEG grammar by Roman R Redziejowski http://www.romanredz.se/Mouse/Java.1.7.peg

Oleg Mazko
  • 1,720
  • 15
  • 10
3

I don't know of a Java parser per se, but here are some parser generators for Javascript:

If is one of these is ready for prime-time, you should be able to translate the grammar for Java into the requisite form and then use the PGS's to generate a Java parser in Javascript.

Of course, that will only give you a parser. If you want to do type analysis (as your comment seems to be saying), that's not what a parser does.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thank you for your reply, Stephen. I was working with some of these parser generators. I think its performance is not good enough for parsing large amounts of code with C++ like syntax(like Java), unfortunatelly. – Ilya Lakhin May 13 '11 at 15:25
  • 2
    @Eliah - it might seem counter-intuitive, but generated parsers tend to be faster than hand written parsers, especially if the PGS is mature. – Stephen C May 13 '11 at 15:35
  • 1
    I noticed that ANTLR will also generate parsers in Javascript. – Stephen C May 13 '11 at 16:10
  • 2
    @Eliah: You seem concerned about parsing complex ("C++-like") syntax. What specific syntax concerns you, and why do you think a modern parser generator can't handle it? (C++ is in a league by itself and does give modern parser generators real trouble, http://stackoverflow.com/questions/243383/why-cant-c-be-parsed-with-a-lr1-parser/1004737#1004737 but Java is not C++). – Ira Baxter Sep 08 '15 at 08:24
-3

Try the Rhino engine?

WEFX
  • 8,298
  • 8
  • 66
  • 102