Evaluating an ExprListSyntax

Hello,

I'm playing around with the new OptionSet pitch to add some functionalities.

I'm trying to figure out if it's possible to evaluate expressions inside the macro generation code. For example, I'm trying to add an @attribute that takes a range, e.g. 10..<12 as one of its argument and evaluate this in the macro context. I could read each component manually and just make this a requirement, but I was wondering if it was possible to just evaluate the expression and get a Range object out of it.

Does SwiftSyntax allow you to do that?

SwiftSyntax doesn't have any evaluation built in, nor does it perform any typechecking.

You can, in your macro, read out the lower bound and upper bound as literals and emit a diagnostic if they aren't literals, then build your Range manually.

Alternatively you could syntactically expand the function to store the range to a unique local variable and use thatVariable.lowerBound and thatVariable.upperBound in the expanded macro contents.

Yeah I suspected as much, and no, I need the values in my macro so I'll do the manual parsing. Thank you!

1 Like