The writer adapter class
Base class of exceptions from OptionParser
.
A compiler is a visitor that returns the value of each node as it visits. This is as opposed to a visitor which will only walk the tree. This can be useful when you are trying to compile a tree into a different format.
For example, to build a representation of the tree as s-expressions, you could write:
class SExpressions < Prism::Compiler def visit_arguments_node(node) = [:arguments, super] def visit_call_node(node) = [:call, super] def visit_integer_node(node) = [:integer] def visit_program_node(node) = [:program, super] end Prism.parse("1 + 2").value.accept(SExpressions.new) # => [:program, [[[:call, [[:integer], [:arguments, [[:integer]]]]]]]]
DesugarCompiler
is a compiler that desugars Ruby code into a more primitive form. This is useful for consumers that want to deal with fewer node types.
The dispatcher class fires events for nodes that are found while walking an AST to all registered listeners. It’s useful for performing different types of analysis on the AST while only having to walk the tree once.
To use the dispatcher, you would first instantiate it and register listeners for the events you’re interested in:
class OctalListener def on_integer_node_enter(node) if node.octal? && !node.slice.start_with?("0o") warn("Octal integers should be written with the 0o prefix") end end end dispatcher = Dispatcher.new dispatcher.register(listener, :on_integer_node_enter)
Then, you can walk any number of trees and dispatch events to the listeners:
result = Prism.parse("001 + 002 + 003") dispatcher.dispatch(result.value)
Optionally, you can also use ‘#dispatch_once` to dispatch enter and leave events for a single node without recursing further down the tree. This can be useful in circumstances where you want to reuse the listeners you already have registers but want to stop walking the tree at a certain point.
integer = result.value.statements.body.first.receiver.receiver dispatcher.dispatch_once(integer)
This visitor walks through the tree and copies each node as it is being visited. This is useful for consumers that want to mutate the tree, as you can change subtrees in place without effecting the rest of the tree.
Represents the use of the ‘alias` keyword to alias a global variable.
alias $foo $bar ^^^^^^^^^^^^^^^
Represents an array pattern in pattern matching.
foo in 1, 2 ^^^^^^^^^^^ foo in [1, 2] ^^^^^^^^^^^^^ foo in *1 ^^^^^^^^^ foo in Bar[] ^^^^^^^^^^^^ foo in Bar[1, 2, 3] ^^^^^^^^^^^^^^^^^^^
Represents the use of the ‘||=` operator on a call.
foo.bar ||= value ^^^^^^^^^^^^^^^^^
Represents assigning to a method call.
foo.bar, = 1 ^^^^^^^ begin rescue => foo.bar ^^^^^^^ end for foo.bar in baz do end ^^^^^^^
Represents assigning to a local variable in pattern matching.
foo => [bar => baz] ^^^^^^^^^^^^
Represents assigning to a class variable using an operator that isn’t ‘=`.
@@target += value ^^^^^^^^^^^^^^^^^
Represents writing to a class variable in a context that doesn’t have an explicit value.
@@foo, @@bar = baz ^^^^^ ^^^^^
Represents assigning to a constant using an operator that isn’t ‘=`.
Target += value ^^^^^^^^^^^^^^^
Represents assigning to a constant path using an operator that isn’t ‘=`.
Parent::Child += value ^^^^^^^^^^^^^^^^^^^^^^
Represents writing to a constant path in a context that doesn’t have an explicit value.
Foo::Foo, Bar::Bar = baz ^^^^^^^^ ^^^^^^^^
Represents writing to a constant in a context that doesn’t have an explicit value.
Foo, Bar = baz ^^^ ^^^
Represents a find pattern in pattern matching.
foo in *bar, baz, *qux ^^^^^^^^^^^^^^^ foo in [*bar, baz, *qux] ^^^^^^^^^^^^^^^^^ foo in Foo(*bar, baz, *qux) ^^^^^^^^^^^^^^^^^^^^
Represents the use of the ‘..` or `…` operators to create flip flops.
baz if foo .. bar ^^^^^^^^^^