to_source 0.2.11 → 0.2.12

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.
data/Changelog.md CHANGED
@@ -1,8 +1,14 @@
1
1
  # v0.2.11 2013-01-9
2
2
 
3
+ * [fixed] Emit edge cases with dynamic literals correctly
4
+
5
+ [Compare v0.2.11..v0.2.12](https://github.com/mbj/to_source/compare/v0.2.11...v0.2.12)
6
+
7
+ # v0.2.11 2013-01-9
8
+
3
9
  * [fixed] Allow all nodes to be entrypoints
4
10
 
5
- [Compare v0.2.10..v0.2.11](https://github.com/mbj/to_source/compare/v0.2.9...v0.2.11)
11
+ [Compare v0.2.10..v0.2.11](https://github.com/mbj/to_source/compare/v0.2.10...v0.2.11)
6
12
 
7
13
  # v0.2.10 2013-01-7
8
14
 
data/lib/to_source.rb CHANGED
@@ -7,6 +7,7 @@ require 'to_source/command'
7
7
  require 'to_source/state'
8
8
  require 'to_source/emitter'
9
9
  require 'to_source/emitter/literal'
10
+ require 'to_source/emitter/literal/dynamic'
10
11
  require 'to_source/emitter/access'
11
12
  require 'to_source/emitter/formal_arguments'
12
13
  require 'to_source/emitter/actual_arguments'
@@ -130,99 +130,6 @@ module ToSource
130
130
  end
131
131
  end
132
132
 
133
- class Dynamic < self
134
-
135
- def dispatch
136
- emit(node.string.inspect[0..-2])
137
- array.each_with_index do |member, index|
138
- emit_member(member, index)
139
- end
140
- end
141
-
142
- def array
143
- node.array
144
- end
145
-
146
- def max
147
- array.length - 1
148
- end
149
- memoize :max
150
-
151
- def emit_primitive_member(member, index)
152
- last = index < max ? -2 : -1
153
- range = 1..last
154
- emit(member.string.inspect[range])
155
- end
156
-
157
- def emit_member(member, index)
158
- case member
159
- when Rubinius::AST::StringLiteral
160
- emit_primitive_member(member, index)
161
- else
162
- visit(member)
163
- end
164
- end
165
-
166
- class Symbol < self
167
-
168
- handle(Rubinius::AST::DynamicSymbol)
169
-
170
- def dispatch
171
- emit(':')
172
- super
173
- end
174
-
175
- end
176
-
177
- class String < self
178
-
179
- handle(Rubinius::AST::DynamicString)
180
-
181
- end
182
-
183
- class Execute < self
184
-
185
- handle(Rubinius::AST::DynamicExecuteString)
186
-
187
- def dispatch
188
- emit('`')
189
- emit(node.string.inspect[1..-2])
190
- array.each_with_index do |member, index|
191
- emit_member(member, index)
192
- end
193
- emit('`')
194
- end
195
-
196
- def emit_primitive_member(member, index)
197
- last = index < max ? -3 : -2
198
- range = 1..last
199
- emit(member.string.inspect[range])
200
- end
201
-
202
- end
203
-
204
- class Regex < self
205
-
206
- handle(Rubinius::AST::DynamicRegex)
207
-
208
- def dispatch
209
- emit('/')
210
- emit(Regexp.new(node.string).inspect[1..-2])
211
- array.each_with_index do |member, index|
212
- emit_member(member, index)
213
- end
214
- emit('/')
215
- end
216
-
217
- def emit_primitive_member(member, index)
218
- last = index < max ? -3 : -2
219
- range = 1..last
220
- emit(Regexp.new(member.string).inspect[range])
221
- end
222
-
223
- end
224
- end
225
-
226
133
 
227
134
  class PassThrough < self
228
135
 
@@ -0,0 +1,103 @@
1
+ module ToSource
2
+ class Emitter
3
+ class Literal
4
+ class Dynamic < self
5
+
6
+ private
7
+
8
+ def dispatch
9
+ emit_open
10
+ emit_first
11
+ emit_segments
12
+ emit_close
13
+ end
14
+
15
+ def emit_open
16
+ emit(self.class::OPEN)
17
+ end
18
+
19
+ def emit_close
20
+ emit(self.class::CLOSE)
21
+ end
22
+
23
+ def first_value
24
+ node.string
25
+ end
26
+
27
+ def emit_literal(literal)
28
+ emit(literal.inspect[1..-2])
29
+ end
30
+
31
+ def emit_literal_node(node)
32
+ emit_literal(node.string)
33
+ end
34
+
35
+ def emit_first
36
+ emit_literal(first_value)
37
+ end
38
+
39
+ def emit_segments
40
+ array.each_with_index do |segment, index|
41
+ emit_segment(segment, index)
42
+ end
43
+ end
44
+
45
+ def emit_segment(segment, index)
46
+ case segment
47
+ when Rubinius::AST::StringLiteral
48
+ emit_literal_node(segment)
49
+ else
50
+ visit(segment)
51
+ end
52
+ end
53
+
54
+ def array
55
+ node.array
56
+ end
57
+
58
+ def max
59
+ array.length - 1
60
+ end
61
+ memoize :max
62
+
63
+ class Symbol < self
64
+
65
+ handle(Rubinius::AST::DynamicSymbol)
66
+
67
+ OPEN = ':"'
68
+ CLOSE = '"'
69
+
70
+ end
71
+
72
+ class String < self
73
+
74
+ handle(Rubinius::AST::DynamicString)
75
+
76
+ OPEN = CLOSE = '"'.freeze
77
+
78
+ end
79
+
80
+ class Execute < self
81
+
82
+ handle(Rubinius::AST::DynamicExecuteString)
83
+
84
+ OPEN = CLOSE = '`'.freeze
85
+
86
+ end
87
+
88
+ class Regex < self
89
+
90
+ OPEN = CLOSE = '/'.freeze
91
+
92
+ handle(Rubinius::AST::DynamicRegex)
93
+
94
+ def emit_literal(literal)
95
+ emit(Regexp.new(literal).source)[1..-2]
96
+ end
97
+
98
+ end
99
+
100
+ end
101
+ end
102
+ end
103
+ end
@@ -357,6 +357,14 @@ describe ToSource,'.to_source' do
357
357
  context 'with escapes' do
358
358
  assert_source '"fo\no#{bar}b\naz"'
359
359
  end
360
+
361
+ context 'with dynamic segment in the front' do
362
+ assert_source '"#{bar}foo"'
363
+ end
364
+
365
+ context 'with dynamic segment in the end' do
366
+ assert_source '"foo#{bar}"'
367
+ end
360
368
  end
361
369
 
362
370
  context 'dynamic symbol' do
@@ -367,6 +375,14 @@ describe ToSource,'.to_source' do
367
375
  context 'with escapes' do
368
376
  assert_source ':"fo\no#{bar}b\naz"'
369
377
  end
378
+
379
+ context 'with dynamic segment in the front' do
380
+ assert_source ':"#{bar}foo"'
381
+ end
382
+
383
+ context 'with dynamic segment in the end' do
384
+ assert_source ':"foo#{bar}"'
385
+ end
370
386
  end
371
387
 
372
388
  context 'dynamic execute' do
@@ -377,6 +393,14 @@ describe ToSource,'.to_source' do
377
393
  context 'with escapes' do
378
394
  assert_source '`fo\no#{bar}b\naz`'
379
395
  end
396
+
397
+ context 'with dynamic segment in the front' do
398
+ assert_source '`#{bar}foo`'
399
+ end
400
+
401
+ context 'with dynamic segment in the end' do
402
+ assert_source '`foo#{bar}`'
403
+ end
380
404
  end
381
405
 
382
406
  context 'dynamic regexp' do
@@ -387,6 +411,14 @@ describe ToSource,'.to_source' do
387
411
  context 'with escapes' do
388
412
  assert_source '/fo\no#{bar}b\naz/'
389
413
  end
414
+
415
+ context 'with dynamic segment in the front' do
416
+ assert_source '/#{bar}foo/'
417
+ end
418
+
419
+ context 'with dynamic segment in the end' do
420
+ assert_source '/foo#{bar}/'
421
+ end
390
422
  end
391
423
  end
392
424
 
data/to_source.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  Gem::Specification.new do |s|
3
3
  s.name = 'to_source'
4
- s.version = '0.2.11'
4
+ s.version = '0.2.12'
5
5
  s.authors = ['Markus Schirp']
6
6
  s.email = ['mbj@seonic.net']
7
7
  s.homepage = 'http://github.com/mbj/to_source'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: to_source
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.11
4
+ version: 0.2.12
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -134,6 +134,7 @@ files:
134
134
  - lib/to_source/emitter/iter.rb
135
135
  - lib/to_source/emitter/keyword_value.rb
136
136
  - lib/to_source/emitter/literal.rb
137
+ - lib/to_source/emitter/literal/dynamic.rb
137
138
  - lib/to_source/emitter/match3.rb
138
139
  - lib/to_source/emitter/module.rb
139
140
  - lib/to_source/emitter/multiple_assignment.rb