steadfast 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 65edd414b2b9a53a27c9965da47829809b1abdfc7e3cd2f9dfb26e9e155976b6
4
- data.tar.gz: 6560196ff027bb0c1d7a669ce7f80c1af4e4d6419792bd872a637594f39eee74
3
+ metadata.gz: 3856e55251f5e48be25a1cb9f03df36743ab36e8bfa18068b33045a977205cfc
4
+ data.tar.gz: f04e002ad7a9b54e3f80f848a82cbb8efd0fd71980b7066e581473a5512b4b9a
5
5
  SHA512:
6
- metadata.gz: 01f35b02170b51dbb77f363b65d2ac812fae08c66796f8754d6a491c5357114f418ac6baccbf9646c912885f9f9ef0e23d483641f4cf63e653fc16ebcabdb4df
7
- data.tar.gz: dd8a6449757d1b35bfeb5005ff2b5209b2873c869ee7425f5caf36c5b368e352d7fc364f71ef64db5962698a4a547832aeda7a21b2e2070c9279ad04601f84fd
6
+ metadata.gz: f31437cf5dd75e56eae0342d6055c50fcd82726130b22b07cbababa4dd45f7955b03497ca5ac572747a101d9bf6e5aced9b49c8c21c55bae9bf73e7c1e2b6f1c
7
+ data.tar.gz: 2cc985fff855606d568445392bbaf289eb12d0876bd6879dcad7a6d01686aad46f883e0c981254b686fabeddc4d9222b4358145f733e9ae69ccd572c96547cc2
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ # 0.5.0 2026-11-26
2
+
3
+ - Add examples
4
+ - Sourcifier: fix source formatting for `ForwardingSuperNode`, `ImaginaryNode`, `ProgramNode`
5
+ - Add support for AlternationPatternNode
6
+ - Add `Steadfast.quote` - Include `Steadfast::Quote` methods in `Steadfast`
7
+ - `Steadfast.rewrite` - raise on invalid AST
8
+
1
9
  # 0.4.0 2026-11-25
2
10
 
3
11
  - Improve sourcifier output
data/README.md CHANGED
@@ -21,4 +21,206 @@
21
21
  <img src="./steadfast.png">
22
22
  </p>
23
23
 
24
- More information coming soon...
24
+ Steadfast is a gem for working with Ruby [Prism](https://github.com/ruby/prism/)
25
+ [ASTs](https://en.wikipedia.org/wiki/Abstract_syntax_tree). Steadfast provides a
26
+ convenient, rich API for generating and transforming ASTs.
27
+
28
+ ## Features
29
+
30
+ - Convert any proc or method to AST, convert any AST to source code.
31
+ - Rewrite, transform, compose and take apart ASTs.
32
+ - Comprehensive set of tools for creating synthetic AST nodes.
33
+ - Code quoting and templating using quote / unquote.
34
+ - Partial evaluation / specialization.
35
+ - Additional tools for iterating over ASTs and finding nodes.
36
+
37
+ ## Requirements
38
+
39
+ Steadfast requires Ruby 4.1 or newer.
40
+
41
+ ## Getting Started
42
+
43
+ Steadfast provides a variety of tools for working with ASTs. Here are the main
44
+ ones:
45
+
46
+ - `Steadfast.to_ast` - used to get the AST of a proc or a method.
47
+ - `Steadfast.rewrite` - used to create modified copies of ASTs.
48
+ - `Steadfast.quote` and `Steadfast.unquote` - used to generate ASTs and create
49
+ code templates.
50
+ - `Steadfast.eval_ast` - used to evaluate ASTs.
51
+
52
+ In the following example, we'll take a lambda that returns an integer, extract
53
+ its AST, and modify it to return an interpolated string instead.
54
+
55
+ ```ruby
56
+ require 'steadfast'
57
+
58
+ l = -> { 42 }
59
+ ast = Steadfast.to_ast(l)
60
+
61
+ msg = 'Hello'
62
+ ast2 = Steadfast.rewrite(ast) do |n|
63
+ next n if !(n in Ruby::Prism::IntegerNode(value: 42))
64
+
65
+ # Inject the value of a local variable into the string
66
+ Steadfast.quote(binding) { "#{unquote(msg)} from Steadfast!" }
67
+ end
68
+
69
+ l2 = Steadfast.eval_ast(ast2)
70
+ l2.() #=> "Hello from Steadfast!"
71
+ ```
72
+
73
+ ## Rewriting ASTs
74
+
75
+ The `Steadfast.rewrite` method is a general-purpose tool for modifying ASTs.
76
+ Since Prism ASTs are immutable, `Steadfast.rewrite` lets you modify an AST by
77
+ creating a copy of it with the requisite node replacements. You can replace
78
+ nodes by providing a hash with the replacements to be made. For example:
79
+
80
+ ```ruby
81
+ ast = Steadfast.to_ast(->() { foo() })
82
+ call_node = Steadfast.find(ast) { n in Ruby::Prism::CallNode(name: :foo)) }
83
+
84
+ # replace foo call with bar call
85
+ ast2 = Steadfast.rewrite(ast, call_node => quote { bar() })
86
+ ```
87
+
88
+ Another way to use Steadfast.rewrite is by providing a block that will be
89
+ invoked for each node in the AST. The block should return eith the node passed
90
+ to it, or a replacement node if it should be replaced. For example:
91
+
92
+ ```ruby
93
+ ast = Steadfast.to_ast(->() { foo() })
94
+
95
+ # replace foo call with bar call
96
+ ast2 = Steadfast.rewrite(ast) { |n|
97
+ # use next to allow the iteration to continue
98
+ next n if !(n in Ruby::Prism::CallNode(name: :foo))
99
+
100
+ Steadfast.quote { bar() }
101
+ }
102
+ ```
103
+
104
+ ### Recursive rewriting
105
+
106
+ When using the block form of `Steadfast.rewrite`, if a block returns a
107
+ replacement node, it will not be iterated on. In order to allow recursive
108
+ processing of replacement nodes, `Steadfast.rewrite` provides the block with a
109
+ reference to itself, as a block argument, which you can use to make recursive
110
+ calls to `Steadfast.rewrite`:
111
+
112
+ ```ruby
113
+ Steadfast.rewrite(ast) { |node, &block|
114
+ if (replacement = should_replace_node?(node))
115
+ Steadfast.rewrite(replacement, &block)
116
+ else
117
+ node
118
+ end
119
+ }
120
+ ```
121
+
122
+ ## Code Quoting and Templating
123
+
124
+ Steadfast provides a quote / unquote mechanism similar to that found in
125
+ languages such as Elixir or Lisp, to facilitate code generation and code
126
+ templating.
127
+
128
+ `Steadfast.quote` is used to quote code, returning an AST for the code block
129
+ provided to it:
130
+
131
+ ```ruby
132
+ Steadfast.quote(binding) { 42 } #=> IntegerNode
133
+ Steadfast.quote(binding) { foo(42) } #=> CallNode
134
+ Steadfast.quote(binding) { -> { 42 } } #=> LambdaNode
135
+ Steadfast.quote(binding) { foo; bar } #=> StatementsNode
136
+ ```
137
+
138
+ `Steadfast.unquote` is used to convert a literal value into an AST. Normally, it
139
+ is used inside a `quote` block in order to inject the result of an expression or
140
+ block. The `unquote` call lets you inject arbitrary values into the quoted code:
141
+
142
+ ```ruby
143
+ foo = 42
144
+ Steadfast.quote(binding) { x + unquote(foo) } #=> x + 42
145
+ ```
146
+
147
+ ## Creating Nodes
148
+
149
+ The easiest way to create new AST nodes is by using `Steadfast.quote`, as
150
+ explained above. Literal values can also be created using `Steadfast.unquote`:
151
+
152
+ ```ruby
153
+ Steadfast.unquote({ foo: 1, bar: 2 })
154
+ ```
155
+
156
+ Arbitrary types of nodescan be created by using `Steadfast.make_node` and
157
+ providing a string containing Ruby syntax:
158
+
159
+ ```ruby
160
+ Steadfast.make_node('foo.bar(:baz)') #=> CallNode
161
+ ```
162
+
163
+ The `Steadfast.make_node` method takes the following optional arguments:
164
+
165
+ - `location`: a Prism node or Prism location which will be used as the location
166
+ for the generated AST.
167
+ - `lvars`: an array containing local variable names used in the given code
168
+ snippet.
169
+ - `binding`: a binding for determining local variable names used in the given
170
+ code snippet.
171
+
172
+ In addition to `Steadfast.make_node`, additional methods allow the creation of
173
+ specific node types:
174
+
175
+ - `Steadfast.make_call(name:, receiver:, args:)` - create a call node.
176
+ - `Steadfast.make_def(name:, params:, :body)` - create a method definition.
177
+
178
+ ## Traversing ASTs and Finding Nodes
179
+
180
+ Use `Steadfast.walk` to visit each node in an AST:
181
+
182
+ ```ruby
183
+ Steadfast.walk(ast) { p it } #=> print out each node in the AST
184
+ ```
185
+
186
+ To find a node, use `Steadfast.find`. This method will return the first node for
187
+ which the given block returns true, or nil if none found:
188
+
189
+ ```ruby
190
+ # look for a call to foo
191
+ Steadfast.find(ast) { n in Ruby::CallNode(receiver: nil, name: :foo) }
192
+ ```
193
+
194
+ To find all nodes that respond to specific criteria, use `Steadfast.find_all`:
195
+
196
+ ```ruby
197
+ # find all method calls without a receiver
198
+ Steadfast.find_all(ast) { n in Ruby::CallNode(receiver: nil) }
199
+ ```
200
+
201
+ ## AST Conversion and Evaluation
202
+
203
+ Additional Steadfast allow you to convert a method or proc to AST, to convert an
204
+ AST to source code, and to evaluate ASTs. To get the AST for a proc or method,
205
+ use `Steadfast.to_ast`:
206
+
207
+ ```ruby
208
+ def foo
209
+ 42
210
+ end
211
+
212
+ foo_ast = Steadfast.to_ast(method(:foo))
213
+ ```
214
+
215
+ To convert an AST to source code, use `Steadfast.to_source`:
216
+
217
+ ```ruby
218
+ Steadfast.to_source(foo_ast) #=> "def foo..."
219
+ ```
220
+
221
+ To evaluate an AST use `Steadfast.eval_ast`:
222
+
223
+ ```ruby
224
+ ast = quote { puts 'Hi!' }
225
+ Steadfast.eval_ast(ast) #=> 'Hi!!!'
226
+ ```
@@ -13,6 +13,8 @@ module Steadfast
13
13
  # @replacements [Hash] replacements
14
14
  # @return [Ruby::Prism::Node]
15
15
  def self.rewrite(ast, **replacements, &rewrite_block)
16
+ raise ArgumentError, "Not an AST" if !ast.is_a?(Ruby::Prism::Node)
17
+
16
18
  rewrite_block ||= ->(n) do
17
19
  if (v = replacements[n]) && v.is_a?(Hash)
18
20
  n.copy(**v)
@@ -17,6 +17,7 @@ module Steadfast
17
17
  VISIT_PLANS = {
18
18
  alias_global_variable: [:keyword_loc, :whitespace, :new_name, :whitespace, :old_name],
19
19
  alias_method: [:keyword_loc, :whitespace, :new_name, :whitespace, :old_name],
20
+ alternation_pattern: [:left, :whitespace, :operator_loc, :whitespace, :right],
20
21
  and: [:left, :whitespace, :operator_loc, :whitespace, :right],
21
22
  assoc_splat: [:operator_loc, :value],
22
23
  back_reference_read: :emit_verbatim,
@@ -50,11 +51,10 @@ module Steadfast
50
51
  :statements, :newline, :end_keyword_loc],
51
52
  forwarding_arguments: :emit_verbatim,
52
53
  forwarding_parameter: :emit_verbatim,
53
- forwarding_super: :emit_verbatim,
54
+ forwarding_super: [:keyword_loc, :block],
54
55
  global_variable_read: :emit_verbatim,
55
56
  global_variable_target: :emit_verbatim,
56
57
  global_variable_write: [:name_loc, :operator_loc, :value],
57
- imaginary: :emit_verbatim,
58
58
  implicit: :nop,
59
59
  implicit_rest: :nop,
60
60
  in: [:in_loc, :whitespace, :pattern, :whitespace, :then_loc],
@@ -88,7 +88,7 @@ module Steadfast
88
88
  optional_keyword_parameter: [:name_loc, :whitespace, :value],
89
89
  or: [:left, :whitespace, :operator_loc, :whitespace, :right],
90
90
  parentheses: [:opening_loc, :body, :closing_loc],
91
- program: :emit_verbatim,
91
+ program: [:statements],
92
92
  pinned_expression: [:operator_loc, :lparen_loc, :expression, :rparen_loc],
93
93
  pinned_variable: [:operator_loc, :variable],
94
94
  rational: :emit_verbatim,
@@ -360,6 +360,11 @@ module Steadfast
360
360
  emit_code(node.subsequent)
361
361
  end
362
362
 
363
+ def visit_imaginary_node(node)
364
+ emit_code(node.numeric)
365
+ emit('i')
366
+ end
367
+
363
368
  def visit_interpolated_symbol_node(node)
364
369
  heredoc = node.opening_loc && node.opening_loc.slice =~ /\<\</
365
370
  emit_code(node.opening_loc)
@@ -672,6 +677,16 @@ module Steadfast
672
677
  # @param node [Ruby::Prism::Node]
673
678
  # @return [void]
674
679
  def emit_verbatim(node)
680
+ if node.child_nodes.size > 0
681
+ puts
682
+ puts '*' * 40
683
+ pp node
684
+ puts
685
+ pp node.child_nodes
686
+ puts
687
+ puts caller.join("\n")
688
+ exit!
689
+ end
675
690
  emit_code(node.location)
676
691
  end
677
692
 
@@ -1,4 +1,4 @@
1
1
  module Steadfast
2
2
  # Steadfast version
3
- VERSION = '0.4.0'
3
+ VERSION = '0.5.0'
4
4
  end
data/lib/steadfast.rb CHANGED
@@ -31,6 +31,7 @@ module Steadfast
31
31
 
32
32
  class << self
33
33
  include Steadfast::Make
34
+ include Steadfast::Quote
34
35
 
35
36
  # Returns an AST for the given object.
36
37
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: steadfast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner