是否可以通过宏获得评论?

Is that possible to get comments with macro?

我试图解析一些代码并重新格式化它们,但似乎引用只会忽略注释。

有什么办法可以实现吗?我想我必须深入到 erlang 方面?

不,您无法在宏中获取代码注释。它们永远不会成为 AST 的一部分,并且仍然在 Elixir 的分词器中被丢弃。

似乎注释是在分词器级别处理的,因此解析器甚至看不到它们。来自 elixir core 的 relevant parts from the elixir tokenizer indicate that comments are discarded pretty early in the pipeline. This test case 告诉我们同样的事情:

comments_test() ->
  [{number, {1,1,2}, 1},{eol, {1,3,4}},{number,{2,1,2},2}] = tokenize("1 # Comment\n2").