classheaderrascalpublic-methodclair

ClaiR/Rascal: Best way to list public functions?


I am parsing an C++ header file using ClaiR and want to get a list of the public functions.

visit(ast) {
    case \class(_, name(n), _, decs): {
        println("class name: <n>");
        isPublic = true;
        for (dec <- decs) {
            switch(dec) {
                case \visibilityLabel(\public()): {
                    println("Public functions");
                    isPublic = true;
                }
                case \visibilityLabel(\protected()): {
                    println("Protected functions");
                    isPublic = false;
                }
                case \visibilityLabel(\private()): {
                    println("Private functions");
                    isPublic = false;
                }
                case \simpleDeclaration(_, [\functionDeclarator([*_], [*_], name(na), [*_], [*_])]): {
                    if (isPublic) {
                        println("public function: <na>");
                    }
                }
            }
        }
    }
}

The above code works. But is there a better (smaller) way of acquiring the public functions?


Solution

  • There's an M3 modifiers relation which might have the info you're looking for:

    However, that relation must be extracted of course. Perhaps that still needs to be added to ClaiR?