steadfast 0.2.0 → 0.4.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 +4 -4
- data/CHANGELOG.md +16 -0
- data/lib/steadfast/make.rb +142 -0
- data/lib/steadfast/quote.rb +173 -0
- data/lib/steadfast/rewriter.rb +5 -19
- data/lib/steadfast/source_mapping.rb +4 -4
- data/lib/steadfast/sourcifier.rb +14 -7
- data/lib/steadfast/version.rb +1 -1
- data/lib/steadfast/visitor.rb +39 -0
- data/lib/steadfast.rb +44 -3
- metadata +8 -21
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 65edd414b2b9a53a27c9965da47829809b1abdfc7e3cd2f9dfb26e9e155976b6
|
|
4
|
+
data.tar.gz: 6560196ff027bb0c1d7a669ce7f80c1af4e4d6419792bd872a637594f39eee74
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 01f35b02170b51dbb77f363b65d2ac812fae08c66796f8754d6a491c5357114f418ac6baccbf9646c912885f9f9ef0e23d483641f4cf63e653fc16ebcabdb4df
|
|
7
|
+
data.tar.gz: dd8a6449757d1b35bfeb5005ff2b5209b2873c869ee7425f5caf36c5b368e352d7fc364f71ef64db5962698a4a547832aeda7a21b2e2070c9279ad04601f84fd
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
# 0.4.0 2026-11-25
|
|
2
|
+
|
|
3
|
+
- Improve sourcifier output
|
|
4
|
+
- Implement `quote` and `unquote`
|
|
5
|
+
- Implement additional `make_xxx` methods
|
|
6
|
+
- Implement `make_statements`
|
|
7
|
+
- Implement `make_literal`
|
|
8
|
+
- Implement `make_node`
|
|
9
|
+
|
|
10
|
+
# 0.3.0 2026-11-24
|
|
11
|
+
|
|
12
|
+
- Implement `Steadfast.find_all`
|
|
13
|
+
- Implement `Steadfast.find`
|
|
14
|
+
- Implement `Steadfast.walk`
|
|
15
|
+
- Add `Visitor` base class, base `Rewriter` off it
|
|
16
|
+
|
|
1
17
|
# 0.2.0 2026-11-24
|
|
2
18
|
|
|
3
19
|
- Use frozen string literals in `eval_ast`
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Steadfast
|
|
4
|
+
module Make
|
|
5
|
+
# @param src [String]
|
|
6
|
+
# @param location [Ruby::Prism::Node, Ruby::Prism::Location]
|
|
7
|
+
# @param lvars [Array<Symbol>] optional local var list
|
|
8
|
+
# @param binding [Binding] optional binding
|
|
9
|
+
# @return [Ruby::Prism::Node]
|
|
10
|
+
def make_node(src, location: nil, lvars: nil, binding: nil)
|
|
11
|
+
loc = location && __make_get_source_location__(location)
|
|
12
|
+
scopes = [(binding&.local_variables || []), lvars].compact
|
|
13
|
+
opts = {
|
|
14
|
+
raise_error: true,
|
|
15
|
+
frozen_string_literal: true,
|
|
16
|
+
filepath: loc && loc[0],
|
|
17
|
+
line: loc && loc[1],
|
|
18
|
+
scopes: scopes
|
|
19
|
+
}
|
|
20
|
+
ast = Ruby::Prism.parse(src, **opts).value.statements.body[0]
|
|
21
|
+
ast.send(:source).__filename__ = loc[0] if loc
|
|
22
|
+
ast
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
IDENTIFIER_RE = /^[^\(\)\;\:\s]+$/
|
|
26
|
+
|
|
27
|
+
# Makes a def node.
|
|
28
|
+
#
|
|
29
|
+
# @param name [String, Symbol]
|
|
30
|
+
# @param params [Ruby::Prism::Node, nil]
|
|
31
|
+
# @param body [Ruby::Prism::StatementsNode]
|
|
32
|
+
# @param location [Ruby::Prism::Location]
|
|
33
|
+
# @return [Ruby::Prism::Node]
|
|
34
|
+
def make_def(name, params: nil, body:, location: nil)
|
|
35
|
+
raise SyntaxError, 'Invalid method name' if !(IDENTIFIER_RE === name)
|
|
36
|
+
|
|
37
|
+
n = make_node("def #{name}(x); 1; end", location:)
|
|
38
|
+
n.copy(body:, parameters: params)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Makes a local variable read node.
|
|
42
|
+
#
|
|
43
|
+
# @param name [String, Symbol]
|
|
44
|
+
# @param location [Ruby::Prism::Location]
|
|
45
|
+
# @return [Ruby::Prism::Node]
|
|
46
|
+
def make_lvar_read(name, location: nil)
|
|
47
|
+
raise SyntaxError, 'Invalid identifier name' if !(IDENTIFIER_RE === name)
|
|
48
|
+
|
|
49
|
+
make_node(name.to_s, lvars: [name.to_sym], location:)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Makes a literal array node.
|
|
53
|
+
#
|
|
54
|
+
# @param array [Array]
|
|
55
|
+
# @param location [Ruby::Prism::Location]
|
|
56
|
+
# @return [Ruby::Prism::Node]
|
|
57
|
+
def make_array(array, location: nil)
|
|
58
|
+
raise ArgumentError, "Not an array" if !array.is_a?(Array)
|
|
59
|
+
|
|
60
|
+
n = make_node('[]', location:)
|
|
61
|
+
Steadfast.rewrite(n, n => { elements: array.map { make_literal(it, location:) } })
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Makes a literal hash node.
|
|
65
|
+
#
|
|
66
|
+
# @param hash [Hash]
|
|
67
|
+
# @param location [Ruby::Prism::Location]
|
|
68
|
+
# @return [Ruby::Prism::Node]
|
|
69
|
+
def make_hash(hash, location: nil)
|
|
70
|
+
raise ArgumentError, "Not a hash" if !hash.is_a?(Hash)
|
|
71
|
+
|
|
72
|
+
n = make_node('{ x => 1 }', location:)
|
|
73
|
+
assoc_node = n.elements[0]
|
|
74
|
+
elements = hash.map { |k, v|
|
|
75
|
+
Steadfast.rewrite(
|
|
76
|
+
assoc_node,
|
|
77
|
+
assoc_node => {
|
|
78
|
+
key: make_literal(k, location:),
|
|
79
|
+
value: make_literal(v, location:)
|
|
80
|
+
}
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
Steadfast.rewrite(n, n => { elements: })
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Makes a literal value node.
|
|
87
|
+
#
|
|
88
|
+
# @param value [any]
|
|
89
|
+
# @param location [Ruby::Prism::Location]
|
|
90
|
+
# @return [Ruby::Prism::Node]
|
|
91
|
+
def make_literal(value, location: nil)
|
|
92
|
+
case value
|
|
93
|
+
when Integer, Float, String, Symbol, Range, Regexp, nil, true, false, Class, Module
|
|
94
|
+
make_node(value.inspect, location:)
|
|
95
|
+
when Array
|
|
96
|
+
make_array(value, location:)
|
|
97
|
+
when Hash
|
|
98
|
+
make_hash(value, location:)
|
|
99
|
+
else
|
|
100
|
+
raise ArgumentError, "Can't make AST node for #{value.inspect}"
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Makes a StatementsNode containing the given statement.
|
|
105
|
+
#
|
|
106
|
+
# @param statements [Array<Ruby::Prism::Node>]
|
|
107
|
+
# @param location [Ruby::Prism::Location]
|
|
108
|
+
# @return [Ruby::Prism::Node]
|
|
109
|
+
def make_statements(statements, location: nil)
|
|
110
|
+
raise ArgumentError, "Not an array" if !statements.is_a?(Array)
|
|
111
|
+
raise ArgumentError, "Empty array given" if statements.size == 0
|
|
112
|
+
raise ArgumentError, "Invalid array given" if statements.any? { !it.is_a?(Ruby::Prism::Node) }
|
|
113
|
+
|
|
114
|
+
n = make_node('(1)', location:)
|
|
115
|
+
Steadfast.rewrite(n.body, n.body => { body: statements })
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Makes a constant node.
|
|
119
|
+
#
|
|
120
|
+
# @param name [String, Symbol]
|
|
121
|
+
# @param location [Ruby::Prism::Location]
|
|
122
|
+
# @return [Ruby::Prism::Node]
|
|
123
|
+
def make_constant(name, location: nil)
|
|
124
|
+
raise SyntaxError, 'Invalid identifier name' if name !~ /^[A-Z][A-Za-z_\d]*$/
|
|
125
|
+
|
|
126
|
+
make_node(name.to_s, location:)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
private
|
|
130
|
+
|
|
131
|
+
# Returns the source location information (filename, line) for the given
|
|
132
|
+
# location.
|
|
133
|
+
#
|
|
134
|
+
# @param location [Ruby::Prism::Node, Ruby::Prism::Location]
|
|
135
|
+
# @return [Array] [filename, lineno]
|
|
136
|
+
def __make_get_source_location__(location)
|
|
137
|
+
filename = location.send(:source).__filename__ || raise('No filename set')
|
|
138
|
+
lineno = location.start_line
|
|
139
|
+
[filename, lineno]
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Steadfast
|
|
4
|
+
# Quote implements the quote / unquote code quoting tools. The quote/unquote
|
|
5
|
+
# pair of coding tools permits creation of code templates.
|
|
6
|
+
module Quote
|
|
7
|
+
extend self
|
|
8
|
+
|
|
9
|
+
# Quotes the code in the given block, interpolating all unquote expressions.
|
|
10
|
+
# Returns the resulting AST. The unquote call is used to perform a
|
|
11
|
+
# computation at the time of building the AST. The result of unquote is an
|
|
12
|
+
# object that can be literalized (See #unquote). A call to unquote can also
|
|
13
|
+
# be used to inject an AST into the code. The quote method takes a binding,
|
|
14
|
+
# which is then used for any unquote computations.
|
|
15
|
+
#
|
|
16
|
+
# Example:
|
|
17
|
+
#
|
|
18
|
+
# i = 4
|
|
19
|
+
# ast = Steadfast.quote(binding) { ->(x) { unquote(i) + x } }
|
|
20
|
+
# l = Steadfast.eval_ast(ast) #=> ->(x) { 4 + x }
|
|
21
|
+
# l + 3 #=> 7
|
|
22
|
+
#
|
|
23
|
+
# A call to unquote can also be in the form of a block, e.g. if the
|
|
24
|
+
# computation consists of more than one statement. Nested quote calls can be
|
|
25
|
+
# made in order to compute an AST that can be injected into the quoted code.
|
|
26
|
+
# Alternating calls to unquote and quote can be nested arbitrarily.
|
|
27
|
+
#
|
|
28
|
+
# Example:
|
|
29
|
+
#
|
|
30
|
+
# i = 4
|
|
31
|
+
# ast = Steadfast.quote(binding) {
|
|
32
|
+
# x = unquote {
|
|
33
|
+
# j = i + 3
|
|
34
|
+
# quote { (x + unquote(j)) }
|
|
35
|
+
# }
|
|
36
|
+
# }
|
|
37
|
+
#
|
|
38
|
+
# Nested calls to unquote and quote should be unqualified (i.e. quote
|
|
39
|
+
# instead of Steadfast.quote). In addition, nested calls to quote should not
|
|
40
|
+
# provide a binding, just a block. They will automatically inherit the
|
|
41
|
+
# binding of the outermost quote.
|
|
42
|
+
#
|
|
43
|
+
# @param binding [Binding, nil] binding for unquote computation
|
|
44
|
+
# @return [Ruby::Prism::Node] quoted code
|
|
45
|
+
def quote(binding, &block)
|
|
46
|
+
raise Steadfast::Error, "No block given" if !block
|
|
47
|
+
|
|
48
|
+
ast = Steadfast.to_ast(block)
|
|
49
|
+
statements = __quote_select_statements_node__(statements)
|
|
50
|
+
raise Steadfast::Error, "Block can't be quoted" if !ast
|
|
51
|
+
|
|
52
|
+
quoted = Steadfast.rewrite(ast) { |n|
|
|
53
|
+
next n if !(n in Ruby::Prism::CallNode(receiver: nil, name: :unquote))
|
|
54
|
+
|
|
55
|
+
__unquote_evaluate__(n, binding)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
__quote_normalize_return_value__(quoted)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Returns the AST for the given literal value. The value should be one of
|
|
62
|
+
# the builtin types: true, false, nil, String, Float, Integer, Symbol,
|
|
63
|
+
# Array, Hash, Module, Class, or Ruby::Prism::Node (an AST). If value is an
|
|
64
|
+
# AST, unquote will return it. All other values are converted to an AST
|
|
65
|
+
# representation as literal values.
|
|
66
|
+
#
|
|
67
|
+
# If a node location is provided, the resulting AST will use that location
|
|
68
|
+
# information (filename + lineno), for purposes of providing correct
|
|
69
|
+
# backtraces.
|
|
70
|
+
#
|
|
71
|
+
# The return value is an AST. If the unquote computation's result is an
|
|
72
|
+
# array of AST nodes, it is returned wrapped in a StatementsNode.
|
|
73
|
+
#
|
|
74
|
+
# @param value [expr] expression to evaluate and inject into surrounding code
|
|
75
|
+
# @return [Ruby::Prism::Node]
|
|
76
|
+
def unquote(value, location = nil)
|
|
77
|
+
__unquote_convert_value__(value, location)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
# Selects the StatementsNode out of the given AST node. If the
|
|
83
|
+
# statementsNode contains a single statement, returns that statement.
|
|
84
|
+
# Otherwise, returns the StatementsNode.
|
|
85
|
+
#
|
|
86
|
+
# @param node [Ruby::Prism::Node]
|
|
87
|
+
# @return [Ruby::Prism::Node]
|
|
88
|
+
def __quote_normalize_return_value__(node)
|
|
89
|
+
statements = __quote_select_statements_node__(node)
|
|
90
|
+
return node if !statements
|
|
91
|
+
|
|
92
|
+
(statements.body.size == 1) ? statements.body[0] : statements
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Selects the outermost StatementsNode for the given AST, according to the
|
|
96
|
+
# node type.
|
|
97
|
+
#
|
|
98
|
+
# @param node [Ruby::Prism::Node]
|
|
99
|
+
# @return [Ruby::Prism::StatementsNode, nil]
|
|
100
|
+
def __quote_select_statements_node__(node)
|
|
101
|
+
case node
|
|
102
|
+
when Ruby::Prism::StatementsNode
|
|
103
|
+
node
|
|
104
|
+
when Ruby::Prism::BlockNode
|
|
105
|
+
node.body
|
|
106
|
+
when Ruby::Prism::CallNode
|
|
107
|
+
node.block.body
|
|
108
|
+
else
|
|
109
|
+
nil
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Evaluates the given AST node by sourcifying it and eval'ing the resulting
|
|
114
|
+
# code with the given binding. The resulting value is rendered into an AST
|
|
115
|
+
# and returned.
|
|
116
|
+
#
|
|
117
|
+
# @param node [Ruby::Prism::Node]
|
|
118
|
+
# @param binding [Binding]
|
|
119
|
+
# @return [Ruby::Prism::Node]
|
|
120
|
+
def __unquote_evaluate__(node, binding)
|
|
121
|
+
if node.arguments && (node.arguments.arguments.size == 1) && !node.block
|
|
122
|
+
arg = __unquote_rewrite_nested_quote__(node.arguments.arguments[0], binding)
|
|
123
|
+
v = Steadfast.eval_ast(arg, nil, binding)
|
|
124
|
+
__unquote_convert_value__(v, node)
|
|
125
|
+
elsif node.block && !node.arguments
|
|
126
|
+
ast = __unquote_rewrite_nested_quote__(node.block.body, binding)
|
|
127
|
+
v = Steadfast.eval_ast(node.block.body, nil, binding)
|
|
128
|
+
__unquote_convert_value__(v, node)
|
|
129
|
+
else
|
|
130
|
+
raise ArgumentError, "Invalid unquote arguments"
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Rewrites nested calls to quote inside an unquote block or expression.
|
|
135
|
+
#
|
|
136
|
+
# @param node [Ruby::Prism::Node]
|
|
137
|
+
# @param binding [Binding]
|
|
138
|
+
# @return [Ruby::Prism::Node]
|
|
139
|
+
def __unquote_rewrite_nested_quote__(node, binding)
|
|
140
|
+
Steadfast.rewrite(node) { |n2|
|
|
141
|
+
next n2 if !(n2 in Ruby::Prism::CallNode(receiver: nil, name: :quote))
|
|
142
|
+
|
|
143
|
+
raise ArgumentError, "Invalid arguments in call to quote" if n2.arguments
|
|
144
|
+
raise ArgumentError, "Missing block in call to quote" if !n2.block
|
|
145
|
+
|
|
146
|
+
n3 = Steadfast.make_node("Steadfast::Quote.quote(binding) do\nnil \nend", location: n2)
|
|
147
|
+
Steadfast.rewrite(n3, n3.block.body => n2.block.body)
|
|
148
|
+
}
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# Returns an AST for the given value. This method converts the result of the
|
|
152
|
+
# unquote computation into an AST.
|
|
153
|
+
#
|
|
154
|
+
# @param value [any]
|
|
155
|
+
# @param location [Ruby::Prism::Location]
|
|
156
|
+
# @return [Ruby::Prism::Node]
|
|
157
|
+
def __unquote_convert_value__(value, location)
|
|
158
|
+
case value
|
|
159
|
+
when Integer, Float, String, Symbol, Range, Regexp, nil, true, false, Hash, Class, Module
|
|
160
|
+
Steadfast.make_literal(value, location:)
|
|
161
|
+
when Array
|
|
162
|
+
if value.first.is_a?(Ruby::Prism::Node)
|
|
163
|
+
return Steadfast.make_statements(value)
|
|
164
|
+
end
|
|
165
|
+
Steadfast.make_literal(value, location:)
|
|
166
|
+
when Ruby::Prism::Node
|
|
167
|
+
value
|
|
168
|
+
else
|
|
169
|
+
raise Steadfast::Error, "Don't know how to unquote #{value.inspect}"
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
end
|
data/lib/steadfast/rewriter.rb
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'steadfast/visitor'
|
|
4
|
+
|
|
3
5
|
module Steadfast
|
|
4
6
|
|
|
5
7
|
# The Rewriter class is used for rewriting ASTs.
|
|
6
|
-
class Rewriter
|
|
8
|
+
class Rewriter < Visitor
|
|
7
9
|
# Rewrites the given AST with a either a hash of node replacements, or a
|
|
8
10
|
# block which is invoked for each node in the AST.
|
|
9
11
|
#
|
|
@@ -18,23 +20,7 @@ module Steadfast
|
|
|
18
20
|
replacements.key?(n) ? replacements[n] : n
|
|
19
21
|
end
|
|
20
22
|
end
|
|
21
|
-
ast.accept(new(rewrite_block))
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
# Initializes the rewriter.
|
|
25
|
-
#
|
|
26
|
-
# @param rewrite_block [Proc]
|
|
27
|
-
# @return [void]
|
|
28
|
-
def initialize(rewrite_block)
|
|
29
|
-
@rewrite_block = rewrite_block
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
# Intercepts visit_xxxx method calls.
|
|
33
|
-
#
|
|
34
|
-
# @param sym [Symbol]
|
|
35
|
-
# @return [void]
|
|
36
|
-
def method_missing(sym, *, **, &)
|
|
37
|
-
sym =~ /^visit_/ ? visit(*) : super(sym, *, **, &)
|
|
23
|
+
ast.accept(new(&rewrite_block))
|
|
38
24
|
end
|
|
39
25
|
|
|
40
26
|
# Visits the given node, and subsequently its subnodes, invoking the rewrite
|
|
@@ -43,7 +29,7 @@ module Steadfast
|
|
|
43
29
|
# @param n [Ruby::Prism::Node]
|
|
44
30
|
# @return [Ruby::Prism::Node]
|
|
45
31
|
def visit(n)
|
|
46
|
-
n2 = @
|
|
32
|
+
n2 = @block.(n, &@block)
|
|
47
33
|
return n2 if n2 != n
|
|
48
34
|
|
|
49
35
|
changes = {}
|
|
@@ -17,7 +17,7 @@ module Steadfast
|
|
|
17
17
|
def set(path1, line1, path2, line2)
|
|
18
18
|
(@map ||= {})["#{path1}:#{line1}"] ||= "#{path2}:#{line2}"
|
|
19
19
|
end
|
|
20
|
-
|
|
20
|
+
|
|
21
21
|
# Translates entries in the given backtrace according to the global source
|
|
22
22
|
# map.
|
|
23
23
|
#
|
|
@@ -36,7 +36,7 @@ module Steadfast
|
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
private
|
|
39
|
-
|
|
39
|
+
|
|
40
40
|
# Returns a mapped location for the given location entry.
|
|
41
41
|
#
|
|
42
42
|
# @param loc [String] location string of the form file:line
|
|
@@ -44,9 +44,9 @@ module Steadfast
|
|
|
44
44
|
def find_sourcemap_entry(loc)
|
|
45
45
|
loc = @map[loc]
|
|
46
46
|
return nil if !loc
|
|
47
|
-
|
|
47
|
+
|
|
48
48
|
(loc =~ /^\(steadfast-[0-9a-f]+\)\:\d+$/) ? find_sourcemap_entry(loc) : loc
|
|
49
|
-
end
|
|
49
|
+
end
|
|
50
50
|
end
|
|
51
51
|
end
|
|
52
52
|
end
|
data/lib/steadfast/sourcifier.rb
CHANGED
|
@@ -18,7 +18,6 @@ module Steadfast
|
|
|
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
20
|
and: [:left, :whitespace, :operator_loc, :whitespace, :right],
|
|
21
|
-
assoc: [:key, :whitespace, :operator_loc, :whitespace, :value],
|
|
22
21
|
assoc_splat: [:operator_loc, :value],
|
|
23
22
|
back_reference_read: :emit_verbatim,
|
|
24
23
|
block: [:opening_loc, :parameters, :newline, :body, :newline, :closing_loc],
|
|
@@ -89,6 +88,7 @@ module Steadfast
|
|
|
89
88
|
optional_keyword_parameter: [:name_loc, :whitespace, :value],
|
|
90
89
|
or: [:left, :whitespace, :operator_loc, :whitespace, :right],
|
|
91
90
|
parentheses: [:opening_loc, :body, :closing_loc],
|
|
91
|
+
program: :emit_verbatim,
|
|
92
92
|
pinned_expression: [:operator_loc, :lparen_loc, :expression, :rparen_loc],
|
|
93
93
|
pinned_variable: [:operator_loc, :variable],
|
|
94
94
|
rational: :emit_verbatim,
|
|
@@ -173,6 +173,12 @@ module Steadfast
|
|
|
173
173
|
visit_comma_separated_nodes(node.arguments[subscript])
|
|
174
174
|
end
|
|
175
175
|
|
|
176
|
+
def visit_assoc_node(node)
|
|
177
|
+
emit_code(node.key)
|
|
178
|
+
node.operator_loc ? emit(' => ') : emit_whitespace
|
|
179
|
+
emit_code(node.value)
|
|
180
|
+
end
|
|
181
|
+
|
|
176
182
|
def visit_begin_node(node)
|
|
177
183
|
emit_code(node.begin_keyword_loc)
|
|
178
184
|
emit_newline
|
|
@@ -210,8 +216,8 @@ module Steadfast
|
|
|
210
216
|
if (ml = node.message_loc)
|
|
211
217
|
ol = node.opening_loc
|
|
212
218
|
emit_message_loc = !ol || (ol.start_line != ml.start_line) || (ol.start_column != ml.start_column)
|
|
213
|
-
emit_whitespace if node.name =~
|
|
214
|
-
emit_code(node.message_loc) if emit_message_loc
|
|
219
|
+
emit_whitespace if node.name =~ /^\+|\-|\*|\/|\&|\|\*\*|\&\&|\|\||\%|={2,3}$/
|
|
220
|
+
emit_code(node.message_loc, inhibit_new_line: true) if emit_message_loc
|
|
215
221
|
end
|
|
216
222
|
emit_code(node.opening_loc)
|
|
217
223
|
emit_whitespace if !node.opening_loc && node.arguments
|
|
@@ -673,11 +679,11 @@ module Steadfast
|
|
|
673
679
|
#
|
|
674
680
|
# @param loc [Ruby::Prism::Location, Ruby::Prism::Node, nil]
|
|
675
681
|
# @return [void]
|
|
676
|
-
def emit_code(loc)
|
|
682
|
+
def emit_code(loc, inhibit_new_line: false)
|
|
677
683
|
return if !loc
|
|
678
684
|
return visit(loc) if loc.is_a?(Ruby::Prism::Node)
|
|
679
685
|
|
|
680
|
-
record_loc_source_mapping(loc) if @sourcemap
|
|
686
|
+
record_loc_source_mapping(loc, inhibit_new_line:) if @sourcemap
|
|
681
687
|
emit(loc.slice.chomp)
|
|
682
688
|
end
|
|
683
689
|
|
|
@@ -688,7 +694,8 @@ module Steadfast
|
|
|
688
694
|
#
|
|
689
695
|
# @param loc [Ruby::Prism::Location] code location
|
|
690
696
|
# @return [void]
|
|
691
|
-
def record_loc_source_mapping(loc)
|
|
697
|
+
def record_loc_source_mapping(loc, inhibit_new_line:)
|
|
698
|
+
last_loc = loc
|
|
692
699
|
source = loc.send(:source)
|
|
693
700
|
last_source = @last_loc&.send(:source)
|
|
694
701
|
different_source = source.__filename__ && source != last_source
|
|
@@ -697,7 +704,7 @@ module Steadfast
|
|
|
697
704
|
return if !changed
|
|
698
705
|
|
|
699
706
|
if (filename = source.__filename__) && (filename || last_source.__filename__)
|
|
700
|
-
emit_newline if different_source && last_source
|
|
707
|
+
emit_newline if !inhibit_new_line && different_source && last_source
|
|
701
708
|
Steadfast::SourceMapping.set(
|
|
702
709
|
@filename, @line, filename, loc.start_line
|
|
703
710
|
)
|
data/lib/steadfast/version.rb
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Steadfast
|
|
4
|
+
|
|
5
|
+
# The Visitor class is used for traversing an AST, visiting each node in it.
|
|
6
|
+
class Visitor
|
|
7
|
+
# Initializes the visitor with the given visitor block.
|
|
8
|
+
#
|
|
9
|
+
# @param block [Proc]
|
|
10
|
+
# @return [void]
|
|
11
|
+
def initialize(&block)
|
|
12
|
+
@block = block
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Intercepts visit_xxxx method calls.
|
|
16
|
+
#
|
|
17
|
+
# @param sym [Symbol]
|
|
18
|
+
# @return [void]
|
|
19
|
+
def method_missing(sym, *, **, &)
|
|
20
|
+
sym =~ /^visit_/ ? visit(*) : super(sym, *, **, &)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Visits the given node, and subsequently its subnodes, invoking the block.
|
|
24
|
+
#
|
|
25
|
+
# @param n [Ruby::Prism::Node]
|
|
26
|
+
# @return [void]
|
|
27
|
+
def visit(n)
|
|
28
|
+
@block.(n, &@block)
|
|
29
|
+
|
|
30
|
+
n.deconstruct_keys(nil).each do |k, v|
|
|
31
|
+
if v.is_a?(Ruby::Prism::Node)
|
|
32
|
+
visit(v)
|
|
33
|
+
elsif v in [Ruby::Prism::Node, *]
|
|
34
|
+
v.each { visit(it) }
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
data/lib/steadfast.rb
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
|
|
3
5
|
require 'steadfast/source_mapping'
|
|
4
6
|
require 'steadfast/sourcifier'
|
|
7
|
+
require 'steadfast/visitor'
|
|
5
8
|
require 'steadfast/rewriter'
|
|
9
|
+
require 'steadfast/make'
|
|
10
|
+
require 'steadfast/quote'
|
|
6
11
|
|
|
7
12
|
# Tell Ruby to keep script lines in memory, so calling Proc#syntax_tree or
|
|
8
13
|
# Method#syntax_tree will work.
|
|
@@ -25,6 +30,8 @@ module Steadfast
|
|
|
25
30
|
end
|
|
26
31
|
|
|
27
32
|
class << self
|
|
33
|
+
include Steadfast::Make
|
|
34
|
+
|
|
28
35
|
# Returns an AST for the given object.
|
|
29
36
|
#
|
|
30
37
|
# @param obj [Proc, Method, UnboundedMethod, Ruby::Prism::Node]
|
|
@@ -32,7 +39,11 @@ module Steadfast
|
|
|
32
39
|
def to_ast(obj)
|
|
33
40
|
case obj
|
|
34
41
|
when Proc, Method, UnboundMethod
|
|
35
|
-
obj.syntax_tree
|
|
42
|
+
ast = obj.syntax_tree
|
|
43
|
+
raise Steadfast::Error, "AST not found for given object" if !ast
|
|
44
|
+
|
|
45
|
+
set_source_filename(ast, obj)
|
|
46
|
+
ast
|
|
36
47
|
when Ruby::Prism::Node
|
|
37
48
|
obj
|
|
38
49
|
else
|
|
@@ -75,7 +86,7 @@ module Steadfast
|
|
|
75
86
|
# However, while `eval` respects a starting lineno set to 0, Prism does
|
|
76
87
|
# not, which can lead to off-by-one line nos in exception backtraces for
|
|
77
88
|
# synthetic ASTs.
|
|
78
|
-
#
|
|
89
|
+
#
|
|
79
90
|
# Note that when source_location is not nil, we do decrement the start
|
|
80
91
|
# lineno. Hopefully some day this wart will be removed from Ruby.G
|
|
81
92
|
source_location = ["(steadfast-#{SecureRandom.hex(4)})", 1]
|
|
@@ -87,12 +98,42 @@ module Steadfast
|
|
|
87
98
|
end
|
|
88
99
|
src = to_source(ast, source_location:, sourcemap: true, frozen_string_literal: true)
|
|
89
100
|
if eval_receiver
|
|
90
|
-
eval_receiver.instance_eval(src)
|
|
101
|
+
eval_receiver.instance_eval(src, source_location[0], source_location[1])
|
|
91
102
|
else
|
|
92
103
|
eval(src, eval_binding || binding, source_location[0], source_location[1])
|
|
93
104
|
end
|
|
94
105
|
end
|
|
95
106
|
|
|
107
|
+
# Visits the given AST, passing each node in the AST to the given block.
|
|
108
|
+
#
|
|
109
|
+
# @param ast [Ruby::Prism::Node]
|
|
110
|
+
# @return [void]
|
|
111
|
+
def walk(ast, &)
|
|
112
|
+
ast.accept(Visitor.new(&))
|
|
113
|
+
ast
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# Visits the given AST, passing each node in the AST to the given block.
|
|
117
|
+
# Returns the first node for which the block returns true.
|
|
118
|
+
#
|
|
119
|
+
# @param ast [Ruby::Prism::Node]
|
|
120
|
+
# @return [Ruby::Prism::Node, nil]
|
|
121
|
+
def find(ast, &block)
|
|
122
|
+
ast.accept(Visitor.new { return it if block.(it) })
|
|
123
|
+
nil
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Visits the given AST, passing each node in the AST to the given block.
|
|
127
|
+
# Returns all nodes for which the block returns true.
|
|
128
|
+
#
|
|
129
|
+
# @param ast [Ruby::Prism::Node]
|
|
130
|
+
# @return [Array<Ruby::Prism::Node>]
|
|
131
|
+
def find_all(ast, &block)
|
|
132
|
+
nodes = []
|
|
133
|
+
ast.accept(Visitor.new { nodes << it if block.(it) })
|
|
134
|
+
nodes
|
|
135
|
+
end
|
|
136
|
+
|
|
96
137
|
private
|
|
97
138
|
|
|
98
139
|
# Sets the source filename for the given ast to the source location of the
|
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
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sharon Rosner
|
|
@@ -9,22 +9,6 @@ bindir: bin
|
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
|
-
- !ruby/object:Gem::Dependency
|
|
13
|
-
name: debug_inspector
|
|
14
|
-
requirement: !ruby/object:Gem::Requirement
|
|
15
|
-
requirements:
|
|
16
|
-
-
|
|
17
|
-
- ">="
|
|
18
|
-
- !ruby/object:Gem::Version
|
|
19
|
-
version: "0"
|
|
20
|
-
type: :runtime
|
|
21
|
-
prerelease: false
|
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
-
requirements:
|
|
24
|
-
-
|
|
25
|
-
- ">="
|
|
26
|
-
- !ruby/object:Gem::Version
|
|
27
|
-
version: "0"
|
|
28
12
|
- !ruby/object:Gem::Dependency
|
|
29
13
|
name: minitest
|
|
30
14
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -62,17 +46,17 @@ dependencies:
|
|
|
62
46
|
requirement: !ruby/object:Gem::Requirement
|
|
63
47
|
requirements:
|
|
64
48
|
-
|
|
65
|
-
- "
|
|
49
|
+
- "="
|
|
66
50
|
- !ruby/object:Gem::Version
|
|
67
|
-
version:
|
|
51
|
+
version: 6.3.0
|
|
68
52
|
type: :development
|
|
69
53
|
prerelease: false
|
|
70
54
|
version_requirements: !ruby/object:Gem::Requirement
|
|
71
55
|
requirements:
|
|
72
56
|
-
|
|
73
|
-
- "
|
|
57
|
+
- "="
|
|
74
58
|
- !ruby/object:Gem::Version
|
|
75
|
-
version:
|
|
59
|
+
version: 6.3.0
|
|
76
60
|
email: sharon@noteflakes.com
|
|
77
61
|
executables: []
|
|
78
62
|
extensions: []
|
|
@@ -82,10 +66,13 @@ files:
|
|
|
82
66
|
- CHANGELOG.md
|
|
83
67
|
- README.md
|
|
84
68
|
- lib/steadfast.rb
|
|
69
|
+
- lib/steadfast/make.rb
|
|
70
|
+
- lib/steadfast/quote.rb
|
|
85
71
|
- lib/steadfast/rewriter.rb
|
|
86
72
|
- lib/steadfast/source_mapping.rb
|
|
87
73
|
- lib/steadfast/sourcifier.rb
|
|
88
74
|
- lib/steadfast/version.rb
|
|
75
|
+
- lib/steadfast/visitor.rb
|
|
89
76
|
homepage: "http://github.com/digital-fabric/steadfast"
|
|
90
77
|
licenses:
|
|
91
78
|
- MIT
|