unparser 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -76,7 +76,9 @@ module Unparser
76
76
  K_THEN = 'then'
77
77
 
78
78
  TERMINATED = [
79
- :int, :float, :self, :kwbegin, :const, :regexp
79
+ :int, :float, :self, :kwbegin, :const, :regexp, :args, :lvar,
80
+ :ivar, :gvar, :cvar, :if, :case, :module, :class, :sclass, :super,
81
+ :yield, :zsuper, :break, :next, :defined?, :str, :block
80
82
  ].to_set
81
83
 
82
84
  KEYWORDS = constants.each_with_object([]) do |name, keywords|
@@ -134,9 +134,7 @@ module Unparser
134
134
  # @api private
135
135
  #
136
136
  def dispatch
137
- if parent_type == :mlhs
138
- parentheses { delimited(children) }
139
- else
137
+ maybe_parentheses(parent_type == :mlhs) do
140
138
  delimited(children)
141
139
  end
142
140
  end
@@ -10,7 +10,7 @@ module Unparser
10
10
  :and => T_AND
11
11
  }.freeze
12
12
 
13
- handle *MAP.keys
13
+ handle(*MAP.keys)
14
14
 
15
15
  # Test if expression is terminated
16
16
  #
@@ -33,9 +33,7 @@ module Unparser
33
33
  def emit_block_arguments
34
34
  return if arguments.children.empty?
35
35
  ws
36
- parentheses(T_PIPE, T_PIPE) do
37
- visit(arguments)
38
- end
36
+ visit_parentheses(arguments, T_PIPE, T_PIPE)
39
37
  end
40
38
 
41
39
  end # Block
@@ -16,10 +16,10 @@ module Unparser
16
16
  # @api private
17
17
  #
18
18
  def dispatch
19
- write(K_BREAK)
20
- return unless arguments
21
- parentheses do
22
- visit(arguments)
19
+ maybe_parentheses(parent_type == :or || parent_type == :and) do
20
+ write(K_BREAK)
21
+ return unless arguments
22
+ visit_parentheses(arguments)
23
23
  end
24
24
  end
25
25
 
@@ -45,9 +45,7 @@ module Unparser
45
45
  #
46
46
  def emit_arguments
47
47
  return if arguments.children.empty?
48
- parentheses do
49
- visit(arguments)
50
- end
48
+ visit_parentheses(arguments)
51
49
  end
52
50
 
53
51
  # Instance def emitter
@@ -17,9 +17,7 @@ module Unparser
17
17
  #
18
18
  def dispatch
19
19
  write(K_DEFINED)
20
- parentheses do
21
- visit(subject)
22
- end
20
+ visit_parentheses(subject)
23
21
  end
24
22
 
25
23
  end # Defined
@@ -34,7 +34,7 @@ module Unparser
34
34
  # @api private
35
35
  #
36
36
  def unless?
37
- !if_branch
37
+ !if_branch && else_branch
38
38
  end
39
39
 
40
40
  # Return keyword
@@ -64,8 +64,13 @@ module Unparser
64
64
  # @api private
65
65
  #
66
66
  def emit_if_branch
67
- return unless if_branch
68
- visit_indented(if_branch)
67
+ if if_branch
68
+ visit_indented(if_branch)
69
+ end
70
+
71
+ if !if_branch && !else_branch
72
+ nl
73
+ end
69
74
  end
70
75
 
71
76
  # Emit else branch
@@ -15,9 +15,7 @@ module Unparser
15
15
  #
16
16
  def dispatch
17
17
  util = self.class
18
- parentheses(util::OPEN, util::CLOSE) do
19
- visit(dynamic_body)
20
- end
18
+ visit_parentheses(dynamic_body, util::OPEN, util::CLOSE)
21
19
  end
22
20
 
23
21
  # Return dynamic body
@@ -93,7 +93,7 @@ module Unparser
93
93
  # @api private
94
94
  #
95
95
  def emit_interpolated_segment(node)
96
- parentheses(OPEN, CLOSE) { visit(node) }
96
+ visit_parentheses(node, OPEN, CLOSE)
97
97
  end
98
98
 
99
99
  # Dynamic string body
@@ -17,9 +17,7 @@ module Unparser
17
17
  # @api private
18
18
  #
19
19
  def dispatch
20
- parentheses(OPEN, CLOSE) do
21
- visit(dynamic_body)
22
- end
20
+ visit_parentheses(dynamic_body, OPEN, CLOSE)
23
21
  end
24
22
 
25
23
  # Return dynamic body
@@ -18,11 +18,9 @@ module Unparser
18
18
  #
19
19
  def dispatch
20
20
  parentheses(DELIMITER, DELIMITER) do
21
- # stupid for now
22
21
  body.each do |child|
23
22
  write_body(child)
24
23
  end
25
- #visit(dynamic_body)
26
24
  end
27
25
  visit(children.last)
28
26
  end
@@ -14,9 +14,11 @@ module Unparser
14
14
  # @api private
15
15
  #
16
16
  def dispatch
17
- write(K_NEXT)
18
- return if children.empty?
19
- parentheses { visit(children.first) }
17
+ maybe_parentheses(parent_type == :or || parent_type == :and) do
18
+ write(K_NEXT)
19
+ return if children.empty?
20
+ visit_parentheses(children.first)
21
+ end
20
22
  end
21
23
 
22
24
  end # Next
@@ -17,20 +17,8 @@ module Unparser
17
17
  #
18
18
  def dispatch
