to_source 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Readme.md +8 -1
- data/lib/to_source/version.rb +1 -1
- data/lib/to_source/visitor.rb +90 -0
- data/test/to_source/visitor_test.rb +44 -0
- metadata +2 -2
data/Readme.md
CHANGED
@@ -18,7 +18,14 @@ transform themselves into source code. It's the reverse of Rubinius' builtin
|
|
18
18
|
|
19
19
|
to_source needs Rubinius 2.0 to run, in either 1.8 or 1.9 mode.
|
20
20
|
|
21
|
-
|
21
|
+
To install it as a gem:
|
22
|
+
|
23
|
+
$ gem install to_source
|
24
|
+
|
25
|
+
And `require 'to_source'` from your code. Automatically, your AST nodes respond
|
26
|
+
to the `#to_source` method.
|
27
|
+
|
28
|
+
But if you're using Bundler, just put this in your Gemfile:
|
22
29
|
|
23
30
|
gem 'to_source'
|
24
31
|
|
data/lib/to_source/version.rb
CHANGED
data/lib/to_source/visitor.rb
CHANGED
@@ -87,6 +87,12 @@ module ToSource
|
|
87
87
|
node.finish.lazy_visit self, node
|
88
88
|
end
|
89
89
|
|
90
|
+
def range_exclude(node, parent)
|
91
|
+
node.start.lazy_visit self, node
|
92
|
+
emit '...'
|
93
|
+
node.finish.lazy_visit self, node
|
94
|
+
end
|
95
|
+
|
90
96
|
def regex_literal(node, parent)
|
91
97
|
emit '/'
|
92
98
|
emit node.source
|
@@ -217,6 +223,90 @@ module ToSource
|
|
217
223
|
emit node.name
|
218
224
|
end
|
219
225
|
|
226
|
+
def if(node, parent)
|
227
|
+
body, else_body = node.body, node.else
|
228
|
+
keyword = 'if'
|
229
|
+
|
230
|
+
if node.body.is_a?(Rubinius::AST::NilLiteral) && !node.else.is_a?(Rubinius::AST::NilLiteral)
|
231
|
+
|
232
|
+
body, else_body = else_body, body
|
233
|
+
keyword = 'unless'
|
234
|
+
end
|
235
|
+
|
236
|
+
emit keyword << ' '
|
237
|
+
node.condition.lazy_visit self, node
|
238
|
+
emit "\n"
|
239
|
+
|
240
|
+
@indentation += 1
|
241
|
+
|
242
|
+
if body.is_a?(Rubinius::AST::Block)
|
243
|
+
body.lazy_visit self, parent, true
|
244
|
+
else
|
245
|
+
emit current_indentation
|
246
|
+
body.lazy_visit self, parent
|
247
|
+
end
|
248
|
+
|
249
|
+
emit "\n"
|
250
|
+
|
251
|
+
if else_body.is_a?(Rubinius::AST::NilLiteral)
|
252
|
+
emit 'end'
|
253
|
+
return
|
254
|
+
end
|
255
|
+
|
256
|
+
emit "else\n"
|
257
|
+
|
258
|
+
if else_body.is_a?(Rubinius::AST::Block)
|
259
|
+
else_body.lazy_visit self, parent, true
|
260
|
+
else
|
261
|
+
emit current_indentation
|
262
|
+
else_body.lazy_visit self, parent
|
263
|
+
end
|
264
|
+
|
265
|
+
emit "\n"
|
266
|
+
emit 'end'
|
267
|
+
end
|
268
|
+
|
269
|
+
def while(node, parent)
|
270
|
+
emit 'while '
|
271
|
+
node.condition.lazy_visit self, node
|
272
|
+
emit "\n"
|
273
|
+
|
274
|
+
@indentation += 1
|
275
|
+
|
276
|
+
if node.body.is_a?(Rubinius::AST::Block)
|
277
|
+
node.body.lazy_visit self, parent, true
|
278
|
+
else
|
279
|
+
emit current_indentation
|
280
|
+
node.body.lazy_visit self, parent
|
281
|
+
end
|
282
|
+
|
283
|
+
emit "\n"
|
284
|
+
emit "end"
|
285
|
+
end
|
286
|
+
|
287
|
+
def until(node, parent)
|
288
|
+
emit 'until '
|
289
|
+
node.condition.lazy_visit self, node
|
290
|
+
emit "\n"
|
291
|
+
|
292
|
+
@indentation += 1
|
293
|
+
|
294
|
+
if node.body.is_a?(Rubinius::AST::Block)
|
295
|
+
node.body.lazy_visit self, parent, true
|
296
|
+
else
|
297
|
+
emit current_indentation
|
298
|
+
node.body.lazy_visit self, parent
|
299
|
+
end
|
300
|
+
|
301
|
+
emit "\n"
|
302
|
+
emit "end"
|
303
|
+
end
|
304
|
+
|
305
|
+
def return(node, parent)
|
306
|
+
emit 'return '
|
307
|
+
node.value.lazy_visit self, parent
|
308
|
+
end
|
309
|
+
|
220
310
|
private
|
221
311
|
|
222
312
|
def process_binary_operator(node, parent)
|
@@ -71,6 +71,10 @@ module ToSource
|
|
71
71
|
assert_source '20..34'
|
72
72
|
end
|
73
73
|
|
74
|
+
def test_range_exclude
|
75
|
+
assert_source '20...34'
|
76
|
+
end
|
77
|
+
|
74
78
|
def test_regex
|
75
79
|
assert_source '/.*/'
|
76
80
|
end
|
@@ -122,5 +126,45 @@ module ToSource
|
|
122
126
|
assert_source "!1"
|
123
127
|
assert_source "!!1"
|
124
128
|
end
|
129
|
+
|
130
|
+
def test_if
|
131
|
+
assert_source "if 3\n 9\nend"
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_if_with_multiple_blocks
|
135
|
+
assert_source "if 3\n 9\n 8\nend"
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_else
|
139
|
+
assert_source "if 3\n 9\nelse\n 9\nend"
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_else_with_multiple_blocks
|
143
|
+
assert_source "if 3\n 9\n 8\nelse\n 9\n 8\nend"
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_unless
|
147
|
+
assert_source "unless 3\n 9\nend"
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_while
|
151
|
+
assert_source "while false\n 3\nend"
|
152
|
+
end
|
153
|
+
|
154
|
+
def test_while_with_multiple_blocks
|
155
|
+
assert_source "while false\n 3\n 5\nend"
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_until
|
159
|
+
assert_source "until false\n 3\nend"
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_until_with_multiple_blocks
|
163
|
+
assert_source "while false\n 3\n 5\nend"
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_return
|
167
|
+
assert_source "return 9"
|
168
|
+
end
|
125
169
|
end
|
126
170
|
end
|
metadata
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
name: to_source
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0
|
5
|
+
version: 0.1.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Josep M. Bach
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-24 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Transform your Rubinius AST nodes back to source code. Reverse parsing!
|
15
15
|
email:
|