Module: Jrr::DefaultScanners

Included in:
Scanner
Defined in:
lib/jrr/default_scanners.rb

Instance Method Summary collapse

Instance Method Details

#accessObject



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

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

#arithmetic_operatorObject



59
60
61
62
63
64
65
66
# File 'lib/jrr/default_scanners.rb', line 59

def arithmetic_operator
  names = {
    pow: '^', add: '+', subtract: '-', multiply: '*', divide: '/', mod: '%',
    bitor: '|', bitand: '&'
  }.invert

  new(:arithmetic_operator, '\^|\+|-|\*|\/|%|\||&', ->(raw) { names.fetch(raw) })
end

#booleanObject



91
92
93
# File 'lib/jrr/default_scanners.rb', line 91

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

#boolean_operatorObject



50
51
52
53
54
55
56
57
# File 'lib/jrr/default_scanners.rb', line 50

def boolean_operator
  names = { and: '&&', or: '||' }.invert

  new(:boolean_operator, '(and|or|&&|\|\|)\s+', ->(raw) {
    norm = raw.strip.downcase
    names.fetch(norm) { norm.to_sym }
  })
end

#case_statementObject



78
79
80
81
# File 'lib/jrr/default_scanners.rb', line 78

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

#comparison_operatorObject



83
84
85
86
87
88
89
# File 'lib/jrr/default_scanners.rb', line 83

def comparison_operator
  names = { le: '<=', ge: '>=', ne: '!=', lt: '<', gt: '>', eq: '=' }.invert
  alternate = { ne: '<>', eq: '==' }.invert
  new(:comparison_operator, '<=|>=|!=|<>|<|>|==|=', ->(raw) {
    names.fetch(raw) { alternate.fetch(raw) }
  })
end

#datetimeObject



15
16
17
18
19
# File 'lib/jrr/default_scanners.rb', line 15

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}))?/, ->(raw) {
    Time.parse(raw).to_datetime
  })
end

#double_quoted_stringObject



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

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

#functionObject



95
96
97
98
99
100
101
102
103
# File 'lib/jrr/default_scanners.rb', line 95

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

#groupingObject



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

def grouping
  names = { open: '(', close: ')', comma: ',' }.invert
  new(:grouping, '\(|\)|,', ->(raw) { names.fetch(raw) })
end

#hexadecimalObject



27
28
29
# File 'lib/jrr/default_scanners.rb', line 27

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

#identifierObject



105
106
107
# File 'lib/jrr/default_scanners.rb', line 105

def identifier
  new(:identifier, '[\w\.]+\b', ->(raw) { standardize_case(raw.strip) })
end

#negateObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/jrr/default_scanners.rb', line 39

def negate
  new(:arithmetic_operator, '-', ->(raw) { :negate }, ->(previous_token) {
    previous_token.nil?                      ||
    previous_token.is?(:arithmetic_operator) ||
    previous_token.is?(:comparison_operator) ||
    previous_token.is?(:boolean_operator)    ||
    previous_token.value == :open            ||
    previous_token.value == :comma
  })
end

#nullObject



7
8
9
# File 'lib/jrr/default_scanners.rb', line 7

def null
  new(:null, 'null\b', ->(*) { [nil] })
end

#numericObject



21
22
23
24
25
# File 'lib/jrr/default_scanners.rb', line 21

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

#single_quoted_stringObject



35
36
37
# File 'lib/jrr/default_scanners.rb', line 35

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

#standardize_case(value) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/jrr/default_scanners.rb', line 109

def standardize_case(value)
  if @case_sensitive
    value
  else
    value.downcase
  end
end

#whitespaceObject



11
12
13
# File 'lib/jrr/default_scanners.rb', line 11

def whitespace
  new(:whitespace, '\s+', ->(*) { ' ' })
end