Prism Ruby parser
|
Prism is a parser for the Ruby programming language. It is designed to be portable, error tolerant, and maintainable. It is written in C99 and has no dependencies. It is currently being integrated into CRuby, JRuby, TruffleRuby, Sorbet, and Syntax Tree.
If you're vendoring this project and compiling it statically then as long as you have a C99 compiler you will be fine. If you're linking against it as shared library, then you should compile with -fvisibility=hidden
and -DPRISM_EXPORT_SYMBOLS
to tell prism to make only its public interface visible.
In order to parse Ruby code, the structures and functions that you're going to want to use and be aware of are:
pm_parser_t
- the main parser structurepm_parser_init
- initialize a parserpm_parse
- parse and return the root nodepm_node_destroy
- deallocate the root node returned by pm_parse
pm_parser_free
- free the internal memory of the parserPutting all of this together would look something like:
All of the nodes "inherit" from pm_node_t
by embedding those structures as their first member. This means you can downcast and upcast any node in the tree to a pm_node_t
.
Prism provides the ability to serialize the AST and its related metadata into a binary format. This format is designed to be portable to different languages and runtimes so that you only need to make one FFI call in order to parse Ruby code. The structures and functions that you're going to want to use and be aware of are:
pm_buffer_t
- a small buffer object that will hold the serialized ASTpm_buffer_free
- free the memory associated with the bufferpm_serialize
- serialize the AST into a bufferpm_serialize_parse
- parse and serialize the AST into a bufferPutting all of this together would look something like:
Prism provides the ability to inspect the AST by pretty-printing nodes. You can do this with the pm_prettyprint
function, which you would use like: