How to visit class and method defs with expanded macros by parser/compiler?

Hello! I am trying (for better knowledge) to parse crystal source with Crystal::Parser and Visitor - very easy!

But how can I catch for example all class defs and method defs including those generated by macros?

Can you point me to some docs or example code somewhere, which I could study? I am repeatedly reading crystal compiler sources, but it’s a lot of code without even a few lines of comments :)

Thanks very much!


What are you trying to do? I ask because depending on what you want to do you might not need to do that…

I’m just playing around - but imagine that I want to create a “browser” (a tool)… which shows all modules, classes and methods (even generated by macros) from some Crystal code base.

This is also needed for proper code completion, for example.

I guess that reviewing how the semantic specs works will give you some insight.

The main thing to know if whether you need the output of the parser or a whole semantic pass.

There are some tools/experiments/demo that plays at different levels of the compiler internals. But the internals are not expected to be stable api. Some of these experiments served their purpose and probably don’t work currently.

1 Like

@bcardiff Are you able to compile this program by latest crystal on your computer?

You could probably just use the output from the docs generator for this. You can look at the implementation and integrate it into your code, or literally just call crystal docs and parse the generated JSON file. (see for the JSON mappings).

1 Like

I did not know about the existence of the index.json, wow! Thanks!