Class: CodeRay::Scanners::Ruby::StringState

Inherits:
Struct
  • Object
show all
Defined in:
lib/coderay/scanners/ruby/string_state.rb

Constant Summary collapse

CLOSING_PAREN =
Hash[ *%w[
  ( )
  [ ]
  < >
  { }
] ].each { |k,v| k.freeze; v.freeze }
STRING_PATTERN =
Hash.new do |h, k|
  delim, interpreted = *k
  # delim = delim.dup  # workaround for old Ruby
  delim_pattern = Regexp.escape(delim)
  if closing_paren = CLOSING_PAREN[delim]
    delim_pattern << Regexp.escape(closing_paren)
  end
  delim_pattern << '\\\\' unless delim == '\\'
  
  # special_escapes =
  #   case interpreted
  #   when :regexp_symbols
  #     '| [|?*+(){}\[\].^$]'
  #   end
  
  h[k] =
    if interpreted && delim != '#'
      / (?= [#{delim_pattern}] | \# [{$@] ) /mx
    else
      / (?= [#{delim_pattern}] ) /mx
    end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind, interpreted, delim, heredoc = false) ⇒ StringState

Returns a new instance of StringState.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/coderay/scanners/ruby/string_state.rb', line 40

def initialize kind, interpreted, delim, heredoc = false
  if heredoc
    pattern = heredoc_pattern delim, interpreted, heredoc == :indented
    delim = nil
  else
    pattern = STRING_PATTERN[ [delim, interpreted] ]
    if closing_paren = CLOSING_PAREN[delim]
      opening_paren = delim
      delim = closing_paren
      paren_depth = 1
    end
  end
  super kind, interpreted, delim, heredoc, opening_paren, paren_depth, pattern, :initial
end

Instance Attribute Details

#delimObject

Returns the value of attribute delim

Returns:

  • (Object)

    the current value of delim



7
8
9
# File 'lib/coderay/scanners/ruby/string_state.rb', line 7

def delim
  @delim
end

#heredocObject

Returns the value of attribute heredoc

Returns:

  • (Object)

    the current value of heredoc



7
8
9
# File 'lib/coderay/scanners/ruby/string_state.rb', line 7

def heredoc
  @heredoc
end

#interpretedObject

Returns the value of attribute interpreted

Returns:

  • (Object)

    the current value of interpreted



7
8
9
# File 'lib/coderay/scanners/ruby/string_state.rb', line 7

def interpreted
  @interpreted
end

#next_stateObject

Returns the value of attribute next_state

Returns:

  • (Object)

    the current value of next_state



7
8
9
# File 'lib/coderay/scanners/ruby/string_state.rb', line 7

def next_state
  @next_state
end

#opening_parenObject

Returns the value of attribute opening_paren

Returns:

  • (Object)

    the current value of opening_paren



7
8
9
# File 'lib/coderay/scanners/ruby/string_state.rb', line 7

def opening_paren
  @opening_paren
end

#paren_depthObject

Returns the value of attribute paren_depth

Returns:

  • (Object)

    the current value of paren_depth



7
8
9
# File 'lib/coderay/scanners/ruby/string_state.rb', line 7

def paren_depth
  @paren_depth
end

#patternObject

Returns the value of attribute pattern

Returns:

  • (Object)

    the current value of pattern



7
8
9
# File 'lib/coderay/scanners/ruby/string_state.rb', line 7

def pattern
  @pattern
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



7
8
9
# File 'lib/coderay/scanners/ruby/string_state.rb', line 7

def type
  @type
end

Instance Method Details

#heredoc_pattern(delim, interpreted, indented) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/coderay/scanners/ruby/string_state.rb', line 55

def heredoc_pattern delim, interpreted, indented
  # delim = delim.dup  # workaround for old Ruby
  delim_pattern = Regexp.escape(delim)
  delim_pattern = / (?:\A|\n) #{ '(?>[ \t]*)' if indented } #{ Regexp.new delim_pattern } $ /x
  if interpreted
    / (?= #{delim_pattern}() | \\ | \# [{$@] ) /mx  # $1 set == end of heredoc
  else
    / (?= #{delim_pattern}() | \\ ) /mx
  end
end