Module: Fugit::Nat::Parser

Includes:
Raabro
Defined in:
lib/fugit/nat.rb

Constant Summary collapse

NUMS =
%w[
zero
one two three four five six seven eight nine
ten eleven twelve ]
WEEKDAYS =
Fugit::Cron::Parser::WEEKDS + Fugit::Cron::Parser::WEEKDAYS
NHOURS =
{ 'noon' => [ 12, 0 ], 'midnight' => [ 0, 0 ] }

Instance Method Summary collapse

Instance Method Details

#_tz(i) ⇒ Object



136
# File 'lib/fugit/nat.rb', line 136

def _tz(i); alt(:tz, i, :_tz_delta, :_tz_name); end

#_tz_delta(i) ⇒ Object



133
134
135
# File 'lib/fugit/nat.rb', line 133

def _tz_delta(i)
  rex(nil, i, /[-+]([01][0-9]|2[0-4]):?(00|15|30|45)/)
end

#_tz_name(i) ⇒ Object



130
131
132
# File 'lib/fugit/nat.rb', line 130

def _tz_name(i)
  rex(nil, i, /[A-Z][a-zA-Z0-9]+(\/[A-Z][a-zA-Z0-9\_]+){0,2}/)
end

#biz_day(i) ⇒ Object



127
# File 'lib/fugit/nat.rb', line 127

def biz_day(i); rex(:biz_day, i, /(biz|business|week) *day/i); end

#datum(i) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/fugit/nat.rb', line 150

def datum(i)
  alt(nil, i,
    :plain_day, :biz_day, :name_day,
    :_tz,
    :flag,
    :duration,
    :name_hour, :numeral_hour, :digital_hour, :simple_hour)
end

#digital_hour(i) ⇒ Object

piece parsers bottom to top



113
114
115
# File 'lib/fugit/nat.rb', line 113

def digital_hour(i)
  rex(:digital_hour, i, /(2[0-4]|[01][0-9]):?[0-5]\d/)
end

#duration(i) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/fugit/nat.rb', line 138

def duration(i)
  rex(
    :duration, i,
    /
      \d+
      \s?
      (mon(ths?)?|d(ays?)?|h(ours?)?|m(in(ute)?s?)?|s(ec(ond)?s?)?)
    /ix)
end

#elt(i) ⇒ Object



161
# File 'lib/fugit/nat.rb', line 161

def elt(i); alt(nil, i, :sugar, :datum); end

#flag(i) ⇒ Object



148
# File 'lib/fugit/nat.rb', line 148

def flag(i); rex(:flag, i, /(every|at|after|am|pm|on|in)/i); end

#name_day(i) ⇒ Object



128
# File 'lib/fugit/nat.rb', line 128

def name_day(i); rex(:name_day, i, /#{WEEKDAYS.reverse.join('|')}/i); end

#name_hour(i) ⇒ Object



122
123
124
# File 'lib/fugit/nat.rb', line 122

def name_hour(i)
  rex(:name_hour, i, /(#{NHOURS.keys.join('|')})/i)
end

#nat(i) ⇒ Object



162
# File 'lib/fugit/nat.rb', line 162

def nat(i); rep(:nat, i, :elt, 1); end

#numeral_hour(i) ⇒ Object



119
120
121
# File 'lib/fugit/nat.rb', line 119

def numeral_hour(i)
  rex(:numeral_hour, i, /(#{NUMS.join('|')})/i)
end

#plain_day(i) ⇒ Object



126
# File 'lib/fugit/nat.rb', line 126

def plain_day(i); rex(:plain_day, i, /day/i); end

#rewrite_nat(t) ⇒ Object

rewrite parsed tree



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/fugit/nat.rb', line 166

def rewrite_nat(t)

#Raabro.pp(t, colours: true)
  t
    .subgather(nil)
    .collect { |tt|

      k = tt.name
      v = tt.string.downcase

      case k
      when :tz
        [ k, [ tt.string.strip, EtOrbi.get_tzone(tt.string.strip) ] ]
      when :duration
        [ k, [ Fugit::Duration.parse(tt.string.strip) ] ]
      when :numeral_hour
        [ k, NUMS.index(v) ]
      when :simple_hour
        [ k, v.to_i ]
      when :digital_hour
        v = v.gsub(/:/, '')
        [ k, [ v[0, 2], v[2, 2] ] ]
      when :name_hour
        [ :digital_hour, NHOURS[v] ]
      when :name_day
        [ k, WEEKDAYS.index(v[0, 3]) ]
      else
        [ k, v ]
      end }
end

#simple_hour(i) ⇒ Object



116
117
118
# File 'lib/fugit/nat.rb', line 116

def simple_hour(i)
  rex(:simple_hour, i, /(2[0-4]|[01]?[0-9])/)
end

#sugar(i) ⇒ Object



159
# File 'lib/fugit/nat.rb', line 159

def sugar(i); rex(nil, i, /(and|or|[, \t]+)/i); end