blob: 42bb4ca49e7fde187783b50748b7c505df0c0a5e (
plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
(define-module (vcomponent duration)
:use-module (util)
:use-module (util exceptions)
:use-module (datetime)
:use-module (ice-9 peg)
:use-module (ice-9 match)
:use-module (srfi srfi-9 gnu)
:use-module (srfi srfi-1)
:export (duration parse-duration))
(define-immutable-record-type <duration>
(make-duration sign week day dur-time)
duration?
(sign duration-sign)
(week duration-week)
(day duration-day)
(dur-time duration-time))
(define* (duration
key: (sign '+)
week day time)
(when (and week (or day time))
(error "Can't give week together with day or time"))
(make-duration sign week day time))
(define-peg-pattern number all (+ (range #\0 #\9)))
(define-peg-pattern time-pattern body
(and (ignore "T")
(and (? (capture (and number "H")))
(? (and (? (capture (and number "M")))
(? (capture (and number "S"))))))))
(define-peg-pattern dur-pattern body
(and (capture (? (or "+" "-")))
(and "P"
(or (capture (and number "W"))
(or (capture (and (and number "D")
(? time-pattern)))
(capture time-pattern))))))
(define (parse-duration str)
(let ((m (match-pattern dur-pattern str)))
(unless m
(throw 'parse-error "~a doesn't appar to be a duration" str))
(unless (= (peg:end m) (string-length str))
(warning "Garbage at end of duration"))
(let* ((tree (peg:tree m))
(sign (case (string->symbol (car tree))
[(+ -) => identity]
[(P) '+]))
(lst (concatenate
(map (match-lambda
[(('number num) type)
(let ((n (string->number num)))
(case (string->symbol type)
[(W) `(week: ,n)]
[(D) `(day: ,n)]
[(H) `(hour: ,n)]
[(M) `(minute: ,n)]
[(S) `(second: ,n)]
[else (error "Invalid key")]))]
[#\T '()])
(cdr (member "P" tree))))))
(apply duration
(cons* sign: sign
(let loop ((rem lst))
(if (null? rem)
'()
;; NOTE a potentially prettier way would be
;; to capture the T above, and use that as
;; the delimiter for the time.
(if (memv (car rem) '(hour: minute: second:))
(list time: (apply time rem))
(cons* (car rem) (cadr rem)
(loop (cddr rem)))))))))))
|