在Rascal MPL中跳出访问语句的优雅方式是什么?



我在无赖中使用visit来迭代字符串。一旦找到匹配的案子,我就会跳出来。换句话说,我想要实现以下确切的行为:

str toVisit = "abcdefgh";
while(true) {
visit(toVisit) {
case /^bc/: println("before");
case /^de/: break;
case /^fg/: println("after");
}
break;
}

,但没有while循环。在访问的手册页中没有提到这一点。因此,我的问题是:在Rascal MPL中是否有一种优雅的方式来打破访问声明?

visit通常更擅长递归遍历而不是迭代,但return是人们跳出访问的典型方式:

void myFunction() {
visit (toVisit) {
case /^bc/: println("before");
case /^de/: return;
case /^fg/: println("after");
}
}

这样有利于更小的函数:-)

否则,正则表达式本身具有出色的回溯行为,用于搜索线性模式,如:

for (/<before:.*>bc<middle:.*>de<after:.*>fg/ := "abcdefgh") {
println("match! <before> <middle> <after>");
// fail brings you to the next match (also for visit, if, etc)
fail;
// break jumps out of the loop
break;
// continue brings you to the next match as well
continue;
}

最新更新