繁体   English   中英

如何使用Rascal从Java Eclipse项目中提取特定语句

[英]How to extract specific statements from java eclipse project using rascal

我是rascal的新手,想从Java项目中提取条件语句(if,while等)。

最好的方法似乎在http://tutor.rascal-mpl.org/Rascal/Libraries/analysis/m3/Core/containment/containment.html#/Rascal/Libraries/analysis/m3/AST/AST.html

到目前为止,我的代码是

    void statements(loc location) {
        ast = createAstFromFile(location,true,javaVersion="1.7");
        for(/Statement s := ast) println(readFile(s@src));
    }

但这将返回所有语句,包括注释。 我如何过滤语句以仅在条件等情况下返回条件语句?

Rascal为此实现了Visitor模式。 您可以在ast变量上执行以下操作:

visit(ast){ 
    case \if(icond,ithen,ielse): {
        println(" if-then-else statement with condition <icond> found"); } 
    case \if(icond,ithen): {
        println(" if-then statement with condition <icond> found"); } 
};

本示例从代码返回if语句。

您可以在lang :: java :: m3 :: AST包中找到用作案例模式的模式定义。

@ Geert-Jan Hut的答案是最好的,因为这是访问的目的。

这是其他一些方法:

for(/\if(icond,ithen,ielse) s := ast) 
  println(readFile(s@src));

for(/\if(icond,ithen) s := ast) 
  println(readFile(s@src));

或者,因为两个构造函数具有相同的名称,所以可以使用is

for (/Statement s := ast, s is if)
  println(readFile(s@src));

或者,首先收集它们,然后打印:

conds = { c | /Statement s := ast, s is if };
for (c <- conds) 
   println(readFile(c@src));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM