module Jrr::DefaultScanners

Public Instance Methods

access() click to toggle source
# File lib/jrr/default_scanners.rb, line 73
def access
  names = { lbracket: '[', rbracket: ']' }.invert
  new(:access, '\[|\]', ->(raw) { names.fetch(raw) })
end
arithmetic_operator() click to toggle source
# 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
boolean() click to toggle source
# File lib/jrr/default_scanners.rb, line 91
def boolean
  new(:boolean, '(true|false)\b', ->(raw) { raw.strip.downcase == 'true' })
end
boolean_operator() click to toggle source
# 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_statement() click to toggle source
# 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_operator() click to toggle source
# 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
datetime() click to toggle source
# 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_string() click to toggle source
# File lib/jrr/default_scanners.rb, line 31
def double_quoted_string
  new(:string, '"[^"]*"', ->(raw) { raw.gsub(/^"|"$/, '') })
end
function() click to toggle source
# 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
grouping() click to toggle source
# File lib/jrr/default_scanners.rb, line 68
def grouping
  names = { open: '(', close: ')', comma: ',' }.invert
  new(:grouping, '\(|\)|,', ->(raw) { names.fetch(raw) })
end
hexadecimal() click to toggle source
# 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
identifier() click to toggle source
# File lib/jrr/default_scanners.rb, line 105
def identifier
  new(:identifier, '[\w\.]+\b', ->(raw) { standardize_case(raw.strip) })
end
negate() click to toggle source
# 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
null() click to toggle source
# File lib/jrr/default_scanners.rb, line 7
def null
  new(:null, 'null\b', ->(*) { [nil] })
end
numeric() click to toggle source
# 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_string() click to toggle source
# File lib/jrr/default_scanners.rb, line 35
def single_quoted_string
  new(:string, "'[^']*'", ->(raw) { raw.gsub(/^'|'$/, '') })
end
standardize_case(value) click to toggle source
# File lib/jrr/default_scanners.rb, line 109
def standardize_case(value)
  if @case_sensitive
    value
  else
    value.downcase
  end
end
whitespace() click to toggle source
# File lib/jrr/default_scanners.rb, line 11
def whitespace
  new(:whitespace, '\s+', ->(*) { ' ' })
end