Class: Dentaku::TokenScanner

Inherits:
Object
  • Object
show all
Extended by:
StringCasing
Defined in:
lib/dentaku/token_scanner.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StringCasing

standardize_case

Constructor Details

#initialize(category, regexp, converter = nil, condition = nil) ⇒ TokenScanner



10
11
12
13
14
15
# File 'lib/dentaku/token_scanner.rb', line 10

def initialize(category, regexp, converter = nil, condition = nil)
  @category  = category
  @regexp    = %r{\A(#{ regexp })}i
  @converter = converter
  @condition = condition || ->(*) { true }
end

Class Attribute Details

.case_sensitiveObject (readonly)

Returns the value of attribute case_sensitive.



31
32
33
# File 'lib/dentaku/token_scanner.rb', line 31

def case_sensitive
  @case_sensitive
end

Class Method Details

.accessObject



132
133
134
135
# File 'lib/dentaku/token_scanner.rb', line 132

def access
  names = { lbracket: '[', rbracket: ']' }.invert
  new(:access, '\[|\]', lambda { |raw| names[raw] })
end

.available_scannersObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dentaku/token_scanner.rb', line 33

def available_scanners
  [
    :null,
    :whitespace,
    :datetime, # before numeric so it can pick up timestamps
    :numeric,
    :hexadecimal,
    :double_quoted_string,
    :single_quoted_string,
    :negate,
    :combinator,
    :operator,
    :grouping,
    :access,
    :case_statement,
    :comparator,
    :boolean,
    :function,
    :identifier
  ]
end

.booleanObject



156
157
158
# File 'lib/dentaku/token_scanner.rb', line 156

def boolean
  new(:logical, '(true|false)\b', lambda { |raw| raw.strip.downcase == 'true' })
end

.case_statementObject



137
138
139
140
# File 'lib/dentaku/token_scanner.rb', line 137

def case_statement
  names = { open: 'case', close: 'end', then: 'then', when: 'when', else: 'else' }.invert
  new(:case, '(case|end|then|when|else)\b', lambda { |raw| names[raw.downcase] })
end

.combinatorObject



148
149
150
151
152
153
154
# File 'lib/dentaku/token_scanner.rb', line 148

def combinator
  names = { and: '&&', or: '||' }.invert
  new(:combinator, '(and|or|&&|\|\|)\s', lambda { |raw|
    norm = raw.strip.downcase
    names.fetch(norm) { norm.to_sym }
  })
end

.comparatorObject



142
143
144
145
146
# File 'lib/dentaku/token_scanner.rb', line 142

def comparator
  names = { le: '<=', ge: '>=', ne: '!=', lt: '<', gt: '>', eq: '=' }.invert
  alternate = { ne: '<>', eq: '==' }.invert
  new(:comparator, '<=|>=|!=|<>|<|>|==|=', lambda { |raw| names[raw] || alternate[raw] })
end

.datetimeObject

NOTE: Convert to DateTime as Array(Time) returns the parts of the time for some reason



87
88
89
# File 'lib/dentaku/token_scanner.rb', line 87

def datetime
  new(:datetime, /\d{2}\d{2}?-\d{1,2}-\d{1,2}( \d{1,2}:\d{1,2}:\d{1,2})? ?(Z|((\+|\-)\d{2}\:?\d{2}))?/, lambda { |raw| Time.parse(raw).to_datetime })
end

.double_quoted_stringObject



101
102
103
# File 'lib/dentaku/token_scanner.rb', line 101

def double_quoted_string
  new(:string, '"[^"]*"', lambda { |raw| raw.gsub(/^"|"$/, '') })
end

.functionObject



160
161
162
163
164
165
166
167
168
# File 'lib/dentaku/token_scanner.rb', line 160

def function
  new(:function, '\w+!?\s*\(', lambda do |raw|
    function_name = raw.gsub('(', '')
    [
      Token.new(:function, function_name.strip.downcase.to_sym, function_name),
      Token.new(:grouping, :open, '(')
    ]
  end)
end

.groupingObject



127
128
129
130
# File 'lib/dentaku/token_scanner.rb', line 127

def grouping
  names = { open: '(', close: ')', comma: ',' }.invert
  new(:grouping, '\(|\)|,', lambda { |raw| names[raw] })
end

.hexadecimalObject



97
98
99
# File 'lib/dentaku/token_scanner.rb', line 97

def hexadecimal
  new(:numeric, '(0x[0-9a-f]+)\b', lambda { |raw| raw[2..-1].to_i(16) })
end

.identifierObject



170
171
172
# File 'lib/dentaku/token_scanner.rb', line 170

def identifier
  new(:identifier, '[[[:word:]]\.]+\b', lambda { |raw| standardize_case(raw.strip) })
end

.negateObject



109
110
111
112
113
114
115
116
117
118
# File 'lib/dentaku/token_scanner.rb', line 109

def negate
  new(:operator, '-', lambda { |raw| :negate }, lambda { |last_token|
    last_token.nil?             ||
    last_token.is?(:operator)   ||
    last_token.is?(:comparator) ||
    last_token.is?(:combinator) ||
    last_token.value == :open   ||
    last_token.value == :comma
  })
end

.nullObject



82
83
84
# File 'lib/dentaku/token_scanner.rb', line 82

def null
  new(:null, 'null\b')
end

.numericObject



91
92
93
94
95
# File 'lib/dentaku/token_scanner.rb', line 91

def numeric
  new(:numeric, '((?:\d+(\.\d+)?|\.\d+)(?:(e|E)(\+|-)?\d+)?)\b', lambda { |raw|
    raw =~ /\./ ? BigDecimal.new(raw) : raw.to_i
  })
end

.operatorObject



120
121
122
123
124
125
# File 'lib/dentaku/token_scanner.rb', line 120

def operator
  names = {
    pow: '^', add: '+', subtract: '-', multiply: '*', divide: '/', mod: '%', bitor: '|', bitand: '&'
  }.invert
  new(:operator, '\^|\+|-|\*|\/|%|\||&', lambda { |raw| names[raw] })
end

.register_default_scannersObject



55
56
57
# File 'lib/dentaku/token_scanner.rb', line 55

def register_default_scanners
  register_scanners(available_scanners)
end

.register_scanner(id, scanner) ⇒ Object



65
66
67
# File 'lib/dentaku/token_scanner.rb', line 65

def register_scanner(id, scanner)
  @scanners[id] = scanner
end

.register_scanners(scanner_ids) ⇒ Object



59
60
61
62
63
# File 'lib/dentaku/token_scanner.rb', line 59

def register_scanners(scanner_ids)
  @scanners = scanner_ids.each_with_object({}) do |id, scanners|
    scanners[id] = self.send(id)
  end
end

.scanners(options = {}) ⇒ Object



73
74
75
76
# File 'lib/dentaku/token_scanner.rb', line 73

def scanners(options = {})
  @case_sensitive = options.fetch(:case_sensitive, false)
  @scanners.values
end

.scanners=(scanner_ids) ⇒ Object



69
70
71
# File 'lib/dentaku/token_scanner.rb', line 69

def scanners=(scanner_ids)
  @scanners.select! { |k, v| scanner_ids.include?(k) }
end

.single_quoted_stringObject



105
106
107
# File 'lib/dentaku/token_scanner.rb', line 105

def single_quoted_string
  new(:string, "'[^']*'", lambda { |raw| raw.gsub(/^'|'$/, '') })
end

.whitespaceObject



78
79
80
# File 'lib/dentaku/token_scanner.rb', line 78

def whitespace
  new(:whitespace, '\s+')
end

Instance Method Details

#scan(string, last_token = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dentaku/token_scanner.rb', line 17

def scan(string, last_token = nil)
  if (m = @regexp.match(string)) && @condition.call(last_token)
    value = raw = m.to_s
    value = @converter.call(raw) if @converter

    return Array(value).map do |v|
      Token === v ? v : Token.new(@category, v, raw)
    end
  end

  false
end