unparser 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +7 -0
  2. data/.circle.yml +4 -0
  3. data/.gitignore +2 -0
  4. data/.rspec +3 -0
  5. data/.travis.yml +16 -0
  6. data/Changelog.md +3 -0
  7. data/Gemfile +6 -0
  8. data/Gemfile.devtools +60 -0
  9. data/Guardfile +18 -0
  10. data/README.md +41 -0
  11. data/Rakefile +24 -0
  12. data/TODO +1 -0
  13. data/config/devtools.yml +2 -0
  14. data/config/flay.yml +3 -0
  15. data/config/flog.yml +2 -0
  16. data/config/mutant.yml +3 -0
  17. data/config/reek.yml +107 -0
  18. data/config/yardstick.yml +2 -0
  19. data/lib/unparser/buffer.rb +93 -0
  20. data/lib/unparser/constants.rb +87 -0
  21. data/lib/unparser/emitter/alias.rb +27 -0
  22. data/lib/unparser/emitter/argument.rb +154 -0
  23. data/lib/unparser/emitter/assignment.rb +140 -0
  24. data/lib/unparser/emitter/begin.rb +179 -0
  25. data/lib/unparser/emitter/binary.rb +52 -0
  26. data/lib/unparser/emitter/block.rb +53 -0
  27. data/lib/unparser/emitter/break.rb +28 -0
  28. data/lib/unparser/emitter/case.rb +97 -0
  29. data/lib/unparser/emitter/cbase.rb +22 -0
  30. data/lib/unparser/emitter/class.rb +72 -0
  31. data/lib/unparser/emitter/def.rb +107 -0
  32. data/lib/unparser/emitter/defined.rb +27 -0
  33. data/lib/unparser/emitter/for.rb +50 -0
  34. data/lib/unparser/emitter/hookexe.rb +42 -0
  35. data/lib/unparser/emitter/if.rb +85 -0
  36. data/lib/unparser/emitter/literal/composed.rb +64 -0
  37. data/lib/unparser/emitter/literal/dynamic.rb +54 -0
  38. data/lib/unparser/emitter/literal/dynamic_body.rb +109 -0
  39. data/lib/unparser/emitter/literal/execute_string.rb +38 -0
  40. data/lib/unparser/emitter/literal/primitive.rb +49 -0
  41. data/lib/unparser/emitter/literal/range.rb +33 -0
  42. data/lib/unparser/emitter/literal/regexp.rb +96 -0
  43. data/lib/unparser/emitter/literal/singleton.rb +24 -0
  44. data/lib/unparser/emitter/literal.rb +7 -0
  45. data/lib/unparser/emitter/module.rb +37 -0
  46. data/lib/unparser/emitter/next.rb +22 -0
  47. data/lib/unparser/emitter/not.rb +25 -0
  48. data/lib/unparser/emitter/op_assign.rb +63 -0
  49. data/lib/unparser/emitter/redo.rb +22 -0
  50. data/lib/unparser/emitter/repetition.rb +33 -0
  51. data/lib/unparser/emitter/retry.rb +22 -0
  52. data/lib/unparser/emitter/return.rb +38 -0
  53. data/lib/unparser/emitter/root.rb +7 -0
  54. data/lib/unparser/emitter/send/binary.rb +100 -0
  55. data/lib/unparser/emitter/send/index.rb +82 -0
  56. data/lib/unparser/emitter/send/unary.rb +29 -0
  57. data/lib/unparser/emitter/send.rb +273 -0
  58. data/lib/unparser/emitter/splat.rb +24 -0
  59. data/lib/unparser/emitter/super.rb +46 -0
  60. data/lib/unparser/emitter/undef.rb +25 -0
  61. data/lib/unparser/emitter/variable.rb +83 -0
  62. data/lib/unparser/emitter/yield.rb +28 -0
  63. data/lib/unparser/emitter.rb +308 -0
  64. data/lib/unparser.rb +87 -0
  65. data/spec/integration/unparser/spike_spec.rb +914 -0
  66. data/spec/spec_helper.rb +13 -0
  67. data/spec/unit/unparser/buffer/append_spec.rb +23 -0
  68. data/spec/unit/unparser/buffer/content_spec.rb +38 -0
  69. data/spec/unit/unparser/buffer/indent_spec.rb +18 -0
  70. data/spec/unit/unparser/buffer/nl_spec.rb +16 -0
  71. data/spec/unit/unparser/buffer/unindent_spec.rb +18 -0
  72. data/spec/unit/unparser/class_methods/unparse_spec.rb +16 -0
  73. data/spec/unit/unparser/emitter/class_methods/handle_spec.rb +17 -0
  74. data/spec/unit/unparser/emitter/class_methods/visit_spec.rb +37 -0
  75. data/spec/unit/unparser/emitter/source_map/class_methods/emit_spec.rb +20 -0
  76. data/unparser.gemspec +24 -0
  77. metadata +201 -0
