Created
March 5, 2013 01:02
-
-
Save TALlama/5087160 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
p ||= Chronic::Parser.new | |
def p.get_anchor(tokens, options) | |
puts "get_anchor", " tokens: #{tokens}" | |
grabber = Chronic::Grabber.new(:this) | |
pointer = :future | |
repeaters = get_repeaters(tokens) | |
repeaters.size.times { tokens.pop } | |
puts " grabber: #{grabber}" | |
puts " pointer: #{pointer}" | |
puts " repeaters: #{repeaters}" | |
puts " tokens (after): #{tokens}" | |
if tokens.first && tokens.first.get_tag(Chronic::Grabber) | |
grabber = tokens.shift.get_tag(Chronic::Grabber) | |
end | |
head = repeaters.shift | |
head.start = self.now | |
puts " head: #{head} (a #{head.class})" | |
puts " head.start: #{self.now}" | |
case grabber.type | |
when :last | |
outer_span = head.next(:past) | |
when :this | |
if options[:context] != :past and repeaters.size > 0 | |
puts " head.@current_span: #{head.instance_eval { @current_span }}" | |
puts " head.@range: #{head.instance_eval { @range }}" | |
outer_span = head.this(:none) | |
puts " current span after: #{head.instance_eval { @current_span }}" | |
else | |
puts "going in" | |
outer_span = head.this(options[:context]) | |
end | |
when :next | |
outer_span = head.next(:future) | |
else | |
raise "Invalid grabber" | |
end | |
if Chronic.debug | |
puts "Handler-class: #{head.class}" | |
puts "--#{outer_span}" | |
end | |
find_within(repeaters, outer_span, pointer) | |
end | |
def p.find_within(tags, span, pointer) | |
puts "find_within" | |
puts " tags: #{tags}" | |
puts " span: #{span}" | |
puts " pointer: #{pointer}" | |
return span if tags.empty? | |
head = tags.shift.tap {|o| puts " head: #{o} (a #{o.class})"} | |
fix_repeater(head) | |
head.start = (pointer == :future ? span.begin : span.end).tap {|s| puts " head.start: #{s}"} | |
puts " head.@current_time: #{head.instance_eval { @current_time }}" | |
h = head.this(:none) | |
puts " head.@current_time after: #{head.instance_eval { @current_time }}" | |
puts " h: #{h} (.begin: #{h.begin} .end: #{h.end})" | |
puts " cover begin?: #{span.cover?(h.begin)}" | |
puts " conver end?: #{span.cover?(h.end)}" | |
if span.cover?(h.begin) || span.cover?(h.end) | |
find_within(tags, h, pointer) | |
end | |
end | |
def fix_repeater(r) | |
def r.next(pointer) | |
raise("Start point must be set before calling #next") unless @now | |
puts " Chronic::RepeaterTime#next" | |
puts " @current_time: #{@current_time}" | |
puts " pointer: #{pointer}" | |
half_day = 60 * 60 * 12 | |
full_day = 60 * 60 * 24 | |
first = false | |
unless @current_time | |
first = true | |
midnight = Chronic.time_class.local(@now.year, @now.month, @now.day) | |
yesterday_midnight = midnight - full_day | |
tomorrow_midnight = midnight + full_day | |
offset_fix = midnight.gmt_offset - tomorrow_midnight.gmt_offset | |
tomorrow_midnight += offset_fix | |
puts " first: #{first}" | |
puts " midnight: #{midnight} (a #{midnight.class})" | |
puts " yesterday_midnight: #{yesterday_midnight} (a #{yesterday_midnight.class})" | |
puts " tomorrow_midnight: #{tomorrow_midnight} (a #{tomorrow_midnight.class})" | |
puts " offset_fix: #{offset_fix}" | |
catch :done do | |
if pointer == :future | |
puts " looking for something later than: #{@now}" | |
puts " @type.class: #{@type.class}" | |
puts " @type.time: #{@type.time}" | |
puts " @type.ambiguous?: #{@type.ambiguous?}" | |
if @type.ambiguous? | |
[midnight + @type.time + offset_fix, midnight + half_day + @type.time + offset_fix, tomorrow_midnight + @type.time].each do |t| | |
puts " trying #{t} (a #{t.class})" | |
(@current_time = t; throw :done) if t >= @now | |
end | |
else | |
[midnight + @type.time + offset_fix, tomorrow_midnight + @type.time].each do |t| | |
(@current_time = t; throw :done) if t >= @now | |
end | |
end | |
else # pointer == :past | |
if @type.ambiguous? | |
[midnight + half_day + @type.time + offset_fix, midnight + @type.time + offset_fix, yesterday_midnight + @type.time + half_day].each do |t| | |
(@current_time = t; throw :done) if t <= @now | |
end | |
else | |
[midnight + @type.time + offset_fix, yesterday_midnight + @type.time].each do |t| | |
(@current_time = t; throw :done) if t <= @now | |
end | |
end | |
end | |
end | |
@current_time || raise("Current time cannot be nil at this point") | |
end | |
puts " @current_time found: #{@current_time}" | |
puts " still first: #{first}" | |
unless first | |
increment = @type.ambiguous? ? half_day : full_day | |
@current_time += pointer == :future ? increment : -increment | |
puts " increment: #{increment}" | |
puts " @current_time incremented to: #{@current_time}" | |
end | |
puts " width: #{width}" | |
Chronic::Span.new(@current_time, @current_time + width) | |
end | |
end | |
p.parse "Mar 10, 2013 midnight" | |
p.parse "Mar 10, 2013 12:00am" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment