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
# 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 /\A\s+/
      next i += Regexp.last_match[0].length
    when /\A"(.*?)"/
      match = Regexp.last_match[1]
      type = :quote
    when /\A'(.*?)'/
      match = Regexp.last_match[1]
      type = :quote
    when /\A\-?\d+(\.\d+)?(?=$|[\s"':|<>()])/
      type = :number
    when /\A-/
      type = :minus
    when /\A[^\s:|<>()]+/
      type = :str
    when /\A\|+/
      type = :pipe
    when /\A[()]/
      type = :paren
    when /\A:/
      type = :colon
    when /\A[<>]=?/
      type = :compare
    end
    match ||= Regexp.last_match[0]
    out.push(type: type, value: match)
    i += Regexp.last_match[0].length
  end
  out
end