class Keisan::StringAndGroupParser::StringPortion

Attributes

escaped_string[R]
string[R]

Public Class Methods

new(expression, start_index) click to toggle source
# File lib/keisan/string_and_group_parser.rb, line 14
def initialize(expression, start_index)
  super(start_index)

  @string = expression[start_index]
  @escaped_string = expression[start_index]
  @end_index = start_index + 1

  while @end_index < expression.size
    if expression[@end_index] == quote_type
      @string << quote_type
      @escaped_string << quote_type
      @end_index += 1
      # Successfully parsed the string
      return
    end

    n, c = get_potentially_escaped_next_character(expression, @end_index)
    @escaped_string << c
    @end_index += n
  end

  raise Keisan::Exceptions::TokenizingError.new("Tokenizing error, no closing quote #{quote_type}")
end

Public Instance Methods

size() click to toggle source
# File lib/keisan/string_and_group_parser.rb, line 38
def size
  string.size
end
to_s() click to toggle source
# File lib/keisan/string_and_group_parser.rb, line 42
def to_s
  string
end

Private Instance Methods

escaped_character(character) click to toggle source
# File lib/keisan/string_and_group_parser.rb, line 66
def escaped_character(character)
  case character
  when "\\", '"', "'"
    character
  when "a"
    "\a"
  when "b"
    "\b"
  when "r"
    "\r"
  when "n"
    "\n"
  when "s"
    "\s"
  when "t"
    "\t"
  else
    raise Keisan::Exceptions::TokenizingError.new("Tokenizing error, unknown escape character: \"\\#{character}\"")
  end
end
get_potentially_escaped_next_character(expression, index) click to toggle source

Returns number of processed input characters, and the output character If a sequence like '"' is encountered, the first backslash escapes the second double-quote, and the two characters will act as a one double-quote character.

# File lib/keisan/string_and_group_parser.rb, line 52
def get_potentially_escaped_next_character(expression, index)
  @string << expression[index]
  if expression[index] == "\\" && index + 1 < expression.size
    @string << expression[index + 1]
    return [2, escaped_character(expression[index + 1])]
  else
    return [1, expression[index]]
  end
end
quote_type() click to toggle source
# File lib/keisan/string_and_group_parser.rb, line 62
def quote_type
  @string[0]
end