lib/bade/parser/parser_ruby_code.rb
# Parse ruby code, ended with outer delimiters
#
# @param [String, Regexp] outer_delimiters
#
# @return [Void] parsed ruby code
#
Parse ruby code, ended with outer delimiters
Parse ruby code ended with outer delimiters
def parse_ruby_code(outer_delimiters, allow_multiline: false)
      code = String.new
      end_re = if outer_delimiters.is_a?(Regexp)
                 outer_delimiters
               else
                 /\A\s*[#{Regexp.escape outer_delimiters.to_s}]/
               end
      delimiters = []
      string_start_quote_char = nil
      loop do
        break if !allow_multiline && @line.empty?
        break if allow_multiline && @line.empty? && (@lines && @lines.empty?)
        break if delimiters.empty? && @line =~ end_re
        if @line.empty? && allow_multiline && !(@lines && @lines.empty?)
          next_line
          code << "\n"
        end
        char = @line[0]
        if char == '\\' && RUBY_ALL_DELIMITERS.include?(@line[1])
          code << @line.slice!(0, 2)
          next
        end
        case char
        when RUBY_START_DELIMITERS_RE
          if RUBY_NOT_NESTABLE_DELIMITERS.include?(char) && delimiters.last == char
            delimiters.pop
            string_start_quote_char = nil
          else
            delimiters << char if string_start_quote_char.nil?
            if RUBY_NOT_NESTABLE_DELIMITERS.include?(char) && string_start_quote_char.nil?
              string_start_quote_char = char
            end
          end
        when RUBY_END_DELIMITERS_RE
          delimiters.pop if char == RUBY_DELIMITERS_REVERSE[delimiters.last]
        end
        code << @line.slice!(0)
      end
      syntax_error('Unexpected end of ruby code') unless delimiters.empty?
      code.strip
    end
def parse_ruby_code ( outer_delimiters , allow_multiline : false ) code = String . new end_re = if outer_delimiters . is_a? ( Regexp ) outer_delimiters else / \A \s *[ #{ Regexp . escape outer_delimiters . to_s } ] / end delimiters = [ ] string_start_quote_char = nil loop do break if ! allow_multiline && @line . empty? break if allow_multiline && @line . empty? && ( @lines && @lines . empty? ) break if delimiters . empty? && @line =~ end_re if @line . empty? && allow_multiline && ! ( @lines && @lines . empty? ) next_line code << "\n" end char = @line [ 0 ] if char == '\\' && RUBY_ALL_DELIMITERS . include? ( @line [ 1 ] ) code << @line . slice! ( 0 , 2 ) next end case char when RUBY_START_DELIMITERS_RE if RUBY_NOT_NESTABLE_DELIMITERS . include? ( char ) && delimiters . last == char delimiters . pop string_start_quote_char = nil else delimiters << char if string_start_quote_char . nil? if RUBY_NOT_NESTABLE_DELIMITERS . include? ( char ) && string_start_quote_char . nil? string_start_quote_char = char end end when RUBY_END_DELIMITERS_RE delimiters . pop if char == RUBY_DELIMITERS_REVERSE [ delimiters . last ] end code << @line . slice! ( 0 ) end syntax_error ( 'Unexpected end of ruby code' ) unless delimiters . empty? code . strip end
Parse ruby code, ended with outer delimiters
Parse ruby code ended with outer delimiters
# backslash escaped delimiter # end char of not nestable delimiter # diving into nestable delimiters # mark start char of the not nestable delimiters, for example strings # rising
parsed ruby code
parsed ruby code
Void
outer_delimiters
allow_multiline
None