ruby_parser 2.2.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of ruby_parser might be problematic. Click here for more details.
- data.tar.gz.sig +0 -0
- data/History.txt +13 -0
- data/Rakefile +6 -0
- data/lib/ruby_lexer.rb +8 -2
- data/lib/ruby_parser.rb +10 -3
- data/lib/ruby_parser.y +10 -3
- data/lib/ruby_parser_extras.rb +2 -3
- data/test/test_ruby_lexer.rb +1 -1
- data/test/test_ruby_parser.rb +180 -127
- metadata +5 -5
- metadata.gz.sig +0 -0
data.tar.gz.sig
CHANGED
Binary file
|
data/History.txt
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
=== 2.3.0 / 2011-09-06
|
2
|
+
|
3
|
+
* 2 minor enhancements:
|
4
|
+
|
5
|
+
* Add -g flag to parser compile if DEBUG
|
6
|
+
* Lexer now embeds line number in yacc_value for keywords, helping fix up line numbers
|
7
|
+
|
8
|
+
* 3 bug fixes:
|
9
|
+
|
10
|
+
* Fix method line numbers when no args and no parens (quix)
|
11
|
+
* Fixed line numbers on return/break/next w/ result expr. (pjnz)
|
12
|
+
* Fixed some lexing state in order to parse: 'f (1), 2' as 'f(1, 2)'. (invernizzi)
|
13
|
+
|
1
14
|
=== 2.2.0 / 2011-08-23
|
2
15
|
|
3
16
|
* 2 minor enhancements:
|
data/Rakefile
CHANGED
@@ -17,6 +17,8 @@ Hoe.spec 'ruby_parser' do
|
|
17
17
|
dependency 'sexp_processor', '~> 3.0'
|
18
18
|
|
19
19
|
self.perforce_ignore << "lib/ruby_parser.rb" if plugin? :perforce
|
20
|
+
|
21
|
+
self.racc_flags << " -g" if plugin?(:racc) && ENV["DEBUG"]
|
20
22
|
end
|
21
23
|
|
22
24
|
task :clean do
|
@@ -101,4 +103,8 @@ task :huh? do
|
|
101
103
|
puts "ruby #{Hoe::RUBY_FLAGS} bin/ruby_parse -q -g ..."
|
102
104
|
end
|
103
105
|
|
106
|
+
task :irb => [:isolate] do
|
107
|
+
sh "GEM_HOME=#{Gem.path.first} irb -rubygems -Ilib -rruby_parser;"
|
108
|
+
end
|
109
|
+
|
104
110
|
# vim: syntax=Ruby
|
data/lib/ruby_lexer.rb
CHANGED
@@ -847,6 +847,7 @@ class RubyLexer
|
|
847
847
|
end
|
848
848
|
|
849
849
|
self.expr_beg_push "{"
|
850
|
+
self.command_start = true unless result == :tLBRACE
|
850
851
|
|
851
852
|
return result
|
852
853
|
elsif src.scan(/[+-]/) then
|
@@ -1249,7 +1250,12 @@ class RubyLexer
|
|
1249
1250
|
if keyword then
|
1250
1251
|
state = lex_state
|
1251
1252
|
self.lex_state = keyword.state
|
1252
|
-
self.yacc_value = token
|
1253
|
+
self.yacc_value = [token, src.lineno]
|
1254
|
+
|
1255
|
+
if state == :expr_fname then
|
1256
|
+
self.yacc_value = keyword.name
|
1257
|
+
return keyword.id0
|
1258
|
+
end
|
1253
1259
|
|
1254
1260
|
if keyword.id0 == :kDO then
|
1255
1261
|
self.command_start = true
|
@@ -1259,7 +1265,7 @@ class RubyLexer
|
|
1259
1265
|
return :kDO
|
1260
1266
|
end
|
1261
1267
|
|
1262
|
-
return keyword.id0 if state == :expr_beg
|
1268
|
+
return keyword.id0 if state == :expr_beg or state == :expr_value
|
1263
1269
|
|
1264
1270
|
self.lex_state = :expr_beg if keyword.id0 != keyword.id1
|
1265
1271
|
|
data/lib/ruby_parser.rb
CHANGED
@@ -3174,19 +3174,22 @@ end
|
|
3174
3174
|
# reduce 43 omitted
|
3175
3175
|
|
3176
3176
|
def _reduce_44(val, _values, result)
|
3177
|
-
|
3177
|
+
line = val[0].last
|
3178
|
+
result = s(:return, ret_args(val[1])).line(line)
|
3178
3179
|
|
3179
3180
|
result
|
3180
3181
|
end
|
3181
3182
|
|
3182
3183
|
def _reduce_45(val, _values, result)
|
3183
|
-
|
3184
|
+
line = val[0].last
|
3185
|
+
result = s(:break, ret_args(val[1])).line(line)
|
3184
3186
|
|
3185
3187
|
result
|
3186
3188
|
end
|
3187
3189
|
|
3188
3190
|
def _reduce_46(val, _values, result)
|
3189
|
-
|
3191
|
+
line = val[0].last
|
3192
|
+
result = s(:next, ret_args(val[1])).line(line)
|
3190
3193
|
|
3191
3194
|
result
|
3192
3195
|
end
|
@@ -4663,18 +4666,21 @@ end
|
|
4663
4666
|
|
4664
4667
|
def _reduce_343(val, _values, result)
|
4665
4668
|
result = 0
|
4669
|
+
self.lexer.command_start = true
|
4666
4670
|
|
4667
4671
|
result
|
4668
4672
|
end
|
4669
4673
|
|
4670
4674
|
def _reduce_344(val, _values, result)
|
4671
4675
|
result = 0
|
4676
|
+
self.lexer.command_start = true
|
4672
4677
|
|
4673
4678
|
result
|
4674
4679
|
end
|
4675
4680
|
|
4676
4681
|
def _reduce_345(val, _values, result)
|
4677
4682
|
result = val[1]
|
4683
|
+
self.lexer.command_start = true
|
4678
4684
|
|
4679
4685
|
result
|
4680
4686
|
end
|
@@ -5242,6 +5248,7 @@ end
|
|
5242
5248
|
def _reduce_444(val, _values, result)
|
5243
5249
|
result = val[1]
|
5244
5250
|
lexer.lex_state = :expr_beg
|
5251
|
+
self.lexer.command_start = true
|
5245
5252
|
|
5246
5253
|
result
|
5247
5254
|
end
|
data/lib/ruby_parser.y
CHANGED
@@ -212,15 +212,18 @@ rule
|
|
212
212
|
| block_command
|
213
213
|
| kRETURN call_args
|
214
214
|
{
|
215
|
-
|
215
|
+
line = val[0].last
|
216
|
+
result = s(:return, ret_args(val[1])).line(line)
|
216
217
|
}
|
217
218
|
| kBREAK call_args
|
218
219
|
{
|
219
|
-
|
220
|
+
line = val[0].last
|
221
|
+
result = s(:break, ret_args(val[1])).line(line)
|
220
222
|
}
|
221
223
|
| kNEXT call_args
|
222
224
|
{
|
223
|
-
|
225
|
+
line = val[0].last
|
226
|
+
result = s(:next, ret_args(val[1])).line(line)
|
224
227
|
}
|
225
228
|
|
226
229
|
block_command: block_call
|
@@ -1190,14 +1193,17 @@ rule
|
|
1190
1193
|
| tPIPE tPIPE
|
1191
1194
|
{
|
1192
1195
|
result = 0
|
1196
|
+
self.lexer.command_start = true
|
1193
1197
|
}
|
1194
1198
|
| tOROP
|
1195
1199
|
{
|
1196
1200
|
result = 0
|
1201
|
+
self.lexer.command_start = true
|
1197
1202
|
}
|
1198
1203
|
| tPIPE block_var tPIPE
|
1199
1204
|
{
|
1200
1205
|
result = val[1]
|
1206
|
+
self.lexer.command_start = true
|
1201
1207
|
}
|
1202
1208
|
|
1203
1209
|
do_block: kDO_BLOCK
|
@@ -1596,6 +1602,7 @@ xstring_contents: none
|
|
1596
1602
|
{
|
1597
1603
|
result = val[1]
|
1598
1604
|
lexer.lex_state = :expr_beg
|
1605
|
+
self.lexer.command_start = true
|
1599
1606
|
}
|
1600
1607
|
| f_args term
|
1601
1608
|
{
|
data/lib/ruby_parser_extras.rb
CHANGED
@@ -123,7 +123,7 @@ class RPStringScanner < StringScanner
|
|
123
123
|
end
|
124
124
|
|
125
125
|
class RubyParser < Racc::Parser
|
126
|
-
VERSION = '2.
|
126
|
+
VERSION = '2.3.0' unless constants.include? "VERSION" # SIGH
|
127
127
|
|
128
128
|
attr_accessor :lexer, :in_def, :in_single, :file
|
129
129
|
attr_reader :env, :comments
|
@@ -506,7 +506,7 @@ class RubyParser < Racc::Parser
|
|
506
506
|
end
|
507
507
|
|
508
508
|
def new_defn val
|
509
|
-
(
|
509
|
+
(_, line), name, args, body = val[0], val[1], val[3], val[4]
|
510
510
|
body ||= s(:nil)
|
511
511
|
|
512
512
|
body ||= s(:block)
|
@@ -514,7 +514,6 @@ class RubyParser < Racc::Parser
|
|
514
514
|
|
515
515
|
result = s(:defn, name.to_sym, args, s(:scope, body))
|
516
516
|
result.line = line
|
517
|
-
result.line -= 1 if bol
|
518
517
|
result.comments = self.comments.pop
|
519
518
|
result
|
520
519
|
end
|
data/test/test_ruby_lexer.rb
CHANGED
@@ -1820,7 +1820,7 @@ class TestRubyLexer < MiniTest::Unit::TestCase
|
|
1820
1820
|
token = args.shift
|
1821
1821
|
value = args.shift
|
1822
1822
|
assert @lex.advance, "no more tokens"
|
1823
|
-
assert_equal [token, value], [@lex.token, @lex.yacc_value]
|
1823
|
+
assert_equal [token, value], [@lex.token, [@lex.yacc_value].flatten.first]
|
1824
1824
|
end
|
1825
1825
|
|
1826
1826
|
deny @lex.advance, "must be empty, but had #{[@lex.token, @lex.yacc_value].inspect}"
|
data/test/test_ruby_parser.rb
CHANGED
@@ -32,10 +32,22 @@ class RubyParserTestCase < ParseTreeTestCase
|
|
32
32
|
end
|
33
33
|
|
34
34
|
class TestRubyParser < RubyParserTestCase
|
35
|
+
attr_accessor :result, :processor
|
36
|
+
|
35
37
|
def setup
|
36
38
|
super
|
37
39
|
|
38
|
-
|
40
|
+
self.processor = RubyParser.new
|
41
|
+
end
|
42
|
+
|
43
|
+
def assert_parse rb, pt
|
44
|
+
self.result = processor.parse rb
|
45
|
+
assert_equal pt, result
|
46
|
+
end
|
47
|
+
|
48
|
+
def assert_parse_line rb, pt, line
|
49
|
+
assert_parse rb, pt
|
50
|
+
assert_equal line, result.line, "call should have line number"
|
39
51
|
end
|
40
52
|
|
41
53
|
def test_attrasgn_array_lhs
|
@@ -49,44 +61,42 @@ class TestRubyParser < RubyParserTestCase
|
|
49
61
|
s(:call, nil, :to, s(:arglist))),
|
50
62
|
s(:array, s(:str, "a"), s(:str, "b"), s(:str, "c"))))
|
51
63
|
|
52
|
-
|
53
|
-
|
54
|
-
assert_equal pt, result
|
64
|
+
assert_parse rb, pt
|
55
65
|
end
|
56
66
|
|
57
67
|
def test_block_append
|
58
68
|
head = s(:args)
|
59
69
|
tail = s(:zsuper)
|
60
70
|
expected = s(:block, s(:args), s(:zsuper))
|
61
|
-
assert_equal expected,
|
71
|
+
assert_equal expected, processor.block_append(head, tail)
|
62
72
|
end
|
63
73
|
|
64
74
|
def test_block_append_begin_begin
|
65
75
|
head = s(:begin, s(:args))
|
66
76
|
tail = s(:begin, s(:args))
|
67
77
|
expected = s(:block, s(:args), s(:begin, s(:args)))
|
68
|
-
assert_equal expected,
|
78
|
+
assert_equal expected, processor.block_append(head, tail)
|
69
79
|
end
|
70
80
|
|
71
81
|
def test_block_append_block
|
72
82
|
head = s(:block, s(:args))
|
73
83
|
tail = s(:zsuper)
|
74
84
|
expected = s(:block, s(:args), s(:zsuper))
|
75
|
-
assert_equal expected,
|
85
|
+
assert_equal expected, processor.block_append(head, tail)
|
76
86
|
end
|
77
87
|
|
78
88
|
def test_block_append_nil_head
|
79
89
|
head = nil
|
80
90
|
tail = s(:zsuper)
|
81
91
|
expected = s(:zsuper)
|
82
|
-
assert_equal expected,
|
92
|
+
assert_equal expected, processor.block_append(head, tail)
|
83
93
|
end
|
84
94
|
|
85
95
|
def test_block_append_nil_tail
|
86
96
|
head = s(:args)
|
87
97
|
tail = nil
|
88
98
|
expected = s(:args)
|
89
|
-
assert_equal expected,
|
99
|
+
assert_equal expected, processor.block_append(head, tail)
|
90
100
|
end
|
91
101
|
|
92
102
|
def test_block_append_tail_block
|
@@ -95,14 +105,15 @@ class TestRubyParser < RubyParserTestCase
|
|
95
105
|
expected = s(:block,
|
96
106
|
s(:call, nil, :f1, s(:arglist)),
|
97
107
|
s(:block, s(:undef, s(:lit, :x)), s(:undef, s(:lit, :y))))
|
98
|
-
assert_equal expected,
|
108
|
+
assert_equal expected, processor.block_append(head, tail)
|
99
109
|
end
|
100
110
|
|
101
111
|
def test_call_env
|
102
|
-
|
103
|
-
|
112
|
+
processor.env[:a] = :lvar
|
113
|
+
rb = "a.happy"
|
114
|
+
pt = s(:call, s(:lvar, :a), :happy, s(:arglist))
|
104
115
|
|
105
|
-
|
116
|
+
assert_parse rb, pt
|
106
117
|
end
|
107
118
|
|
108
119
|
def test_dasgn_icky2
|
@@ -118,7 +129,7 @@ class TestRubyParser < RubyParserTestCase
|
|
118
129
|
s(:array, s(:const, :Exception), s(:lasgn, :v, s(:gvar, :$!))),
|
119
130
|
s(:break)))))
|
120
131
|
|
121
|
-
|
132
|
+
assert_parse rb, pt
|
122
133
|
end
|
123
134
|
|
124
135
|
def test_class_comments
|
@@ -127,11 +138,10 @@ class TestRubyParser < RubyParserTestCase
|
|
127
138
|
s(:scope,
|
128
139
|
s(:defn, :blah, s(:args), s(:scope, s(:block, s(:nil))))))
|
129
140
|
|
130
|
-
|
131
|
-
assert_equal pt, actual
|
141
|
+
assert_parse rb, pt
|
132
142
|
|
133
|
-
assert_equal "# blah 1\n# blah 2\n\n",
|
134
|
-
assert_equal "# blah 3\n",
|
143
|
+
assert_equal "# blah 1\n# blah 2\n\n", result.comments
|
144
|
+
assert_equal "# blah 3\n", result.scope.defn.comments
|
135
145
|
end
|
136
146
|
|
137
147
|
def test_module_comments
|
@@ -140,28 +150,25 @@ class TestRubyParser < RubyParserTestCase
|
|
140
150
|
s(:scope,
|
141
151
|
s(:defn, :blah, s(:args), s(:scope, s(:block, s(:nil))))))
|
142
152
|
|
143
|
-
|
144
|
-
assert_equal
|
145
|
-
assert_equal "# blah
|
146
|
-
assert_equal "# blah 3\n", actual.scope.defn.comments
|
153
|
+
assert_parse rb, pt
|
154
|
+
assert_equal "# blah 1\n\n# blah 2\n\n", result.comments
|
155
|
+
assert_equal "# blah 3\n", result.scope.defn.comments
|
147
156
|
end
|
148
157
|
|
149
158
|
def test_defn_comments
|
150
159
|
rb = "# blah 1\n# blah 2\n\ndef blah\nend"
|
151
160
|
pt = s(:defn, :blah, s(:args), s(:scope, s(:block, s(:nil))))
|
152
161
|
|
153
|
-
|
154
|
-
assert_equal
|
155
|
-
assert_equal "# blah 1\n# blah 2\n\n", actual.comments
|
162
|
+
assert_parse rb, pt
|
163
|
+
assert_equal "# blah 1\n# blah 2\n\n", result.comments
|
156
164
|
end
|
157
165
|
|
158
166
|
def test_defs_comments
|
159
167
|
rb = "# blah 1\n# blah 2\n\ndef self.blah\nend"
|
160
168
|
pt = s(:defs, s(:self), :blah, s(:args), s(:scope, s(:block)))
|
161
169
|
|
162
|
-
|
163
|
-
assert_equal
|
164
|
-
assert_equal "# blah 1\n# blah 2\n\n", actual.comments
|
170
|
+
assert_parse rb, pt
|
171
|
+
assert_equal "# blah 1\n# blah 2\n\n", result.comments
|
165
172
|
end
|
166
173
|
|
167
174
|
def test_do_bug # TODO: rename
|
@@ -172,7 +179,7 @@ class TestRubyParser < RubyParserTestCase
|
|
172
179
|
s(:call, s(:call, nil, :a, s(:arglist)), :b, s(:arglist)),
|
173
180
|
s(:lasgn, :c)))
|
174
181
|
|
175
|
-
|
182
|
+
assert_parse rb, pt
|
176
183
|
end
|
177
184
|
|
178
185
|
def test_bug_comment_eq_begin
|
@@ -180,43 +187,73 @@ class TestRubyParser < RubyParserTestCase
|
|
180
187
|
pt = nil
|
181
188
|
exp = rb.strip + "\n"
|
182
189
|
|
183
|
-
|
184
|
-
assert_equal exp,
|
190
|
+
assert_parse rb, pt
|
191
|
+
assert_equal exp, processor.lexer.comments
|
192
|
+
end
|
193
|
+
|
194
|
+
def test_bug_call_arglist_parens
|
195
|
+
rb = 'g ( 1), 2'
|
196
|
+
pt = s(:call, nil, :g, s(:arglist, s(:lit, 1), s(:lit, 2)))
|
197
|
+
|
198
|
+
assert_parse rb, pt
|
199
|
+
|
200
|
+
rb = <<-CODE
|
201
|
+
def f
|
202
|
+
g ( 1), 2
|
203
|
+
end
|
204
|
+
CODE
|
205
|
+
|
206
|
+
pt = s(:defn, :f, s(:args),
|
207
|
+
s(:scope,
|
208
|
+
s(:block,
|
209
|
+
s(:call, nil, :g,
|
210
|
+
s(:arglist,
|
211
|
+
s(:lit, 1), s(:lit, 2))))))
|
212
|
+
|
213
|
+
assert_parse rb, pt
|
214
|
+
|
215
|
+
rb = <<-CODE
|
216
|
+
def f()
|
217
|
+
g (1), 2
|
218
|
+
end
|
219
|
+
CODE
|
220
|
+
|
221
|
+
assert_parse rb, pt
|
185
222
|
end
|
186
223
|
|
187
224
|
def test_dstr_evstr
|
188
225
|
rb = "\"#\{'a'}#\{b}\""
|
189
226
|
pt = s(:dstr, "a", s(:evstr, s(:call, nil, :b, s(:arglist))))
|
190
227
|
|
191
|
-
|
228
|
+
assert_parse rb, pt
|
192
229
|
end
|
193
230
|
|
194
231
|
def test_dstr_str
|
195
232
|
rb = "\"#\{'a'} b\""
|
196
233
|
pt = s(:str, "a b")
|
197
234
|
|
198
|
-
|
235
|
+
assert_parse rb, pt
|
199
236
|
end
|
200
237
|
|
201
238
|
def test_empty
|
202
239
|
rb = ""
|
203
240
|
pt = nil
|
204
241
|
|
205
|
-
|
242
|
+
assert_parse rb, pt
|
206
243
|
end
|
207
244
|
|
208
245
|
def test_evstr_evstr
|
209
246
|
rb = "\"#\{a}#\{b}\""
|
210
247
|
pt = s(:dstr, "", s(:evstr, s(:call, nil, :a, s(:arglist))), s(:evstr, s(:call, nil, :b, s(:arglist))))
|
211
248
|
|
212
|
-
|
249
|
+
assert_parse rb, pt
|
213
250
|
end
|
214
251
|
|
215
252
|
def test_evstr_str
|
216
253
|
rb = "\"#\{a} b\""
|
217
254
|
pt = s(:dstr, "", s(:evstr, s(:call, nil, :a, s(:arglist))), s(:str, " b"))
|
218
255
|
|
219
|
-
|
256
|
+
assert_parse rb, pt
|
220
257
|
end
|
221
258
|
|
222
259
|
def test_lasgn_env
|
@@ -224,8 +261,8 @@ class TestRubyParser < RubyParserTestCase
|
|
224
261
|
pt = s(:lasgn, :a, s(:lit, 42))
|
225
262
|
expected_env = { :a => :lvar }
|
226
263
|
|
227
|
-
|
228
|
-
assert_equal expected_env,
|
264
|
+
assert_parse rb, pt
|
265
|
+
assert_equal expected_env, processor.env.all
|
229
266
|
end
|
230
267
|
|
231
268
|
def test_list_append
|
@@ -233,22 +270,22 @@ class TestRubyParser < RubyParserTestCase
|
|
233
270
|
b = s(:lit, 2)
|
234
271
|
c = s(:lit, 3)
|
235
272
|
|
236
|
-
result =
|
273
|
+
result = processor.list_append(s(:array, b.dup), c.dup)
|
237
274
|
|
238
275
|
assert_equal s(:array, b, c), result
|
239
276
|
|
240
|
-
result =
|
277
|
+
result = processor.list_append(b.dup, c.dup)
|
241
278
|
|
242
279
|
assert_equal s(:array, b, c), result
|
243
280
|
|
244
|
-
result =
|
281
|
+
result = processor.list_append(result, a.dup)
|
245
282
|
|
246
283
|
assert_equal s(:array, b, c, a), result
|
247
284
|
|
248
285
|
lhs, rhs = s(:array, s(:lit, :iter)), s(:when, s(:const, :BRANCHING), nil)
|
249
286
|
expected = s(:array, s(:lit, :iter), s(:when, s(:const, :BRANCHING), nil))
|
250
287
|
|
251
|
-
assert_equal expected,
|
288
|
+
assert_equal expected, processor.list_append(lhs, rhs)
|
252
289
|
end
|
253
290
|
|
254
291
|
def test_list_prepend
|
@@ -256,15 +293,15 @@ class TestRubyParser < RubyParserTestCase
|
|
256
293
|
b = s(:lit, 2)
|
257
294
|
c = s(:lit, 3)
|
258
295
|
|
259
|
-
result =
|
296
|
+
result = processor.list_prepend(b.dup, s(:array, c.dup))
|
260
297
|
|
261
298
|
assert_equal s(:array, b, c), result
|
262
299
|
|
263
|
-
result =
|
300
|
+
result = processor.list_prepend(b.dup, c.dup)
|
264
301
|
|
265
302
|
assert_equal s(:array, b, c), result
|
266
303
|
|
267
|
-
result =
|
304
|
+
result = processor.list_prepend(a.dup, result)
|
268
305
|
|
269
306
|
assert_equal s(:array, a, b, c), result
|
270
307
|
end
|
@@ -285,27 +322,27 @@ class TestRubyParser < RubyParserTestCase
|
|
285
322
|
s(:str, "\t"),
|
286
323
|
s(:evstr, s(:call, s(:ivar, :@fetch_error), :message)))
|
287
324
|
|
288
|
-
assert_equal expected,
|
325
|
+
assert_equal expected, processor.literal_concat(lhs, rhs)
|
289
326
|
end
|
290
327
|
|
291
328
|
def test_literal_concat_dstr_evstr
|
292
329
|
lhs, rhs = s(:dstr, "a"), s(:evstr, s(:call, nil, :b, s(:arglist)))
|
293
330
|
expected = s(:dstr, "a", s(:evstr, s(:call, nil, :b, s(:arglist))))
|
294
331
|
|
295
|
-
assert_equal expected,
|
332
|
+
assert_equal expected, processor.literal_concat(lhs, rhs)
|
296
333
|
end
|
297
334
|
|
298
335
|
def test_literal_concat_evstr_evstr
|
299
336
|
lhs, rhs = s(:evstr, s(:lit, 1)), s(:evstr, s(:lit, 2))
|
300
337
|
expected = s(:dstr, "", s(:evstr, s(:lit, 1)), s(:evstr, s(:lit, 2)))
|
301
338
|
|
302
|
-
assert_equal expected,
|
339
|
+
assert_equal expected, processor.literal_concat(lhs, rhs)
|
303
340
|
end
|
304
341
|
|
305
342
|
def test_literal_concat_str_evstr
|
306
343
|
lhs, rhs = s(:str, ""), s(:evstr, s(:str, "blah"))
|
307
344
|
|
308
|
-
assert_equal s(:str, "blah"),
|
345
|
+
assert_equal s(:str, "blah"), processor.literal_concat(lhs, rhs)
|
309
346
|
end
|
310
347
|
|
311
348
|
def test_logop_12
|
@@ -313,7 +350,7 @@ class TestRubyParser < RubyParserTestCase
|
|
313
350
|
rhs = s(:lit, 2)
|
314
351
|
exp = s(:and, s(:lit, 1), s(:lit, 2))
|
315
352
|
|
316
|
-
assert_equal exp,
|
353
|
+
assert_equal exp, processor.logop(:and, lhs, rhs)
|
317
354
|
end
|
318
355
|
|
319
356
|
def test_logop_1234_5
|
@@ -329,7 +366,7 @@ class TestRubyParser < RubyParserTestCase
|
|
329
366
|
s(:lit, 4),
|
330
367
|
s(:lit, 5)))))
|
331
368
|
|
332
|
-
assert_equal exp,
|
369
|
+
assert_equal exp, processor.logop(:and, lhs, rhs)
|
333
370
|
end
|
334
371
|
|
335
372
|
def test_logop_123_4
|
@@ -343,7 +380,7 @@ class TestRubyParser < RubyParserTestCase
|
|
343
380
|
s(:lit, 3),
|
344
381
|
s(:lit, 4))))
|
345
382
|
|
346
|
-
assert_equal exp,
|
383
|
+
assert_equal exp, processor.logop(:and, lhs, rhs)
|
347
384
|
end
|
348
385
|
|
349
386
|
def test_logop_12_3
|
@@ -351,7 +388,7 @@ class TestRubyParser < RubyParserTestCase
|
|
351
388
|
rhs = s(:lit, 3)
|
352
389
|
exp = s(:and, s(:lit, 1), s(:and, s(:lit, 2), s(:lit, 3)))
|
353
390
|
|
354
|
-
assert_equal exp,
|
391
|
+
assert_equal exp, processor.logop(:and, lhs, rhs)
|
355
392
|
end
|
356
393
|
|
357
394
|
def test_logop_nested_mix
|
@@ -364,22 +401,24 @@ class TestRubyParser < RubyParserTestCase
|
|
364
401
|
lhs.paren = true
|
365
402
|
rhs.paren = true
|
366
403
|
|
367
|
-
assert_equal exp,
|
404
|
+
assert_equal exp, processor.logop(:or, lhs, rhs)
|
368
405
|
end
|
369
406
|
|
370
407
|
def test_str_evstr
|
371
408
|
rb = "\"a #\{b}\""
|
372
409
|
pt = s(:dstr, "a ", s(:evstr, s(:call, nil, :b, s(:arglist))))
|
373
410
|
|
374
|
-
|
411
|
+
assert_parse rb, pt
|
375
412
|
end
|
376
413
|
|
377
414
|
def test_dsym_to_sym
|
378
|
-
|
379
|
-
|
415
|
+
pt = s(:alias, s(:lit, :<<), s(:lit, :>>))
|
416
|
+
|
417
|
+
rb = 'alias :<< :>>'
|
418
|
+
assert_parse rb, pt
|
380
419
|
|
381
|
-
|
382
|
-
|
420
|
+
rb = 'alias :"<<" :">>"'
|
421
|
+
assert_parse rb, pt
|
383
422
|
end
|
384
423
|
|
385
424
|
def test_regexp
|
@@ -392,7 +431,7 @@ class TestRubyParser < RubyParserTestCase
|
|
392
431
|
}
|
393
432
|
|
394
433
|
regexps.each do |rb, lit|
|
395
|
-
|
434
|
+
assert_parse rb, s(:lit, lit)
|
396
435
|
end
|
397
436
|
|
398
437
|
# TODO: add more including interpolation etc
|
@@ -402,28 +441,29 @@ class TestRubyParser < RubyParserTestCase
|
|
402
441
|
rb = "%Q[before [#\{nest}] after]"
|
403
442
|
pt = s(:dstr, "before [", s(:evstr, s(:call, nil, :nest, s(:arglist))), s(:str, "] after"))
|
404
443
|
|
405
|
-
|
444
|
+
assert_parse rb, pt
|
406
445
|
end
|
407
446
|
|
408
|
-
#
|
409
|
-
#
|
410
|
-
#
|
411
|
-
|
412
|
-
#
|
413
|
-
#
|
447
|
+
# def test_str_pct_nested_nested
|
448
|
+
# rb = "%{ { #\{ \"#\{1}\" } } }"
|
449
|
+
# assert_equal " { 1 } ", eval(rb)
|
450
|
+
# pt = s(:dstr, " { ", s(:evstr, s(:lit, 1)), s(:str, " } "))
|
451
|
+
#
|
452
|
+
# assert_parse rb, pt
|
453
|
+
# end
|
414
454
|
|
415
455
|
def test_str_str
|
416
456
|
rb = "\"a #\{'b'}\""
|
417
457
|
pt = s(:str, "a b")
|
418
458
|
|
419
|
-
|
459
|
+
assert_parse rb, pt
|
420
460
|
end
|
421
461
|
|
422
462
|
def test_str_str_str
|
423
463
|
rb = "\"a #\{'b'} c\""
|
424
464
|
pt = s(:str, "a b c")
|
425
465
|
|
426
|
-
|
466
|
+
assert_parse rb, pt
|
427
467
|
end
|
428
468
|
|
429
469
|
STARTING_LINE = {
|
@@ -449,22 +489,17 @@ class TestRubyParser < RubyParserTestCase
|
|
449
489
|
assert_equal expected, @result.line, "should have proper line number"
|
450
490
|
end
|
451
491
|
|
452
|
-
def
|
492
|
+
def test_parse_line_block
|
453
493
|
rb = "a = 42\np a"
|
454
494
|
pt = s(:block,
|
455
495
|
s(:lasgn, :a, s(:lit, 42)),
|
456
496
|
s(:call, nil, :p, s(:arglist, s(:lvar, :a))))
|
457
497
|
|
458
|
-
|
459
|
-
|
460
|
-
assert_equal pt, result
|
461
|
-
|
462
|
-
assert_equal 1, result.line, "block should have line number"
|
498
|
+
assert_parse_line rb, pt, 1
|
463
499
|
assert_equal 1, result.lasgn.line, "lasgn should have line number"
|
464
500
|
assert_equal 2, result.call.line, "call should have line number"
|
465
501
|
|
466
|
-
expected = "
|
467
|
-
|
502
|
+
expected = "(string)"
|
468
503
|
assert_equal expected, result.file
|
469
504
|
assert_equal expected, result.lasgn.file
|
470
505
|
assert_equal expected, result.call.file
|
@@ -473,7 +508,7 @@ class TestRubyParser < RubyParserTestCase
|
|
473
508
|
assert_same result.file, result.call.file
|
474
509
|
end
|
475
510
|
|
476
|
-
def
|
511
|
+
def test_parse_line_call_no_args
|
477
512
|
rb = "f do |x, y|\n x + y\nend"
|
478
513
|
|
479
514
|
pt = s(:iter,
|
@@ -481,17 +516,41 @@ class TestRubyParser < RubyParserTestCase
|
|
481
516
|
s(:masgn, s(:array, s(:lasgn, :x), s(:lasgn, :y))),
|
482
517
|
s(:call, s(:lvar, :x), :+, s(:arglist, s(:lvar, :y))))
|
483
518
|
|
484
|
-
|
485
|
-
|
486
|
-
assert_equal pt, result
|
487
|
-
|
519
|
+
assert_parse_line rb, pt, 1
|
488
520
|
assert_equal 1, result[1].line, "call should have line number"
|
489
|
-
assert_equal 1, result.line, "iter should have line number"
|
490
521
|
assert_equal 1, result[2].line, "masgn should have line number"
|
491
522
|
assert_equal 2, result[3].line, "call should have line number"
|
492
523
|
end
|
493
524
|
|
494
|
-
def
|
525
|
+
def test_parse_line_defn_no_parens
|
526
|
+
pt = s(:defn, :f, s(:args), s(:scope, s(:block, s(:nil))))
|
527
|
+
|
528
|
+
rb = "def f\nend"
|
529
|
+
assert_parse_line rb, pt, 1
|
530
|
+
|
531
|
+
rb = "def f\n\nend"
|
532
|
+
assert_parse_line rb, pt, 1
|
533
|
+
end
|
534
|
+
|
535
|
+
def test_parse_line_defn_complex
|
536
|
+
rb = "def x(y)\n p(y)\n y *= 2\n return y;\nend" # TODO: remove () & ;
|
537
|
+
pt = s(:defn, :x, s(:args, :y),
|
538
|
+
s(:scope,
|
539
|
+
s(:block,
|
540
|
+
s(:call, nil, :p, s(:arglist, s(:lvar, :y))),
|
541
|
+
s(:lasgn, :y,
|
542
|
+
s(:call, s(:lvar, :y), :*, s(:arglist, s(:lit, 2)))),
|
543
|
+
s(:return, s(:lvar, :y)))))
|
544
|
+
|
545
|
+
assert_parse_line rb, pt, 1
|
546
|
+
|
547
|
+
body = result.scope.block
|
548
|
+
assert_equal 2, body.call.line, "call should have line number"
|
549
|
+
assert_equal 3, body.lasgn.line, "lasgn should have line number"
|
550
|
+
assert_equal 4, body.return.line, "return should have line number"
|
551
|
+
end
|
552
|
+
|
553
|
+
def test_parse_line_iter_call_parens
|
495
554
|
rb = "f(a) do |x, y|\n x + y\nend"
|
496
555
|
|
497
556
|
pt = s(:iter,
|
@@ -499,18 +558,14 @@ class TestRubyParser < RubyParserTestCase
|
|
499
558
|
s(:masgn, s(:array, s(:lasgn, :x), s(:lasgn, :y))),
|
500
559
|
s(:call, s(:lvar, :x), :+, s(:arglist, s(:lvar, :y))))
|
501
560
|
|
502
|
-
|
503
|
-
|
504
|
-
assert_equal pt, result
|
561
|
+
assert_parse_line rb, pt, 1
|
505
562
|
|
506
563
|
assert_equal 1, result[1].line, "call should have line number"
|
507
|
-
assert_equal 1, result.line, "iter should have line number"
|
508
564
|
assert_equal 1, result[2].line, "masgn should have line number"
|
509
565
|
assert_equal 2, result[3].line, "call should have line number"
|
510
|
-
# flunk "not yet"
|
511
566
|
end
|
512
567
|
|
513
|
-
def
|
568
|
+
def test_parse_line_iter_call_no_parens
|
514
569
|
rb = "f a do |x, y|\n x + y\nend"
|
515
570
|
|
516
571
|
pt = s(:iter,
|
@@ -518,39 +573,14 @@ class TestRubyParser < RubyParserTestCase
|
|
518
573
|
s(:masgn, s(:array, s(:lasgn, :x), s(:lasgn, :y))),
|
519
574
|
s(:call, s(:lvar, :x), :+, s(:arglist, s(:lvar, :y))))
|
520
575
|
|
521
|
-
|
576
|
+
assert_parse_line rb, pt, 1
|
522
577
|
|
523
|
-
assert_equal pt, result
|
524
|
-
|
525
|
-
assert_equal 1, result.line, "iter should have line number"
|
526
578
|
assert_equal 1, result[1].line, "call should have line number"
|
527
579
|
assert_equal 1, result[2].line, "masgn should have line number"
|
528
580
|
assert_equal 2, result[3].line, "call should have line number"
|
529
581
|
end
|
530
582
|
|
531
|
-
def
|
532
|
-
rb = "def x(y)\n p(y)\n y *= 2\n return y;\nend" # TODO: remove () & ;
|
533
|
-
pt = s(:defn, :x, s(:args, :y),
|
534
|
-
s(:scope,
|
535
|
-
s(:block,
|
536
|
-
s(:call, nil, :p, s(:arglist, s(:lvar, :y))),
|
537
|
-
s(:lasgn, :y,
|
538
|
-
s(:call, s(:lvar, :y), :*, s(:arglist, s(:lit, 2)))),
|
539
|
-
s(:return, s(:lvar, :y)))))
|
540
|
-
|
541
|
-
result = @processor.parse(rb)
|
542
|
-
|
543
|
-
assert_equal pt, result
|
544
|
-
|
545
|
-
body = result.scope.block
|
546
|
-
|
547
|
-
assert_equal 1, result.line, "defn should have line number"
|
548
|
-
assert_equal 2, body.call.line, "call should have line number"
|
549
|
-
assert_equal 3, body.lasgn.line, "lasgn should have line number"
|
550
|
-
assert_equal 4, body.return.line, "return should have line number"
|
551
|
-
end
|
552
|
-
|
553
|
-
def test_position_info_heredoc
|
583
|
+
def test_parse_line_heredoc
|
554
584
|
rb = <<-CODE
|
555
585
|
string = <<-HEREDOC
|
556
586
|
very long string
|
@@ -558,11 +588,34 @@ class TestRubyParser < RubyParserTestCase
|
|
558
588
|
puts string
|
559
589
|
CODE
|
560
590
|
|
561
|
-
result =
|
591
|
+
result = processor.parse rb
|
562
592
|
assert_equal 1, result.lasgn.line
|
563
593
|
assert_equal 4, result.call.line
|
564
594
|
end
|
565
595
|
|
596
|
+
def test_parse_line_return
|
597
|
+
rb = <<-RUBY
|
598
|
+
def blah
|
599
|
+
if true then
|
600
|
+
return 42
|
601
|
+
end
|
602
|
+
end
|
603
|
+
RUBY
|
604
|
+
|
605
|
+
pt = s(:defn, :blah, s(:args),
|
606
|
+
s(:scope,
|
607
|
+
s(:block,
|
608
|
+
s(:if,
|
609
|
+
s(:true),
|
610
|
+
s(:return, s(:lit, 42)),
|
611
|
+
nil))))
|
612
|
+
|
613
|
+
assert_parse_line rb, pt, 1
|
614
|
+
|
615
|
+
assert_equal 3, result.scope.block.if.return.line
|
616
|
+
assert_equal 3, result.scope.block.if.return.lit.line
|
617
|
+
end
|
618
|
+
|
566
619
|
def test_parse_if_not_canonical
|
567
620
|
rb = "if not var.nil? then 'foo' else 'bar'\nend"
|
568
621
|
pt = s(:if,
|
@@ -570,7 +623,7 @@ class TestRubyParser < RubyParserTestCase
|
|
570
623
|
s(:str, "bar"),
|
571
624
|
s(:str, "foo"))
|
572
625
|
|
573
|
-
|
626
|
+
assert_parse rb, pt
|
574
627
|
end
|
575
628
|
|
576
629
|
def test_parse_if_not_noncanonical
|
@@ -581,9 +634,9 @@ class TestRubyParser < RubyParserTestCase
|
|
581
634
|
s(:str, "foo"),
|
582
635
|
s(:str, "bar"))
|
583
636
|
|
584
|
-
|
637
|
+
processor.canonicalize_conditions = false
|
585
638
|
|
586
|
-
|
639
|
+
assert_parse rb, pt
|
587
640
|
end
|
588
641
|
|
589
642
|
def test_parse_while_not_canonical
|
@@ -592,7 +645,7 @@ class TestRubyParser < RubyParserTestCase
|
|
592
645
|
s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist)),
|
593
646
|
s(:str, "foo"), true)
|
594
647
|
|
595
|
-
|
648
|
+
assert_parse rb, pt
|
596
649
|
end
|
597
650
|
|
598
651
|
def test_parse_while_not_noncanonical
|
@@ -602,9 +655,9 @@ class TestRubyParser < RubyParserTestCase
|
|
602
655
|
s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist))),
|
603
656
|
s(:str, "foo"), true)
|
604
657
|
|
605
|
-
|
658
|
+
processor.canonicalize_conditions = false
|
606
659
|
|
607
|
-
|
660
|
+
assert_parse rb, pt
|
608
661
|
end
|
609
662
|
|
610
663
|
def test_parse_until_not_canonical
|
@@ -614,7 +667,7 @@ class TestRubyParser < RubyParserTestCase
|
|
614
667
|
s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist)),
|
615
668
|
s(:str, "foo"), true)
|
616
669
|
|
617
|
-
|
670
|
+
assert_parse rb, pt
|
618
671
|
end
|
619
672
|
|
620
673
|
def test_parse_until_not_noncanonical
|
@@ -624,8 +677,8 @@ class TestRubyParser < RubyParserTestCase
|
|
624
677
|
s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist))),
|
625
678
|
s(:str, "foo"), true)
|
626
679
|
|
627
|
-
|
680
|
+
processor.canonicalize_conditions = false
|
628
681
|
|
629
|
-
|
682
|
+
assert_parse rb, pt
|
630
683
|
end
|
631
684
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 3
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 2
|
8
|
-
-
|
8
|
+
- 3
|
9
9
|
- 0
|
10
|
-
version: 2.
|
10
|
+
version: 2.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ryan Davis
|
@@ -36,7 +36,7 @@ cert_chain:
|
|
36
36
|
FBHgymkyj/AOSqKRIpXPhjC6
|
37
37
|
-----END CERTIFICATE-----
|
38
38
|
|
39
|
-
date: 2011-
|
39
|
+
date: 2011-09-06 00:00:00 Z
|
40
40
|
dependencies:
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: sexp_processor
|
@@ -181,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
181
181
|
requirements: []
|
182
182
|
|
183
183
|
rubyforge_project: parsetree
|
184
|
-
rubygems_version: 1.8.
|
184
|
+
rubygems_version: 1.8.10
|
185
185
|
signing_key:
|
186
186
|
specification_version: 3
|
187
187
|
summary: ruby_parser (RP) is a ruby parser written in pure ruby (utilizing racc--which does by default use a C extension)
|
metadata.gz.sig
CHANGED
Binary file
|