Class: Jrr::Scanner

Inherits:
Object
  • Object
show all
Extended by:
DefaultScanners
Defined in:
lib/jrr/scanner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DefaultScanners

access, arithmetic_operator, boolean, boolean_operator, case_statement, comparison_operator, datetime, double_quoted_string, function, grouping, hexadecimal, identifier, negate, null, numeric, single_quoted_string, standardize_case, whitespace

Constructor Details

#initialize(category, regex, converter = nil, condition = nil) ⇒ Scanner

Returns a new instance of Scanner.



9
10
11
12
13
14
# 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

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



6
7
8
# File 'lib/jrr/scanner.rb', line 6

def category
  @category
end

#conditionObject (readonly)

Returns the value of attribute condition.



6
7
8
# File 'lib/jrr/scanner.rb', line 6

def condition
  @condition
end

#converterObject (readonly)

Returns the value of attribute converter.



6
7
8
# File 'lib/jrr/scanner.rb', line 6

def converter
  @converter
end

#regexObject (readonly)

Returns the value of attribute regex.



6
7
8
# File 'lib/jrr/scanner.rb', line 6

def regex
  @regex
end

Class Method Details

.default_scannersObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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

.register_default_scannersObject



65
66
67
# 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) ⇒ Object



73
74
75
# File 'lib/jrr/scanner.rb', line 73

def self.register_scanner(key, scanner)
  @scanners.push([key, scanner])
end

.scanners(options = {}) ⇒ Object



77
78
79
# File 'lib/jrr/scanner.rb', line 77

def self.scanners(options={})
  @scanners.map { |(_, scanner)| scanner }
end

.scanners=(keys) ⇒ Object



69
70
71
# File 'lib/jrr/scanner.rb', line 69

def self.scanners=(keys)
  @scanners.select! { |(key,_)| keys.include?(key) }
end

Instance Method Details

#continue?(previous_token) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/jrr/scanner.rb', line 31

def continue?(previous_token)
  condition.call(previous_token)
end

#convert(raw_value) ⇒ Object



35
36
37
38
39
40
41
# 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) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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