@@ -0,0 +1,13 @@
1
+ require 'unparser'
2
+ require 'devtools/spec_helper'
3
+
4
+ module SpecHelper
5
+ def s(type, *children)
6
+ Parser::AST::Node.new(type, children)
7
+ end
8
+ end
9
+
10
+ RSpec.configure do |config|
11
+ config.include(SpecHelper)
12
+ config.extend(SpecHelper)
13
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Unparser::Buffer, '#append' do
4
+ subject { object.append(string) }
5
+
6
+ let(:object) { described_class.new }
7
+ let(:string) { 'foo' }
8
+
9
+ specify do
10
+ expect { subject }.to change { object.content }.from('').to('foo')
11
+ end
12
+
13
+ # Yeah duplicate, mutant will be improved ;)
14
+ it 'should prefix with indentation if line is empty' do
15
+ object.append('foo')
16
+ object.indent
17
+ object.append('bar')
18
+ object.append('baz')
19
+ expect(object.content).to eql("foo\n barbaz")
20
+ end
21
+
22
+ it_should_behave_like 'a command method'
23
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Unparser::Buffer, '#content' do
4
+ subject { object.content }
5
+
6
+ let(:object) { described_class.new }
7
+
8
+ shared_examples_for 'buffer content' do
9
+ it 'contains expected content' do
10
+ should eql(expected_content)
11
+ end
12
+
13
+ it { should be_frozen }
14
+
15
+ it 'returns fresh string copies' do
16
+ first = object.content
17
+ second = object.content
18
+ expect(first).to eql(second)
19
+ expect(first).to_not be(second)
20
+ end
21
+ end
22
+
23
+ context 'with empty buffer' do
24
+ let(:expected_content) { '' }
25
+
26
+ it_should_behave_like 'buffer content'
27
+ end
28
+
29
+ context 'with filled buffer' do
30
+ before do
31
+ object.append('foo')
32
+ end
33
+
34
+ let(:expected_content) { 'foo' }
35
+
36
+ it_behaves_like 'buffer content'
37
+ end
38
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Unparser::Buffer, '#indent' do
4
+ let(:object) { described_class.new }
5
+
6
+ subject { object.indent }
7
+
8
+ it 'should indent with two chars' do
9
+ object.append('foo')
10
+ object.indent
11
+ object.append('bar')
12
+ object.indent
13
+ object.append('baz')
14
+ expect(object.content).to eql("foo\n bar\n baz")
15
+ end
16
+
17
+ it_should_behave_like 'a command method'
18
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Unparser::Buffer, '#nl' do
4
+ let(:object) { described_class.new }
5
+
6
+ subject { object.nl }
7
+
8
+ it 'writes a newline' do
9
+ object.append('foo')
10
+ subject
11
+ object.append('bar')
12
+ expect(object.content).to eql("foo\nbar")
13
+ end
14
+
15
+ it_should_behave_like 'a command method'
16
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Unparser::Buffer, '#unindent' do
4
+ let(:object) { described_class.new }
5
+
6
+ subject { object.unindent }
7
+
8
+ it 'unindents two chars' do
9
+ object.append('foo')
10
+ object.indent
11
+ object.append('bar')
12
+ object.unindent
13
+ object.append('baz')
14
+ expect(object.content).to eql("foo\n bar\nbaz")
15
+ end
16
+
17
+ it_should_behave_like 'a command method'
18
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Unparser, '.unparse' do
4
+ subject { described_class.unparse(node) }
5
+
6
+ let(:node) { mock('Node') }
7
+
8
+ before do
9
+ described_class::Emitter.should_receive(:visit) do |node, buffer|
10
+ node.should be(node)
11
+ buffer.append('foo')
12
+ end
13
+ end
14
+
15
+ it { should eql('foo') }
16
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Unparser::Emitter, '.handle' do
4
+ subject { class_under_test.class_eval { handle :foo } }
5
+
6
+ let(:class_under_test) do
7
+ Class.new(described_class)
8
+ end
9
+
10
+ before do
11
+ stub_const('Unparser::Emitter::REGISTRY', {})
12
+ end
13
+
14
+ it 'should register emitter' do
15
+ expect { subject }.to change { Unparser::Emitter::REGISTRY }.from({}).to(:foo => class_under_test)
16
+ end
17
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Unparser::Emitter, '.visit' do
4
+ subject { object.visit(node, buffer) }
5
+ let(:object) { described_class }
6
+
7
+ let(:node) { mock('Node', :type => type, :source_map => nil) }
8
+ let(:buffer) { Unparser::Buffer.new }
9
+
10
+ before do
11
+ stub_const('Unparser::Emitter::REGISTRY', { :dummy => Dummy })
12
+ end
13
+
14
+ class Dummy < Unparser::Emitter
15
+ def self.emit(node, buffer, parent)
16
+ buffer.append('foo')
17
+ end
18
+ end
19
+
20
+ context 'when handler for type is registred' do
21
+ let(:type) { :dummy }
22
+ it_should_behave_like 'a command method'
23
+
24
+ it 'should call emitter' do
25
+ subject
26
+ buffer.content.should eql('foo')
27
+ end
28
+ end
29
+
30
+ context 'when handler for type is NOT registred' do
31
+ let(:type) { :unknown }
32
+
33
+ it 'should raise error' do
34
+ expect { subject }.to raise_error(ArgumentError, 'No emitter for node: :unknown')
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Unparser::Emitter::SourceMap, '.emit' do
4
+ let(:object) { described_class }
5
+ let(:subject) { object.emit(node, buffer) }
6
+ let(:buffer) { Unparser::Buffer.new }
7
+
8
+ let(:node) do
9
+ mock('Node',
10
+ :type => :foo,
11
+ :location => mock('SourceMap',
12
+ :expression => mock('SourceRange', :source => 'foo')
13
+ )
14
+ )
15
+ end
16
+
17
+ it 'should append source map expression to buffer' do
18
+ expect { subject }.to change { buffer.content }.from('').to('foo')
19
+ end
20
+ end
data/unparser.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'unparser'
5
+ s.version = '0.0.1'
6
+
7
+ s.authors = ['Markus Schirp']
8
+ s.email = 'mbj@schir-dso.com'
9
+ s.summary = 'Generate equivalent source for parser gem AST nodes'
10
+ s.description = s.summary
11
+ s.homepage = 'http://github.com/mbj/unparser'
12
+ s.license = 'MIT'
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
16
+ s.require_paths = %w(lib)
17
+ s.extra_rdoc_files = %w(README.md)
18
+
19
+ s.add_dependency('parser', '~> 2.0.0.beta5')
20
+ s.add_dependency('concord', '~> 0.1.0')
21
+ s.add_dependency('adamantium', '~> 0.0.7')
22
+ s.add_dependency('equalizer', '~> 0.0.5')
23
+ s.add_dependency('abstract_type', '~> 0.0.5')
24
+ end
metadata ADDED
@@ -0,0 +1,201 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unparser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Markus Schirp
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: parser
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.0.beta5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.0.0.beta5
27
+ - !ruby/object:Gem::Dependency
28
+ name: concord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: adamantium
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.0.7
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.7
55
+ - !ruby/object:Gem::Dependency
56
+ name: equalizer
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.0.5
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.0.5
69
+ - !ruby/object:Gem::Dependency
70
+ name: abstract_type
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 0.0.5
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 0.0.5
83
+ description: Generate equivalent source for parser gem AST nodes
84
+ email: mbj@schir-dso.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files:
88
+ - README.md
89
+ files:
90
+ - .circle.yml
91
+ - .gitignore
92
+ - .rspec
93
+ - .travis.yml
94
+ - Changelog.md
95
+ - Gemfile
96
+ - Gemfile.devtools
97
+ - Guardfile
98
+ - README.md
99
+ - Rakefile
100
+ - TODO
101
+ - config/devtools.yml
102
+ - config/flay.yml
103
+ - config/flog.yml
104
+ - config/mutant.yml
105
+ - config/reek.yml
106
+ - config/yardstick.yml
107
+ - lib/unparser.rb
108
+ - lib/unparser/buffer.rb
109
+ - lib/unparser/constants.rb
110
+ - lib/unparser/emitter.rb
111
+ - lib/unparser/emitter/alias.rb
112
+ - lib/unparser/emitter/argument.rb
113
+ - lib/unparser/emitter/assignment.rb
114
+ - lib/unparser/emitter/begin.rb
115
+ - lib/unparser/emitter/binary.rb
116
+ - lib/unparser/emitter/block.rb
117
+ - lib/unparser/emitter/break.rb
118
+ - lib/unparser/emitter/case.rb
119
+ - lib/unparser/emitter/cbase.rb
120
+ - lib/unparser/emitter/class.rb
121
+ - lib/unparser/emitter/def.rb
122
+ - lib/unparser/emitter/defined.rb
123
+ - lib/unparser/emitter/for.rb
124
+ - lib/unparser/emitter/hookexe.rb
125
+ - lib/unparser/emitter/if.rb
126
+ - lib/unparser/emitter/literal.rb
127
+ - lib/unparser/emitter/literal/composed.rb
128
+ - lib/unparser/emitter/literal/dynamic.rb
129
+ - lib/unparser/emitter/literal/dynamic_body.rb
130
+ - lib/unparser/emitter/literal/execute_string.rb
131
+ - lib/unparser/emitter/literal/primitive.rb
132
+ - lib/unparser/emitter/literal/range.rb
133
+ - lib/unparser/emitter/literal/regexp.rb
134
+ - lib/unparser/emitter/literal/singleton.rb
135
+ - lib/unparser/emitter/module.rb
136
+ - lib/unparser/emitter/next.rb
137
+ - lib/unparser/emitter/not.rb
138
+ - lib/unparser/emitter/op_assign.rb
139
+ - lib/unparser/emitter/redo.rb
140
+ - lib/unparser/emitter/repetition.rb
141
+ - lib/unparser/emitter/retry.rb
142
+ - lib/unparser/emitter/return.rb
143
+ - lib/unparser/emitter/root.rb
144
+ - lib/unparser/emitter/send.rb
145
+ - lib/unparser/emitter/send/binary.rb
146
+ - lib/unparser/emitter/send/index.rb
147
+ - lib/unparser/emitter/send/unary.rb
148
+ - lib/unparser/emitter/splat.rb
149
+ - lib/unparser/emitter/super.rb
150
+ - lib/unparser/emitter/undef.rb
151
+ - lib/unparser/emitter/variable.rb
152
+ - lib/unparser/emitter/yield.rb
153
+ - spec/integration/unparser/spike_spec.rb
154
+ - spec/spec_helper.rb
155
+ - spec/unit/unparser/buffer/append_spec.rb
156
+ - spec/unit/unparser/buffer/content_spec.rb
157
+ - spec/unit/unparser/buffer/indent_spec.rb
158
+ - spec/unit/unparser/buffer/nl_spec.rb
159
+ - spec/unit/unparser/buffer/unindent_spec.rb
160
+ - spec/unit/unparser/class_methods/unparse_spec.rb
161
+ - spec/unit/unparser/emitter/class_methods/handle_spec.rb
162
+ - spec/unit/unparser/emitter/class_methods/visit_spec.rb
163
+ - spec/unit/unparser/emitter/source_map/class_methods/emit_spec.rb
164
+ - unparser.gemspec
165
+ homepage: http://github.com/mbj/unparser
166
+ licenses:
167
+ - MIT
168
+ metadata: {}
169
+ post_install_message:
170
+ rdoc_options: []
171
+ require_paths:
172
+ - lib
173
+ required_ruby_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - '>='
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ required_rubygems_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - '>='
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ requirements: []
184
+ rubyforge_project:
185
+ rubygems_version: 2.0.2
186
+ signing_key:
187
+ specification_version: 4
188
+ summary: Generate equivalent source for parser gem AST nodes
189
+ test_files:
190
+ - spec/integration/unparser/spike_spec.rb
191
+ - spec/spec_helper.rb
192
+ - spec/unit/unparser/buffer/append_spec.rb
193
+ - spec/unit/unparser/buffer/content_spec.rb
194
+ - spec/unit/unparser/buffer/indent_spec.rb
195
+ - spec/unit/unparser/buffer/nl_spec.rb
196
+ - spec/unit/unparser/buffer/unindent_spec.rb
197
+ - spec/unit/unparser/class_methods/unparse_spec.rb
198
+ - spec/unit/unparser/emitter/class_methods/handle_spec.rb
199
+ - spec/unit/unparser/emitter/class_methods/visit_spec.rb
200
+ - spec/unit/unparser/emitter/source_map/class_methods/emit_spec.rb
201
+ has_rdoc: