[英]How to extract specific statements from java eclipse project using rascal
我是rascal的新手,想從Java項目中提取條件語句(if,while等)。
到目前為止,我的代碼是
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.