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

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

问题描述

我正在尝试使用 Clojure 分析 Java 源文件,但找不到方法.

首先,我想使用 Eclipse AST 插件(通过将必要的 JAR 复制到我的 Clojure 项目中),但在看到 Eclipse AST 的 API(基于访问者的 walker)后我放弃了.

然后我尝试用 ANTLR 创建一个 Java 解析器.我只能为 ANTLR 找到一种 Java 1.6 语法( 一个(仅限 Java 1.5 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天全站免登陆