class NumberMuncher::Tokenizer

Constants

INVALID_REGEX

Attributes

string[R]
tokens[R]

Public Class Methods

new(string) click to toggle source
# File lib/number_muncher/tokenizer.rb, line 11
def initialize(string)
  @string = string
end

Public Instance Methods

call(raise: false) click to toggle source
# File lib/number_muncher/tokenizer.rb, line 15
def call(raise: false)
  @tokens = []
  scan(raise) until scanner.eos?
  tokens
end

Private Instance Methods

handle_invalid(raise) click to toggle source
# File lib/number_muncher/tokenizer.rb, line 43
def handle_invalid(raise)
  if raise
    raise InvalidNumber if scanner.match?(INVALID_REGEX)
  else
    scanner.skip(INVALID_REGEX)
  end
end
next_token() click to toggle source
# File lib/number_muncher/tokenizer.rb, line 37
def next_token
  Token::Fraction.scan(scanner) ||
    Token::Float.scan(scanner) ||
    Token::Int.scan(scanner)
end
scan(raise) click to toggle source
# File lib/number_muncher/tokenizer.rb, line 27
def scan(raise)
  scanner.skip(/\s+/)
  token = next_token
  tokens << token if token
  handle_invalid(raise)

rescue ZeroDivisionError
  Kernel.raise if raise
end
scanner() click to toggle source
# File lib/number_muncher/tokenizer.rb, line 23
def scanner
  @scanner ||= StringScanner.new(string)
end