class Keisan::StringAndGroupParser::GroupPortion

Constants

OPENING_TO_CLOSING_BRACE

Attributes

closing_brace[R]
opening_brace[R]
portions[R]
size[R]

Public Class Methods

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

  case expression[start_index]
  when OPEN_GROUP_REGEX
    @opening_brace = expression[start_index]
  else
    raise Keisan::Exceptions::TokenizingError.new("Internal error, GroupPortion did not start with brace")
  end

  @closing_brace = OPENING_TO_CLOSING_BRACE[opening_brace]

  parser = StringAndGroupParser.new(expression, start_index: start_index + 1, ending_character: closing_brace)
  @portions = parser.portions
  @size = parser.size + 2

  if start_index + size > expression.size || expression[start_index + size - 1] != closing_brace
    raise Keisan::Exceptions::TokenizingError.new("Tokenizing error, group with opening brace #{opening_brace} did not have closing brace")
  end
end

Public Instance Methods

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