Sha256: 302d4a68a3b696ad149e94058892f0ccfceb7a56e63d5b8f6a248542d2168afc

Contents?: true

Size: 1.96 KB

Versions: 8

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module RipperRubyParser
  module SexpHandlers
    # Sexp handlers for loops
    module Loops
      def process_until(exp)
        handle_conditional_loop :until, :while, exp
      end

      def process_until_mod(exp)
        handle_conditional_loop_mod :until, :while, exp
      end

      def process_while(exp)
        handle_conditional_loop :while, :until, exp
      end

      def process_while_mod(exp)
        handle_conditional_loop_mod :while, :until, exp
      end

      def process_for(exp)
        _, var, coll, block = exp.shift 4
        coll = process(coll)
        var = process(var)

        var.sexp_type = :lasgn if var.sexp_type == :lvar
        block = unwrap_nil process(block)
        if block
          s(:for, coll, var, block)
        else
          s(:for, coll, var)
        end
      end

      private

      def check_at_start?(block)
        block.sexp_type != :begin
      end

      def handle_conditional_loop(type, negated_type, exp)
        _, cond, body = exp.shift 3

        construct_conditional_loop(type, negated_type,
                                   unwrap_begin(process(cond)),
                                   unwrap_nil(process(body)),
                                   true)
      end

      def handle_conditional_loop_mod(type, negated_type, exp)
        _, cond, body = exp.shift 3

        cond = process(cond)
        body = process(body)
        check_at_start = check_at_start?(body)
        construct_conditional_loop(type, negated_type,
                                   unwrap_begin(cond),
                                   unwrap_begin(body),
                                   check_at_start)
      end

      def construct_conditional_loop(type, negated_type, cond, body, check_at_start)
        if cond.sexp_type == :not
          _, inner = cond
          s(negated_type, inner, body, check_at_start)
        else
          s(type, cond, body, check_at_start)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ripper_ruby_parser-1.11.0 lib/ripper_ruby_parser/sexp_handlers/loops.rb
ripper_ruby_parser-1.10.0 lib/ripper_ruby_parser/sexp_handlers/loops.rb
ripper_ruby_parser-1.9.0 lib/ripper_ruby_parser/sexp_handlers/loops.rb
ripper_ruby_parser-1.8.0 lib/ripper_ruby_parser/sexp_handlers/loops.rb
ripper_ruby_parser-1.7.2 lib/ripper_ruby_parser/sexp_handlers/loops.rb
ripper_ruby_parser-1.7.1 lib/ripper_ruby_parser/sexp_handlers/loops.rb
ripper_ruby_parser-1.7.0 lib/ripper_ruby_parser/sexp_handlers/loops.rb
ripper_ruby_parser-1.6.1 lib/ripper_ruby_parser/sexp_handlers/loops.rb