File: separator.rb

package info (click to toggle)
libchronic-ruby 0.2.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 268 kB
  • ctags: 329
  • sloc: ruby: 2,686; makefile: 7
file content (76 lines) | stat: -rw-r--r-- 1,902 bytes parent folder | download
1
2
3
4
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
module Chronic

  class Separator < Tag #:nodoc:
    def self.scan(tokens)
      tokens.each_index do |i|
        if t = self.scan_for_commas(tokens[i]) then tokens[i].tag(t); next end
        if t = self.scan_for_slash_or_dash(tokens[i]) then tokens[i].tag(t); next end
        if t = self.scan_for_at(tokens[i]) then tokens[i].tag(t); next end
        if t = self.scan_for_in(tokens[i]) then tokens[i].tag(t); next end
      end
      tokens
    end
    
    def self.scan_for_commas(token)
      scanner = {/^,$/ => :comma}
      scanner.keys.each do |scanner_item|
        return SeparatorComma.new(scanner[scanner_item]) if scanner_item =~ token.word
      end
      return nil
    end
    
    def self.scan_for_slash_or_dash(token)
      scanner = {/^-$/ => :dash,
                 /^\/$/ => :slash}
      scanner.keys.each do |scanner_item|
        return SeparatorSlashOrDash.new(scanner[scanner_item]) if scanner_item =~ token.word
      end
      return nil
    end
    
    def self.scan_for_at(token)
      scanner = {/^(at|@)$/ => :at}
      scanner.keys.each do |scanner_item|
        return SeparatorAt.new(scanner[scanner_item]) if scanner_item =~ token.word
      end
      return nil
    end
    
    def self.scan_for_in(token)
      scanner = {/^in$/ => :in}
      scanner.keys.each do |scanner_item|
        return SeparatorIn.new(scanner[scanner_item]) if scanner_item =~ token.word
      end
      return nil
    end
    
    def to_s
      'separator'
    end
  end
  
  class SeparatorComma < Separator #:nodoc:
    def to_s
      super << '-comma'
    end
  end
  
  class SeparatorSlashOrDash < Separator #:nodoc:
    def to_s
      super << '-slashordash-' << @type.to_s
    end
  end
  
  class SeparatorAt < Separator #:nodoc:
    def to_s
      super << '-at'
    end
  end
  
  class SeparatorIn < Separator #:nodoc:
    def to_s
      super << '-in'
    end
  end

end