1

可能な順列ごとに具体的なパターンマッチングを行わずに、複数のオプショナルを処理する方法を知りたいです。

以下は、私が直面している問題の簡単な例です。

lexical Int = [0-9]+;
syntax Bool = "True" | "False";
syntax Period = "Day" | "Month" | "Quarter" | "Year";
layout Standard = [\ \t\n\f\r]*; 
syntax Optionals = Int? i Bool? b Period? p;

str printOptionals(Optionals opt){
    str res = "";
    if(!isEmpty("<opt.i>")) { // opt has i is always true (same for opt.i?)
        res += printInt(opt.i);
    }
    if(!isEmpty("<opt.b>")){
        res += printBool(opt.b);
    }
    if(!isEmpty("<opt.p>")) {
        res += printPeriod(opt.period);
    }
    return res;
}

str printInt(Int i) = "<i>";
str printBool(Bool b) = "<b>";
str printPeriod(Period p) = "<p>";

ただし、これによりエラーメッセージが表示されます。

The called signature: printInt(opt(lex("Int"))), does not match the declared signature: str printInt(sort("Int"));

opt 部分があることがわかっている場合、どうすればそれを取り除くことができますか?

4

3 に答える 3

1

これがどれほど理想的かはわかりませんが、今のところこれを行うことができます:

if (/Int i := opt.i) {
    res += printInt(i);
}

これにより、そこにある場合はIntfromが抽出されますが、オプションの 1 つとして指定されていない場合、一致は失敗します。opt.iInt

于 2014-05-13T13:01:38.907 に答える