Module: CommandSearch::Lexer

Defined in:
lib/command_search/lexer.rb

Class Method Summary collapse

Class Method Details

.lex(input) ⇒ Object



5
6
7
8
9
10
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
37
38
39
40
41
42
# File 'lib/command_search/lexer.rb', line 5

def lex(input)
  out = []
  i = 0
  while i < input.length
    match = nil
    case input[i..-1]
    when /^\s+/
      i += Regexp.last_match[0].length
      next
    when /^"(.*?)"/
      match = Regexp.last_match[1]
      type = :quoted_str
    when /^'(.*?)'/
      match = Regexp.last_match[1]
      type = :quoted_str
    when /^\-?\d+(\.\d+)?(?=$|[\s"':|<>)(])/
      type = :number
    when /^\|+/
      type = :pipe
    when /^-/
      type = :minus
    when /^:/
      type = :colon
    when /^[()]/
      type = :paren
    when /^[<>]=?/
      type = :compare
    when /^\d*[^\d\s:)][^\s"'|:<>)(]*/
      type = :str
    when /^./
      type = :str
    end
    match = match || Regexp.last_match[0]
    out.push(type: type, value: match)
    i += Regexp.last_match[0].length
  end
  out
end