I'm looking for implementation of Java source code parser written in JavaScript language. Do you know any?
-
2For what purpose? Interpreting the code and running it? Colorizing it in HTML? Mapping it into a JSON object?Jeff– Jeff2011-05-13 14:57:54 +00:00Commented May 13, 2011 at 14:57
-
2What exactly are you trying to do?gen_Eric– gen_Eric2011-05-13 14:58:00 +00:00Commented May 13, 2011 at 14:58
-
@Jeff In the result I would like to have some kind of Abstract Syntax Tree.Ilya Lakhin– Ilya Lakhin2011-05-13 15:03:04 +00:00Commented May 13, 2011 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– Ilya Lakhin2011-05-13 15:10:27 +00:00Commented May 13, 2011 at 15:10
-
1I don't think I've seen one. I've seen syntax highlighters, but never parsers.gen_Eric– gen_Eric2011-05-13 15:11:17 +00:00Commented May 13, 2011 at 15:11
4 Answers
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 :)
3 Comments
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
Comments
I don't know of a Java parser per se, but here are some parser generators for Javascript:
- http://jscc.jmksf.com/
- http://code.google.com/p/cruiser/wiki/Parse
- http://pegjs.majda.cz/
- http://zaach.github.com/jison/
- and more are listed here including PGS's that are written in other languages and target 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.
4 Comments
Try the Rhino engine?