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_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
  
  if interpreted && delim != '#'
    / (?= [#{delim_pattern}] | \# [{$@] ) /mx
  else
    / (?= [#{delim_pattern}] ) /mx
  end.tap do |pattern|
    h[k] = pattern if (delim.respond_to?(:ord) ? delim.ord : delim[0]) < 256
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of StringState.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/coderay/scanners/ruby/string_state.rb', line 48

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

Class Method Details

.simple_key_pattern(delim) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/coderay/scanners/ruby/string_state.rb', line 40

def self.simple_key_pattern delim
  if delim == "'"
    / (?> (?: [^\\']+ | \\. )* ) ' : /mx
  else
    / (?> (?: [^\\"\#]+ | \\. | \#\$[\\"] | \#\{[^\{\}]+\} | \#(?!\{) )* ) " : /mx
  end
end

Instance Method Details

#heredoc_pattern(delim, interpreted, indented) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/coderay/scanners/ruby/string_state.rb', line 63

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