簡體   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