lib/fugit/nat.rb in fugit-1.2.3 vs lib/fugit/nat.rb in fugit-1.3.0
- old
+ new
@@ -17,13 +17,13 @@
#p s; Raabro.pp(Parser.parse(s, debug: 3), colours: true)
a = Parser.parse(s)
return nil unless a
- return parse_cron(a, opts) \
+ return parse_crons(s, a, opts) \
if a.include?([ :flag, 'every' ])
- return parse_cron(a, opts) \
+ return parse_crons(s, a, opts) \
if a.include?([ :flag, 'from' ]) && a.find { |e| e[0] == :day_range }
nil
end
@@ -31,12 +31,40 @@
parse(s, opts) ||
fail(ArgumentError.new("could not parse a nat #{s.inspect}"))
end
- def parse_cron(a, opts={})
+ protected
+ def parse_crons(s, a, opts)
+
+ dhs, aa =
+ a.partition { |e| e[0] == :digital_hour }
+ dms =
+ dhs.collect { |dh| dh[1][1] }.uniq
+
+ crons =
+ if dhs.empty? || dms.size == 1
+ [ parse_cron(a, opts) ]
+ else
+ dhs.collect { |dh| parse_cron([ dh ] + aa, opts) }
+ end
+
+ fail ArgumentError.new(
+ "multiple crons in #{s.inspect} " +
+ "(#{crons.collect(&:original).join(' | ')})"
+ ) if opts[:multi] == :fail && crons.size > 1
+
+ if opts[:multi]
+ crons
+ else
+ crons.first
+ end
+ end
+
+ def parse_cron(a, opts)
+
h = { min: nil, hou: [], dom: nil, mon: nil, dow: nil }
hkeys = h.keys
a.each do |key, val|
if key == :biz_day
@@ -48,12 +76,10 @@
(h[:min] ||= []) << val[1].to_i
elsif key == :name_day
(h[:dow] ||= []) << val
elsif key == :day_range
(h[:dow] ||= []) << val.collect { |v| v.to_s[0, 3] }.join('-')
- elsif key == :flag && val == 'pm' && h[:hou]
- h[:hou][-1] = h[:hou][-1] + 12
elsif key == :tz
h[:tz] = val
elsif key == :duration
process_duration(h, *val[0].to_h.first)
end
@@ -73,12 +99,10 @@
#p s
Fugit::Cron.parse(s)
end
- protected
-
def process_duration(h, interval, value)
send("process_duration_#{interval}", h, value)
end
@@ -127,19 +151,32 @@
NHOURS =
{ 'noon' => [ 12, 0 ], 'midnight' => [ 0, 0 ] }
# piece parsers bottom to top
+ def am_pm(i)
+ rex(:am_pm, i, / *(am|pm)/i)
+ end
+
def digital_hour(i)
rex(:digital_hour, i, /(2[0-4]|[01][0-9]):?[0-5]\d/)
end
+
+ def _simple_hour(i)
+ rex(:sh, i, /(2[0-4]|[01]?[0-9])/)
+ end
def simple_hour(i)
- rex(:simple_hour, i, /(2[0-4]|[01]?[0-9])/)
+ seq(:simple_hour, i, :_simple_hour, :am_pm, '?')
end
+
+ def _numeral_hour(i)
+ rex(:nh, i, /(#{NUMS.join('|')})/i)
+ end
def numeral_hour(i)
- rex(:numeral_hour, i, /(#{NUMS.join('|')})/i)
+ seq(:numeral_hour, i, :_numeral_hour, :am_pm, '?')
end
+
def name_hour(i)
rex(:name_hour, i, /(#{NHOURS.keys.join('|')})/i)
end
def plain_day(i); rex(:plain_day, i, /day/i); end
@@ -168,11 +205,11 @@
\s?
(mon(ths?)?|d(ays?)?|h(ours?)?|m(in(ute)?s?)?|s(ec(ond)?s?)?)
/ix)
end
- def flag(i); rex(:flag, i, /(every|from|at|after|am|pm|on|in)/i); end
+ def flag(i); rex(:flag, i, /(every|from|at|after|on|in)/i); end
def datum(i)
alt(nil, i,
:day_range,
:plain_day, :biz_day, :name_day,
@@ -202,22 +239,23 @@
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]) ]
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
end