thor 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,196 @@
1
+
2
+ $TESTING ||= false
3
+
4
+ module UnifiedRuby
5
+ def rewrite_argscat(exp)
6
+ raise "unknown type #{exp.inspect}" unless exp[1][0] == :array
7
+ exp[1][0] = :arglist
8
+ exp
9
+ end
10
+
11
+ def rewrite_bmethod(exp)
12
+ exp[0] = :scope
13
+
14
+ args =
15
+ if exp.masgn and exp.masgn.dasgn_curr then
16
+ arg = exp.masgn(true).dasgn_curr(true).sexp_body
17
+ raise "nope: #{arg.size}" unless arg.size == 1
18
+ s(:args, :"*#{arg.last}")
19
+ else
20
+ args = exp.dasgn_curr(true)
21
+ if args then
22
+ s(:args, *args.sexp_body)
23
+ else
24
+ exp.delete_at 1 # nil
25
+ s(:args)
26
+ end
27
+ end
28
+
29
+ exp = s(:scope, s(:block, *exp.sexp_body)) unless exp.block
30
+ exp.block.insert 1, args
31
+ exp.find_and_replace_all(:dvar, :lvar)
32
+
33
+ exp
34
+ end
35
+
36
+ def rewrite_call(exp)
37
+ args = exp.last
38
+ case args
39
+ when nil
40
+ exp.pop
41
+ when Array
42
+ case args.first
43
+ when :array, :arglist then
44
+ args[0] = :arglist
45
+ when :argscat, :splat then
46
+ # do nothing
47
+ else
48
+ raise "unknown type in call #{args.first.inspect}"
49
+ end
50
+ return exp
51
+ end
52
+
53
+ exp << s(:arglist)
54
+
55
+ exp
56
+ end
57
+
58
+ ##
59
+ # :defn is one of the most complex of all the ASTs in ruby. We do
60
+ # one of 3 different translations:
61
+ #
62
+ # 1) From:
63
+ #
64
+ # s(:defn, :name, s(:scope, s(:block, s(:args, ...), ...)))
65
+ # s(:defn, :name, s(:bmethod, s(:masgn, s(:dasgn_curr, :args)), s(:block, ...)))
66
+ # s(:defn, :name, s(:fbody, s(:bmethod, s(:masgn, s(:dasgn_curr, :splat)), s(:block, ...))))
67
+ #
68
+ # to:
69
+ #
70
+ # s(:defn, :name, s(:args, ...), s(:scope, s:(block, ...)))
71
+ #
72
+ # 2) From:
73
+ #
74
+ # s(:defn, :writer=, s(:attrset, :@name))
75
+ #
76
+ # to:
77
+ #
78
+ # s(:defn, :writer=, s(:args), s(:attrset, :@name))
79
+ #
80
+ # 3) From:
81
+ #
82
+ # s(:defn, :reader, s(:ivar, :@name))
83
+ #
84
+ # to:
85
+ #
86
+ # s(:defn, :reader, s(:args), s(:ivar, :@name))
87
+ #
88
+ #
89
+
90
+ def rewrite_defn(exp)
91
+ weirdo = exp.ivar || exp.attrset
92
+
93
+ # move args up
94
+ args = exp.scope.block.args(true) unless weirdo
95
+ exp.insert 2, args if args
96
+
97
+ # move block_arg up and in
98
+ block_arg = exp.scope.block.block_arg(true) rescue nil
99
+ exp.args << block_arg if block_arg
100
+
101
+ # patch up attr_accessor methods
102
+ exp.insert 2, s(:args) if weirdo
103
+
104
+ exp
105
+ end
106
+
107
+ def rewrite_defs(exp)
108
+ receiver = exp.delete_at 1
109
+
110
+ # TODO: I think this would be better as rewrite_scope, but that breaks others
111
+ exp = s(exp.shift, exp.shift,
112
+ s(:scope,
113
+ s(:block, exp.scope.args))) if exp.scope.args
114
+
115
+ result = rewrite_defn(exp)
116
+ result.insert 1, receiver
117
+
118
+ result
119
+ end
120
+
121
+ def rewrite_dmethod(exp)
122
+ exp.shift # type
123
+ exp.shift # dmethod name
124
+ exp.shift # scope / block / body
125
+ end
126
+
127
+ def rewrite_fbody(exp)
128
+ return *exp.sexp_body
129
+ end
130
+
131
+ def rewrite_fcall(exp)
132
+ exp[0] = :call
133
+ exp.insert 1, nil
134
+ exp.push nil if exp.size <= 3
135
+
136
+ rewrite_call(exp)
137
+ end
138
+
139
+ def rewrite_resbody(exp) # TODO: clean up and move to unified
140
+ result = s()
141
+
142
+ code = result
143
+ while exp and exp.first == :resbody do
144
+ code << exp.shift
145
+ list = exp.shift || s(:array)
146
+ body = exp.empty? ? nil : exp.shift
147
+ exp = exp.empty? ? nil : exp.shift
148
+
149
+ # code may be nil, :lasgn, or :block
150
+ case body.first
151
+ when nil then
152
+ # do nothing
153
+ when :lasgn then
154
+ # TODO: check that it is assigning $!
155
+ list << body
156
+ body = nil
157
+ when :block then
158
+ # TODO: check that it is assigning $!
159
+ list << body.delete_at(1) if body[1].first == :lasgn
160
+ else
161
+ # do nothing (expression form)
162
+ end if body
163
+
164
+ code << list << body
165
+ if exp then
166
+ code = s()
167
+ result << code
168
+ end
169
+ end
170
+
171
+ if $DEBUG or $TESTING then
172
+ structure = result.structure
173
+ raise "result structure wrong: #{structure[0..1].inspect}" unless
174
+ structure.flatten[0] == :resbody
175
+ raise "result structure wrong: #{structure[0..1].inspect}" unless
176
+ s(:array, :splat, :argscat).include? structure.flatten[1]
177
+ raise "result body wrong: #{structure[2].inspect}" unless
178
+ structure[2].nil? or not structure[2].empty?
179
+ end
180
+
181
+ result
182
+ end
183
+
184
+ def rewrite_vcall(exp)
185
+ exp.push nil
186
+ rewrite_fcall(exp)
187
+ end
188
+ end
189
+
190
+ ##
191
+ # Quick and easy SexpProcessor that unified the sexp structure.
192
+
193
+ class Unifier < SexpProcessor
194
+ include UnifiedRuby
195
+ end
196
+
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.2
5
+ platform: ruby
6
+ authors:
7
+ - Yehuda Katz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-13 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A gem that maps options to a class
17
+ email: wycats@gmail.com
18
+ executables:
19
+ - thor
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.markdown
24
+ - LICENSE
25
+ files:
26
+ - LICENSE
27
+ - README.markdown
28
+ - Rakefile
29
+ - bin/thor
30
+ - lib/getopt.rb
31
+ - lib/thor
32
+ - lib/thor/tasks.rb
33
+ - lib/thor.rb
34
+ - lib/vendor
35
+ - lib/vendor/ruby2ruby.rb
36
+ - lib/vendor/sexp.rb
37
+ - lib/vendor/sexp_processor.rb
38
+ - lib/vendor/unified_ruby.rb
39
+ has_rdoc: true
40
+ homepage: http://yehudakatz.com
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project: thor
61
+ rubygems_version: 1.1.1
62
+ signing_key:
63
+ specification_version: 2
64
+ summary: A gem that maps options to a class
65
+ test_files: []
66
+