class Keisan::StringAndGroupParser

Constants

CLOSED_GROUP_REGEX
COMMENT_CHARACTER_REGEX
OPEN_GROUP_REGEX
STRING_CHARACTER_REGEX

Attributes

portions[R]

An ordered array of “portions”, which

size[R]

An ordered array of “portions”, which

Public Class Methods

new(expression, start_index: 0, ending_character: nil) click to toggle source

Ending character is used as a second ending condition besides expression size

# File lib/keisan/string_and_group_parser.rb, line 197
def initialize(expression, start_index: 0, ending_character: nil)
  index = start_index
  @portions = []

  while index < expression.size && (ending_character.nil? || expression[index] != ending_character)
    case expression[index]
    when STRING_CHARACTER_REGEX
      portion = StringPortion.new(expression, index)
      index = portion.end_index
      @portions << portion

    when OPEN_GROUP_REGEX
      portion = GroupPortion.new(expression, index)
      index += portion.size
      @portions << portion

    when CLOSED_GROUP_REGEX
      raise Keisan::Exceptions::TokenizingError.new("Tokenizing error, unexpected closing brace #{expression[start_index]}")

    when COMMENT_CHARACTER_REGEX
      portion = CommentPortion.new(expression, index)
      index += portion.size
      @portions << portion

    else
      portion = OtherPortion.new(expression, index)
      index += portion.size
      @portions << portion
    end
  end

  @size = index - start_index
end

Public Instance Methods

to_s() click to toggle source
# File lib/keisan/string_and_group_parser.rb, line 231
def to_s
  portions.map(&:to_s).join
end