ruby_parser 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of ruby_parser might be problematic. Click here for more details.
- data/.autotest +26 -3
- data/History.txt +108 -0
- data/Manifest.txt +3 -0
- data/README.txt +1 -1
- data/Rakefile +126 -28
- data/bin/ruby_parse +89 -0
- data/lib/ruby_lexer.rb +1117 -2536
- data/lib/ruby_parser.rb +5407 -5849
- data/lib/ruby_parser.y +1763 -1621
- data/lib/ruby_parser_extras.rb +1051 -0
- data/test/test_ruby_lexer.rb +1607 -267
- data/test/test_ruby_parser.rb +317 -175
- data/test/test_ruby_parser_extras.rb +177 -0
- metadata +27 -10
data/test/test_ruby_parser.rb
CHANGED
@@ -8,59 +8,29 @@ $: << File.expand_path('~/Work/p4/zss/src/ParseTree/dev/test')
|
|
8
8
|
|
9
9
|
require 'pt_testcase'
|
10
10
|
|
11
|
-
class
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
next if Array === data['Ruby'] # runtime only crap
|
17
|
-
"def test_#{node}
|
18
|
-
rb = #{data['Ruby'].inspect}
|
19
|
-
pt = #{data['ParseTree'].inspect}
|
20
|
-
|
21
|
-
assert_not_nil rb, \"Ruby for #{node} undefined\"
|
22
|
-
assert_not_nil pt, \"ParseTree for #{node} undefined\"
|
23
|
-
|
24
|
-
assert_equal Sexp.from_array(pt), @processor.parse(rb)
|
25
|
-
end"
|
26
|
-
}.compact.join("\n")
|
27
|
-
|
28
|
-
# Scour the world and compare against ParseTree
|
29
|
-
if ENV['ZOMGPONIES'] or File.exist? 'zomgponies' then
|
30
|
-
require 'parse_tree'
|
31
|
-
|
32
|
-
base = "/usr/lib/ruby"
|
33
|
-
base = "unit"
|
34
|
-
|
35
|
-
files = Dir[File.join(base, "**/*.rb")]
|
36
|
-
|
37
|
-
# these files/patterns cause parse_tree_show to bus error (or just suck):
|
38
|
-
files.reject! { |f| f =~ /environments.environment|rss.maker.base|rails_generator|ferret.browser|rubinius.spec.core.module.(constants|name|remove_const)_spec|tkextlib.tcllib.tablelist/ }
|
39
|
-
|
40
|
-
# these are rejected for dasgn_curr ordering failures... I'll fix them later.
|
41
|
-
# (or mri parse errors--I should have separated them out)
|
42
|
-
files.reject! { |f| f =~ /lib.flog|lib.autotest.notify|lib.analyzer.tools.rails.stat|flog.lib.flog|rakelib.struct.generator|rubinius.kernel.core.array|lib.rbosa.rb|src.rbosa.rb|spec.spec.mocks.mock.spec.rb|dsl.shared.behaviour.spec.rb|spec.spec.dsl.behaviour.spec|lib.hpricot.parse.rb|resolve.rb|parsers.parse.f95|rubinius.shotgun.lib.primitives.ltm|rubinius.lib.bin.compile|rubinius.kernel.core.object|rubinius.kernel.core.file|rubinius.compiler2.garnet.bindingagent|ruby_to_c_test_r2ctestcase|lib.more.like.this|resolv\.rb|test.r2ctestcase/ }
|
43
|
-
|
44
|
-
warn "Generating #{files.size} tests from #{base}"
|
45
|
-
|
46
|
-
eval files.map { |file|
|
47
|
-
name = file[base.size..-1].gsub(/\W+/, '_')
|
11
|
+
class RubyParser
|
12
|
+
def process input
|
13
|
+
parse input
|
14
|
+
end
|
15
|
+
end
|
48
16
|
|
49
|
-
|
17
|
+
class RubyParserTestCase < ParseTreeTestCase
|
18
|
+
def self.previous key
|
19
|
+
"Ruby"
|
20
|
+
end
|
50
21
|
|
51
|
-
|
52
|
-
|
53
|
-
|
22
|
+
def self.generate_test klass, node, data, input_name, output_name
|
23
|
+
return if node.to_s =~ /bmethod|dmethod/
|
24
|
+
return if Array === data['Ruby']
|
54
25
|
|
55
|
-
|
56
|
-
assert_not_nil pt, \"ParseTree for #{name} undefined\"
|
26
|
+
output_name = "ParseTree"
|
57
27
|
|
58
|
-
|
59
|
-
assert_equal Sexp.from_array(pt).first, rp, \"RP different from PT\"
|
60
|
-
File.unlink #{file.inspect}
|
61
|
-
end"
|
62
|
-
}.compact.join("\n")
|
28
|
+
super
|
63
29
|
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class TestRubyParser < RubyParserTestCase
|
33
|
+
alias :refute_nil :assert_not_nil unless defined? Mini
|
64
34
|
|
65
35
|
def setup
|
66
36
|
super
|
@@ -70,29 +40,29 @@ class TestRubyParser < Test::Unit::TestCase # ParseTreeTestCase
|
|
70
40
|
@processor = RubyParser.new
|
71
41
|
end
|
72
42
|
|
73
|
-
def
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
43
|
+
def test_attrasgn_array_lhs
|
44
|
+
rb = '[1, 2, 3, 4][from .. to] = ["a", "b", "c"]'
|
45
|
+
pt = s(:attrasgn,
|
46
|
+
s(:array, s(:lit, 1), s(:lit, 2), s(:lit, 3), s(:lit, 4)),
|
47
|
+
:[]=,
|
48
|
+
s(:arglist,
|
49
|
+
s(:dot2,
|
50
|
+
s(:call, nil, :from, s(:arglist)),
|
51
|
+
s(:call, nil, :to, s(:arglist))),
|
52
|
+
s(:array, s(:str, "a"), s(:str, "b"), s(:str, "c"))))
|
53
|
+
|
54
|
+
result = @processor.parse(rb)
|
55
|
+
|
56
|
+
assert_equal pt, result
|
78
57
|
end
|
79
58
|
|
80
|
-
def
|
81
|
-
head = s(:
|
59
|
+
def test_block_append
|
60
|
+
head = s(:args)
|
82
61
|
tail = s(:zsuper)
|
83
62
|
expected = s(:block, s(:args), s(:zsuper))
|
84
63
|
assert_equal expected, @processor.block_append(head, tail)
|
85
64
|
end
|
86
65
|
|
87
|
-
def test_block_append_tail_block
|
88
|
-
head = s(:vcall, :f1)
|
89
|
-
tail = s(:block, s(:undef, s(:lit, :x)), s(:undef, s(:lit, :y)))
|
90
|
-
expected = s(:block,
|
91
|
-
s(:vcall, :f1),
|
92
|
-
s(:block, s(:undef, s(:lit, :x)), s(:undef, s(:lit, :y))))
|
93
|
-
assert_equal expected, @processor.block_append(head, tail)
|
94
|
-
end
|
95
|
-
|
96
66
|
def test_block_append_begin_begin
|
97
67
|
head = s(:begin, s(:args))
|
98
68
|
tail = s(:begin, s(:args))
|
@@ -100,6 +70,13 @@ class TestRubyParser < Test::Unit::TestCase # ParseTreeTestCase
|
|
100
70
|
assert_equal expected, @processor.block_append(head, tail)
|
101
71
|
end
|
102
72
|
|
73
|
+
def test_block_append_block
|
74
|
+
head = s(:block, s(:args))
|
75
|
+
tail = s(:zsuper)
|
76
|
+
expected = s(:block, s(:args), s(:zsuper))
|
77
|
+
assert_equal expected, @processor.block_append(head, tail)
|
78
|
+
end
|
79
|
+
|
103
80
|
def test_block_append_nil_head
|
104
81
|
head = nil
|
105
82
|
tail = s(:zsuper)
|
@@ -114,18 +91,123 @@ class TestRubyParser < Test::Unit::TestCase # ParseTreeTestCase
|
|
114
91
|
assert_equal expected, @processor.block_append(head, tail)
|
115
92
|
end
|
116
93
|
|
94
|
+
def test_block_append_tail_block
|
95
|
+
head = s(:call, nil, :f1, s(:arglist))
|
96
|
+
tail = s(:block, s(:undef, s(:lit, :x)), s(:undef, s(:lit, :y)))
|
97
|
+
expected = s(:block,
|
98
|
+
s(:call, nil, :f1, s(:arglist)),
|
99
|
+
s(:block, s(:undef, s(:lit, :x)), s(:undef, s(:lit, :y))))
|
100
|
+
assert_equal expected, @processor.block_append(head, tail)
|
101
|
+
end
|
102
|
+
|
117
103
|
def test_call_env
|
118
104
|
@processor.env[:a] = :lvar
|
119
|
-
expected = s(:call, s(:lvar, :a), :happy)
|
105
|
+
expected = s(:call, s(:lvar, :a), :happy, s(:arglist))
|
120
106
|
|
121
107
|
assert_equal expected, @processor.parse('a.happy')
|
122
108
|
end
|
123
109
|
|
110
|
+
def test_dasgn_icky2
|
111
|
+
rb = "a do\n v = nil\n begin\n yield\n rescue Exception => v\n break\n end\nend"
|
112
|
+
pt = s(:iter,
|
113
|
+
s(:call, nil, :a, s(:arglist)),
|
114
|
+
nil,
|
115
|
+
s(:block,
|
116
|
+
s(:lasgn, :v, s(:nil)),
|
117
|
+
s(:rescue,
|
118
|
+
s(:yield),
|
119
|
+
s(:resbody,
|
120
|
+
s(:array, s(:const, :Exception), s(:lasgn, :v, s(:gvar, :$!))),
|
121
|
+
s(:break)))))
|
122
|
+
|
123
|
+
assert_equal pt, @processor.parse(rb)
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_class_comments
|
127
|
+
rb = "# blah 1\n# blah 2\n\nclass X\n # blah 3\n def blah\n # blah 4\n end\nend"
|
128
|
+
pt = s(:class, :X, nil,
|
129
|
+
s(:scope,
|
130
|
+
s(:defn, :blah, s(:args), s(:scope, s(:block, s(:nil))))))
|
131
|
+
|
132
|
+
actual = @processor.parse(rb)
|
133
|
+
assert_equal pt, actual
|
134
|
+
|
135
|
+
assert_equal "# blah 1\n# blah 2\n\n", actual.comments
|
136
|
+
assert_equal "# blah 3\n", actual.scope.defn.comments
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_module_comments
|
140
|
+
rb = "# blah 1\n \n # blah 2\n\nmodule X\n # blah 3\n def blah\n # blah 4\n end\nend"
|
141
|
+
pt = s(:module, :X,
|
142
|
+
s(:scope,
|
143
|
+
s(:defn, :blah, s(:args), s(:scope, s(:block, s(:nil))))))
|
144
|
+
|
145
|
+
actual = @processor.parse(rb)
|
146
|
+
assert_equal pt, actual
|
147
|
+
assert_equal "# blah 1\n\n# blah 2\n\n", actual.comments
|
148
|
+
assert_equal "# blah 3\n", actual.scope.defn.comments
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_defn_comments
|
152
|
+
rb = "# blah 1\n# blah 2\n\ndef blah\nend"
|
153
|
+
pt = s(:defn, :blah, s(:args), s(:scope, s(:block, s(:nil))))
|
154
|
+
|
155
|
+
actual = @processor.parse(rb)
|
156
|
+
assert_equal pt, actual
|
157
|
+
assert_equal "# blah 1\n# blah 2\n\n", actual.comments
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_defs_comments
|
161
|
+
rb = "# blah 1\n# blah 2\n\ndef self.blah\nend"
|
162
|
+
pt = s(:defs, s(:self), :blah, s(:args), s(:scope, s(:block)))
|
163
|
+
|
164
|
+
actual = @processor.parse(rb)
|
165
|
+
assert_equal pt, actual
|
166
|
+
assert_equal "# blah 1\n# blah 2\n\n", actual.comments
|
167
|
+
end
|
168
|
+
|
124
169
|
def test_do_bug # TODO: rename
|
125
170
|
rb = "a 1\na.b do |c|\n # do nothing\nend"
|
126
171
|
pt = s(:block,
|
127
|
-
s(:
|
128
|
-
s(:iter,
|
172
|
+
s(:call, nil, :a, s(:arglist, s(:lit, 1))),
|
173
|
+
s(:iter,
|
174
|
+
s(:call, s(:call, nil, :a, s(:arglist)), :b, s(:arglist)),
|
175
|
+
s(:lasgn, :c)))
|
176
|
+
|
177
|
+
assert_equal pt, @processor.parse(rb)
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_dstr_evstr
|
181
|
+
rb = "\"#\{'a'}#\{b}\""
|
182
|
+
pt = s(:dstr, "a", s(:evstr, s(:call, nil, :b, s(:arglist))))
|
183
|
+
|
184
|
+
assert_equal pt, @processor.parse(rb)
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_dstr_str
|
188
|
+
rb = "\"#\{'a'} b\""
|
189
|
+
pt = s(:str, "a b")
|
190
|
+
|
191
|
+
assert_equal pt, @processor.parse(rb)
|
192
|
+
end
|
193
|
+
|
194
|
+
def test_empty
|
195
|
+
rb = ""
|
196
|
+
pt = nil
|
197
|
+
|
198
|
+
assert_equal pt, @processor.parse(rb)
|
199
|
+
end
|
200
|
+
|
201
|
+
def test_evstr_evstr
|
202
|
+
rb = "\"#\{a}#\{b}\""
|
203
|
+
pt = s(:dstr, "", s(:evstr, s(:call, nil, :a, s(:arglist))), s(:evstr, s(:call, nil, :b, s(:arglist))))
|
204
|
+
|
205
|
+
assert_equal pt, @processor.parse(rb)
|
206
|
+
end
|
207
|
+
|
208
|
+
def test_evstr_str
|
209
|
+
rb = "\"#\{a} b\""
|
210
|
+
pt = s(:dstr, "", s(:evstr, s(:call, nil, :a, s(:arglist))), s(:str, " b"))
|
129
211
|
|
130
212
|
assert_equal pt, @processor.parse(rb)
|
131
213
|
end
|
@@ -134,11 +216,91 @@ class TestRubyParser < Test::Unit::TestCase # ParseTreeTestCase
|
|
134
216
|
rb = 'a = 42'
|
135
217
|
pt = s(:lasgn, :a, s(:lit, 42))
|
136
218
|
expected_env = { :a => :lvar }
|
137
|
-
|
219
|
+
|
138
220
|
assert_equal pt, @processor.parse(rb)
|
139
221
|
assert_equal expected_env, @processor.env.all
|
140
222
|
end
|
141
223
|
|
224
|
+
def test_list_append
|
225
|
+
a = s(:lit, 1)
|
226
|
+
b = s(:lit, 2)
|
227
|
+
c = s(:lit, 3)
|
228
|
+
|
229
|
+
result = @processor.list_append(s(:array, b.dup), c.dup)
|
230
|
+
|
231
|
+
assert_equal s(:array, b, c), result
|
232
|
+
|
233
|
+
result = @processor.list_append(b.dup, c.dup)
|
234
|
+
|
235
|
+
assert_equal s(:array, b, c), result
|
236
|
+
|
237
|
+
result = @processor.list_append(result, a.dup)
|
238
|
+
|
239
|
+
assert_equal s(:array, b, c, a), result
|
240
|
+
|
241
|
+
lhs, rhs = s(:array, s(:lit, :iter)), s(:when, s(:const, :BRANCHING), nil)
|
242
|
+
expected = s(:array, s(:lit, :iter), s(:when, s(:const, :BRANCHING), nil))
|
243
|
+
|
244
|
+
assert_equal expected, @processor.list_append(lhs, rhs)
|
245
|
+
end
|
246
|
+
|
247
|
+
def test_list_prepend
|
248
|
+
a = s(:lit, 1)
|
249
|
+
b = s(:lit, 2)
|
250
|
+
c = s(:lit, 3)
|
251
|
+
|
252
|
+
result = @processor.list_prepend(b.dup, s(:array, c.dup))
|
253
|
+
|
254
|
+
assert_equal s(:array, b, c), result
|
255
|
+
|
256
|
+
result = @processor.list_prepend(b.dup, c.dup)
|
257
|
+
|
258
|
+
assert_equal s(:array, b, c), result
|
259
|
+
|
260
|
+
result = @processor.list_prepend(a.dup, result)
|
261
|
+
|
262
|
+
assert_equal s(:array, a, b, c), result
|
263
|
+
end
|
264
|
+
|
265
|
+
def test_literal_concat_dstr_dstr
|
266
|
+
lhs = s(:dstr, "Failed to download spec ",
|
267
|
+
s(:evstr, s(:call, nil, :spec_name, s(:arglist))),
|
268
|
+
s(:str, " from "),
|
269
|
+
s(:evstr, s(:call, nil, :source_uri, s(:arglist))),
|
270
|
+
s(:str, ":\n"))
|
271
|
+
rhs = s(:dstr, "\t",
|
272
|
+
s(:evstr, s(:call, s(:ivar, :@fetch_error), :message)))
|
273
|
+
expected = s(:dstr, "Failed to download spec ",
|
274
|
+
s(:evstr, s(:call, nil, :spec_name, s(:arglist))),
|
275
|
+
s(:str, " from "),
|
276
|
+
s(:evstr, s(:call, nil, :source_uri, s(:arglist))),
|
277
|
+
s(:str, ":\n"),
|
278
|
+
s(:str, "\t"),
|
279
|
+
s(:evstr, s(:call, s(:ivar, :@fetch_error), :message)))
|
280
|
+
|
281
|
+
assert_equal expected, @processor.literal_concat(lhs, rhs)
|
282
|
+
end
|
283
|
+
|
284
|
+
def test_literal_concat_dstr_evstr
|
285
|
+
lhs, rhs = s(:dstr, "a"), s(:evstr, s(:call, nil, :b, s(:arglist)))
|
286
|
+
expected = s(:dstr, "a", s(:evstr, s(:call, nil, :b, s(:arglist))))
|
287
|
+
|
288
|
+
assert_equal expected, @processor.literal_concat(lhs, rhs)
|
289
|
+
end
|
290
|
+
|
291
|
+
def test_literal_concat_evstr_evstr
|
292
|
+
lhs, rhs = s(:evstr, s(:lit, 1)), s(:evstr, s(:lit, 2))
|
293
|
+
expected = s(:dstr, "", s(:evstr, s(:lit, 1)), s(:evstr, s(:lit, 2)))
|
294
|
+
|
295
|
+
assert_equal expected, @processor.literal_concat(lhs, rhs)
|
296
|
+
end
|
297
|
+
|
298
|
+
def test_literal_concat_str_evstr
|
299
|
+
lhs, rhs = s(:str, ""), s(:evstr, s(:str, "blah"))
|
300
|
+
|
301
|
+
assert_equal s(:str, "blah"), @processor.literal_concat(lhs, rhs)
|
302
|
+
end
|
303
|
+
|
142
304
|
def test_logop_12
|
143
305
|
lhs = s(:lit, 1)
|
144
306
|
rhs = s(:lit, 2)
|
@@ -147,10 +309,18 @@ class TestRubyParser < Test::Unit::TestCase # ParseTreeTestCase
|
|
147
309
|
assert_equal exp, @processor.logop(:and, lhs, rhs)
|
148
310
|
end
|
149
311
|
|
150
|
-
def
|
151
|
-
lhs = s(:and, s(:lit, 1), s(:lit, 2))
|
152
|
-
rhs = s(:lit,
|
153
|
-
exp = s(:and,
|
312
|
+
def test_logop_1234_5
|
313
|
+
lhs = s(:and, s(:lit, 1), s(:and, s(:lit, 2), s(:and, s(:lit, 3), s(:lit, 4))))
|
314
|
+
rhs = s(:lit, 5)
|
315
|
+
exp = s(:and,
|
316
|
+
s(:lit, 1),
|
317
|
+
s(:and,
|
318
|
+
s(:lit, 2),
|
319
|
+
s(:and,
|
320
|
+
s(:lit, 3),
|
321
|
+
s(:and,
|
322
|
+
s(:lit, 4),
|
323
|
+
s(:lit, 5)))))
|
154
324
|
|
155
325
|
assert_equal exp, @processor.logop(:and, lhs, rhs)
|
156
326
|
end
|
@@ -169,28 +339,20 @@ class TestRubyParser < Test::Unit::TestCase # ParseTreeTestCase
|
|
169
339
|
assert_equal exp, @processor.logop(:and, lhs, rhs)
|
170
340
|
end
|
171
341
|
|
172
|
-
def
|
173
|
-
lhs = s(:and, s(:lit, 1), s(:
|
174
|
-
rhs = s(:lit,
|
175
|
-
exp = s(:and,
|
176
|
-
s(:lit, 1),
|
177
|
-
s(:and,
|
178
|
-
s(:lit, 2),
|
179
|
-
s(:and,
|
180
|
-
s(:lit, 3),
|
181
|
-
s(:and,
|
182
|
-
s(:lit, 4),
|
183
|
-
s(:lit, 5)))))
|
342
|
+
def test_logop_12_3
|
343
|
+
lhs = s(:and, s(:lit, 1), s(:lit, 2))
|
344
|
+
rhs = s(:lit, 3)
|
345
|
+
exp = s(:and, s(:lit, 1), s(:and, s(:lit, 2), s(:lit, 3)))
|
184
346
|
|
185
347
|
assert_equal exp, @processor.logop(:and, lhs, rhs)
|
186
348
|
end
|
187
349
|
|
188
350
|
def test_logop_nested_mix
|
189
|
-
lhs = s(:or, s(:
|
190
|
-
rhs = s(:and, s(:
|
351
|
+
lhs = s(:or, s(:call, nil, :a, s(:arglist)), s(:call, nil, :b, s(:arglist)))
|
352
|
+
rhs = s(:and, s(:call, nil, :c, s(:arglist)), s(:call, nil, :d, s(:arglist)))
|
191
353
|
exp = s(:or,
|
192
|
-
s(:or, s(:
|
193
|
-
s(:and, s(:
|
354
|
+
s(:or, s(:call, nil, :a, s(:arglist)), s(:call, nil, :b, s(:arglist))),
|
355
|
+
s(:and, s(:call, nil, :c, s(:arglist)), s(:call, nil, :d, s(:arglist))))
|
194
356
|
|
195
357
|
lhs.paren = true
|
196
358
|
rhs.paren = true
|
@@ -198,48 +360,16 @@ class TestRubyParser < Test::Unit::TestCase # ParseTreeTestCase
|
|
198
360
|
assert_equal exp, @processor.logop(:or, lhs, rhs)
|
199
361
|
end
|
200
362
|
|
201
|
-
def
|
202
|
-
|
203
|
-
|
204
|
-
assert_equal s(:str, "blah"), @processor.literal_concat(lhs, rhs)
|
205
|
-
end
|
206
|
-
|
207
|
-
def test_literal_concat_evstr_evstr
|
208
|
-
lhs, rhs = s(:evstr, s(:lit, 1)), s(:evstr, s(:lit, 2))
|
209
|
-
expected = s(:dstr, "", s(:evstr, s(:lit, 1)), s(:evstr, s(:lit, 2)))
|
210
|
-
|
211
|
-
assert_equal expected, @processor.literal_concat(lhs, rhs)
|
212
|
-
end
|
213
|
-
|
214
|
-
def test_literal_concat_dstr_evstr
|
215
|
-
lhs, rhs = s(:dstr, "a"), s(:evstr, s(:vcall, :b))
|
216
|
-
expected = s(:dstr, "a", s(:evstr, s(:vcall, :b)))
|
217
|
-
|
218
|
-
assert_equal expected, @processor.literal_concat(lhs, rhs)
|
219
|
-
end
|
220
|
-
|
221
|
-
def test_literal_concat_dstr_dstr
|
222
|
-
lhs = s(:dstr, "Failed to download spec ",
|
223
|
-
s(:evstr, s(:vcall, :spec_name)),
|
224
|
-
s(:str, " from "),
|
225
|
-
s(:evstr, s(:vcall, :source_uri)),
|
226
|
-
s(:str, ":\n"))
|
227
|
-
rhs = s(:dstr, "\t",
|
228
|
-
s(:evstr, s(:call, s(:ivar, :@fetch_error), :message)))
|
229
|
-
expected = s(:dstr, "Failed to download spec ",
|
230
|
-
s(:evstr, s(:vcall, :spec_name)),
|
231
|
-
s(:str, " from "),
|
232
|
-
s(:evstr, s(:vcall, :source_uri)),
|
233
|
-
s(:str, ":\n"),
|
234
|
-
s(:str, "\t"),
|
235
|
-
s(:evstr, s(:call, s(:ivar, :@fetch_error), :message)))
|
363
|
+
def test_str_evstr
|
364
|
+
rb = "\"a #\{b}\""
|
365
|
+
pt = s(:dstr, "a ", s(:evstr, s(:call, nil, :b, s(:arglist))))
|
236
366
|
|
237
|
-
assert_equal
|
367
|
+
assert_equal pt, @processor.parse(rb)
|
238
368
|
end
|
239
369
|
|
240
370
|
def test_str_pct_Q_nested
|
241
371
|
rb = "%Q[before [#\{nest}] after]"
|
242
|
-
pt = s(:dstr, "before [", s(:evstr, s(:
|
372
|
+
pt = s(:dstr, "before [", s(:evstr, s(:call, nil, :nest, s(:arglist))), s(:str, "] after"))
|
243
373
|
|
244
374
|
assert_equal pt, @processor.parse(rb)
|
245
375
|
end
|
@@ -258,69 +388,81 @@ class TestRubyParser < Test::Unit::TestCase # ParseTreeTestCase
|
|
258
388
|
assert_equal pt, @processor.parse(rb)
|
259
389
|
end
|
260
390
|
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
391
|
+
STARTING_LINE = {
|
392
|
+
"begin_def" => 2,
|
393
|
+
"block_stmt_after" => 2,
|
394
|
+
"block_stmt_after_mri_verbose_flag" => 2,
|
395
|
+
"block_stmt_before" => 2,
|
396
|
+
"block_stmt_before_mri_verbose_flag" => 2,
|
397
|
+
"block_stmt_both" => 2,
|
398
|
+
"block_stmt_both_mri_verbose_flag" => 2,
|
399
|
+
"case_nested_inner_no_expr" => 2,
|
400
|
+
"case_no_expr" => 2,
|
401
|
+
"case_splat" => 2,
|
402
|
+
"cvasgn" => 2,
|
403
|
+
"defn_args_none" => 2,
|
404
|
+
"defn_zarray" => 2,
|
405
|
+
"structure_unused_literal_wwtt" => 3, # yes, 3... odd test
|
406
|
+
"super_0" => 2,
|
407
|
+
"super_1" => 2,
|
408
|
+
"super_1_array" => 2,
|
409
|
+
"super_n" => 2,
|
410
|
+
"super_multi" => 2,
|
411
|
+
"undef_block_1" => 2,
|
412
|
+
"undef_block_2" => 2,
|
413
|
+
"undef_block_3" => 2,
|
414
|
+
"undef_block_wtf" => 2,
|
415
|
+
"zsuper" => 2,
|
416
|
+
}
|
417
|
+
|
418
|
+
def after_process_hook klass, node, data, input_name, output_name
|
419
|
+
expected = STARTING_LINE[node] || 1
|
420
|
+
assert_equal expected, @result.line, "should have proper line number"
|
266
421
|
end
|
267
422
|
|
268
|
-
def
|
269
|
-
rb = "
|
270
|
-
pt = s(:
|
271
|
-
|
272
|
-
|
273
|
-
end
|
423
|
+
def test_position_info
|
424
|
+
rb = "a = 42\np a"
|
425
|
+
pt = s(:block,
|
426
|
+
s(:lasgn, :a, s(:lit, 42)),
|
427
|
+
s(:call, nil, :p, s(:arglist, s(:lvar, :a))))
|
274
428
|
|
275
|
-
|
276
|
-
rb = "\"#\{'a'}#\{b}\""
|
277
|
-
pt = s(:dstr, "a", s(:evstr, s(:vcall, :b)))
|
429
|
+
result = @processor.parse(rb, "blah.rb")
|
278
430
|
|
279
|
-
assert_equal pt,
|
280
|
-
end
|
431
|
+
assert_equal pt, result
|
281
432
|
|
282
|
-
|
283
|
-
|
284
|
-
|
433
|
+
assert_equal 1, result.line, "block should have line number"
|
434
|
+
assert_equal 1, result.lasgn.line, "lasgn should have line number"
|
435
|
+
assert_equal 2, result.call.line, "call should have line number"
|
285
436
|
|
286
|
-
|
287
|
-
end
|
437
|
+
expected = "blah.rb"
|
288
438
|
|
289
|
-
|
290
|
-
|
291
|
-
|
439
|
+
assert_equal expected, result.file
|
440
|
+
assert_equal expected, result.lasgn.file
|
441
|
+
assert_equal expected, result.call.file
|
292
442
|
|
293
|
-
|
443
|
+
assert_same result.file, result.lasgn.file
|
444
|
+
assert_same result.file, result.call.file
|
294
445
|
end
|
295
446
|
|
447
|
+
def test_position_info2
|
448
|
+
rb = "def x(y)\n p(y)\n y *= 2\n return y;\nend" # TODO: remove () & ;
|
449
|
+
pt = s(:defn, :x, s(:args, :y),
|
450
|
+
s(:scope,
|
451
|
+
s(:block,
|
452
|
+
s(:call, nil, :p, s(:arglist, s(:lvar, :y))),
|
453
|
+
s(:lasgn, :y,
|
454
|
+
s(:call, s(:lvar, :y), :*, s(:arglist, s(:lit, 2)))),
|
455
|
+
s(:return, s(:lvar, :y)))))
|
296
456
|
|
297
|
-
|
298
|
-
rb = "a do\n v = nil\n begin\n yield\n rescue Exception => v\n break\n end\nend"
|
299
|
-
pt = s(:iter,
|
300
|
-
s(:fcall, :a),
|
301
|
-
nil,
|
302
|
-
s(:block,
|
303
|
-
s(:dasgn_curr, :v, s(:nil)),
|
304
|
-
s(:begin,
|
305
|
-
s(:rescue,
|
306
|
-
s(:yield),
|
307
|
-
s(:resbody,
|
308
|
-
s(:array, s(:const, :Exception)),
|
309
|
-
s(:block, s(:dasgn_curr, :v, s(:gvar, :$!)), s(:break)))))))
|
457
|
+
result = @processor.parse(rb)
|
310
458
|
|
311
|
-
assert_equal pt,
|
312
|
-
end
|
459
|
+
assert_equal pt, result
|
313
460
|
|
314
|
-
|
315
|
-
lhs, rhs = s(:array, s(:lit, :iter)), s(:when, s(:const, :BRANCHING), nil)
|
316
|
-
expected = s(:array, s(:lit, :iter), s(:when, s(:const, :BRANCHING), nil))
|
461
|
+
body = result.scope.block
|
317
462
|
|
318
|
-
assert_equal
|
463
|
+
assert_equal 1, result.line, "defn should have line number"
|
464
|
+
assert_equal 2, body.call.line, "call should have line number"
|
465
|
+
assert_equal 3, body.lasgn.line, "lasgn should have line number"
|
466
|
+
assert_equal 4, body.return.line, "return should have line number"
|
319
467
|
end
|
320
468
|
end
|
321
|
-
|
322
|
-
__END__
|
323
|
-
|
324
|
-
# blah18.rb
|
325
|
-
|
326
|
-
assert_equal("sub", $_)
|