如何使用Clojure分析Java源文件 [英] How to analyze Java source files with Clojure

查看:102
本文介绍了如何使用Clojure分析Java源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



首先,我想使用Eclipse AST plugin()通过复制必要的JAR到我的Clojure项目),但我看到Eclipse AST的API(基于访问者的步行者)后放弃。



然后我尝试创建一个Java解析器与ANTLR 。我只能为ANTLR找到一个Java 1.6语法((Java 1.5 only AFAIK),并将其命名为从你的Clojure代码。


I'm trying to analyze Java source files with Clojure but I couldn't find a way to do that.

First, I thought using Eclipse AST plugin(by copying necessary JAR's to my Clojure project) but I gave up after seeing Eclipse AST's API(visitor based walker).

Then I've tried creating a Java parser with ANTLR. I can only find one Java 1.6 grammar for ANTLR( http://openjdk.java.net/projects/compiler-grammar/antlrworks/Java.g ) and it doesn't compile with latest ANTLR(here's the errors I'm getting ).

Now I have no idea how can I do that. At worst I'll try to go with Eclipse AST.

Does anyone know a better way to parse Java files with Clojure?

Thanks.

Edit: To clarify my point: I need to find some specific method calls in Java projects and inspect it's parameters(we have multiple definitions of the method, with different type of parameters). Right now I have a simple solution written in Java(Eclipse AST) but I want to use Clojure in this project as much as possible.

解决方案

... and it doesn't compile with latest ANTLR ...

I could not reproduce that.

Using ANTLR v3.2, I got some warnings, but no errors. Using both ANTLR v3.3 and v3.4 (latest version), I have no problems generating a parser.

You didn't mention how you're (trying) to generate a lexer/parser, but here's how it works for me:

java -cp antlr-3.4.jar org.antlr.Tool Java.g

EDIT 1

Here's my output when running the commands:

ls
wget http://www.antlr.org/download/antlr-3.4-complete.jar
wget http://openjdk.java.net/projects/compiler-grammar/antlrworks/Java.g
java -cp antlr-3.4-complete.jar org.antlr.Tool Java.g
ls

As you can see, the .java files of the lexer and parser are properly created.

EDIT 2

Instead of generating a parser yourself (from a grammar), you could use an existing parser like this one (Java 1.5 only AFAIK) and call it from your Clojure code.

这篇关于如何使用Clojure分析Java源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