ruby_parser 3.8.4 → 3.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ruby_lexer.rb CHANGED
@@ -3,9 +3,9 @@
3
3
  class RubyLexer
4
4
 
5
5
  # :stopdoc:
6
- RUBY19 = "".respond_to? :encoding
6
+ HAS_ENC = "".respond_to? :encoding
7
7
 
8
- IDENT_CHAR = if RUBY19 then
8
+ IDENT_CHAR = if HAS_ENC then
9
9
  /[\w\u0080-\u{10ffff}]/u
10
10
  else
11
11
  /[\w\x80-\xFF]/n
@@ -285,7 +285,7 @@ class RubyLexer
285
285
  end
286
286
 
287
287
  def ruby22_label?
288
- ruby22? and is_label_possible?
288
+ ruby22plus? and is_label_possible?
289
289
  end
290
290
 
291
291
  def is_label_possible?
@@ -481,7 +481,7 @@ class RubyLexer
481
481
  return if in_lex_state?(:expr_beg, :expr_value, :expr_class,
482
482
  :expr_fname, :expr_dot, :expr_labelarg)
483
483
 
484
- if scan(/([\ \t\r\f\v]*)\./) then
484
+ if scan(/([\ \t\r\f\v]*)(\.|&)/) then
485
485
  self.space_seen = true unless ss[1].empty?
486
486
 
487
487
  ss.pos -= 1
@@ -778,7 +778,7 @@ class RubyLexer
778
778
  when lpar_beg && lpar_beg == paren_nest then
779
779
  self.lpar_beg = nil
780
780
  self.paren_nest -= 1
781
- result(state, :kDO_LAMBDA, value)
781
+ expr_result(:kDO_LAMBDA, value)
782
782
  when cond.is_in_state then
783
783
  result(state, :kDO_COND, value)
784
784
  when cmdarg.is_in_state && lex_state != :expr_cmdarg then
@@ -909,11 +909,7 @@ class RubyLexer
909
909
  end
910
910
 
911
911
  def ruby18
912
- Ruby18Parser === parser
913
- end
914
-
915
- def ruby19
916
- Ruby19Parser === parser
912
+ RubyParser::V18 === parser
917
913
  end
918
914
 
919
915
  def scan re
@@ -1047,7 +1043,7 @@ class RubyLexer
1047
1043
  t = Regexp.escape term
1048
1044
  x = Regexp.escape(paren) if paren && paren != "\000"
1049
1045
  re = if qwords then
