Why does Meta.parse ignores comments?

I am really surprised that Meta.parse seems to actively kick out some information

julia> Meta.parse("""
       function hi()
       # comment
:(function hi()
      #= none:1 =#
      #= none:1 =#

The comment is missing apparently…

EDIT: using Julia 1.10.0

Comments have no semantic meanings, I think JuliaParser.jl keeps track of them.

1 Like

JuliaParser is no longer maintained. This is not an option

also I think comments can indeed have semantic meaning. especially as julia has macros, it may make sense to but semantic information inside comments, why not?

It’s almost the definition of a comment, something that appears in the raw source code but has no semantic meaning?


Semantic meaning for code. Comments by design don’t affect code at all, so they were always ignored in expressions in any language. Hypothetically I suppose it could be incorporated into expressions and then ignored later, but there’s no real reason to do that because we’re supposed to read source code. In fact it would bloat the size of expressions only to allow a secondary place to read comments.


Sorry freudian slip. JuliaSyntax.jl or CSTParser.jl

It sounds like you want to implement something like a pragma?
You can just add an argument to the macro to pass in additional information rather than depend on comments.

If you need to perform custom parsing you can use string macros Metaprogramming · The Julia Language

Comments not having semantic meaning comes from Julia’s LISP heritage.

From Comments (GNU Emacs Lisp Reference Manual)

The Lisp reader discards comments; they do not become part of the Lisp objects which represent the program within the Lisp system.