19
19
  write(K_RETURN)
20
- emit_argument
21
- end
22
-
23
- # Emit argument
24
- #
25
- # @return [undefined]
26
- #
27
- # @api private
28
- #
29
- def emit_argument
30
20
  return unless argument
31
- parentheses do
32
- visit(argument)
33
- end
21
+ visit_parentheses(argument)
34
22
  end
35
23
 
36
24
  end # Return
@@ -202,13 +202,42 @@ module Unparser
202
202
  #
203
203
  def visit_terminated(node)
204
204
  emitter = emitter(node)
205
- unless emitter.terminated?
206
- parentheses { emitter.write_to_buffer }
207
- return
205
+ maybe_parentheses(!emitter.terminated?) do
206
+ emitter.write_to_buffer
208
207
  end
209
208
  emitter.write_to_buffer
210
209
  end
211
210
 
211
+ # Visit within parentheses
212
+ #
213
+ # @param [Parser::AST::Node] node
214
+ #
215
+ # @return [undefined]
216
+ #
217
+ # @api private
218
+ #
219
+ def visit_parentheses(node, *arguments)
220
+ parentheses(*arguments) do
221
+ visit(node)
222
+ end
223
+ end
224
+
225
+ # Call block in optional parentheses
226
+ #
227
+ # @param [true, false] flag
228
+ #
229
+ # @return [undefined]
230
+ #
231
+ # @api private
232
+ #
233
+ def maybe_parentheses(flag)
234
+ if flag
235
+ parentheses { yield }
236
+ else
237
+ yield
238
+ end
239
+ end
240
+
212
241
  # Return emitter for node
213
242
  #
214
243
  # @param [Parser::AST::Node] node
@@ -143,6 +143,7 @@ describe Unparser do
143
143
  assert_generates '1..2', %q(1..2)
144
144
  assert_source '(0.0 / 0.0)..1'
145
145
  assert_source '1..(0.0 / 0.0)'
146
+ assert_source '(0.0 / 0.0)..100'
146
147
  end
147
148
 
148
149
  context 'erange' do
@@ -317,9 +318,67 @@ describe Unparser do
317
318
  end
318
319
  RUBY
319
320
 
320
- # Special cases
321
+ assert_source <<-RUBY
322
+ foo.bar do
323
+ end.baz
324
+ RUBY
325
+
321
326
  assert_source '(1..2).max'
327
+ assert_source '1..2.max'
322
328
  assert_source '(a = b).bar'
329
+ assert_source '@ivar.bar'
330
+ assert_source '//.bar'
331
+ assert_source '$var.bar'
332
+ assert_source '"".bar'
333
+ assert_source 'defined?(@foo).bar'
334
+
335
+ assert_source <<-RUBY
336
+ begin
337
+ rescue
338
+ end.bar
339
+ RUBY
340
+
341
+ assert_source <<-RUBY
342
+ case foo
343
+ when bar
344
+ end.baz
345
+ RUBY
346
+
347
+ assert_source <<-RUBY
348
+ class << self
349
+ end.bar
350
+ RUBY
351
+
352
+ assert_source 'break.foo'
353
+ assert_source 'next.foo'
354
+ assert_source 'super(a).foo'
355
+ assert_source 'super.foo'
356
+ assert_source 'yield(a).foo'
357
+ assert_source 'yield.foo'
358
+ assert_source 'array[i].foo'
359
+ assert_source '(array[i] = 1).foo'
360
+ assert_source 'array[1..2].foo'
361
+ assert_source '(a.attribute ||= foo).bar'
362
+
363
+ assert_source <<-RUBY
364
+ class Foo
365
+ end.bar
366
+ RUBY
367
+
368
+ assert_source <<-RUBY
369
+ module Foo
370
+ end.bar
371
+ RUBY
372
+
373
+ assert_source <<-RUBY
374
+ if foo
375
+ end.baz
376
+ RUBY
377
+
378
+ assert_source <<-RUBY
379
+ local = 1
380
+ local.bar
381
+ RUBY
323
382
 
324
383
  assert_source 'foo.bar(*args)'
325
384
  assert_source 'foo.bar(*arga, foo, *argb)'
@@ -604,6 +663,11 @@ describe Unparser do
604
663
  9
605
664
  end
606
665
  RUBY
666
+
667
+ assert_source <<-RUBY
668
+ if foo
669
+ end
670
+ RUBY
607
671
  end
608
672
 
609
673
  context 'def' do
@@ -900,6 +964,8 @@ describe Unparser do
900
964
  assert_source "a #{operator} b"
901
965
  assert_source "(a #{operator} b).foo"
902
966
  end
967
+
968
+ assert_source 'left / right'
903
969
  end
904
970
 
905
971
  context 'nested binary operators' do
@@ -917,6 +983,7 @@ describe Unparser do
917
983
 
918
984
  { :or => :'||', :and => :'&&' }.each do |word, symbol|
919
985
  assert_generates "a #{word} break foo", "a #{symbol} (break(foo))"
986
+ assert_generates "a #{word} next foo", "a #{symbol} (next(foo))"
920
987
  end
921
988
 
922
989
  context 'expansion of shortcuts' do
data/unparser.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'unparser'
5
- s.version = '0.1.0'
5
+ s.version = '0.1.1'
6
6
 
7
7
  s.authors = ['Markus Schirp']
8
8
  s.email = 'mbj@schir-dso.com'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unparser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-05 00:00:00.000000000 Z
12
+ date: 2013-09-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: parser