thor 0.9.2 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,196 +0,0 @@
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
-