How do I get the location of the number?

Suppose I want to print all locations with a hardcoded value, in context d was an M3 declaration:

top-down visit(d) {
    case \number(str numberValue) :
        println("hardcode value <numberValue> encountered at <d@\src>");
}

      

The problem is that the location of <d @ \ src> is too general, it gives the whole declaration (like the whole procedure). The expression <numberValue @ \ src> seems more appropriate, but it is not valid, perhaps because we are too small in the parse tree.

So the question is how to get the parent E (closest parent as well as the expression itself) <numberValue> such that <E @ \ src> is defined?

One option is to add an additional top-down traversal layer:

top-down visit(d) {
    case \expressionStatement(Expression stmt): {
        case \number(str numberValue) :
            println("hardcode value <numberValue> encountered at <stmt@\src>");
    }
}

      

This works, but has some disadvantages:

  • This is ugly, to cover all cases, we have to add many more options;
  • This is very inefficient.

So, what's the correct way to get the location of numberValue (and similar low-level constructs like stringValue, etc.)?

+3


source to share


2 answers


You should be able to do the following:

top-down visit(d) { case n:\number(str numberValue) : println("hardcode value <numberValue> encountered at <n@\src>"); }



The utterance n:\number(str numberValue)

will associate a name n

with a \number

node that matches the case. Then you can use that in your post to get the location for n

. Just make sure n

not already in scope. You should also create similar templates for the other scenarios you mentioned.

+1


source


Thanks, I knew it would be easy to solve!



0


source







All Articles