1050
- if RUBY19 then
1046
+ if HAS_ENC then
1051
1047
  /[^#{t}#{x}\#\0\\\s]+|./ # |. to pick up whatever
1052
1048
  else
1053
1049
  /[^#{t}#{x}\#\0\\\s\v]+|./ # argh. 1.8's \s doesn't pick up \v
@@ -1098,7 +1094,7 @@ class RubyLexer
1098
1094
  else
1099
1095
  s
1100
1096
  end
1101
- x.force_encoding "UTF-8" if RUBY19
1097
+ x.force_encoding "UTF-8" if HAS_ENC
1102
1098
  x
1103
1099
  end
1104
1100
 
@@ -1106,9 +1102,8 @@ class RubyLexer
1106
1102
  # do nothing for now
1107
1103
  end
1108
1104
 
1109
- def ruby22?
1110
- Ruby22Parser === parser or
1111
- Ruby23Parser === parser
1105
+ def ruby22plus?
1106
+ parser.class.version >= 22
1112
1107
  end
1113
1108
 
1114
1109
  def process_string # TODO: rewrite / remove
@@ -1120,7 +1115,7 @@ class RubyLexer
1120
1115
 
1121
1116
  token_type, c = token
1122
1117
 
1123
- if ruby22? && token_type == :tSTRING_END && ["'", '"'].include?(c) then
1118
+ if ruby22plus? && token_type == :tSTRING_END && ["'", '"'].include?(c) then
1124
1119
  if (([:expr_beg, :expr_endfn].include?(lex_state) &&
1125
1120
  !cond.is_in_state) || is_arg?) &&
1126
1121
  is_label_suffix? then
data/lib/ruby_parser.rb CHANGED
@@ -1,7 +1,86 @@
1
- require 'ruby18_parser'
2
- require 'ruby19_parser'
3
- require 'ruby20_parser'
4
- require 'ruby21_parser'
5
- require 'ruby22_parser'
6
- require 'ruby23_parser'
7
- require 'ruby_parser_extras'
1
+ require "ruby_parser_extras"
2
+ require "racc/parser"
3
+
4
+ ##
5
+ # RubyParser is a compound parser that uses all known versions to
6
+ # attempt to parse.
7
+
8
+ class RubyParser
9
+
10
+ VERSIONS = []
11
+
12
+ class Parser < Racc::Parser
13
+ include RubyParserStuff
14
+
15
+ def self.inherited x
16
+ RubyParser::VERSIONS << x
17
+ end
18
+
19
+ def self.version
20
+ Parser > self and self.name[/V(\d+)$/, 1].to_i
21
+ end
22
+ end
23
+
24
+ class SyntaxError < RuntimeError; end
25
+
26
+ def process s, f = "(string)", t = 10
27
+ e = nil
28
+ VERSIONS.each do |klass|
29
+ parser = klass.new
30
+ begin
31
+ return parser.process s, f, t
32
+ rescue Racc::ParseError, RubyParser::SyntaxError => exc
33
+ e = exc
34
+ end
35
+ end
36
+ raise e
37
+ end
38
+
39
+ alias :parse :process
40
+
41
+ def reset
42
+ # do nothing
43
+ end
44
+
45
+ def self.latest
46
+ VERSIONS.first.new
47
+ end
48
+
49
+ def self.for_current_ruby
50
+ name = "V#{RUBY_VERSION[/^\d+\.\d+/].delete "."}"
51
+ klass = if const_defined? name then
52
+ const_get name
53
+ else
54
+ latest = VERSIONS.first
55
+ warn "NOTE: RubyParser::#{name} undefined, using #{latest}."
56
+ latest
57
+ end
58
+
59
+ klass.new
60
+ end
61
+ end
62
+
63
+ ##
64
+ # Unfortunately a problem with racc is that it won't let me namespace
65
+ # properly, so instead of RubyParser::V18, I still have to generate
66
+ # the old Ruby23Parser and shove it in as V23.
67
+
68
+ require "ruby18_parser"
69
+ require "ruby19_parser"
70
+ require "ruby20_parser"
71
+ require "ruby21_parser"
72
+ require "ruby22_parser"
73
+ require "ruby23_parser"
74
+ require "ruby24_parser"
75
+
76
+ class RubyParser # HACK
77
+ VERSIONS.clear # also a HACK caused by racc namespace issues
78
+
79
+ class V24 < ::Ruby24Parser; end
80
+ class V23 < ::Ruby23Parser; end
81
+ class V22 < ::Ruby22Parser; end
82
+ class V21 < ::Ruby21Parser; end
83
+ class V20 < ::Ruby20Parser; end
84
+ class V19 < ::Ruby19Parser; end
85
+ class V18 < ::Ruby18Parser; end
86
+ end
data/lib/ruby_parser.yy CHANGED
@@ -1,13 +1,17 @@
1
1
  # -*- racc -*-
2
2
 
3
- #if defined(RUBY20)
3
+ #if V==20
4
4
  class Ruby20Parser
5
- #elif defined(RUBY21)
5
+ #elif V==21
6
6
  class Ruby21Parser
7
- #elif defined(RUBY22)
7
+ #elif V == 22
8
8
  class Ruby22Parser
9
- #elif defined(RUBY23)
9
+ #elif V == 23
10
10
  class Ruby23Parser
11
+ #elif V == 24
12
+ class Ruby24Parser
13
+ #else
14
+ fail "version not specified or supported on code generation"
11
15
  #endif
12
16
 
13
17
  token kCLASS kMODULE kDEF kUNDEF kBEGIN kRESCUE kENSURE kEND kIF kUNLESS
@@ -27,13 +31,13 @@ token kCLASS kMODULE kDEF kUNDEF kBEGIN kRESCUE kENSURE kEND kIF kUNLESS
27
31
  tWORDS_BEG tQWORDS_BEG tSTRING_DBEG tSTRING_DVAR tSTRING_END
28
32
  tSTRING tSYMBOL tNL tEH tCOLON tCOMMA tSPACE tSEMI tLAMBDA
29
33
  tLAMBEG tDSTAR tCHAR tSYMBOLS_BEG tQSYMBOLS_BEG tSTRING_DEND tUBANG
30
- #if defined(RUBY21) || defined(RUBY22) || defined(RUBY23)
34
+ #if V >= 21
31
35
  tRATIONAL tIMAGINARY
32
36
  #endif
33
- #if defined(RUBY22) || defined(RUBY23)
37
+ #if V >= 22
34
38
  tLABEL_END
35
39
  #endif
36
- #if defined(RUBY23)
40
+ #if V >= 23
37
41
  tLONELY
38
42
  #endif
39
43
 
@@ -227,15 +231,15 @@ rule
227
231
  }
228
232
  | lhs tEQL mrhs
229
233
  {
230
- result = self.node_assign val[0], s(:svalue, val[2])
234
+ result = new_assign val[0], s(:svalue, val[2])
231
235
  }
232
- #if defined(RUBY20)
236
+ #if V == 20
233
237
  | mlhs tEQL arg_value
234
238
  {
235
239
  result = new_masgn val[0], val[2], :wrap
236
240
  }
237
241
  | mlhs tEQL mrhs
238
- #elif defined(RUBY21) || defined(RUBY22 || defined(RUBY23))
242
+ #else
239
243
  | mlhs tEQL mrhs_arg
240
244
  #endif
241
245
  {
@@ -245,21 +249,21 @@ rule
245
249
 
246
250
  command_asgn: lhs tEQL command_call
247
251
  {
248
- result = self.node_assign val[0], val[2]
252
+ result = new_assign val[0], val[2]
249
253
  }
250
254
  | lhs tEQL command_asgn
251
255
  {
252
- result = self.node_assign val[0], val[2]
256
+ result = new_assign val[0], val[2]
253
257
  }
254
258
 
255
259
  expr: command_call
256
260
  | expr kAND expr
257
261
  {
258
- result = logop(:and, val[0], val[2])
262
+ result = logical_op :and, val[0], val[2]
259
263
  }
260
264
  | expr kOR expr
261
265
  {
262
- result = logop(:or, val[0], val[2])
266
+ result = logical_op :or, val[0], val[2]
263
267
  }
264
268
  | kNOT opt_nl expr
265
269
  {
@@ -612,7 +616,7 @@ rule
612
616
  | tNEQ | tLSHFT | tRSHFT | tPLUS | tMINUS | tSTAR2
613
617
  | tSTAR | tDIVIDE | tPERCENT | tPOW | tDSTAR | tBANG | tTILDE
614
618
  | tUPLUS | tUMINUS | tAREF | tASET | tBACK_REF2
615
- #if defined(RUBY20)
619
+ #if V == 20
616
620
  | tUBANG
617
621
  #endif
618
622
 
@@ -628,11 +632,11 @@ rule
628
632
 
629
633
  arg: lhs tEQL arg
630
634
  {
631
- result = self.node_assign val[0], val[2]
635
+ result = new_assign val[0], val[2]
632
636
  }
633
637
  | lhs tEQL arg kRESCUE_MOD arg
634
638
  {
635
- result = self.node_assign val[0], s(:rescue, val[2], new_resbody(s(:array), val[4]))
639
+ result = new_assign val[0], s(:rescue, val[2], new_resbody(s(:array), val[4]))
636
640
  }
637
641
  | var_lhs tOP_ASGN arg
638
642
  {
@@ -714,18 +718,18 @@ rule
714
718
  {
715
719
  result = new_call val[0], :**, argl(val[2])
716
720
  }
717
- #if defined(RUBY20)
721
+ #if V == 20
718
722
  | tUMINUS_NUM tINTEGER tPOW arg
719
723
  {
720
724
  result = new_call(new_call(s(:lit, val[1]), :"**", argl(val[3])), :"-@")
721
725
  }
722
726
  | tUMINUS_NUM tFLOAT tPOW arg
723
- #elif defined(RUBY21) || defined(RUBY22) || defined(RUBY23)
727
+ #else
724
728
  | tUMINUS_NUM simple_numeric tPOW arg
725
729
  #endif
726
730
  {
727
731
  result = new_call(new_call(s(:lit, val[1]), :"**", argl(val[3])), :"-@")
728
- #if defined(RUBY20)
732
+ #if V == 20
729
733
  ## TODO: why is this 2.0 only?
730
734
  debug20 12, val, result
731
735
  #endif
@@ -784,11 +788,11 @@ rule
784
788
  }
785
789
  | arg tMATCH arg
786
790
  {
787
- result = self.get_match_node val[0], val[2]
791
+ result = new_match val[0], val[2]
788
792
  }
789
793
  | arg tNMATCH arg
790
794
  {
791
- result = s(:not, self.get_match_node(val[0], val[2]))
795
+ result = s(:not, new_match(val[0], val[2]))
792
796
  }
793
797
  | tBANG arg
794
798
  {
@@ -812,11 +816,11 @@ rule
812
816
  }
813
817
  | arg tANDOP arg
814
818
  {
815
- result = logop(:and, val[0], val[2])
819
+ result = logical_op :and, val[0], val[2]
816
820
  }
817
821
  | arg tOROP arg
818
822
  {
819
- result = logop(:or, val[0], val[2])
823
+ result = logical_op :or, val[0], val[2]
820
824
  }
821
825
  | kDEFINED opt_nl arg
822
826
  {
@@ -939,7 +943,7 @@ rule
939
943
  result = self.list_append val[0], s(:splat, val[3])
940
944
  }
941
945
 
942
- #if defined(RUBY21) || defined(RUBY22 || defined(RUBY23))
946
+ #if V >= 21
943
947
  mrhs_arg: mrhs
944
948
  {
945
949
  result = new_masgn_arg val[0]
@@ -1022,9 +1026,13 @@ rule
1022
1026
  result = val[1] || s(:array)
1023
1027
  result[0] = :array # aref_args is :args
1024
1028
  }
1025
- | tLBRACE assoc_list tRCURLY
1029
+ | tLBRACE
1030
+ {
1031
+ result = self.lexer.lineno
1032
+ }
1033
+ assoc_list tRCURLY
1026
1034
  {
1027
- result = s(:hash, *val[1].values) # TODO: array_to_hash?
1035
+ result = new_hash val
1028
1036
  }
1029
1037
  | kRETURN
1030
1038
  {
@@ -1332,7 +1340,6 @@ rule
1332
1340
  args, _, _, _, args2 = val
1333
1341
 
1334
1342
  result = block_var args, :*, args2
1335
- debug20 16, val, result
1336
1343
  }
1337
1344
  | tSTAR f_norm_arg
1338
1345
  {
@@ -1345,12 +1352,10 @@ rule
1345
1352
  _, splat, _, args = val
1346
1353
 
1347
1354
  result = block_var :"*#{splat}", args
1348
- debug20 17, val, result
1349
1355
  }
1350
1356
  | tSTAR
1351
1357
  {
1352
1358
  result = block_var :*
1353
- debug20 18, val, result
1354
1359
  }
1355
1360
  | tSTAR tCOMMA f_marg_list
1356
1361
  {
@@ -1671,7 +1676,7 @@ opt_block_args_tail: tCOMMA block_args_tail
1671
1676
  _, klasses, var, _, body, rest = val
1672
1677
 
1673
1678
  klasses ||= s(:array)
1674
- klasses << node_assign(var, s(:gvar, :"$!")) if var
1679
+ klasses << new_assign(var, s(:gvar, :"$!")) if var
1675
1680
 
1676
1681
  result = new_resbody(klasses, body)
1677
1682
  result << rest if rest # UGH, rewritten above
@@ -1886,9 +1891,9 @@ regexp_contents: none
1886
1891
  }
1887
1892
  compstmt tRCURLY
1888
1893
  {
1889
- #if defined(RUBY20)
1894
+ #if V == 20
1890
1895
  # TODO: tRCURLY -> tSTRING_DEND
1891
- #elif defined(RUBY21) || defined(RUBY22 || defined(RUBY23))
1896
+ #else
1892
1897
  # TODO: tRCURLY -> tSTRING_END
1893
1898
  #endif
1894
1899
  _, memo, stmt, _ = val
@@ -1956,17 +1961,17 @@ regexp_contents: none
1956
1961
  end
1957
1962
  }
1958
1963
 
1959
- #if defined(RUBY20)
1964
+ #if V == 20
1960
1965
  numeric: tINTEGER
1961
1966
  | tFLOAT
1962
1967
  | tUMINUS_NUM tINTEGER =tLOWEST
1963
- #elif defined(RUBY21) || defined(RUBY22) || defined(RUBY23)
1968
+ #else
1964
1969
  numeric: simple_numeric
1965
1970
  | tUMINUS_NUM simple_numeric
1966
1971
  #endif
1967
1972
  {
1968
1973
  result = -val[1] # TODO: pt_testcase
1969
- #if defined(RUBY20)
1974
+ #if V == 20
1970
1975
  }
1971
1976
  | tUMINUS_NUM tFLOAT =tLOWEST
1972
1977
  {
@@ -1974,7 +1979,7 @@ regexp_contents: none
1974
1979
  #endif
1975
1980
  }
1976
1981
 
1977
- #if defined(RUBY21) || defined(RUBY22) || defined(RUBY23)
1982
+ #if V >= 21
1978
1983
  simple_numeric: tINTEGER
1979
1984
  | tFLOAT
1980
1985
  | tRATIONAL
@@ -2173,7 +2178,7 @@ keyword_variable: kNIL { result = s(:nil) }
2173
2178
  result = identifier
2174
2179
  }
2175
2180
 
2176
- #if defined(RUBY22) || defined(RUBY23)
2181
+ #if V >= 22
2177
2182
  f_arg_asgn: f_norm_arg
2178
2183
 
2179
2184
  f_arg_item: f_arg_asgn
@@ -2215,9 +2220,9 @@ keyword_variable: kNIL { result = s(:nil) }
2215
2220
  result << item
2216
2221
  }
2217
2222
 
2218
- #if defined(RUBY20)
2223
+ #if V == 20
2219
2224
  f_kw: tLABEL arg_value
2220
- #elif defined(RUBY21) || defined(RUBY22) || defined(RUBY23)
2225
+ #else
2221
2226
  f_label: tLABEL
2222
2227
 
2223
2228
  f_kw: f_label arg_value
@@ -2230,7 +2235,7 @@ keyword_variable: kNIL { result = s(:nil) }
2230
2235
 
2231
2236
  result = s(:array, s(:kwarg, identifier, val[1]))
2232
2237
  }
2233
- #if defined(RUBY21) || defined(RUBY22) || defined(RUBY23)
2238
+ #if V >= 21
2234
2239
  | f_label
2235
2240
  {
2236
2241
  label, _ = val[0] # TODO: fix lineno?
@@ -2241,9 +2246,9 @@ keyword_variable: kNIL { result = s(:nil) }
2241
2246
  }
2242
2247
  #endif
2243
2248
 
2244
- #if defined(RUBY20)
2249
+ #if V == 20
2245
2250
  f_block_kw: tLABEL primary_value
2246
- #elif defined(RUBY21) || defined(RUBY22) || defined(RUBY23)
2251
+ #else
2247
2252
  f_block_kw: f_label primary_value
2248
2253
  #endif
2249
2254
  {
@@ -2254,7 +2259,7 @@ keyword_variable: kNIL { result = s(:nil) }
2254
2259
 
2255
2260
  result = s(:array, s(:kwarg, identifier, val[1]))
2256
2261
  }
2257
- #if defined(RUBY21) || defined(RUBY22) || defined(RUBY23)
2262
+ #if V >= 21
2258
2263
  | f_label
2259
2264
  {
2260
2265
  label, _ = val[0] # TODO: fix lineno?
@@ -2290,11 +2295,11 @@ keyword_variable: kNIL { result = s(:nil) }
2290
2295
  result = :"**"
2291
2296
  }
2292
2297
 
2293
- #if defined(RUBY20)
2298
+ #if V == 20
2294
2299
  f_opt: tIDENTIFIER tEQL arg_value
2295
- #elif defined(RUBY21)
2300
+ #elif V == 21
2296
2301
  f_opt: f_norm_arg tEQL arg_value
2297
- #elif defined(RUBY22) || defined(RUBY23)
2302
+ #else
2298
2303
  f_opt: f_arg_asgn tEQL arg_value
2299
2304
  #endif
2300
2305
  {
@@ -2302,11 +2307,11 @@ keyword_variable: kNIL { result = s(:nil) }
2302
2307
  # TODO: detect duplicate names
2303
2308
  }
2304
2309
 
2305
- #if defined(RUBY20)
2310
+ #if V == 20
2306
2311
  f_block_opt: tIDENTIFIER tEQL primary_value
2307
- #elif defined(RUBY21)
2312
+ #elif V == 21
2308
2313
  f_block_opt: f_norm_arg tEQL primary_value
2309
- #elif defined(RUBY22) || defined(RUBY23)
2314
+ #else
2310
2315
  f_block_opt: f_arg_asgn tEQL primary_value
2311
2316
  #endif
2312
2317
  {
@@ -2396,7 +2401,6 @@ keyword_variable: kNIL { result = s(:nil) }
2396
2401
  list.push(*more) unless more.empty?
2397
2402
  result = list
2398
2403
  result[0] = :hash
2399
- # TODO: shouldn't this be a hash?
2400
2404
  }
2401
2405
 
2402
2406
  assoc: arg_value tASSOC arg_value
@@ -2407,7 +2411,7 @@ keyword_variable: kNIL { result = s(:nil) }
2407
2411
  {
2408
2412
  result = s(:array, s(:lit, val[0][0].to_sym), val[1])
2409
2413
  }
2410
- #if defined(RUBY22) || defined(RUBY23)
2414
+ #if V >= 22
2411
2415
  | tSTRING_BEG string_contents tLABEL_END arg_value
2412
2416
  {
2413
2417
  _, sym, _, value = val
@@ -2430,7 +2434,7 @@ keyword_variable: kNIL { result = s(:nil) }
2430
2434
  operation3: tIDENTIFIER | tFID | op
2431
2435
  dot_or_colon: tDOT | tCOLON2
2432
2436
  call_op: tDOT
2433
- #if defined(RUBY23)
2437
+ #if V >= 23
2434
2438
  | tLONELY
2435
2439
  #endif
2436
2440
  opt_terms: | terms