2017-03-05

So, I've been able to figure out a ... let's call it a workaround ... which I can live with at the moment. Maybe it helps others with similar problems.

First to my two proposed approaches:

Changing the SyntaxContext: far to complex if you need to handle Idents at unknown positions. This would require a visitor pattern which is able to modify the AST.

Parsing the whole function (using syn): Syn does not expose all subparsers necessary to build a modified function definition parser. One would need to fork syn and expose them manually---a large effort, especially from a maintainance point of view. Further I'm not interested in the remaning structure of the function definition---only in the parts contained in library's other macros.

My current approach now is still to parse the function definition but ignore its structure whenever possible. Most of this is done by simple string processing and some invariants of the macro invocations. First all of the macros we are interested in act like statements or blocks. Second their expansions only yield statements. Third if the function itself has syntax errors or uses the macros incorrectly it does not matter if our macros are handled correctly (this might be bad for error reporting).

Convert the token stream of the function def. into a string.

Let the remaining string which is left to parse be the function definition.

Find the next macro invocation we are interested in. In this case I know that they can only occur where a statement might occur.
a. Search for the next substring which looks like a macro invocation.
b. Check if the the macro invocation is not contained in a string or a comment, which is rather easy to do compared to the other options. Anything else should violate the invariants of the macro usage.

Handle the macro invocation using using syn and quote (or by any other means)

Stitch together the generated code and the unparsed part right to the macro invocation and go back to 3.

Collect all generated parts and generate a single function definition then parsed as usual by the syntax extension.

Show more