Search code examples
rascal

Interpolation in Concrete Syntax Matching


I'm working with a Java 8 grammar and I want to find occurrences of a method invocation, more specifically it.hasNext(), when it is an Iterator.

This works:

visit(unit) {
    case (MethodInvocation)`it . <TypeArguments? ta> hasNext()`: {
        println("found");
    }
}

Ideally I would like to match with any identifier, not just it.

So I tried using String interpolation, which compiles but doesn't match:

str iteratorId = "it";
visit(unit) {
    case (MethodInvocation)`$iteratorId$ . <TypeArguments? ta> hasNext()`: {
        println("achei");
    }
}

I also tried several other ways, including pattern variable uses (as seen in the docs) but I can't get this to work.

Is this kind of matching possible in rascal? If yes, how can it be done?


Solution

  • The answer specifically depends on the grammar you are using, which I did not look up, but in general in concrete syntax fragments this notation is used for placeholders: <NonTerminal variableName>

    So your pattern should look something like the following:

    str iteratorId = "it";
    visit(unit) {
        case (MethodInvocation)`<MethodName name>.<TypeArguments? ta>hasNext()`: 
           if (iteratorId == "<name>") println("bingo!");
    }
    

    That is assuming that MethodName is indeed a non-terminal in your Java8 grammar and part of the syntax rule for method invocations.