class OpeningHoursConverter::Tokenizer

Attributes

tokens[R]

Public Class Methods

new(opening_hours_string) click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 9
def initialize(opening_hours_string)
  @opening_hours_string = opening_hours_string
  @index = 0
  @tokens = []
  tokenize
  @tokens_handler = OpeningHoursConverter::TokensHandler.new(@tokens)
  @tokens = @tokens_handler.tokens.map(&:value)
end

Public Instance Methods

tokenize() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 18
def tokenize
  counter = 0
  while @index < @opening_hours_string.length
    raise ParseError if counter > 200
    skip_white_spaces
    @tokens << handle_string if string?

    skip_white_spaces
    @tokens << handle_integer if integer?

    skip_white_spaces
    @tokens << handle_quote if quote?

    skip_white_spaces
    @tokens << handle_slash if slash?

    skip_white_spaces
    @tokens << handle_opening_square_bracket if opening_square_bracket?

    skip_white_spaces
    @tokens << handle_closing_square_bracket if closing_square_bracket?

    skip_white_spaces
    @tokens << handle_colon if colon?

    skip_white_spaces
    @tokens << handle_comma if comma?

    skip_white_spaces
    @tokens << handle_hyphen if hyphen?
    counter += 1
  end
end

Private Instance Methods

closing_square_bracket?() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 224
def closing_square_bracket?
  current_character == ']'
end
colon?() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 204
def colon?
  current_character == ':'
end
comma?() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 216
def comma?
  current_character == ','
end
current_character() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 54
def current_character
  @opening_hours_string[@index]
end
current_character?() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 58
def current_character?
  !current_character.nil?
end
handle_closing_square_bracket() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 129
def handle_closing_square_bracket
  type = :closing_square_bracket
  start_index = @index

  value = current_character
  @index += 1

  token(value, type, start_index)
end
handle_colon() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 139
def handle_colon
  type = :colon
  start_index = @index

  value = current_character
  @index += 1

  token(value, type, start_index)
end
handle_comma() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 149
def handle_comma
  type = :comma
  start_index = @index

  value = current_character
  @index += 1

  token(value, type, start_index)
end
handle_hyphen() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 159
def handle_hyphen
  type = :hyphen
  start_index = @index

  value = current_character
  @index += 1

  token(value, type, start_index)
end
handle_integer() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 75
def handle_integer
  type = :integer
  start_index = @index
  value = ''

  while integer? && current_character?
    value << current_character
    @index += 1
  end

  token(value, type, start_index)
end
handle_opening_square_bracket() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 119
def handle_opening_square_bracket
  type = :opening_square_bracket
  start_index = @index

  value = current_character
  @index += 1

  token(value, type, start_index)
end
handle_quote() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 88
def handle_quote
  type = :quote
  start_index = @index

  value = current_character
  @index += 1

  while current_character?
    if quote?
      value << current_character
      @index += 1
      break
    else
      value << current_character
      @index += 1
    end
  end

  token(value, type, start_index)
end
handle_slash() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 109
def handle_slash
  type = :slash
  start_index = @index

  value = current_character
  @index += 1

  token(value, type, start_index)
end
handle_string() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 62
def handle_string
  type = :string
  start_index = @index
  value = ''

  while string? && current_character?
    value << current_character
    @index += 1
  end

  token(value, type, start_index)
end
hyphen?() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 200
def hyphen?
  current_character == '-'
end
integer?() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 179
def integer?
  !(current_character =~ /[0-9]/).nil?
end
opening_square_bracket?() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 220
def opening_square_bracket?
  current_character == '['
end
punctuation?() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 192
def punctuation?
  !(current_character =~ /[.,;:"'()\[\]]/).nil?
end
quote?() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 196
def quote?
  current_character == '"'
end
semicolon?() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 212
def semicolon?
  current_character == ';'
end
skip_white_spaces() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 173
def skip_white_spaces
  until current_character.nil? || !white_space?
    @index += 1
  end
end
slash?() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 208
def slash?
  current_character == '/'
end
string?() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 187
def string?
  # all char but space, digits and punctuations
  !(current_character =~ /[^\s\d.,;:"()\[\]\-\_]/).nil?
end
token(value, type, start_index) click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 169
def token(value, type, start_index)
  OpeningHoursConverter::Token.new(value, type, start_index)
end
white_space?() click to toggle source
# File lib/opening_hours_converter/tokenizer.rb, line 183
def white_space?
  !(current_character =~ /\s/).nil?
end