Quantcast

AST from source?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

AST from source?

Xavier Noria
Is there a one-liner to get an AST from source code?

I have seen you can get it from the .beam file if debug_info was enabled, and have also seen some multiline snippet that uses erl_scan and erl_parse, but nothing as encapsulated as what you have for generating core or kernel.


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: AST from source?

Alex S.
epp:parse_file?

> 17 апр. 2017 г., в 11:43, Xavier Noria <[hidden email]> написал(а):
>
> Is there a one-liner to get an AST from source code?
>
> I have seen you can get it from the .beam file if debug_info was enabled, and have also seen some multiline snippet that uses erl_scan and erl_parse, but nothing as encapsulated as what you have for generating core or kernel.
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: AST from source?

Dmytro Lytovchenko
In reply to this post by Xavier Noria
case compile:file(Filename, [to_kernel, binary, report]) of {ok, ModuleName, Code} ->...
change to_kernel to to_core, or to 'S' for you know what.
Binary will get you the result as a part of return value
Report will print errors

2017-04-17 10:43 GMT+02:00 Xavier Noria <[hidden email]>:
Is there a one-liner to get an AST from source code?

I have seen you can get it from the .beam file if debug_info was enabled, and have also seen some multiline snippet that uses erl_scan and erl_parse, but nothing as encapsulated as what you have for generating core or kernel.


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: AST from source?

José Valim-2
It can also be done from the command line:

erlc +to_pp path/to/file.erl

Besides +to_pp, you can pass:
  • +to_pp - pretty printed Erlang format after parse transforms and file imports
  • +to_exp - pretty printed Erlang format after parse transforms, imports, records and others
  • +to_core - Core Erlang
  • +to_kernel - Kernel representation
  • +to_asm - ASM representation
We may have others but from memory those are the main ones.

José Valim
Skype: jv.ptec
Founder and Director of R&D


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: AST from source?

Xavier Noria
Awesome, I knew about +to_core, and +to_kernel but was not aware of the other ones.

Also, it seems that +to_pp is the AST of the Erlang code c(File, 'P') yields, and similar for +to_exp and 'E'.

I see 'P' and 'E' documented in the docs of compile:file/2, but none of the flags in that list... are those considered to be private maybe? Could volunteer a doc patch otherwise.


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Loading...