Module: CommandSearch::Lexer

Defined in:
lib/command_search/lexer.rb

Class Method Summary collapse

Class Method Details

.char_token(char) ⇒ Object



38
39
40
# File 'lib/command_search/lexer.rb', line 38

def char_token(char)
  { type: char_type(char), value: char }
end

.char_type(char) ⇒ Object

This currently does not support numbers with commas in them



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/command_search/lexer.rb', line 11

def char_type(char)
  case char
  when /["']/
    :quote
  when /[()]/
    :paren
  when /[<>]/
    :compare
  when /\s/
    :space
  when /\d/
    :number
  when '.'
    :period
  when '-'
    :minus
  when ':'
    :colon
  when '='
    :equal
  when '|'
    :pipe
  else
    :str
  end
end

.full_tokens(char_token_list) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/command_search/lexer.rb', line 75

def full_tokens(char_token_list)
  out = char_token_list.clone

  out = group_quoted_strings(out)

  out = group_pattern(out, :pipe,    [:pipe,    :pipe])
  out = group_pattern(out, :compare, [:compare, :equal])

  out = group_pattern(out, :number,  [:number,  :period, :number])
  out = group_pattern(out, :number,  [:number,  :number])
  out = group_pattern(out, :number,  [:minus,   :number])

  out = group_pattern(out, :str,     [:equal])
  out = group_pattern(out, :str,     [:period])
  out = group_pattern(out, :str,     [:number,  :str])
  out = group_pattern(out, :str,     [:number,  :minus])
  out = group_pattern(out, :str,     [:str,     :number])
  out = group_pattern(out, :str,     [:str,     :minus])
  out = group_pattern(out, :str,     [:str,     :str])

  out = out.reject { |x| x[:type] == :space }
  out
end

.group_pattern(input, group_type, pattern) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/command_search/lexer.rb', line 63

def group_pattern(input, group_type, pattern)
  out = input
  len = pattern.count
  while (out.map { |x| x[:type] }).each_cons(len).find_index(pattern)
    i = (out.map { |x| x[:type] }).each_cons(len).find_index(pattern)
    span = i..(i + len - 1)
    val = out[span].map { |x| x[:value] }.join()
    out[span] = { type: group_type, value: val }
  end
  out
end

.group_quoted_strings(input) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/command_search/lexer.rb', line 46

def group_quoted_strings(input)
  out = input
  while value_indices("'", out).length >= 2 || value_indices('"', out).length >= 2
    (a, b) = value_indices("'", out).first(2)
    (c, d) = value_indices('"', out).first(2)
    if a && b && (c.nil? || (a < c))
      (x, y) = [a, b]
    else
      (x, y) = [c, d]
    end
    vals = out[x..y].map { |i| i[:value] }
    trimmed_vals = vals.take(vals.length - 1).drop(1)
    out[x..y] = { type: :quoted_str, value: trimmed_vals.join }
  end
  out
end

.lex(input) ⇒ Object



99
100
101
102
# File 'lib/command_search/lexer.rb', line 99

def lex(input)
  char_tokens = input.split('').map(&method(:char_token))
  full_tokens(char_tokens)
end

.value_indices(match, list) ⇒ Object



42
43
44
# File 'lib/command_search/lexer.rb', line 42

def value_indices(match, list)
  list.each_index.select { |i| list[i][:value] == match }
end