| 1 | # Ruby API |
| 2 | |
| 3 | The `prism` gem provides a Ruby API for accessing the syntax tree. |
| 4 | |
| 5 | For the most part, the API for accessing the tree mirrors that found in the [Syntax Tree](https://github.com/ruby-syntax-tree/syntax_tree) project. This means: |
| 6 | |
| 7 | * Walking the tree involves creating a visitor and passing it to the `#accept` method on any node in the tree |
| 8 | * Nodes in the tree respond to named methods for accessing their children as well as `#child_nodes` |
| 9 | * Nodes respond to the pattern matching interfaces `#deconstruct` and `#deconstruct_keys` |
| 10 | |
| 11 | Every entry in `config.yml` will generate a Ruby class as well as the code that builds the nodes themselves. |
| 12 | Creating a syntax tree involves calling one of the class methods on the `Prism` module. |
| 13 | The full API is documented below. |
| 14 | |
| 15 | ## API |
| 16 | |
| 17 | * `Prism.dump(source)` - parse the syntax tree corresponding to the given source string, and serialize it to a string |
| 18 | * `Prism.dump_file(filepath)` - parse the syntax tree corresponding to the given source file and serialize it to a string |
| 19 | * `Prism.lex(source)` - parse the tokens corresponding to the given source string and return them as an array within a parse result |
| 20 | * `Prism.lex_file(filepath)` - parse the tokens corresponding to the given source file and return them as an array within a parse result |
| 21 | * `Prism.parse(source)` - parse the syntax tree corresponding to the given source string and return it within a parse result |
| 22 | * `Prism.parse_file(filepath)` - parse the syntax tree corresponding to the given source file and return it within a parse result |
| 23 | * `Prism.parse_stream(io)` - parse the syntax tree corresponding to the source that is read out of the given IO object using the `#gets` method and return it within a parse result |
| 24 | * `Prism.parse_lex(source)` - parse the syntax tree corresponding to the given source string and return it within a parse result, along with the tokens |
| 25 | * `Prism.parse_lex_file(filepath)` - parse the syntax tree corresponding to the given source file and return it within a parse result, along with the tokens |
| 26 | * `Prism.load(source, serialized, freeze = false)` - load the serialized syntax tree using the source as a reference into a syntax tree |
| 27 | * `Prism.parse_comments(source)` - parse the comments corresponding to the given source string and return them |
| 28 | * `Prism.parse_file_comments(source)` - parse the comments corresponding to the given source file and return them |
| 29 | * `Prism.parse_success?(source)` - parse the syntax tree corresponding to the given source string and return true if it was parsed without errors |
| 30 | * `Prism.parse_file_success?(filepath)` - parse the syntax tree corresponding to the given source file and return true if it was parsed without errors |
| 31 | |
| 32 | ## Nodes |
| 33 | |
| 34 | Once you have nodes in hand coming out of a parse result, there are a number of common APIs that are available on each instance. They are: |
| 35 | |
| 36 | * `#accept(visitor)` - a method that will immediately call `visit_*` to specialize for the node type |
| 37 | * `#child_nodes` - a positional array of the child nodes of the node, with `nil` values for any missing children |
| 38 | * `#compact_child_nodes` - a positional array of the child nodes of the node with no `nil` values |
| 39 | * `#each_child_node` - with a block given yields all child nodes, without a block return an enumerator containing all child nodes |
| 40 | * `#copy(**keys)` - a method that allows creating a shallow copy of the node with the given keys overridden |
| 41 | * `#deconstruct`/`#deconstruct_keys(keys)` - the pattern matching interface for nodes |
| 42 | * `#inspect` - a string representation that looks like the syntax tree of the node |
| 43 | * `#location` - a `Location` object that describes the location of the node in the source file |
| 44 | * `#to_dot` - convert the node's syntax tree into graphviz dot notation |
| 45 | * `#type` - a symbol that represents the type of the node, useful for quick comparisons |