ripper_ruby_parser 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +30 -0
- data/README.md +3 -3
- data/Rakefile +4 -4
- data/lib/ripper_ruby_parser/commenting_ripper_parser.rb +5 -7
- data/lib/ripper_ruby_parser/parser.rb +2 -3
- data/lib/ripper_ruby_parser/sexp_handlers/arguments.rb +2 -6
- data/lib/ripper_ruby_parser/sexp_handlers/assignment.rb +16 -17
- data/lib/ripper_ruby_parser/sexp_handlers/blocks.rb +5 -5
- data/lib/ripper_ruby_parser/sexp_handlers/conditionals.rb +6 -7
- data/lib/ripper_ruby_parser/sexp_handlers/hashes.rb +4 -5
- data/lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb +7 -11
- data/lib/ripper_ruby_parser/sexp_handlers/literals.rb +35 -26
- data/lib/ripper_ruby_parser/sexp_handlers/loops.rb +19 -18
- data/lib/ripper_ruby_parser/sexp_handlers/method_calls.rb +15 -15
- data/lib/ripper_ruby_parser/sexp_handlers/methods.rb +9 -18
- data/lib/ripper_ruby_parser/sexp_handlers/operators.rb +10 -10
- data/lib/ripper_ruby_parser/sexp_processor.rb +11 -14
- data/lib/ripper_ruby_parser/syntax_error.rb +1 -3
- data/lib/ripper_ruby_parser/version.rb +1 -1
- data/test/end_to_end/comparison_test.rb +0 -1
- data/test/end_to_end/lib_comparison_test.rb +0 -2
- data/test/end_to_end/line_numbering_test.rb +0 -1
- data/test/end_to_end/samples_comparison_test.rb +1 -1
- data/test/end_to_end/test_comparison_test.rb +0 -3
- data/test/pt_testcase/pt_test.rb +4 -4
- data/test/samples/inline.rb +704 -0
- data/test/test_helper.rb +39 -37
- data/test/unit/commenting_ripper_parser_test.rb +57 -59
- data/test/unit/parser_blocks_test.rb +19 -3
- data/test/unit/parser_conditionals_test.rb +0 -1
- data/test/unit/parser_literals_test.rb +25 -25
- data/test/unit/parser_method_calls_test.rb +19 -15
- data/test/unit/parser_test.rb +31 -24
- data/test/unit/sexp_processor_test.rb +1 -14
- metadata +67 -51
- data/lib/ripper_ruby_parser/sexp_ext.rb +0 -14
data/test/test_helper.rb
CHANGED
@@ -1,9 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
add_filter "/test/"
|
5
|
-
end
|
6
|
-
rescue LoadError
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start do
|
3
|
+
add_filter "/test/"
|
7
4
|
end
|
8
5
|
|
9
6
|
require 'minitest/autorun'
|
@@ -12,45 +9,50 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
12
9
|
|
13
10
|
require 'ripper_ruby_parser'
|
14
11
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
module MiniTest
|
13
|
+
class Spec
|
14
|
+
def formatted exp
|
15
|
+
exp.to_s.gsub(/\), /, "),\n")
|
16
|
+
end
|
19
17
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
18
|
+
def to_comments exp
|
19
|
+
inner = exp.map do |sub_exp|
|
20
|
+
if sub_exp.is_a? Sexp
|
21
|
+
to_comments sub_exp
|
22
|
+
else
|
23
|
+
sub_exp
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
comments = exp.comments.to_s.gsub(/\n\s*\n/, "\n")
|
28
|
+
if comments.empty?
|
29
|
+
s(*inner)
|
24
30
|
else
|
25
|
-
|
31
|
+
s(:comment, comments, s(*inner))
|
26
32
|
end
|
27
33
|
end
|
28
34
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
35
|
+
def assert_parsed_as sexp, code
|
36
|
+
parser = RipperRubyParser::Parser.new
|
37
|
+
result = parser.parse code
|
38
|
+
if sexp.nil?
|
39
|
+
assert_nil result
|
40
|
+
else
|
41
|
+
assert_equal sexp, result
|
42
|
+
end
|
34
43
|
end
|
35
|
-
end
|
36
44
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
45
|
+
def assert_parsed_as_before code
|
46
|
+
oldparser = RubyParser.new
|
47
|
+
newparser = RipperRubyParser::Parser.new
|
48
|
+
expected = oldparser.parse code.dup
|
49
|
+
result = newparser.parse code
|
50
|
+
assert_equal formatted(expected), formatted(result)
|
51
|
+
end
|
41
52
|
end
|
42
53
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
expected = oldparser.parse code.dup
|
47
|
-
result = newparser.parse code
|
48
|
-
assert_equal formatted(expected), formatted(result)
|
54
|
+
module Expectations
|
55
|
+
infect_an_assertion :assert_parsed_as, :must_be_parsed_as
|
56
|
+
infect_an_assertion :assert_parsed_as_before, :must_be_parsed_as_before, :unary
|
49
57
|
end
|
50
58
|
end
|
51
|
-
|
52
|
-
module MiniTest::Expectations
|
53
|
-
infect_an_assertion :assert_parsed_as, :must_be_parsed_as
|
54
|
-
infect_an_assertion :assert_parsed_as_before, :must_be_parsed_as_before, :unary
|
55
|
-
end
|
56
|
-
|
@@ -11,111 +11,109 @@ describe RipperRubyParser::CommentingRipperParser do
|
|
11
11
|
num_params = RUBY_VERSION < "2.0.0" ? 5 : 7
|
12
12
|
s(:params, *([nil] * num_params))
|
13
13
|
end
|
14
|
-
|
15
14
|
end
|
16
15
|
|
17
16
|
describe "handling comments" do
|
18
17
|
it "produces a comment node surrounding a commented def" do
|
19
18
|
result = parse_with_builder "# Foo\ndef foo; end"
|
20
19
|
result.must_equal s(:program,
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
20
|
+
s(s(:comment,
|
21
|
+
"# Foo\n",
|
22
|
+
s(:def,
|
23
|
+
s(:@ident, "foo", s(2, 4)),
|
24
|
+
empty_params_list,
|
25
|
+
s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
|
27
26
|
end
|
28
27
|
|
29
28
|
it "produces a blank comment node surrounding a def that has no comment" do
|
30
29
|
result = parse_with_builder "def foo; end"
|
31
30
|
result.must_equal s(:program,
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
31
|
+
s(s(:comment,
|
32
|
+
"",
|
33
|
+
s(:def,
|
34
|
+
s(:@ident, "foo", s(1, 4)),
|
35
|
+
empty_params_list,
|
36
|
+
s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
|
38
37
|
end
|
39
38
|
|
40
39
|
it "produces a comment node surrounding a commented class" do
|
41
40
|
result = parse_with_builder "# Foo\nclass Foo; end"
|
42
41
|
result.must_equal s(:program,
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
42
|
+
s(s(:comment,
|
43
|
+
"# Foo\n",
|
44
|
+
s(:class,
|
45
|
+
s(:const_ref, s(:@const, "Foo", s(2, 6))),
|
46
|
+
nil,
|
47
|
+
s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
|
49
48
|
end
|
50
49
|
|
51
50
|
it "produce a blank comment node surrounding a class that has no comment" do
|
52
51
|
result = parse_with_builder "class Foo; end"
|
53
52
|
result.must_equal s(:program,
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
53
|
+
s(s(:comment,
|
54
|
+
"",
|
55
|
+
s(:class,
|
56
|
+
s(:const_ref, s(:@const, "Foo", s(1, 6))),
|
57
|
+
nil,
|
58
|
+
s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
|
60
59
|
end
|
61
60
|
|
62
61
|
it "produces a comment node surrounding a commented module" do
|
63
62
|
result = parse_with_builder "# Foo\nmodule Foo; end"
|
64
63
|
result.must_equal s(:program,
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
64
|
+
s(s(:comment,
|
65
|
+
"# Foo\n",
|
66
|
+
s(:module,
|
67
|
+
s(:const_ref, s(:@const, "Foo", s(2, 7))),
|
68
|
+
s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
|
70
69
|
end
|
71
70
|
|
72
71
|
it "produces a blank comment node surrounding a module that has no comment" do
|
73
72
|
result = parse_with_builder "module Foo; end"
|
74
73
|
result.must_equal s(:program,
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
74
|
+
s(s(:comment,
|
75
|
+
"",
|
76
|
+
s(:module,
|
77
|
+
s(:const_ref, s(:@const, "Foo", s(1, 7))),
|
78
|
+
s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
|
80
79
|
end
|
81
80
|
|
82
81
|
it "is not confused by a symbol containing a keyword" do
|
83
82
|
result = parse_with_builder ":class; def foo; end"
|
84
83
|
result.must_equal s(:program,
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
84
|
+
s(s(:symbol_literal, s(:symbol, s(:@kw, "class", s(1, 1)))),
|
85
|
+
s(:comment,
|
86
|
+
"",
|
87
|
+
s(:def,
|
88
|
+
s(:@ident, "foo", s(1, 12)),
|
89
|
+
empty_params_list,
|
90
|
+
s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
|
92
91
|
end
|
93
92
|
|
94
93
|
it "is not confused by a dynamic symbol" do
|
95
94
|
result = parse_with_builder ":'foo'; def bar; end"
|
96
95
|
result.must_equal s(:program,
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
96
|
+
s(s(:dyna_symbol, s(s(:@tstring_content, "foo", s(1, 2)))),
|
97
|
+
s(:comment,
|
98
|
+
"",
|
99
|
+
s(:def,
|
100
|
+
s(:@ident, "bar", s(1, 12)),
|
101
|
+
empty_params_list,
|
102
|
+
s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
|
104
103
|
end
|
105
104
|
|
106
105
|
it "is not confused by a dynamic symbol containing a class definition" do
|
107
106
|
result = parse_with_builder ":\"foo\#{class Bar;end}\""
|
108
107
|
result.must_equal s(:program,
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
108
|
+
s(s(:dyna_symbol,
|
109
|
+
s(s(:@tstring_content, "foo", s(1, 2)),
|
110
|
+
s(:string_embexpr,
|
111
|
+
s(s(:comment,
|
112
|
+
"",
|
113
|
+
s(:class,
|
114
|
+
s(:const_ref, s(:@const, "Bar", s(1, 13))),
|
115
|
+
nil,
|
116
|
+
s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))))))
|
118
117
|
end
|
119
118
|
end
|
120
119
|
end
|
121
|
-
|
@@ -33,11 +33,19 @@ describe RipperRubyParser::Parser do
|
|
33
33
|
s(:args, :bar, :"&baz"))
|
34
34
|
end
|
35
35
|
|
36
|
+
it "handles absent parameter specs" do
|
37
|
+
"foo do; bar; end".
|
38
|
+
must_be_parsed_as s(:iter,
|
39
|
+
s(:call, nil, :foo),
|
40
|
+
0,
|
41
|
+
s(:call, nil, :bar))
|
42
|
+
end
|
43
|
+
|
36
44
|
it "handles empty parameter specs" do
|
37
45
|
"foo do ||; bar; end".
|
38
46
|
must_be_parsed_as s(:iter,
|
39
47
|
s(:call, nil, :foo),
|
40
|
-
|
48
|
+
s(:args),
|
41
49
|
s(:call, nil, :bar))
|
42
50
|
end
|
43
51
|
|
@@ -261,7 +269,15 @@ describe RipperRubyParser::Parser do
|
|
261
269
|
must_be_parsed_as s(:iter,
|
262
270
|
s(:call, nil, :lambda),
|
263
271
|
s(:args, :foo),
|
264
|
-
s(:call, nil, :bar))
|
272
|
+
s(:call, nil, :bar))
|
273
|
+
end
|
274
|
+
|
275
|
+
it "works when there are zero arguments" do
|
276
|
+
"->() { bar }".
|
277
|
+
must_be_parsed_as s(:iter,
|
278
|
+
s(:call, nil, :lambda),
|
279
|
+
s(:args),
|
280
|
+
s(:call, nil, :bar))
|
265
281
|
end
|
266
282
|
|
267
283
|
it "works when there are no arguments" do
|
@@ -269,7 +285,7 @@ describe RipperRubyParser::Parser do
|
|
269
285
|
must_be_parsed_as s(:iter,
|
270
286
|
s(:call, nil, :lambda),
|
271
287
|
0,
|
272
|
-
s(:call, nil, :bar))
|
288
|
+
s(:call, nil, :bar))
|
273
289
|
end
|
274
290
|
end
|
275
291
|
end
|
@@ -1,4 +1,3 @@
|
|
1
|
-
# coding: utf-8
|
2
1
|
require File.expand_path('../test_helper.rb', File.dirname(__FILE__))
|
3
2
|
|
4
3
|
describe RipperRubyParser::Parser do
|
@@ -140,7 +139,7 @@ describe RipperRubyParser::Parser do
|
|
140
139
|
result[1].encoding.to_s.must_equal "UTF-8"
|
141
140
|
end
|
142
141
|
|
143
|
-
describe "with escape sequences" do
|
142
|
+
describe "with double-quoted strings with escape sequences" do
|
144
143
|
it "works for strings with escape sequences" do
|
145
144
|
"\"\\n\"".
|
146
145
|
must_be_parsed_as s(:str, "\n")
|
@@ -156,12 +155,12 @@ describe RipperRubyParser::Parser do
|
|
156
155
|
must_be_parsed_as s(:str, "\\n")
|
157
156
|
end
|
158
157
|
|
159
|
-
it "works for a
|
158
|
+
it "works for a representation of a regex literal with escaped right bracket" do
|
160
159
|
"\"/\\\\)/\"".
|
161
160
|
must_be_parsed_as s(:str, "/\\)/")
|
162
161
|
end
|
163
162
|
|
164
|
-
it "works for a
|
163
|
+
it "works for a uselessly escaped right bracket" do
|
165
164
|
"\"/\\)/\"".
|
166
165
|
must_be_parsed_as s(:str, "/)/")
|
167
166
|
end
|
@@ -247,8 +246,8 @@ describe RipperRubyParser::Parser do
|
|
247
246
|
|
248
247
|
it "works for cvars" do
|
249
248
|
"\"foo\#@@bar\"".must_be_parsed_as s(:dstr,
|
250
|
-
|
251
|
-
|
249
|
+
"foo",
|
250
|
+
s(:evstr, s(:cvar, :@@bar)))
|
252
251
|
end
|
253
252
|
end
|
254
253
|
|
@@ -288,10 +287,10 @@ describe RipperRubyParser::Parser do
|
|
288
287
|
|
289
288
|
it "works for strings with interpolations followed by escape sequences" do
|
290
289
|
'"#{foo}\\n"'.
|
291
|
-
must_be_parsed_as
|
292
|
-
|
293
|
-
|
294
|
-
|
290
|
+
must_be_parsed_as s(:dstr,
|
291
|
+
"",
|
292
|
+
s(:evstr, s(:call, nil, :foo)),
|
293
|
+
s(:str, "\n"))
|
295
294
|
end
|
296
295
|
|
297
296
|
it "works with an empty interpolation" do
|
@@ -324,21 +323,23 @@ describe RipperRubyParser::Parser do
|
|
324
323
|
s(:str, "baz"))
|
325
324
|
end
|
326
325
|
|
327
|
-
|
328
|
-
"
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
326
|
+
describe "when both strings have interpolations" do
|
327
|
+
it "performs the concatenation" do
|
328
|
+
"\"foo\#{bar}\" \"baz\#{qux}\"".
|
329
|
+
must_be_parsed_as s(:dstr,
|
330
|
+
"foo",
|
331
|
+
s(:evstr, s(:call, nil, :bar)),
|
332
|
+
s(:str, "baz"),
|
333
|
+
s(:evstr, s(:call, nil, :qux)))
|
334
|
+
end
|
335
335
|
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
336
|
+
it "removes empty substrings from the concatenation" do
|
337
|
+
"\"foo\#{bar}\" \"\#{qux}\"".
|
338
|
+
must_be_parsed_as s(:dstr,
|
339
|
+
"foo",
|
340
|
+
s(:evstr, s(:call, nil, :bar)),
|
341
|
+
s(:evstr, s(:call, nil, :qux)))
|
342
|
+
end
|
342
343
|
end
|
343
344
|
end
|
344
345
|
end
|
@@ -514,6 +515,5 @@ describe RipperRubyParser::Parser do
|
|
514
515
|
must_be_parsed_as s(:lit, 448)
|
515
516
|
end
|
516
517
|
end
|
517
|
-
|
518
518
|
end
|
519
519
|
end
|
@@ -103,8 +103,8 @@ describe RipperRubyParser::Parser do
|
|
103
103
|
s(:call,
|
104
104
|
s(:call, nil, :foo),
|
105
105
|
:bar),
|
106
|
-
|
107
|
-
|
106
|
+
0,
|
107
|
+
s(:call, nil, :baz))
|
108
108
|
end
|
109
109
|
|
110
110
|
it "works for a do block with several statements" do
|
@@ -113,33 +113,38 @@ describe RipperRubyParser::Parser do
|
|
113
113
|
s(:call,
|
114
114
|
s(:call, nil, :foo),
|
115
115
|
:bar),
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
116
|
+
0,
|
117
|
+
s(:block,
|
118
|
+
s(:call, nil, :baz),
|
119
|
+
s(:call, nil, :qux)))
|
120
120
|
end
|
121
121
|
end
|
122
122
|
end
|
123
123
|
|
124
124
|
describe "for calls to super" do
|
125
125
|
specify { "super".must_be_parsed_as s(:zsuper) }
|
126
|
-
specify
|
127
|
-
|
128
|
-
|
126
|
+
specify do
|
127
|
+
"super foo".must_be_parsed_as s(:super,
|
128
|
+
s(:call, nil, :foo))
|
129
|
+
end
|
130
|
+
specify do
|
129
131
|
"super foo, bar".must_be_parsed_as s(:super,
|
130
132
|
s(:call, nil, :foo),
|
131
|
-
s(:call, nil, :bar))
|
132
|
-
|
133
|
+
s(:call, nil, :bar))
|
134
|
+
end
|
135
|
+
specify do
|
133
136
|
"super foo, *bar".must_be_parsed_as s(:super,
|
134
137
|
s(:call, nil, :foo),
|
135
138
|
s(:splat,
|
136
|
-
s(:call, nil, :bar)))
|
137
|
-
|
139
|
+
s(:call, nil, :bar)))
|
140
|
+
end
|
141
|
+
specify do
|
138
142
|
"super foo, *bar, &baz".
|
139
143
|
must_be_parsed_as s(:super,
|
140
144
|
s(:call, nil, :foo),
|
141
145
|
s(:splat, s(:call, nil, :bar)),
|
142
|
-
s(:block_pass, s(:call, nil, :baz)))
|
146
|
+
s(:block_pass, s(:call, nil, :baz)))
|
147
|
+
end
|
143
148
|
end
|
144
149
|
|
145
150
|
it "handles calling a proc" do
|
@@ -148,4 +153,3 @@ describe RipperRubyParser::Parser do
|
|
148
153
|
end
|
149
154
|
end
|
150
155
|
end
|
151
|
-
|