Class: TimePatterns::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/todo_time_patterns/parser.rb

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



7
8
9
10
11
12
13
14
# File 'lib/todo_time_patterns/parser.rb', line 7

def initialize
  @parser_checks = {
    letter?: WordToken,
    number?: NumberToken,
    symbol?: SymbToken,
    unknown?: UnknownToken
  }
end

Instance Method Details

#parse(input) ⇒ Object



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
43
44
45
# File 'lib/todo_time_patterns/parser.rb', line 16

def parse(input)

  i = 0
  buffer = ""
  tokens = []

  while input[i]
    break if input[i] == nil

    while input[i] == " "
      i += 1
    end

    @parser_checks.each do |check_method, check_class|
      token_start_index = i
      while input[i] != nil and input[i] != " " and input[i].send check_method
        buffer << input[i]
        i += 1
      end

      unless buffer.empty?
        tokens << (check_class.new buffer, token_start_index)
        buffer = ""
      end
    end
  end

  tokens.extend(TokensStringRepresentation)
  tokens
end