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

#_numeral_hour(i) ⇒ Object



178
179
180
# File 'lib/fugit/nat.rb', line 178

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

#_simple_hour(i) ⇒ Object



171
172
173
# File 'lib/fugit/nat.rb', line 171

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

#_tz(i) ⇒ Object



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

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

#_tz_delta(i) ⇒ Object



202
203
204
# File 'lib/fugit/nat.rb', line 202

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

#_tz_name(i) ⇒ Object



199
200
201
# File 'lib/fugit/nat.rb', line 199

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

#am_pm(i) ⇒ Object

piece parsers bottom to top



163
164
165
# File 'lib/fugit/nat.rb', line 163

def am_pm(i)
  rex(:am_pm, i, / *(am|pm)/i)
end

#biz_day(i) ⇒ Object



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

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

#datum(i) ⇒ Object



219
220
221
222
223
224
225
226
227
# File 'lib/fugit/nat.rb', line 219

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

#day_range(i) ⇒ Object



195
196
197
# File 'lib/fugit/nat.rb', line 195

def day_range(i)
  seq(:day_range, i, :name_day, :range_sep, :name_day)
end

#digital_hour(i) ⇒ Object



167
168
169
# File 'lib/fugit/nat.rb', line 167

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

#duration(i) ⇒ Object



207
208
209
210
211
212
213
214
215
# File 'lib/fugit/nat.rb', line 207

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



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

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

#flag(i) ⇒ Object



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

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

#name_day(i) ⇒ Object



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

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

#name_hour(i) ⇒ Object



185
186
187
# File 'lib/fugit/nat.rb', line 185

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

#nat(i) ⇒ Object



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

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

#numeral_hour(i) ⇒ Object



181
182
183
# File 'lib/fugit/nat.rb', line 181

def numeral_hour(i)
  seq(:numeral_hour, i, :_numeral_hour, :am_pm, '?')
end

#plain_day(i) ⇒ Object



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

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

#range_sep(i) ⇒ Object



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

def range_sep(i); rex(nil, i, / *- *| +(to|through) +/); end

#rewrite_nat(t) ⇒ Object

rewrite parsed tree



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/fugit/nat.rb', line 236

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 :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]) ]
      when :day_range
        [ k, tt.subgather(nil).collect { |st| st.string.downcase } ]
      when :numeral_hour, :simple_hour
        vs = tt.subgather(nil).collect { |ttt| ttt.string.downcase.strip }
        v = k == :simple_hour ? vs[0].to_i : NUMS.index(vs[0])
        v += 12 if vs[1] == 'pm'
        [ k, v ]
      else
        [ k, v ]
      end }
end

#simple_hour(i) ⇒ Object



174
175
176
# File 'lib/fugit/nat.rb', line 174

def simple_hour(i)
  seq(:simple_hour, i, :_simple_hour, :am_pm, '?')
end

#sugar(i) ⇒ Object



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

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