Module: Fugit::Duration::Parser
- Includes:
- Raabro
- Defined in:
- lib/fugit/duration.rb
Instance Method Summary collapse
- #day(i) ⇒ Object
- #dur(i) ⇒ Object
- #elt(i) ⇒ Object
- #hou(i) ⇒ Object
-
#merge(h0, h1) ⇒ Object
rewrite parsed tree.
- #min(i) ⇒ Object
- #mon(i) ⇒ Object
- #rewrite_dur(t) ⇒ Object
- #rewrite_sdur(t) ⇒ Object
- #sdur(i) ⇒ Object
- #sec(i) ⇒ Object
- #sek(i) ⇒ Object
-
#sep(i) ⇒ Object
piece parsers bottom to top.
- #sign(i) ⇒ Object
- #wee(i) ⇒ Object
- #yea(i) ⇒ Object
Instance Method Details
#day(i) ⇒ Object
334 |
# File 'lib/fugit/duration.rb', line 334 def day(i); rex(:day, i, /(\d+\.\d*|(\d*\.)?\d+) *d(ays?)?/i); end |
#dur(i) ⇒ Object
346 |
# File 'lib/fugit/duration.rb', line 346 def dur(i); jseq(:dur, i, :sdur, :sep); end |
#elt(i) ⇒ Object
341 |
# File 'lib/fugit/duration.rb', line 341 def elt(i); alt(nil, i, :yea, :mon, :wee, :day, :hou, :min, :sec, :sek); end |
#hou(i) ⇒ Object
335 |
# File 'lib/fugit/duration.rb', line 335 def hou(i); rex(:hou, i, /(\d+\.\d*|(\d*\.)?\d+) *h(ours?)?/i); end |
#merge(h0, h1) ⇒ Object
rewrite parsed tree
350 351 352 353 354 355 |
# File 'lib/fugit/duration.rb', line 350 def merge(h0, h1) sign = h1.delete(:sign) || 1 h1.inject(h0) { |h, (k, v)| h.merge(k => (h[k] || 0) + sign * v) } end |
#min(i) ⇒ Object
336 |
# File 'lib/fugit/duration.rb', line 336 def min(i); rex(:min, i, /(\d+\.\d*|(\d*\.)?\d+) *m(in(ute)?s?)?/); end |
#mon(i) ⇒ Object
332 |
# File 'lib/fugit/duration.rb', line 332 def mon(i); rex(:mon, i, /(\d+\.\d*|(\d*\.)?\d+) *(M|months?)/); end |
#rewrite_dur(t) ⇒ Object
367 368 369 370 371 |
# File 'lib/fugit/duration.rb', line 367 def rewrite_dur(t) #Raabro.pp(t, colours: true) t.children.inject({}) { |h, ct| merge(h, ct.name ? rewrite(ct) : {}) } end |
#rewrite_sdur(t) ⇒ Object
357 358 359 360 361 362 363 364 365 |
# File 'lib/fugit/duration.rb', line 357 def rewrite_sdur(t) h = Fugit::Duration.common_rewrite_dur(t) sign = t.sublookup(:sign) sign = (sign && sign.string == '-') ? -1 : 1 h.merge(sign: sign) end |
#sdur(i) ⇒ Object
344 |
# File 'lib/fugit/duration.rb', line 344 def sdur(i); seq(:sdur, i, :sign, '?', :elt, '+'); end |
#sec(i) ⇒ Object
338 |
# File 'lib/fugit/duration.rb', line 338 def sec(i); rex(:sec, i, /(\d+\.\d*|(\d*\.)?\d+) *s(ec(ond)?)?s?/i); end |
#sek(i) ⇒ Object
339 |
# File 'lib/fugit/duration.rb', line 339 def sek(i); rex(:sec, i, /(\d+\.\d*|\.\d+|\d+)$/); end |
#sep(i) ⇒ Object
piece parsers bottom to top
329 |
# File 'lib/fugit/duration.rb', line 329 def sep(i); rex(nil, i, /([ \t,]+|and)*/i); end |
#sign(i) ⇒ Object
342 |
# File 'lib/fugit/duration.rb', line 342 def sign(i); rex(:sign, i, /[-+]?/); end |
#wee(i) ⇒ Object
333 |
# File 'lib/fugit/duration.rb', line 333 def wee(i); rex(:wee, i, /(\d+\.\d*|(\d*\.)?\d+) *w(eeks?)?/i); end |
#yea(i) ⇒ Object
331 |
# File 'lib/fugit/duration.rb', line 331 def yea(i); rex(:yea, i, /(\d+\.\d*|(\d*\.)?\d+) *y(ears?)?/i); end |