Sha256: e0f9b7b53fcda006d07b66232e832c2c1edb83e5130d49def8e508ddbf681a41

Contents?: true

Size: 1.13 KB

Versions: 27

Compression:

Stored size: 1.13 KB

Contents

module Parser

  class Lexer::Dedenter
    def initialize(dedent_level)
      @dedent_level = dedent_level
      @at_line_begin = true
      @indent_level  = 0
    end

    def dedent(string)
      space_begin = space_end = offset = 0
      last_index  = string.length - 1
      string.chars.each_with_index do |char, index|
        if @at_line_begin
          if char == ?\n || @indent_level >= @dedent_level
            string.slice!(space_begin...space_end)
            offset += space_end - space_begin - 1
            @at_line_begin = false
            redo if char == ?\n
          end

          case char
          when ?\s
            @indent_level += 1
            space_end += 1
          when ?\t
            @indent_level += 8 - @indent_level % 8
            space_end += 1
          end
        elsif char == ?\n && index == last_index
          @at_line_begin = true
          @indent_level  = 0
          space_begin = space_end = index - offset + 1
        end
      end

      if @at_line_begin
        string.slice!(space_begin..space_end)
      end

      nil
    end

    def interrupt
      @at_line_begin = false
    end
  end

end

Version data entries

27 entries across 27 versions & 4 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/parser-2.4.0.2/lib/parser/lexer/dedenter.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/parser-2.4.0.2/lib/parser/lexer/dedenter.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/parser-2.4.0.2/lib/parser/lexer/dedenter.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/parser-2.4.0.2/lib/parser/lexer/dedenter.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/parser-2.4.0.2/lib/parser/lexer/dedenter.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/parser-2.4.0.2/lib/parser/lexer/dedenter.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/parser-2.4.0.0/lib/parser/lexer/dedenter.rb
parser-2.4.0.2 lib/parser/lexer/dedenter.rb
parser-2.4.0.1 lib/parser/lexer/dedenter.rb
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/parser-2.4.0.0/lib/parser/lexer/dedenter.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/parser-2.4.0.0/lib/parser/lexer/dedenter.rb
parser-2.4.0.0 lib/parser/lexer/dedenter.rb
parser-2.3.3.1 lib/parser/lexer/dedenter.rb
parser-2.3.3.0 lib/parser/lexer/dedenter.rb
parser-2.3.2.0 lib/parser/lexer/dedenter.rb
parser-2.3.1.4 lib/parser/lexer/dedenter.rb
parser-2.3.1.3 lib/parser/lexer/dedenter.rb
erruby_parser-2.3.1.2 lib/parser/lexer/dedenter.rb
parser-2.3.1.2 lib/parser/lexer/dedenter.rb
parser-2.3.1.1 lib/parser/lexer/dedenter.rb