-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing.coffee
57 lines (40 loc) · 980 Bytes
/
parsing.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
_ = require 'underscore'
units = require './units'
class ReminderParser
constructor: (text) ->
@tokens = text.split /\s+/
console.log @tokens
peekToken: (offset = 0) -> @tokens[offset]
pullToken: -> @tokens.shift()
parse: ->
target = @pullToken()
time = null
rest = []
task = null
while @peekToken()?
if @isIn() then time = @parseIn()
else rest.push @pullToken()
if rest[0] is 'to' then task = rest[1..].join ' '
{target, time, task}
isIn: ->
_.all [
(@peekToken 0) is 'in'
@isTimeAmount 1
]
parseIn: ->
@pullToken() # 'in'
total = 0
(total += @parseTimeAmount()) while @isTimeAmount()
total
isTimeAmount: (offset = 0) ->
_.all [
(Number @peekToken offset) isnt NaN
units.isUnit @peekToken offset+1
]
parseTimeAmount: ->
amount = Number @pullToken() # amount
unit = @pullToken() # unit
units.toMs unit, amount
module.exports = exports = (text) ->
parser = new ReminderParser text
parser.parse()