Module: RubyParserStuff
- Included in:
- RubyParser::Parser
- Defined in:
- lib/ruby_parser_extras.rb
Defined Under Namespace
Classes: Environment, Keyword, StackState
Constant Summary collapse
- VERSION =
"3.19.1"- ARG_TYPES =
[:arglist, :call_args, :array, :args].map { |k| [k, true] }.to_h
- ENCODING_ORDER =
This is in sorted order of occurrence according to charlock_holmes against 500k files, with UTF_8 forced to the top.
Overwrite this contstant if you need something different.
[ Encoding::UTF_8, # moved to top to reflect default in 2.0 Encoding::ISO_8859_1, Encoding::ISO_8859_2, Encoding::ISO_8859_9, Encoding::SHIFT_JIS, Encoding::WINDOWS_1252, Encoding::EUC_JP ]
- JUMP_TYPE =
[:return, :next, :break, :yield].map { |k| [k, true] }.to_h
- TAB_WIDTH =
8
Instance Attribute Summary collapse
-
#canonicalize_conditions ⇒ Object
Canonicalize conditionals.
-
#comments ⇒ Object
readonly
Returns the value of attribute comments.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#file ⇒ Object
Returns the value of attribute file.
-
#in_def ⇒ Object
Returns the value of attribute in_def.
-
#in_kwarg ⇒ Object
Returns the value of attribute in_kwarg.
-
#in_single ⇒ Object
Returns the value of attribute in_single.
-
#last_token_type ⇒ Object
The last token type returned from #next_token.
-
#lexer ⇒ Object
Returns the value of attribute lexer.
Class Method Summary collapse
Instance Method Summary collapse
- #_racc_do_reduce(arg, act) ⇒ Object
-
#arg_concat(node1, node2) ⇒ Object
TODO: nuke.
- #argl(x) ⇒ Object
- #args(args) ⇒ Object
- #array_pat_concat(lhs, rhs) ⇒ Object
- #array_to_hash(array) ⇒ Object
- #aryset(receiver, index) ⇒ Object
- #assignable(lhs, value = nil) ⇒ Object
- #attrset_id?(id) ⇒ Boolean
- #backref_assign_error(ref) ⇒ Object
- #block_append(head, tail) ⇒ Object
- #block_dup_check(call_or_args, block) ⇒ Object
- #block_var(*args) ⇒ Object
- #call_args(args) ⇒ Object
- #clean_mlhs(sexp) ⇒ Object
- #cond(node) ⇒ Object
- #debug(n) ⇒ Object
- #debug20(n, v = nil, r = nil) ⇒ Object
- #dedent(sexp) ⇒ Object
- #dedent_size(sexp) ⇒ Object
- #dedent_string(string, width) ⇒ Object
-
#do_parse ⇒ Object
for pure ruby systems only.
- #end_args(args) ⇒ Object
- #endless_method_name(defn_or_defs) ⇒ Object
- #gettable(id) ⇒ Object
- #hack_encoding(str, extra = nil) ⇒ Object
-
#handle_encoding(str) ⇒ Object
Returns a UTF-8 encoded string after processing BOMs and magic encoding comments.
- #initialize(options = {}) ⇒ Object
- #invert_block_call(val) ⇒ Object
- #inverted?(val) ⇒ Boolean
-
#list_append(list, item) ⇒ Object
TODO: nuke me sigh.
-
#list_prepend(item, list) ⇒ Object
TODO: nuke me sigh.
-
#literal_concat(head, tail) ⇒ Object
TODO: ugh.
- #local_pop(in_def) ⇒ Object
- #logical_op(type, left, right) ⇒ Object
- #new_aref(val) ⇒ Object
- #new_arg(val) ⇒ Object
- #new_array_pattern(const, pre_arg, arypat, loc) ⇒ Object
- #new_array_pattern_tail(pre_args, has_rest, rest_arg, post_args) ⇒ Object
- #new_assign(lhs, rhs) ⇒ Object
- #new_attrasgn(recv, meth, call_op = :".") ⇒ Object
- #new_begin(val) ⇒ Object
- #new_body(val) ⇒ Object
- #new_brace_body(args, body, lineno) ⇒ Object
- #new_call(recv, meth, args = nil, call_op = :".") ⇒ Object
- #new_case(expr, body, line) ⇒ Object
- #new_class(val) ⇒ Object
- #new_compstmt(val) ⇒ Object
- #new_const_op_asgn(val) ⇒ Object
- #new_defn(val) ⇒ Object
- #new_defs(val) ⇒ Object
- #new_do_body(args, body, lineno) ⇒ Object
- #new_endless_defn(val) ⇒ Object
- #new_endless_defs(val) ⇒ Object
- #new_find_pattern(const, pat) ⇒ Object
- #new_find_pattern_tail(lhs, mid, rhs) ⇒ Object
- #new_for(expr, var, body) ⇒ Object
- #new_hash(val) ⇒ Object
- #new_hash_pattern(const, hash_pat, loc) ⇒ Object
-
#new_hash_pattern_tail(kw_args, kw_rest_arg, line) ⇒ Object
TODO: remove line arg.
- #new_if(c, t, f) ⇒ Object
- #new_in(pat, body, cases, line) ⇒ Object
- #new_iter(call, args, body) ⇒ Object
- #new_masgn(lhs, rhs, wrap = false) ⇒ Object
- #new_masgn_arg(rhs, wrap = false) ⇒ Object
- #new_match(lhs, rhs) ⇒ Object
- #new_module(val) ⇒ Object
- #new_op_asgn(val) ⇒ Object
- #new_op_asgn1(val) ⇒ Object
- #new_op_asgn2(val) ⇒ Object
- #new_qsym_list ⇒ Object
- #new_qsym_list_entry(val) ⇒ Object
- #new_qword_list ⇒ Object
- #new_qword_list_entry(val) ⇒ Object
- #new_regexp(val) ⇒ Object
- #new_resbody(cond, body) ⇒ Object
- #new_rescue(body, resbody) ⇒ Object
- #new_sclass(val) ⇒ Object
- #new_string(val) ⇒ Object
- #new_super(args) ⇒ Object
- #new_symbol(val) ⇒ Object
- #new_symbol_list ⇒ Object
- #new_symbol_list_entry(val) ⇒ Object
- #new_undef(n, m = nil) ⇒ Object
- #new_until(block, expr, pre) ⇒ Object
- #new_until_or_while(type, block, expr, pre) ⇒ Object
- #new_when(cond, body) ⇒ Object
- #new_while(block, expr, pre) ⇒ Object
- #new_word_list ⇒ Object
- #new_word_list_entry(val) ⇒ Object
- #new_xstring(val) ⇒ Object
- #new_yield(args = nil) ⇒ Object
- #next_token ⇒ Object
- #on_error(et, ev, values) ⇒ Object
- #pop_pktbl ⇒ Object
- #pop_pvtbl ⇒ Object
- #prev_value_to_lineno(v) ⇒ Object
-
#process(str, file = "(string)", time = 10) ⇒ Object
(also: #parse)
Parse
strat pathfileand return a sexp. - #push_pktbl ⇒ Object
- #push_pvtbl ⇒ Object
- #remove_begin(node) ⇒ Object (also: #value_expr)
- #reset ⇒ Object
- #ret_args(node) ⇒ Object
- #s(*args) ⇒ Object
- #syntax_error(msg) ⇒ Object (also: #yyerror)
- #void_stmts(node) ⇒ Object
- #warning(s) ⇒ Object
- #whitespace_width(line, remove_width = nil) ⇒ Object (also: #remove_whitespace_width)
- #wrap(type, node) ⇒ Object
Instance Attribute Details
#canonicalize_conditions ⇒ Object
Canonicalize conditionals. Eg:
not x ? a : b
becomes:
x ? b : a
48 49 50 |
# File 'lib/ruby_parser_extras.rb', line 48 def canonicalize_conditions @canonicalize_conditions end |
#comments ⇒ Object (readonly)
Returns the value of attribute comments.
37 38 39 |
# File 'lib/ruby_parser_extras.rb', line 37 def comments @comments end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
37 38 39 |
# File 'lib/ruby_parser_extras.rb', line 37 def env @env end |
#file ⇒ Object
Returns the value of attribute file.
35 36 37 |
# File 'lib/ruby_parser_extras.rb', line 35 def file @file end |
#in_def ⇒ Object
Returns the value of attribute in_def.
35 36 37 |
# File 'lib/ruby_parser_extras.rb', line 35 def in_def @in_def end |
#in_kwarg ⇒ Object
Returns the value of attribute in_kwarg.
36 37 38 |
# File 'lib/ruby_parser_extras.rb', line 36 def in_kwarg @in_kwarg end |
#in_single ⇒ Object
Returns the value of attribute in_single.
35 36 37 |
# File 'lib/ruby_parser_extras.rb', line 35 def in_single @in_single end |
#last_token_type ⇒ Object
The last token type returned from #next_token
53 54 55 |
# File 'lib/ruby_parser_extras.rb', line 53 def last_token_type @last_token_type end |
#lexer ⇒ Object
Returns the value of attribute lexer.
35 36 37 |
# File 'lib/ruby_parser_extras.rb', line 35 def lexer @lexer end |
Class Method Details
.deprecate(old, new) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/ruby_parser_extras.rb', line 66 def self.deprecate old, new define_method old do |*args| warn "DEPRECATED: #{old} -> #{new} from #{caller.first}" send new, *args end end |
Instance Method Details
#_racc_do_reduce(arg, act) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/ruby_parser_extras.rb', line 81 def _racc_do_reduce arg, act x = super @racc_vstack.grep(Sexp).each do |sexp| sexp.check_line_numbers end x end |
#arg_concat(node1, node2) ⇒ Object
TODO: nuke
134 135 136 137 138 139 140 141 |
# File 'lib/ruby_parser_extras.rb', line 134 def arg_concat node1, node2 # TODO: nuke raise "huh" unless node2 splat = s(:splat, node2) splat.line node2.line node1 << splat end |
#argl(x) ⇒ Object
143 144 145 146 |
# File 'lib/ruby_parser_extras.rb', line 143 def argl x x = s(:arglist, x) if x and x.sexp_type == :array x end |
#args(args) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/ruby_parser_extras.rb', line 148 def args args result = s(:args) ss = args.grep Sexp if ss.empty? then result.line lexer.lineno else result.line ss.first.line end args.each do |arg| if arg.instance_of? Array and arg.size == 2 and arg.last.is_a? Numeric then arg = arg.first end case arg when Sexp then case arg.sexp_type when :args, :block, :array, :call_args then # HACK call_args mismatch rest = arg.sexp_body rest.map! { |x| if x.instance_of? Array and x.size == 2 and Numeric === x.last then x.first else x end } result.concat rest when :forward_args then self.env[:*] = :lvar # TODO: arg_var(p, idFWD_REST) ? self.env[:**] = :lvar self.env[:&] = :lvar result << arg when :block_arg then result << :"&#{arg.last}" when :shadow then name = arg.last self.env[name] = :lvar if Sexp === result.last and result.last.sexp_type == :shadow then result.last << name else result << arg end when :masgn, :block_pass, :hash then # HACK: remove. prolly call_args result << arg else raise "unhandled: #{arg.sexp_type} in #{args.inspect}" end when Symbol then name = arg.to_s.delete("&*") self.env[name.to_sym] = :lvar unless name.empty? result << arg when true, false then self.in_kwarg = arg when ",", "|", ";", "(", ")", nil then # ignore else raise "unhandled: #{arg.inspect} in #{args.inspect}" end end result end |
#array_pat_concat(lhs, rhs) ⇒ Object
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 |
# File 'lib/ruby_parser_extras.rb', line 747 def array_pat_concat lhs, rhs case lhs.sexp_type when :PATTERN then lhs.sexp_type = :array_pat end if rhs then case rhs.sexp_type when :array_pat, :array_TAIL, :PATTERN then lhs.concat rhs.sexp_body else lhs << rhs end end end |
#array_to_hash(array) ⇒ Object
236 237 238 239 240 241 242 243 |
# File 'lib/ruby_parser_extras.rb', line 236 def array_to_hash array case array.sexp_type when :kwsplat then array else s(:hash, *array.sexp_body).line array.line end end |
#aryset(receiver, index) ⇒ Object
245 246 247 248 249 250 251 252 |
# File 'lib/ruby_parser_extras.rb', line 245 def aryset receiver, index index ||= s() l = receiver.line result = s(:attrasgn, receiver, :"[]=", *index.sexp_body).compact # [].sexp_body => nil result.line = l result end |
#assignable(lhs, value = nil) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/ruby_parser_extras.rb', line 254 def assignable(lhs, value = nil) id, line = lhs id = id.to_sym result = case id when /^@@/ then asgn = in_def || in_single > 0 s((asgn ? :cvasgn : :cvdecl), id) when /^@/ then s(:iasgn, id) when /^\$/ then s(:gasgn, id) when /^[A-Z]/ then s(:cdecl, id) else case self.env[id] when :lvar, :dvar, nil then s(:lasgn, id) else raise "wtf? unknown type: #{self.env[id]}" end end self.env[id] ||= :lvar if result.sexp_type == :lasgn result << value if value result.line line result end |
#attrset_id?(id) ⇒ Boolean
221 222 223 |
# File 'lib/ruby_parser_extras.rb', line 221 def attrset_id? id id =~ /^\[\]=$|^\w+=$/ end |
#backref_assign_error(ref) ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/ruby_parser_extras.rb', line 284 def backref_assign_error ref # TODO: need a test for this... obviously case ref.sexp_type when :nth_ref then raise "write a test 2" raise SyntaxError, "Can't set variable %p" % ref.last when :back_ref then raise "write a test 3" raise SyntaxError, "Can't set back reference %p" % ref.last else raise "Unknown backref type: #{ref.inspect}" end end |
#block_append(head, tail) ⇒ Object
298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/ruby_parser_extras.rb', line 298 def block_append(head, tail) return head if tail.nil? return tail if head.nil? line = [head.line, tail.line].compact.min head = remove_begin(head) head = s(:block, head).line(line) unless head.sexp_type == :block # head.line = line head << tail end |
#block_dup_check(call_or_args, block) ⇒ Object
311 312 313 314 |
# File 'lib/ruby_parser_extras.rb', line 311 def block_dup_check call_or_args, block syntax_error "Both block arg and actual block given." if block and call_or_args.block_pass? end |
#block_var(*args) ⇒ Object
316 317 318 319 320 |
# File 'lib/ruby_parser_extras.rb', line 316 def block_var *args result = self.args args result.sexp_type = :masgn result end |
#call_args(args) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/ruby_parser_extras.rb', line 322 def call_args args result = s(:call_args) a = args.grep(Sexp).first if a then result.line a.line else result.line lexer.lineno end args.each do |arg| if arg.instance_of? Array and arg.size == 2 and arg.last.is_a? Numeric then arg = arg.first end case arg when Sexp then case arg.sexp_type when :array, :args, :call_args then # HACK? remove array at some point result.concat arg.sexp_body else result << arg end when Symbol then result << arg when Array then id, _line = arg result << id when ",", nil, "(" then # ignore else raise "unhandled: #{arg.inspect} in #{args.inspect}" end end result end |
#clean_mlhs(sexp) ⇒ Object
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/ruby_parser_extras.rb', line 360 def clean_mlhs sexp case sexp.sexp_type when :masgn then if sexp.size == 2 and sexp[1].sexp_type == :array then s(:masgn, *sexp[1].sexp_body.map { |sub| clean_mlhs sub }) else debug20 5 sexp end when :gasgn, :iasgn, :lasgn, :cvasgn then if sexp.size == 2 then sexp.last else debug20 7 sexp # optional value end else raise "unsupported type: #{sexp.inspect}" end end |
#cond(node) ⇒ Object
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/ruby_parser_extras.rb', line 381 def cond node return nil if node.nil? node = value_expr node case node.sexp_type when :lit then if Regexp === node.last then s(:match, node) else node end when :and then _, lhs, rhs = node s(:and, cond(lhs), cond(rhs)) when :or then _, lhs, rhs = node s(:or, cond(lhs), cond(rhs)) when :dot2 then label = "flip#{node.hash}" env[label] = :lvar _, lhs, rhs = node s(:flip2, lhs, rhs) # TODO: recurse? when :dot3 then label = "flip#{node.hash}" env[label] = :lvar _, lhs, rhs = node s(:flip3, lhs, rhs) else node end.line node.line end |
#debug(n) ⇒ Object
1602 1603 1604 1605 1606 1607 1608 |
# File 'lib/ruby_parser_extras.rb', line 1602 def debug n if ENV["PRY"] then require "pry"; binding.pry end raise RubyParser::SyntaxError, "debug #{n}" end |
#debug20(n, v = nil, r = nil) ⇒ Object
62 63 64 |
# File 'lib/ruby_parser_extras.rb', line 62 def debug20 n, v = nil, r = nil raise "not yet #{n} #{v.inspect} => #{r.inspect}" unless $good20[n] end |
#dedent(sexp) ⇒ Object
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'lib/ruby_parser_extras.rb', line 413 def dedent sexp dedent_count = dedent_size sexp skip_one = false sexp.map { |obj| case obj when Symbol then obj when String then obj.lines.map { |l| remove_whitespace_width l, dedent_count }.join when Sexp then case obj.sexp_type when :evstr then skip_one = true obj when :str then _, str = obj str = if skip_one then skip_one = false s1, *rest = str.lines s1 + rest.map { |l| remove_whitespace_width l, dedent_count }.join else str.lines.map { |l| remove_whitespace_width l, dedent_count }.join end s(:str, str).line obj.line else warn "unprocessed sexp %p" % [obj] end else warn "unprocessed: %p" % [obj] end } end |
#dedent_size(sexp) ⇒ Object
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/ruby_parser_extras.rb', line 448 def dedent_size sexp skip_one = false sexp.flat_map { |s| case s when Symbol then next when String then s.lines when Sexp then case s.sexp_type when :evstr then skip_one = true next when :str then _, str = s lines = str.lines if skip_one then skip_one = false lines.shift end lines else warn "unprocessed sexp %p" % [s] end else warn "unprocessed: %p" % [s] end.map { |l| whitespace_width l.chomp } }.compact.min end |
#dedent_string(string, width) ⇒ Object
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
# File 'lib/ruby_parser_extras.rb', line 478 def dedent_string string, width characters_skipped = 0 indentation_skipped = 0 string.chars.each do |char| break if indentation_skipped >= width if char == " " characters_skipped += 1 indentation_skipped += 1 elsif char == "\t" proposed = TAB_WIDTH * (indentation_skipped / TAB_WIDTH + 1) break if proposed > width characters_skipped += 1 indentation_skipped = proposed end end string[characters_skipped..-1] end |
#do_parse ⇒ Object
for pure ruby systems only
76 77 78 |
# File 'lib/ruby_parser_extras.rb', line 76 def do_parse _racc_do_parse_rb(_racc_setup, false) end |
#end_args(args) ⇒ Object
215 216 217 218 219 |
# File 'lib/ruby_parser_extras.rb', line 215 def end_args args lexer.lex_state = RubyLexer::State::Values::EXPR_BEG lexer.command_start = true self.args args end |
#endless_method_name(defn_or_defs) ⇒ Object
225 226 227 228 229 230 231 232 233 234 |
# File 'lib/ruby_parser_extras.rb', line 225 def endless_method_name defn_or_defs name = defn_or_defs[1] name = defn_or_defs[2] unless Symbol === name if attrset_id? name then yyerror "setter method cannot be defined in an endless method definition" end # TODO? token_info_drop(p, "def", loc->beg_pos); end |
#gettable(id) ⇒ Object
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/ruby_parser_extras.rb', line 497 def gettable(id) id = id.to_sym if String === id result = case id.to_s when /^@@/ then s(:cvar, id) when /^@/ then s(:ivar, id) when /^\$/ then s(:gvar, id) when /^[A-Z]/ then s(:const, id) else type = env[id] if type then s(type, id) else new_call(nil, id) end end raise "identifier #{id.inspect} is not valid" unless result result end |
#hack_encoding(str, extra = nil) ⇒ Object
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
# File 'lib/ruby_parser_extras.rb', line 523 def hack_encoding str, extra = nil encodings = ENCODING_ORDER.dup encodings.unshift(extra) unless extra.nil? # terrible, horrible, no good, very bad, last ditch effort. encodings.each do |enc| begin str.force_encoding enc if str.valid_encoding? then str.encode! Encoding::UTF_8 break end rescue ArgumentError # unknown encoding name # do nothing rescue Encoding::InvalidByteSequenceError # do nothing rescue Encoding::UndefinedConversionError # do nothing end end # no amount of pain is enough for you. raise "Bad encoding. Need a magic encoding comment." unless str.encoding.name == "UTF-8" end |
#handle_encoding(str) ⇒ Object
Returns a UTF-8 encoded string after processing BOMs and magic encoding comments.
Holy crap… ok. Here goes:
Ruby’s file handling and encoding support is insane. We need to be able to lex a file. The lexer file is explicitly UTF-8 to make things cleaner. This allows us to deal with extended chars in class and method names. In order to do this, we need to encode all input source files as UTF-8. First, we look for a UTF-8 BOM by looking at the first line while forcing its encoding to ASCII-8BIT. If we find a BOM, we strip it and set the expected encoding to UTF-8. Then, we search for a magic encoding comment. If found, it overrides the BOM. Finally, we force the encoding of the input string to whatever was found, and then encode that to UTF-8 for compatibility with the lexer.
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 |
# File 'lib/ruby_parser_extras.rb', line 567 def handle_encoding str str = str.dup has_enc = str.respond_to? :encoding encoding = nil header = str.each_line.first(2) header.map! { |s| s.force_encoding "ASCII-8BIT" } if has_enc first = header.first || "" encoding, str = +"utf-8", str.b[3..-1] if first =~ /\A\xEF\xBB\xBF/ encoding = $1.strip if header.find { |s| s[/^#.*?-\*-.*?coding:\s*([^ ;]+).*?-\*-/, 1] || s[/^#.*(?:en)?coding(?:\s*[:=])\s*([\w-]+)/, 1] } if encoding then if has_enc then encoding.sub!(/utf-8-.+$/, "utf-8") # HACK for stupid emacs formats hack_encoding str, encoding else warn "Skipping magic encoding comment" end else # nothing specified... ugh. try to encode as utf-8 hack_encoding str if has_enc end str end |
#initialize(options = {}) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/ruby_parser_extras.rb', line 116 def initialize( = {}) super() v = self.class.name[/[23]\d/] raise "Bad Class name #{self.class}" unless v self.lexer = RubyLexer.new v && v.to_i self.lexer.parser = self self.in_kwarg = false @env = RubyParserStuff::Environment.new @comments = [] @canonicalize_conditions = true self.reset end |
#invert_block_call(val) ⇒ Object
598 599 600 601 602 603 604 605 606 607 |
# File 'lib/ruby_parser_extras.rb', line 598 def invert_block_call val ret, iter = val type, call = ret iter.insert 1, call ret = s(type).line ret.line [iter, ret] end |
#inverted?(val) ⇒ Boolean
609 610 611 |
# File 'lib/ruby_parser_extras.rb', line 609 def inverted? val JUMP_TYPE[val[0].sexp_type] end |
#list_append(list, item) ⇒ Object
TODO: nuke me sigh
613 614 615 616 617 |
# File 'lib/ruby_parser_extras.rb', line 613 def list_append list, item # TODO: nuke me *sigh* return s(:array, item) unless list list = s(:array, list) unless Sexp === list && list.sexp_type == :array list << item end |
#list_prepend(item, list) ⇒ Object
TODO: nuke me sigh
619 620 621 622 623 |
# File 'lib/ruby_parser_extras.rb', line 619 def list_prepend item, list # TODO: nuke me *sigh* list = s(:array, list) unless Sexp === list && list.sexp_type == :array list.insert 1, item list end |
#literal_concat(head, tail) ⇒ Object
TODO: ugh. rewrite
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 |
# File 'lib/ruby_parser_extras.rb', line 625 def literal_concat head, tail # TODO: ugh. rewrite return tail unless head return head unless tail htype, ttype = head.sexp_type, tail.sexp_type head = s(:dstr, "", head).line head.line if htype == :evstr case ttype when :str then if htype == :str a, b = head.last, tail.last b = b.dup.force_encoding a.encoding unless Encoding.compatible?(a, b) a << b elsif htype == :dstr and head.size == 2 then head.last << tail.last else head << tail end when :dstr then if htype == :str then lineno = head.line tail[1] = head.last + tail[1] head = tail head.line = lineno else tail.sexp_type = :array tail[1] = s(:str, tail[1]).line tail.line tail.delete_at 1 if tail[1] == s(:str, "") head.push(*tail.sexp_body) end when :evstr then if htype == :str then f, l = head.file, head.line head = s(:dstr, *head.sexp_body) head.file = f head.line = l end if head.size == 2 and tail.size > 1 and tail[1].sexp_type == :str then head.last << tail[1].last head.sexp_type = :str if head.size == 2 # HACK ? else head.push(tail) end else x = [head, tail] raise "unknown type: #{x.inspect}" end return head end |
#local_pop(in_def) ⇒ Object
679 680 681 682 683 684 |
# File 'lib/ruby_parser_extras.rb', line 679 def local_pop in_def lexer.cond.pop # group = local_pop lexer.cmdarg.pop self.env.unextend self.in_def = in_def end |
#logical_op(type, left, right) ⇒ Object
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 |
# File 'lib/ruby_parser_extras.rb', line 686 def logical_op type, left, right left = value_expr left if left and left.sexp_type == type and not left.paren then node, rhs = left, nil loop do _, _lhs, rhs = node break unless rhs && rhs.sexp_type == type and not rhs.paren node = rhs end node.pop node << s(type, rhs, right).line(rhs.line) return left end result = s(type, left, right) result.line left.line if left.line result end |
#new_aref(val) ⇒ Object
709 710 711 712 713 |
# File 'lib/ruby_parser_extras.rb', line 709 def new_aref val val[2] ||= s(:arglist) val[2].sexp_type = :arglist if val[2].sexp_type == :array # REFACTOR new_call val[0], :"[]", val[2] end |
#new_arg(val) ⇒ Object
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 |
# File 'lib/ruby_parser_extras.rb', line 715 def new_arg val arg, = val case arg when Symbol then result = s(:args, arg).line line when Sexp then result = arg when Array then (arg, line), = val result = s(:args, arg).line line else debug20 32 raise "Unknown f_arg type: #{val.inspect}" end result end |
#new_array_pattern(const, pre_arg, arypat, loc) ⇒ Object
734 735 736 737 738 739 740 741 742 743 744 745 |
# File 'lib/ruby_parser_extras.rb', line 734 def new_array_pattern const, pre_arg, arypat, loc result = s(:array_pat, const).line loc result << pre_arg if pre_arg if arypat && arypat.sexp_type == :array_TAIL then result.concat arypat.sexp_body else raise "NO?: %p" % [arypat] end result end |
#new_array_pattern_tail(pre_args, has_rest, rest_arg, post_args) ⇒ Object
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 |
# File 'lib/ruby_parser_extras.rb', line 763 def new_array_pattern_tail pre_args, has_rest, rest_arg, post_args # TODO: remove has_rest once all tests pass !!! rest_arg = if has_rest then :"*#{rest_arg}" else nil end result = s(:array_TAIL).line 666 array_pat_concat result, pre_args result << rest_arg if rest_arg array_pat_concat result, post_args result end |
#new_assign(lhs, rhs) ⇒ Object
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 |
# File 'lib/ruby_parser_extras.rb', line 782 def new_assign lhs, rhs return nil unless lhs rhs = value_expr rhs case lhs.sexp_type when :lasgn, :iasgn, :cdecl, :cvdecl, :gasgn, :cvasgn, :attrasgn, :safe_attrasgn then lhs << rhs when :const then lhs.sexp_type = :cdecl lhs << rhs else raise "unknown lhs #{lhs.inspect} w/ #{rhs.inspect}" end lhs end |
#new_attrasgn(recv, meth, call_op = :".") ⇒ Object
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 |
# File 'lib/ruby_parser_extras.rb', line 800 def new_attrasgn recv, meth, call_op = :"." call_op = call_op.first if Array === call_op meth = :"#{meth}=" result = case call_op.to_sym when :"." s(:attrasgn, recv, meth) when :"&." s(:safe_attrasgn, recv, meth) else raise "unknown call operator: `#{type.inspect}`" end result.line = recv.line result end |
#new_begin(val) ⇒ Object
818 819 820 821 822 823 824 825 |
# File 'lib/ruby_parser_extras.rb', line 818 def new_begin val _, lineno, body, _ = val result = body ? s(:begin, body) : s(:nil) result.line lineno result end |
#new_body(val) ⇒ Object
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 |
# File 'lib/ruby_parser_extras.rb', line 827 def new_body val body, resbody, elsebody, ensurebody = val result = body if resbody then result = s(:rescue) result << body if body res = resbody while res do result << res res = res.resbody(true) end result << elsebody if elsebody result.line = (body || resbody).line end if elsebody and not resbody then warning("else without rescue is useless") result = s(:begin, result).line result.line if result result = block_append(result, elsebody) end if ensurebody lineno = (result || ensurebody).line result = s(:ensure, result, ensurebody).compact.line lineno end result end |
#new_brace_body(args, body, lineno) ⇒ Object
862 863 864 |
# File 'lib/ruby_parser_extras.rb', line 862 def new_brace_body args, body, lineno new_iter(nil, args, body).line lineno end |
#new_call(recv, meth, args = nil, call_op = :".") ⇒ Object
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 |
# File 'lib/ruby_parser_extras.rb', line 866 def new_call recv, meth, args = nil, call_op = :"." call_op = call_op.first if Array === call_op result = case call_op.to_sym when :"." s(:call, recv, meth) when :"&." s(:safe_call, recv, meth) else raise "unknown call operator: `#{type.inspect}`" end # TODO: need a test with f(&b) to produce block_pass # TODO: need a test with f(&b) { } to produce warning if args if ARG_TYPES[args.sexp_type] then result.concat args.sexp_body else result << args end end # line = result.grep(Sexp).map(&:line).compact.min result.line = recv.line if recv result.line ||= lexer.lineno result end |
#new_case(expr, body, line) ⇒ Object
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 |
# File 'lib/ruby_parser_extras.rb', line 900 def new_case expr, body, line result = s(:case, expr) while body and [:when, :in].include? body.sexp_type result << body body = body.delete_at 3 end result[2..-1].each do |node| block = node.block(:delete) node.concat block.sexp_body if block end # else body = nil if body == s(:block) result << body result.line = line result end |
#new_class(val) ⇒ Object
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 |
# File 'lib/ruby_parser_extras.rb', line 921 def new_class val # TODO: get line from class keyword line, path, superclass, body = val[1], val[2], val[3], val[5] path = path.first if path.instance_of? Array result = s(:class, path, superclass) if body then if body.sexp_type == :block then result.push(*body.sexp_body) else result.push body end end result.line = line result.comments = self.comments.pop result end |
#new_compstmt(val) ⇒ Object
942 943 944 945 946 |
# File 'lib/ruby_parser_extras.rb', line 942 def new_compstmt val result = void_stmts(val.grep(Sexp)[0]) result = remove_begin(result) if result result end |
#new_const_op_asgn(val) ⇒ Object
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 |
# File 'lib/ruby_parser_extras.rb', line 948 def new_const_op_asgn val lhs, (asgn_op, _), rhs = val asgn_op = asgn_op.to_sym result = case asgn_op when :"||" then s(:op_asgn_or, lhs, rhs) when :"&&" then s(:op_asgn_and, lhs, rhs) else s(:op_asgn, lhs, asgn_op, rhs) end result.line = lhs.line result end |
#new_defn(val) ⇒ Object
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 |
# File 'lib/ruby_parser_extras.rb', line 965 def new_defn val _, (name, line), in_def, args, body, _ = val body ||= s(:nil).line line args.line line result = s(:defn, name.to_sym, args).line line if body.sexp_type == :block then result.push(*body.sexp_body) else result.push body end result.comments = self.comments.pop [result, in_def] end |
#new_defs(val) ⇒ Object
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 |
# File 'lib/ruby_parser_extras.rb', line 1028 def new_defs val _, recv, (name, line), in_def, args, body, _ = val body ||= s(:nil).line line args.line line result = s(:defs, recv, name.to_sym, args).line line # TODO: remove_begin # TODO: reduce_nodes if body.sexp_type == :block then result.push(*body.sexp_body) else result.push body end result.comments = self.comments.pop [result, in_def] end |
#new_do_body(args, body, lineno) ⇒ Object
1051 1052 1053 |
# File 'lib/ruby_parser_extras.rb', line 1051 def new_do_body args, body, lineno new_iter(nil, args, body).line(lineno) end |
#new_endless_defn(val) ⇒ Object
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 |
# File 'lib/ruby_parser_extras.rb', line 985 def new_endless_defn val (name, line, in_def), args, _, body, _, resbody = val result = if resbody then s(:defn, name, args, new_rescue(body, new_resbody(s(:array).line(line), resbody))).line line else s(:defn, name, args, body).line line end local_pop in_def endless_method_name result result.comments = self.comments.pop result end |
#new_endless_defs(val) ⇒ Object
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 |
# File 'lib/ruby_parser_extras.rb', line 1006 def new_endless_defs val (recv, (name, line, in_def)), args, _, body, _, resbody = val result = if resbody then s(:defs, recv, name, args, new_rescue(body, new_resbody(s(:array).line(line), resbody))).line line else s(:defs, recv, name, args, body).line(line) end self.in_single -= 1 local_pop in_def endless_method_name result result.comments = self.comments.pop result end |
#new_find_pattern(const, pat) ⇒ Object
1055 1056 1057 1058 |
# File 'lib/ruby_parser_extras.rb', line 1055 def new_find_pattern const, pat pat.sexp_type = :find_pat pat.insert 1, const end |
#new_find_pattern_tail(lhs, mid, rhs) ⇒ Object
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 |
# File 'lib/ruby_parser_extras.rb', line 1060 def new_find_pattern_tail lhs, mid, rhs lhs_id, line = lhs rhs_id, _line = rhs # TODO: fpinfo->pre_rest_arg = pre_rest_arg ? assignable(p, pre_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST; lhs_id = "*#{lhs_id}".to_sym rhs_id = "*#{rhs_id}".to_sym mid.sexp_type = :array_pat # HACK? s(:find_pat_TAIL, lhs_id, mid, rhs_id).line line end |
#new_for(expr, var, body) ⇒ Object
1074 1075 1076 1077 1078 |
# File 'lib/ruby_parser_extras.rb', line 1074 def new_for expr, var, body result = s(:for, expr, var).line(var.line) result << body if body result end |
#new_hash(val) ⇒ Object
1080 1081 1082 1083 1084 |
# File 'lib/ruby_parser_extras.rb', line 1080 def new_hash val _, line, assocs = val s(:hash).line(line).concat assocs.sexp_body end |
#new_hash_pattern(const, hash_pat, loc) ⇒ Object
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 |
# File 'lib/ruby_parser_extras.rb', line 1086 def new_hash_pattern const, hash_pat, loc _, pat, kw_args, kw_rest_arg = hash_pat line = (const||hash_pat).line result = s(:hash_pat, const).line line result.concat pat.sexp_body if pat result << kw_args if kw_args result << kw_rest_arg if kw_rest_arg result end |
#new_hash_pattern_tail(kw_args, kw_rest_arg, line) ⇒ Object
TODO: remove line arg
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 |
# File 'lib/ruby_parser_extras.rb', line 1098 def new_hash_pattern_tail kw_args, kw_rest_arg, line # TODO: remove line arg # kw_rest_arg = assignable(kw_rest_arg, nil).line line if kw_rest_arg result = s(:hash_pat).line line result << kw_args if kw_rest_arg then name = kw_rest_arg.value # TODO: I _hate_ this: assignable [name, kw_rest_arg.line] if name != :** result << kw_rest_arg end result end |
#new_if(c, t, f) ⇒ Object
1126 1127 1128 1129 1130 1131 |
# File 'lib/ruby_parser_extras.rb', line 1126 def new_if c, t, f l = [c.line, t && t.line, f && f.line].compact.min c = cond c c, t, f = c.last, f, t if c.sexp_type == :not and canonicalize_conditions s(:if, c, t, f).line(l) end |
#new_in(pat, body, cases, line) ⇒ Object
896 897 898 |
# File 'lib/ruby_parser_extras.rb', line 896 def new_in pat, body, cases, line s(:in, pat, body, cases).line line end |
#new_iter(call, args, body) ⇒ Object
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 |
# File 'lib/ruby_parser_extras.rb', line 1133 def new_iter call, args, body body ||= nil args ||= s(:args) args = s(:args, args) if Symbol === args result = s(:iter) result << call if call result << args result << body if body result.line call.line if call unless args == 0 then args.line call.line if call args.sexp_type = :args end result end |
#new_masgn(lhs, rhs, wrap = false) ⇒ Object
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 |
# File 'lib/ruby_parser_extras.rb', line 1154 def new_masgn lhs, rhs, wrap = false _, ary = lhs line = rhs.line rhs = value_expr(rhs) rhs = ary ? s(:to_ary, rhs) : s(:array, rhs) if wrap rhs.line line if wrap lhs.delete_at 1 if ary.nil? lhs << rhs lhs end |
#new_masgn_arg(rhs, wrap = false) ⇒ Object
1168 1169 1170 1171 1172 1173 |
# File 'lib/ruby_parser_extras.rb', line 1168 def new_masgn_arg rhs, wrap = false rhs = value_expr(rhs) # HACK: could be array if lhs isn't right rhs = s(:to_ary, rhs).line rhs.line if wrap rhs end |
#new_match(lhs, rhs) ⇒ Object
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 |
# File 'lib/ruby_parser_extras.rb', line 1175 def new_match lhs, rhs if lhs then case lhs.sexp_type when :dregx, :dregx_once then # TODO: no test coverage return s(:match2, lhs, rhs).line(lhs.line) when :lit then return s(:match2, lhs, rhs).line(lhs.line) if Regexp === lhs.last end end if rhs then case rhs.sexp_type when :dregx, :dregx_once then # TODO: no test coverage return s(:match3, rhs, lhs).line(lhs.line) when :lit then return s(:match3, rhs, lhs).line(lhs.line) if Regexp === rhs.last end end new_call(lhs, :"=~", argl(rhs)).line lhs.line end |
#new_module(val) ⇒ Object
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 |
# File 'lib/ruby_parser_extras.rb', line 1199 def new_module val # TODO: get line from module keyword line, path, body = val[1], val[2], val[4] path = path.first if path.instance_of? Array result = s(:module, path).line line if body then # REFACTOR? if body.sexp_type == :block then result.push(*body.sexp_body) else result.push body end end result.comments = self.comments.pop result end |
#new_op_asgn(val) ⇒ Object
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 |
# File 'lib/ruby_parser_extras.rb', line 1219 def new_op_asgn val lhs, (op, _line), rhs = val op = op.to_sym name = gettable(lhs.last).line lhs.line arg = remove_begin rhs result = case op # REFACTOR when :"||" then lhs << arg s(:op_asgn_or, name, lhs).line lhs.line when :"&&" then lhs << arg s(:op_asgn_and, name, lhs).line lhs.line else lhs << new_call(name, op, argl(arg)) lhs end result end |
#new_op_asgn1(val) ⇒ Object
1240 1241 1242 1243 1244 1245 1246 1247 1248 |
# File 'lib/ruby_parser_extras.rb', line 1240 def new_op_asgn1 val lhs, _, args, _, (op, _), rhs = val args.sexp_type = :arglist if args result = s(:op_asgn1, lhs, args, op.to_sym, rhs) result.line lhs.line result end |
#new_op_asgn2(val) ⇒ Object
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 |
# File 'lib/ruby_parser_extras.rb', line 1250 def new_op_asgn2 val recv, (call_op, _), (meth, _), (op, _), arg = val meth = :"#{meth}=" result = case call_op.to_sym when :"." s(:op_asgn2, recv, meth, op.to_sym, arg) when :"&." s(:safe_op_asgn2, recv, meth, op.to_sym, arg) else raise "unknown call operator: `#{type.inspect}`" end result.line = recv.line result end |
#new_qsym_list ⇒ Object
1267 1268 1269 |
# File 'lib/ruby_parser_extras.rb', line 1267 def new_qsym_list s(:array).line lexer.lineno end |
#new_qsym_list_entry(val) ⇒ Object
1271 1272 1273 1274 |
# File 'lib/ruby_parser_extras.rb', line 1271 def new_qsym_list_entry val _, (str, line), _ = val s(:lit, str.to_sym).line line end |
#new_qword_list ⇒ Object
1276 1277 1278 |
# File 'lib/ruby_parser_extras.rb', line 1276 def new_qword_list s(:array).line lexer.lineno end |
#new_qword_list_entry(val) ⇒ Object
1280 1281 1282 1283 1284 |
# File 'lib/ruby_parser_extras.rb', line 1280 def new_qword_list_entry val _, (str, line), _ = val str.force_encoding("ASCII-8BIT") unless str.valid_encoding? s(:str, str).line line end |
#new_regexp(val) ⇒ Object
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 |
# File 'lib/ruby_parser_extras.rb', line 1286 def new_regexp val (_, line), node, (, _) = val node ||= s(:str, "").line line o, k = 0, nil .split(//).uniq.each do |c| # FIX: this has a better home v = { "x" => Regexp::EXTENDED, "i" => Regexp::IGNORECASE, "m" => Regexp::MULTILINE, "o" => Regexp::ONCE, "n" => Regexp::ENC_NONE, "e" => Regexp::ENC_EUC, "s" => Regexp::ENC_SJIS, "u" => Regexp::ENC_UTF8, }[c] raise "unknown regexp option: #{c}" unless v o += v end case node.sexp_type when :str then node.sexp_type = :lit node[1] = if k then Regexp.new(node[1], o, k) else begin Regexp.new(node[1], o) rescue RegexpError => e warn "WARNING: #{e.} for #{node[1].inspect} #{.inspect}" begin warn "WARNING: trying to recover with ENC_UTF8" Regexp.new(node[1], Regexp::ENC_UTF8) rescue RegexpError => e warn "WARNING: trying to recover with ENC_NONE" Regexp.new(node[1], Regexp::ENC_NONE) end end end when :dstr then if =~ /o/ then node.sexp_type = :dregx_once else node.sexp_type = :dregx end node << o if o and o != 0 else node = s(:dregx, "", node).line line node.sexp_type = :dregx_once if =~ /o/ node << o if o and o != 0 end node end |
#new_resbody(cond, body) ⇒ Object
1342 1343 1344 1345 1346 1347 1348 1349 1350 |
# File 'lib/ruby_parser_extras.rb', line 1342 def new_resbody cond, body if body && body.sexp_type == :block then body.shift # remove block and splat it in directly else body = [body] end s(:resbody, cond, *body).line cond.line end |
#new_rescue(body, resbody) ⇒ Object
1352 1353 1354 |
# File 'lib/ruby_parser_extras.rb', line 1352 def new_rescue body, resbody s(:rescue, body, resbody).line body.line end |
#new_sclass(val) ⇒ Object
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 |
# File 'lib/ruby_parser_extras.rb', line 1356 def new_sclass val recv, in_def, in_single, body = val[3], val[4], val[6], val[7] result = s(:sclass, recv) if body then if body.sexp_type == :block then result.push(*body.sexp_body) else result.push body end end result.line = val[2] self.in_def = in_def self.in_single = in_single result end |
#new_string(val) ⇒ Object
1375 1376 1377 1378 1379 1380 1381 1382 |
# File 'lib/ruby_parser_extras.rb', line 1375 def new_string val (str, line), = val str.force_encoding("UTF-8") # TODO: remove: str.force_encoding("ASCII-8BIT") unless str.valid_encoding? s(:str, str).line line end |
#new_super(args) ⇒ Object
1384 1385 1386 1387 1388 1389 1390 1391 |
# File 'lib/ruby_parser_extras.rb', line 1384 def new_super args if args && args.sexp_type == :block_pass then s(:super, args).line args.line else args ||= s(:arglist).line lexer.lineno s(:super, *args.sexp_body).line args.line end end |
#new_symbol(val) ⇒ Object
1393 1394 1395 1396 |
# File 'lib/ruby_parser_extras.rb', line 1393 def new_symbol val name = val.last s(:lit, name.to_sym).line lexer.lineno end |
#new_symbol_list ⇒ Object
1398 1399 1400 1401 |
# File 'lib/ruby_parser_extras.rb', line 1398 def new_symbol_list # TODO: hunt down and try to remove ALL lexer.lineno usage! s(:array).line lexer.lineno end |
#new_symbol_list_entry(val) ⇒ Object
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 |
# File 'lib/ruby_parser_extras.rb', line 1403 def new_symbol_list_entry val _, sym, _ = val sym ||= s(:str, "").line lexer.lineno case sym.sexp_type when :dstr then sym.sexp_type = :dsym when :str then sym = s(:lit, sym.last.to_sym).line sym.line else sym = s(:dsym, "", sym).line sym.line end sym end |
#new_undef(n, m = nil) ⇒ Object
1420 1421 1422 1423 1424 1425 1426 |
# File 'lib/ruby_parser_extras.rb', line 1420 def new_undef n, m = nil if m then block_append(n, s(:undef, m).line(m.line)) else s(:undef, n).line n.line end end |
#new_until(block, expr, pre) ⇒ Object
1428 1429 1430 |
# File 'lib/ruby_parser_extras.rb', line 1428 def new_until block, expr, pre new_until_or_while :until, block, expr, pre end |
#new_until_or_while(type, block, expr, pre) ⇒ Object
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 |
# File 'lib/ruby_parser_extras.rb', line 1432 def new_until_or_while type, block, expr, pre other = type == :until ? :while : :until line = [block && block.line, expr.line].compact.min block, pre = block.last, false if block && block.sexp_type == :begin expr = cond expr result = unless expr.sexp_type == :not and canonicalize_conditions then s(type, expr, block, pre) else s(other, expr.last, block, pre) end result.line = line result end |
#new_when(cond, body) ⇒ Object
1449 1450 1451 |
# File 'lib/ruby_parser_extras.rb', line 1449 def new_when cond, body s(:when, cond, body) end |
#new_while(block, expr, pre) ⇒ Object
1453 1454 1455 |
# File 'lib/ruby_parser_extras.rb', line 1453 def new_while block, expr, pre new_until_or_while :while, block, expr, pre end |
#new_word_list ⇒ Object
1457 1458 1459 |
# File 'lib/ruby_parser_extras.rb', line 1457 def new_word_list s(:array).line lexer.lineno end |
#new_word_list_entry(val) ⇒ Object
1461 1462 1463 1464 |
# File 'lib/ruby_parser_extras.rb', line 1461 def new_word_list_entry val _, word, _ = val word.sexp_type == :evstr ? s(:dstr, "", word).line(word.line) : word end |
#new_xstring(val) ⇒ Object
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 |
# File 'lib/ruby_parser_extras.rb', line 1466 def new_xstring val _, node = val node ||= s(:str, "").line lexer.lineno if node then case node.sexp_type when :str node.sexp_type = :xstr when :dstr node.sexp_type = :dxstr else node = s(:dxstr, "", node).line node.line end end node end |
#new_yield(args = nil) ⇒ Object
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 |
# File 'lib/ruby_parser_extras.rb', line 1485 def new_yield args = nil # TODO: raise args.inspect unless [:arglist].include? args.first # HACK raise "write a test 4" if args && args.sexp_type == :block_pass raise SyntaxError, "Block argument should not be given." if args && args.sexp_type == :block_pass args ||= s(:arglist).line lexer.lineno args.sexp_type = :arglist if [:call_args, :array].include? args.sexp_type args = s(:arglist, args).line args.line unless args.sexp_type == :arglist s(:yield, *args.sexp_body).line args.line end |
#next_token ⇒ Object
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 |
# File 'lib/ruby_parser_extras.rb', line 1508 def next_token token = self.lexer.next_token if token and token.first != RubyLexer::EOF then self.last_token_type = token return token elsif !token return self.lexer.next_token else return [false, false] end end |
#on_error(et, ev, values) ⇒ Object
1521 1522 1523 1524 1525 1526 1527 1528 1529 |
# File 'lib/ruby_parser_extras.rb', line 1521 def on_error(et, ev, values) ev = ev.first if ev.instance_of?(Array) && ev.size == 2 && ev.last.is_a?(Integer) super rescue Racc::ParseError => e # I don't like how the exception obscures the error message e..replace "%s:%p :: %s" % [self.file, lexer.lineno, e..strip] warn e. if $DEBUG raise end |
#pop_pktbl ⇒ Object
1117 1118 |
# File 'lib/ruby_parser_extras.rb', line 1117 def pop_pktbl end |
#pop_pvtbl ⇒ Object
1123 1124 |
# File 'lib/ruby_parser_extras.rb', line 1123 def pop_pvtbl end |
#prev_value_to_lineno(v) ⇒ Object
1499 1500 1501 1502 1503 1504 1505 1506 |
# File 'lib/ruby_parser_extras.rb', line 1499 def prev_value_to_lineno v s, n = v if String === s then n else lexer.lineno end end |
#process(str, file = "(string)", time = 10) ⇒ Object Also known as: parse
Parse str at path file and return a sexp. Raises Timeout::Error if it runs for more than time seconds.
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 |
# File 'lib/ruby_parser_extras.rb', line 1535 def process(str, file = "(string)", time = 10) str.freeze Timeout.timeout time do raise "bad val: #{str.inspect}" unless String === str self.lexer.string = handle_encoding str self.file = file.dup @yydebug = ENV.has_key? "DEBUG" do_parse end end |
#push_pktbl ⇒ Object
1114 1115 |
# File 'lib/ruby_parser_extras.rb', line 1114 def push_pktbl end |
#push_pvtbl ⇒ Object
1120 1121 |
# File 'lib/ruby_parser_extras.rb', line 1120 def push_pvtbl end |
#remove_begin(node) ⇒ Object Also known as: value_expr
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 |
# File 'lib/ruby_parser_extras.rb', line 1553 def remove_begin node line = node.line node = node.last while node and node.sexp_type == :begin and node.size == 2 node = s(:nil) if node == s(:begin) node.line ||= line node end |
#reset ⇒ Object
1567 1568 1569 1570 1571 1572 1573 1574 |
# File 'lib/ruby_parser_extras.rb', line 1567 def reset lexer.reset self.in_def = false self.in_single = 0 self.env.reset self.comments.clear self.last_token_type = nil end |
#ret_args(node) ⇒ Object
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 |
# File 'lib/ruby_parser_extras.rb', line 1576 def ret_args node if node then raise "write a test 5" if node.sexp_type == :block_pass raise SyntaxError, "block argument should not be given" if node.sexp_type == :block_pass node.sexp_type = :array if node.sexp_type == :call_args node = node.last if node.sexp_type == :array && node.size == 2 # HACK matz wraps ONE of the FOUR splats in a newline to # distinguish. I use paren for now. ugh node = s(:svalue, node).line node.line if node.sexp_type == :splat and not node.paren node.sexp_type = :svalue if node.sexp_type == :arglist && node[1].sexp_type == :splat end node end |
#s(*args) ⇒ Object
1595 1596 1597 1598 1599 1600 |
# File 'lib/ruby_parser_extras.rb', line 1595 def s(*args) result = Sexp.new(*args) # result.line ||= lexer.lineno if lexer.ss unless ENV["CHECK_LINE_NUMS"] # otherwise... result.file = self.file result end |
#syntax_error(msg) ⇒ Object Also known as: yyerror
1610 1611 1612 |
# File 'lib/ruby_parser_extras.rb', line 1610 def syntax_error msg raise RubyParser::SyntaxError, msg end |
#void_stmts(node) ⇒ Object
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 |
# File 'lib/ruby_parser_extras.rb', line 1616 def void_stmts node return nil unless node return node unless node.sexp_type == :block if node.respond_to? :sexp_body= then node.sexp_body = node.sexp_body.map { |n| remove_begin n } else node[1..-1] = node[1..-1].map { |n| remove_begin(n) } end node end |
#warning(s) ⇒ Object
1629 1630 1631 |
# File 'lib/ruby_parser_extras.rb', line 1629 def warning s # do nothing for now end |
#whitespace_width(line, remove_width = nil) ⇒ Object Also known as: remove_whitespace_width
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 |
# File 'lib/ruby_parser_extras.rb', line 1633 def whitespace_width line, remove_width = nil col = 0 idx = 0 line.chars.each do |c| break if remove_width && col >= remove_width case c when " " then col += 1 when "\t" then n = TAB_WIDTH * (col / TAB_WIDTH + 1) break if remove_width && n > remove_width col = n else break end idx += 1 end if remove_width then line[idx..-1] elsif line[idx].nil? nil else col end end |
#wrap(type, node) ⇒ Object
1663 1664 1665 1666 1667 |
# File 'lib/ruby_parser_extras.rb', line 1663 def wrap type, node value, line = node value = value.to_sym if value.respond_to? :to_sym s(type, value).line line end |