public
Fork of mojombo/chronic
Description: "Chronic is a pure Ruby natural language date parser." + improvements, corrections, speedups, and additions
Homepage: http://chronic.rubyforge.org
Clone URL: git://github.com/jf/chronic.git
jf (author)
Mon Apr 14 09:48:15 -0700 2008
commit  8a931393e9499795a5c53a8fb8208842643cb38b
tree    435714738fc1c79e0df2a8aa548267b982708b47
parent  7a1ed038fdc972bb1b42096d39bc70628e064426
chronic / lib / chronic / scalar.rb
100644 74 lines (64 sloc) 2.021 kb
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
module Chronic
 
  class Scalar < Tag #:nodoc:
    def self.scan(tokens)
      # for each token
      tokens.each_index do |i|
        if t = self.scan_for_scalars(tokens[i], tokens[i + 1]) then tokens[i].tag(t) end
        if t = self.scan_for_days(tokens[i], tokens[i + 1]) then tokens[i].tag(t) end
        if t = self.scan_for_months(tokens[i], tokens[i + 1]) then tokens[i].tag(t) end
        if t = self.scan_for_years(tokens[i], tokens[i + 1]) then tokens[i].tag(t) end
      end
      tokens
    end
  
    def self.scan_for_scalars(token, post_token)
      if token.word =~ /^\d*$/
        unless post_token && %w{am pm morning afternoon evening night}.include?(post_token)
          return Scalar.new(token.word.to_i)
        end
      end
      return nil
    end
    
    def self.scan_for_days(token, post_token)
      if token.word =~ /^\d\d?$/
        unless token.word.to_i > 31 || (post_token && %w{am pm morning afternoon evening night}.include?(post_token))
          return ScalarDay.new(token.word.to_i)
        end
      end
      return nil
    end
    
    def self.scan_for_months(token, post_token)
      if token.word =~ /^\d\d?$/
        unless token.word.to_i > 12 || (post_token && %w{am pm morning afternoon evening night}.include?(post_token))
          return ScalarMonth.new(token.word.to_i)
        end
      end
      return nil
    end
    
    def self.scan_for_years(token, post_token)
      if token.word =~ /^([1-9]\d)?\d\d?$/
        unless post_token && %w{am pm morning afternoon evening night}.include?(post_token)
          return ScalarYear.new(token.word.to_i)
        end
      end
      return nil
    end
    
    def to_s
      'scalar'
    end
  end
  
  class ScalarDay < Scalar #:nodoc:
    def to_s
      super << '-day-' << @type.to_s
    end
  end
  
  class ScalarMonth < Scalar #:nodoc:
    def to_s
      super << '-month-' << @type.to_s
    end
  end
  
  class ScalarYear < Scalar #:nodoc:
    def to_s
      super << '-year-' << @type.to_s
    end
  end
 
end