step_rewrite 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ vendor
2
+ .idea/*
3
+ .bundle
4
+ *.gem
5
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :gemcutter
2
+ source 'http://gems.github.com'
3
+
4
+ # Specify your gem's dependencies in step_rewrite.gemspec
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ step_rewrite (0.0.1)
5
+ ParseTree (~> 3.0.6)
6
+ ruby2ruby (~> 1.2.5)
7
+ sexp_processor (~> 3.0.5)
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ remote: http://gems.github.com/
12
+ remote: http://gems.rubyforge.org/
13
+ specs:
14
+ ParseTree (3.0.6)
15
+ RubyInline (>= 3.7.0)
16
+ sexp_processor (>= 3.0.0)
17
+ RubyInline (3.8.6)
18
+ ZenTest (~> 4.3)
19
+ ZenTest (4.4.0)
20
+ diff-lcs (1.1.2)
21
+ rspec (2.0.0)
22
+ rspec-core (= 2.0.0)
23
+ rspec-expectations (= 2.0.0)
24
+ rspec-mocks (= 2.0.0)
25
+ rspec-core (2.0.0)
26
+ rspec-expectations (2.0.0)
27
+ diff-lcs (>= 1.1.2)
28
+ rspec-mocks (2.0.0)
29
+ rspec-core (= 2.0.0)
30
+ rspec-expectations (= 2.0.0)
31
+ ruby2ruby (1.2.5)
32
+ ruby_parser (~> 2.0)
33
+ sexp_processor (~> 3.0)
34
+ ruby_parser (2.0.5)
35
+ sexp_processor (~> 3.0)
36
+ sexp_processor (3.0.5)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ ParseTree (~> 3.0.6)
43
+ bundler (>= 1.0.0.rc.6)
44
+ rspec (~> 2.0.0.beta.22)
45
+ ruby2ruby (~> 1.2.5)
46
+ sexp_processor (~> 3.0.5)
47
+ step_rewrite!
data/README.textile ADDED
@@ -0,0 +1,24 @@
1
+ h2. LICENSE:
2
+
3
+ (The MIT License)
4
+
5
+ Copyright (c) 2008 FIX
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining
8
+ a copy of this software and associated documentation files (the
9
+ 'Software'), to deal in the Software without restriction, including
10
+ without limitation the rights to use, copy, modify, merge, publish,
11
+ distribute, sublicense, and/or sell copies of the Software, and to
12
+ permit persons to whom the Software is furnished to do so, subject to
13
+ the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+ require 'rspec'
3
+ require 'rspec/core/rake_task'
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ RSpec::Core::RakeTask.new do |t|
7
+ t.rspec_opts = %w(-fs --color)
8
+ end
9
+
data/console ADDED
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/ruby
2
+
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ require 'optparse'
6
+
7
+ options = { :sandbox => false, :irb => irb }
8
+ OptionParser.new do |opt|
9
+ opt.banner = "Usage: console [environment] [options]"
10
+ opt.on('-s', '--sandbox', 'Rollback database modifications on exit.') { |v| options[:sandbox] = v }
11
+ opt.on("--irb=[#{irb}]", 'Invoke a different irb.') { |v| options[:irb] = v }
12
+ opt.on("--debugger", 'Enable ruby-debuggirng for the console.') { |v| options[:debugger] = v }
13
+ opt.parse!(ARGV)
14
+ end
15
+
16
+ libs = %( -r "#{File.dirname(__FILE__)}/lib/step_rewrite")
17
+
18
+
19
+ if options[:debugger]
20
+ begin
21
+ require 'ruby-debug'
22
+ libs << " -r ruby-debug"
23
+ puts "=> Debugger enabled"
24
+ rescue Exception
25
+ puts "You need to install ruby-debug to run the console in debugging mode. With gems, use 'gem install ruby-debug'"
26
+ exit
27
+ end
28
+ end
29
+
30
+ exec "ruby -S #{options[:irb]} #{libs} --simple-prompt"
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'parse_tree'
3
+ require 'parse_tree_extensions'
4
+ require 'Ruby2Ruby'
5
+
6
+ $:.unshift(File.dirname(__FILE__)) unless
7
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
8
+
9
+ require "step_rewrite/sexp_utilities"
10
+ require "step_rewrite/rewriter"
@@ -0,0 +1,61 @@
1
+ module StepRewrite
2
+ class Rewriter < SexpProcessor
3
+ include SexpUtilities
4
+
5
+ def initialize(cb)
6
+ super()
7
+ self.strict = false
8
+ self.require_empty = false
9
+ @cb = cb
10
+ end
11
+
12
+ def process_block(exp)
13
+ exp.shift
14
+ result = exp.reverse.inject([], &method(:accumulate))
15
+ exp.clear
16
+ result.size > 1 ? s(:block, *result) : result.first
17
+ end
18
+
19
+ def accumulate(acc_exp, cur_exp)
20
+ case identify(cur_exp)
21
+ when :special_call then
22
+ cur_exp[3].pop
23
+ inner_exp = acc_exp.size > 1 ? s(:block, *acc_exp) : acc_exp.first
24
+ [s(*[:iter, cur_exp, nil] + [inner_exp].compact)]
25
+ when :special_asgn then
26
+ param = cur_exp[1]
27
+ cur_exp[2][3].pop
28
+ inner_exp = acc_exp.size > 1 ? s(:block, *acc_exp) : acc_exp.first
29
+ [s(*[:iter, cur_exp[2], s(:lasgn, param)] + [inner_exp].compact)]
30
+ when :masgn then
31
+ param = cur_exp[1][1..-1].to_a
32
+ cur_exp[2][1][3].pop
33
+ inner_exp = acc_exp.size > 1 ? s(:block, *acc_exp) : acc_exp.first
34
+ [s(*[:iter, cur_exp[2][1], s(:masgn, s(:array, *param))] + [inner_exp].compact)]
35
+ when :attrasgn then
36
+ next_exp = cur_exp.last.pop.tap{|x| x.last.pop}
37
+ param = cur_exp
38
+ inner_exp = acc_exp.size > 1 ? s(:block, *acc_exp) : acc_exp.first
39
+ [s(*[:iter, next_exp, param] + [inner_exp].compact)]
40
+ else
41
+ acc_exp.unshift(process_inner_expr(cur_exp))
42
+ end
43
+ end
44
+
45
+ def special_call?(exp)
46
+ exp.first == :call &&
47
+ exp[3].last.class == Sexp &&
48
+ exp[3].last.first == :block_pass &&
49
+ exp[3].last.last[2] == @cb
50
+ end
51
+
52
+ def identify(exp)
53
+ return :special_call if special_call?(exp)
54
+ return :special_asgn if exp.first == :lasgn && special_call?(exp[2])
55
+ return :masgn if exp.first == :masgn && special_call?(exp[2][1])
56
+ return :attrasgn if exp.first == :attrasgn && special_call?(exp[3][1])
57
+ end
58
+ end
59
+
60
+
61
+ end
@@ -0,0 +1,20 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ module SexpUtilities
4
+
5
+ def truthy?(sexp)
6
+ sexp.respond_to?(:[]) && (sexp[0] == :true || sexp[0] == :lit || sexp[0] == :str || sexp[0] == :array)
7
+ end
8
+
9
+ def falsy?(sexp)
10
+ sexp.respond_to?(:[]) && (sexp[0] == :nil || sexp[0] == :false)
11
+ end
12
+
13
+ def list?(sexp)
14
+ sexp.respond_to?(:[]) && sexp.respond_to?(:empty?) && sexp.respond_to?(:first)
15
+ end
16
+
17
+ def process_inner_expr(inner)
18
+ inner.kind_of?(Array) ? process(inner) : inner
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module StepRewrite
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,77 @@
1
+ module RSpec
2
+ module Matchers
3
+ class Convert
4
+ def initialize(given_code)
5
+ @given_code = code(&given_code)
6
+ end
7
+
8
+ def matches?(event_proc)
9
+ @new_code = event_proc.call(@given_code.deep_clone)
10
+ @expected_code ? @new_code == @expected_code : @new_code != @given_code
11
+ end
12
+
13
+ def to(&expected_code)
14
+ @expected_code = code(&expected_code)
15
+ self
16
+ end
17
+
18
+ def description
19
+ convert_to = "INTO \n#{expected_code_in_ruby}\n" if @expected_code
20
+ "Convert \n#{given_code_in_ruby}#{convert_to}\n"
21
+ end
22
+
23
+ [:expected, :given, :new].map{|t| "#{t}_code"}.each do |type|
24
+ define_method "#{type}_in_ruby" do
25
+ code = instance_variable_get("@#{type}")
26
+ Ruby2Ruby.new.process(code.deep_clone) rescue code
27
+ end
28
+ end
29
+
30
+ def code(&block)
31
+ Sexp.from_array(block.to_sexp)[3]
32
+ end
33
+
34
+ def failure_message_for_should
35
+ <<-END
36
+ given code was converted to
37
+ #{new_code_in_ruby}
38
+ aka
39
+ expected
40
+ #{@expected_code}
41
+ got
42
+ #{@new_code}
43
+ END
44
+ rescue
45
+ <<-END
46
+ Expected
47
+ #{@expected_code}
48
+ got
49
+ #{@new_code}
50
+ END
51
+ end
52
+
53
+ def failure_message_for_should_not
54
+ <<-END
55
+ given code was converted to
56
+ #{new_code_in_ruby}
57
+ aka
58
+ expected
59
+ #{@given_code}
60
+ got
61
+ #{@new_code}
62
+ END
63
+ rescue
64
+ <<-END
65
+ Expected
66
+ #{@given_code}
67
+ got
68
+ #{@new_code}
69
+ END
70
+ end
71
+ end
72
+
73
+ def convert(&given_code)
74
+ Matchers::Convert.new(given_code)
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,191 @@
1
+ require File.expand_path('../../lib/step_rewrite', __FILE__)
2
+ require File.expand_path('../convert_matcher', __FILE__)
3
+
4
+ describe StepRewrite::Rewriter do
5
+ describe "#process" do
6
+ context 'when it sees the special callback symbol &cb' do
7
+ subject { lambda { |sexp| StepRewrite::Rewriter.new(:cb).process(sexp) } }
8
+
9
+ it do
10
+ should convert {
11
+ foo(&cb)
12
+ bar
13
+ }.to {
14
+ foo { bar }
15
+ }
16
+ end
17
+ end
18
+
19
+ context "when the special callback symbol is _ " do
20
+
21
+ subject { lambda { |sexp| StepRewrite::Rewriter.new(:_).process(sexp) } }
22
+
23
+ it do
24
+ should_not convert {
25
+ a = foo(1)
26
+ bar(a)
27
+ }
28
+ end
29
+
30
+ it do
31
+ should convert {
32
+ foo(&_)
33
+ bar
34
+ }.to {
35
+ foo { bar }
36
+ }
37
+ end
38
+
39
+ it do
40
+ should convert {
41
+ foo(&_)
42
+ bar
43
+ baz
44
+ }.to {
45
+ foo do
46
+ bar
47
+ baz
48
+ end
49
+ }
50
+ end
51
+
52
+ it do
53
+ should convert {
54
+ a = foo
55
+ bar
56
+ baz(a, &_)
57
+ }.to {
58
+ a = foo
59
+ bar
60
+ baz(a) {}
61
+ }
62
+ end
63
+
64
+ it do
65
+ should convert {
66
+ a = foo
67
+ bar(&_)
68
+ j = baz(a)
69
+ quux(j)
70
+ }.to {
71
+ a = foo
72
+ bar do
73
+ j = baz(a)
74
+ quux(j)
75
+ end
76
+ }
77
+ end
78
+
79
+ it do
80
+ should convert {
81
+ a = foo
82
+ bar(&_)
83
+ baz(a, &_)
84
+ quux
85
+ }.to {
86
+ a = foo
87
+ bar { baz(a) { quux } }
88
+ }
89
+ end
90
+
91
+ it do
92
+ should convert {
93
+ foo(a).bar(b, &_)
94
+ baz.qux(c, &_)
95
+ quux
96
+ corge(&_)
97
+ grault
98
+ }.to {
99
+ foo(a).bar(b) do
100
+ baz.qux(c) do
101
+ quux
102
+ corge { grault }
103
+ end
104
+ end
105
+ }
106
+ end
107
+
108
+ it do
109
+ should convert {
110
+ begin
111
+ foo(&_)
112
+ bar
113
+ end until baz
114
+ }.to {
115
+ begin
116
+ foo { bar }
117
+ end until baz
118
+ }
119
+ end
120
+
121
+ it do
122
+ should convert {
123
+ a = foo(&_)
124
+ bar(a)
125
+ }.to {
126
+ foo { |a| bar(a) }
127
+ }
128
+ end
129
+
130
+ it do
131
+ should convert {
132
+ a = 1
133
+ begin
134
+ a = foo(&_)
135
+ bar(a)
136
+ end until baz
137
+ }.to {
138
+ a = 1
139
+ begin
140
+ foo { |a| bar(a) }
141
+ end until baz
142
+ }
143
+ end
144
+
145
+ it do
146
+ should convert {
147
+ a, b, c = foo(&_)
148
+ bar(a, b)
149
+ }.to {
150
+ foo { |a, b, c| bar(a, b) }
151
+ }
152
+ end
153
+
154
+ it do
155
+ should convert {
156
+ *a = foo(&_)
157
+ bar(a)
158
+ }.to {
159
+ foo { |*a| bar(a) }
160
+ }
161
+ end
162
+
163
+ it do
164
+ should convert {
165
+ a, *b = foo(&_)
166
+ bar(b)
167
+ }.to {
168
+ foo { |a, *b| bar(b) }
169
+ }
170
+ end
171
+
172
+ it do
173
+ should convert {
174
+ a.b = foo(&_)
175
+ bar(a)
176
+ }.to {
177
+ foo { |a.b| bar(a) }
178
+ }
179
+ end
180
+
181
+ it do
182
+ should convert {
183
+ a.b.c = foo(&_)
184
+ bar(a.b)
185
+ }.to {
186
+ foo { |a.b.c| bar(a.b) }
187
+ }
188
+ end
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/step_rewrite/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "step_rewrite"
6
+ s.version = StepRewrite::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Vishnu S Iyengar"]
9
+ s.email = %q{pathsny@gmail.com}
10
+ s.homepage = "http://rubygems.org/gems/step_rewrite"
11
+ s.summary = %q{A Gem to Rewrite Ruby code from a special syntax into an evented IO form}
12
+ s.description = %q{A Gem to Rewrite Ruby code from a special syntax into an evented IO form}
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+ s.rubyforge_project = "step_rewrite"
16
+
17
+ s.add_development_dependency "bundler", ">= 1.0.0.rc.6"
18
+ s.add_development_dependency "rspec", "~> 2.0.0.beta.22"
19
+ s.add_dependency "sexp_processor", "~> 3.0.5"
20
+ s.add_dependency 'ruby2ruby', "~> 1.2.5"
21
+ s.add_dependency 'ParseTree', "~> 3.0.6"
22
+
23
+
24
+ s.files = `git ls-files`.split("\n")
25
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
26
+ s.require_path = 'lib'
27
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: step_rewrite
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Vishnu S Iyengar
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-16 00:00:00 +05:30
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: bundler
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 15424057
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 0
34
+ - rc
35
+ - 6
36
+ version: 1.0.0.rc.6
37
+ type: :development
38
+ version_requirements: *id001
39
+ - !ruby/object:Gem::Dependency
40
+ name: rspec
41
+ prerelease: false
42
+ requirement: &id002 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ hash: 62196431
48
+ segments:
49
+ - 2
50
+ - 0
51
+ - 0
52
+ - beta
53
+ - 22
54
+ version: 2.0.0.beta.22
55
+ type: :development
56
+ version_requirements: *id002
57
+ - !ruby/object:Gem::Dependency
58
+ name: sexp_processor
59
+ prerelease: false
60
+ requirement: &id003 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ hash: 13
66
+ segments:
67
+ - 3
68
+ - 0
69
+ - 5
70
+ version: 3.0.5
71
+ type: :runtime
72
+ version_requirements: *id003
73
+ - !ruby/object:Gem::Dependency
74
+ name: ruby2ruby
75
+ prerelease: false
76
+ requirement: &id004 !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ~>
80
+ - !ruby/object:Gem::Version
81
+ hash: 21
82
+ segments:
83
+ - 1
84
+ - 2
85
+ - 5
86
+ version: 1.2.5
87
+ type: :runtime
88
+ version_requirements: *id004
89
+ - !ruby/object:Gem::Dependency
90
+ name: ParseTree
91
+ prerelease: false
92
+ requirement: &id005 !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ~>
96
+ - !ruby/object:Gem::Version
97
+ hash: 11
98
+ segments:
99
+ - 3
100
+ - 0
101
+ - 6
102
+ version: 3.0.6
103
+ type: :runtime
104
+ version_requirements: *id005
105
+ description: A Gem to Rewrite Ruby code from a special syntax into an evented IO form
106
+ email: pathsny@gmail.com
107
+ executables: []
108
+
109
+ extensions: []
110
+
111
+ extra_rdoc_files: []
112
+
113
+ files:
114
+ - .gitignore
115
+ - .idea/encodings.xml
116
+ - .idea/misc.xml
117
+ - .idea/modules.xml
118
+ - .idea/vcs.xml
119
+ - Gemfile
120
+ - Gemfile.lock
121
+ - README.textile
122
+ - Rakefile
123
+ - console
124
+ - lib/step_rewrite.rb
125
+ - lib/step_rewrite/rewriter.rb
126
+ - lib/step_rewrite/sexp_utilities.rb
127
+ - lib/step_rewrite/version.rb
128
+ - spec/convert_matcher.rb
129
+ - spec/step_rewrite_spec.rb
130
+ - step_rewrite.gemspec
131
+ has_rdoc: true
132
+ homepage: http://rubygems.org/gems/step_rewrite
133
+ licenses: []
134
+
135
+ post_install_message:
136
+ rdoc_options: []
137
+
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ hash: 3
146
+ segments:
147
+ - 0
148
+ version: "0"
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ hash: 23
155
+ segments:
156
+ - 1
157
+ - 3
158
+ - 6
159
+ version: 1.3.6
160
+ requirements: []
161
+
162
+ rubyforge_project: step_rewrite
163
+ rubygems_version: 1.3.7
164
+ signing_key:
165
+ specification_version: 3
166
+ summary: A Gem to Rewrite Ruby code from a special syntax into an evented IO form
167
+ test_files: []
168
+