123456789_123456789_123456789_123456789_123456789_

Class: Prism::LexCompat::Heredoc::DashHeredoc

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: lib/prism/lex_compat.rb

Overview

Dash heredocs are a little more complicated. They are a list of tokens that need to be split on “\n” to mimic Ripper’s behavior. We also need to keep track of the state that the heredoc was opened in.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(split) ⇒ DashHeredoc

[ GitHub ]

  
# File 'lib/prism/lex_compat.rb', line 318

def initialize(split)
  @split = split
  @tokens = []
end

Instance Attribute Details

#split (readonly)

[ GitHub ]

  
# File 'lib/prism/lex_compat.rb', line 316

attr_reader :split, :tokens

#tokens (readonly)

[ GitHub ]

  
# File 'lib/prism/lex_compat.rb', line 316

attr_reader :split, :tokens

Instance Method Details

#<<(token)

[ GitHub ]

  
# File 'lib/prism/lex_compat.rb', line 323

def <<(token)
  tokens << token
end

#to_a

[ GitHub ]

  
# File 'lib/prism/lex_compat.rb', line 327

def to_a
  embexpr_balance = 0

  tokens.each_with_object([]) do |token, results| #$ Array[Token]
    case token.event
    when :on_embexpr_beg
      embexpr_balance += 1
      results << token
    when :on_embexpr_end
      embexpr_balance -= 1
      results << token
    when :on_tstring_content
      if embexpr_balance == 0
        lineno = token[0][0]
        column = token[0][1]

        if split
          # Split on "\\\n" to mimic Ripper's behavior. Use a lookbehind
          # to keep the delimiter in the result.
          token.value.split(/(?<=[^\\]\\\n)|(?<=[^\\]\\\r\n)/).each_with_index do |value, index|
            column = 0 if index > 0
            results << Token.new([[lineno, column], :on_tstring_content, value, token.state])
            lineno += value.count("\n")
          end
        else
          results << token
        end
      else
        results << token
      end
    else
      results << token
    end
  end
end