class Jrr::Scanner

Attributes

category[R]
condition[R]
converter[R]
regex[R]

Public Class Methods

default_scanners() click to toggle source
# File lib/jrr/scanner.rb, line 43
def self.default_scanners
  [
    :null,
    :whitespace,
    :datetime,
    :numeric,
    :hexadecimal,
    :double_quoted_string,
    :single_quoted_string,
    :negate,
    :boolean_operator,
    :arithmetic_operator,
    :grouping,
    :access,
    :case_statement,
    :comparison_operator,
    :boolean,
    :function,
    :identifier
  ]
end
new(category, regex, converter=nil, condition=nil) click to toggle source
# File lib/jrr/scanner.rb, line 9
def initialize(category, regex, converter=nil, condition=nil)
  @category  = category
  @regex     = %r[\A(#{ regex })]i
  @converter = converter
  @condition = condition || ->(*) { true }
end
register_default_scanners() click to toggle source
# File lib/jrr/scanner.rb, line 65
def self.register_default_scanners
  @scanners = default_scanners.map { |key| [key, self.send(key)] }
end
register_scanner(key, scanner) click to toggle source
# File lib/jrr/scanner.rb, line 73
def self.register_scanner(key, scanner)
  @scanners.push([key, scanner])
end
scanners(options={}) click to toggle source
# File lib/jrr/scanner.rb, line 77
def self.scanners(options={})
  @scanners.map { |(_, scanner)| scanner }
end
scanners=(keys) click to toggle source
# File lib/jrr/scanner.rb, line 69
def self.scanners=(keys)
  @scanners.select! { |(key,_)| keys.include?(key) }
end

Public Instance Methods

continue?(previous_token) click to toggle source
# File lib/jrr/scanner.rb, line 31
def continue?(previous_token)
  condition.call(previous_token)
end
convert(raw_value) click to toggle source
# File lib/jrr/scanner.rb, line 35
def convert(raw_value)
  if converter
    converter.call(raw_value)
  else
    raw_value
  end
end
scan(input, previous_token=nil) click to toggle source
# File lib/jrr/scanner.rb, line 16
def scan(input, previous_token=nil)
  if (m = regex.match(input)) && continue?(previous_token)
    raw = m.to_s

    return Array(convert(raw)).map do |value|
      case value
      when Token then value
      else Token.new(category, value, raw)
      end
    end
  end

  false
end