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

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

Constant Summary collapse

CLOSING_PAREN =
OPENING_PAREN =
CLOSING_PAREN.invert
STRING_PATTERN =
Hash.new do |h, k|
  delim, interpreted = *k
  delim_pattern = Regexp.escape(delim.dup)  # dup: workaround for old Ruby
  if closing_paren = CLOSING_PAREN[delim]
    delim_pattern = delim_pattern[0..-1] if defined? JRUBY_VERSION  # JRuby fix
    delim_pattern << Regexp.escape(closing_paren)
  end
  delim_pattern << '\\\\' unless delim == '\\'
  
  special_escapes =
    case interpreted
    when :regexp_symbols
      '| ' + REGEXP_SYMBOLS.source
    when :words
      '| \s'
    end
  
  h[k] =
    if interpreted and not delim == '#'
      / (?= [#{delim_pattern}] | \# [{$@] #{special_escapes} ) /mx
    else
      / (?= [#{delim_pattern}] #{special_escapes} ) /mx
    end
end
HEREDOC_PATTERN =
Hash.new do |h, k|
  delim, interpreted, indented = *k
  delim_pattern = Regexp.escape(delim.dup)  # dup: workaround for old Ruby
  delim_pattern = / \n #{ '(?>[\ \t]*)' if indented } #{ Regexp.new delim_pattern } $ /x
  h[k] =
    if interpreted
      / (?= #{delim_pattern}() | \\ | \# [{$@] ) /mx  # $1 set == end of heredoc
    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.



223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/coderay/scanners/ruby/patterns.rb', line 223

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 paren = CLOSING_PAREN[delim]
      delim, paren = paren, delim
      paren_depth = 1
    end
  end
  super kind, interpreted, delim, heredoc, paren, paren_depth, pattern, :initial
end

Instance Attribute Details

#delimObject

Returns the value of attribute delim

Returns:

  • (Object)

    the current value of delim



173
174
175
# File 'lib/coderay/scanners/ruby/patterns.rb', line 173

def delim
  @delim
end

#heredocObject

Returns the value of attribute heredoc

Returns:

  • (Object)

    the current value of heredoc



173
174
175
# File 'lib/coderay/scanners/ruby/patterns.rb', line 173

def heredoc
  @heredoc
end

#interpretedObject

Returns the value of attribute interpreted

Returns:

  • (Object)

    the current value of interpreted



173
174
175
# File 'lib/coderay/scanners/ruby/patterns.rb', line 173

def interpreted
  @interpreted
end

#next_stateObject

Returns the value of attribute next_state

Returns:

  • (Object)

    the current value of next_state



173
174
175
# File 'lib/coderay/scanners/ruby/patterns.rb', line 173

def next_state
  @next_state
end

#parenObject

Returns the value of attribute paren

Returns:

  • (Object)

    the current value of paren



173
174
175
# File 'lib/coderay/scanners/ruby/patterns.rb', line 173

def paren
  @paren
end

#paren_depthObject

Returns the value of attribute paren_depth

Returns:

  • (Object)

    the current value of paren_depth



173
174
175
# File 'lib/coderay/scanners/ruby/patterns.rb', line 173

def paren_depth
  @paren_depth
end

#patternObject

Returns the value of attribute pattern

Returns:

  • (Object)

    the current value of pattern



173
174
175
# File 'lib/coderay/scanners/ruby/patterns.rb', line 173

def pattern
  @pattern
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



173
174
175
# File 'lib/coderay/scanners/ruby/patterns.rb', line 173

def type
  @type
end