-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathperiod.lisp
411 lines (365 loc) · 16.5 KB
/
period.lisp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
;;; -*- mode: lisp; syntax: common-lisp; package: cl-period; encoding: utf-8 -*-
;;; $Id$
;;;
;;; Copyright (c) 2008 William S. Annis. All rights reserved.
;;;
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;; 1. Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.
;;; 2. Redistributions in binary form must reproduce the above copyright
;;; notice, this list of conditions and the following disclaimer in the
;;; documentation and/or other materials provided with the distribution.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
;;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
;;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
;;; OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
;;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
;;; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
;;; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
;;; SUCH DAMAGE.
(in-package :cl-period)
(defparameter +days-of-week+
'(:monday 0 :tuesday 1 :wednesday 2 :thursday 3 :friday 4
:saturday 5 :sunday 6))
(defparameter +months+
'(:january 1 :february 2 :march 3 :april 4 :may 5 :june 6 :july 7
:august 8 :september 9 :october 10 :november 11 :december 12))
(defvar *period-classes* (make-hash-table)
"holds user-defined period classes")
(define-condition contract-violation (error)
((message :initarg :message :accessor contract-violation-message))
(:report (lambda (condition stream)
(format stream (contract-violation-message condition)))))
(define-condition period-arity-violation (contract-violation)
())
(define-condition period-range-violation (contract-violation)
())
(define-condition period-syntax-violation (contract-violation)
())
(defun arity-error (&rest format-args)
(error 'period-arity-violation
:message (apply #'format (cons nil format-args))))
(defun range-error (&rest format-args)
(error 'period-range-violation
:message (apply #'format (cons nil format-args))))
(defun syntax-error (&rest format-args)
(error 'period-syntax-violation
:message (apply #'format (cons nil format-args))))
(define-condition gibberish-cycling-year-range (warning)
((range :initarg :range :accessor gibberish-range))
(:report (lambda (condition stream)
(format stream "cycling year range makes little sense: ~A"
(gibberish-range condition)))))
;;; Blunt, but useful.
(defmacro with-decoded-time (time &body body)
`(multiple-value-bind
(second minute hour date month year day-of-week daylight-p zone)
(decode-universal-time ,time)
(declare (ignorable second minute hour date month year day-of-week daylight-p zone))
,@body))
(defgeneric sanity-check-args (op args)
(:documentation
"ensure correct number and type of arguments for a time specifier"))
(macrolet ((make-sanity-checker (op &body range-test)
`(defmethod sanity-check-args ((op (eql ,op)) args)
(unless (= (length args) 1)
(arity-error "~A takes one argument: ~A" op args))
(unless (apply #',@range-test args)
(range-error "~A not meaningful in ~A clause" args op)))))
(make-sanity-checker :second (lambda (n) (<= 0 n 59)))
(make-sanity-checker :minute (lambda (n) (<= 0 n 59)))
(make-sanity-checker :hour (lambda (n) (<= 0 n 23)))
(make-sanity-checker :date (lambda (n) (<= 1 n 32)))
(make-sanity-checker :year (lambda (n) (<= 1 n)))
(make-sanity-checker :month (lambda (n) (getf +months+ n)))
(make-sanity-checker :day-of-week (lambda (n) (getf +days-of-week+ n)))
(make-sanity-checker 'not (lambda (n) (declare (ignore n)) t))
(make-sanity-checker :class (lambda (n) (gethash n *period-classes*))))
(defun get-unary-arg (op applicand)
"ensure the sanity of a unary op argument and extract the value"
(sanity-check-args op applicand)
(first applicand))
;;; This assumes that START and END have already been sanity-checked.
(defun range-test (p start end)
(if (> start end)
(or (>= p start)
(<= p end))
(<= start p end)))
(defgeneric in-range-p (op val args)
(:documentation "sanity check and test period range"))
(defmethod in-range-p ((op symbol) val args)
(unless (= (length args) 2)
(arity-error "~A range requires two arguments: ~A" op args))
(let ((start (first args))
(end (second args)))
(sanity-check-args op (list start))
(sanity-check-args op (list end))
(range-test val start end)))
(defmethod in-range-p ((op (eql :year)) val args)
(unless (= (length args) 2)
(arity-error "~A range requires two arguments: ~A" op args))
(let ((start (first args))
(end (second args)))
(sanity-check-args op (list start))
(sanity-check-args op (list end))
(when (> start end)
(warn 'gibberish-cycling-year-range :range args))
(range-test val start end)))
(defmethod in-range-p ((op (eql :month)) val args)
(unless (= (length args) 2)
(arity-error "~A range requires two arguments: ~A" op args))
(let ((start (first args))
(end (second args)))
(sanity-check-args op (list start))
(sanity-check-args op (list end))
(range-test val (getf +months+ start) (getf +months+ end))))
(defmethod in-range-p ((op (eql :day-of-week)) val args)
(unless (= (length args) 2)
(arity-error "~A range requires two arguments: ~A" op args))
(let ((start (first args))
(end (second args)))
(sanity-check-args op (list start))
(sanity-check-args op (list end))
(range-test val (getf +days-of-week+ start) (getf +days-of-week+ end))))
(defun eval-period (period time)
(with-decoded-time time
(labels
((evaluate (period)
(let ((op (car period))
(args (cdr period)))
(case op
(and
(dolist (subperiod args t)
(unless (evaluate subperiod)
(return nil))))
(or
(dolist (subperiod args nil)
(when (evaluate subperiod)
(return t))))
(not (not (evaluate (get-unary-arg op args))))
(:second (= second (get-unary-arg op args)))
(:minute (= minute (get-unary-arg op args)))
(:hour (= hour (get-unary-arg op args)))
(:date (= date (get-unary-arg op args)))
(:month (= month (getf +months+ (get-unary-arg op args))))
(:year (= year (get-unary-arg op args)))
(:day-of-week
(= day-of-week (getf +days-of-week+ (get-unary-arg op args))))
(:second-range (in-range-p :second second args))
(:minute-range (in-range-p :minute minute args))
(:hour-range (in-range-p :hour hour args))
(:date-range (in-range-p :date date args))
(:month-range (in-range-p :month month args))
(:year-range (in-range-p :year year args))
(:day-of-week-range (in-range-p :day-of-week day-of-week args))
(:class (progn
(sanity-check-args :class args)
(evaluate (gethash (first args) *period-classes*))))
(otherwise (syntax-error "unknown period specifier: ~A" op))))))
(evaluate period))))
;;; Customized period tokens (called classes in honor of the inspiration
;;; for the notation, cfengine).
(defun add-period-class (class period)
(if (and (keywordp class) (not (find #\- (symbol-name class))))
(setf (gethash class *period-classes*) period)
(syntax-error "period class names must be keywords without dashes: ~A" class)))
;;;
;;; Next, period strings based on cfengine's time classes.
;;;
(defun tokenize-period (string)
(let ((token (make-array 15 :adjustable t :fill-pointer 0
:element-type 'standard-char))
(tokenized ()))
(labels ((op->tok (op)
(case op
(#\. :and)
(#\| :or)
((#\! #\~) :not)
(#\( :oparen)
(#\) :cparen)))
(push-current-token-with-op (op)
(unless (= 0 (length token))
(push (copy-seq token) tokenized)
(setf (fill-pointer token) 0))
(when op
(push (op->tok op) tokenized))))
(loop for c across (string-upcase string)
do (cond ((member c '(#\. #\| #\( #\) #\! #\~) :test #'char=)
(push-current-token-with-op c))
;; ignore whitespace
((member c '(#\Space #\Tab #\Newline) :test #'char=) nil)
(t (vector-push-extend c token)))
finally (push-current-token-with-op nil))
(nreverse tokenized))))
(defun string->keyword (s)
(intern (string-upcase s) "KEYWORD"))
(eval-when (compile load eval) ; for yacc:define-parser
(defun or-expr (a b c)
(declare (ignore b))
(labels ((or-clause-p (c)
(and (listp c)
(eql 'or (first c)))))
(if (or-clause-p a)
(append a (list c))
(list 'or a c))))
(defun and-expr (a b c)
(declare (ignore b))
(labels ((and-clause-p (c)
(and (listp c)
(eql 'and (first c)))))
(if (and-clause-p a)
(append a (list c))
(list 'and a c))))
(defun group-expr (a b c)
(declare (ignore a c))
b)
(defun not-expr (a b)
(declare (ignore a))
(list 'not b))
)
(defun make-exclusive-range (op end)
"Adjust an exclusive range into the default inclusive range notation."
(labels ((rollover-range (n max)
(if (= n 0) max (1- n))))
(case op
((:second :minute) (rollover-range end 59))
(:hour (rollover-range end 23))
(:date (if (= end 1) 32 (1- end)))
(:day-of-week (case end
(:monday :sunday) (:tuesday :monday)
(:wednesday :tuesday) (:thursday :wednesday)
(:friday :thursday) (:saturday :friday)
(:sunday :saturday)))
(:month (case end
(:january :december) (:february :january) (:march :february)
(:april :march) (:may :april) (:june :may) (:july :june)
(:august :july) (:september :august) (:october :september)
(:november :october) (:december :november)))
(:year (1- end)))))
(defun numeric-period-p (token)
"Determine if a period token is a numeric one (Hr, Day, etc), returning
multiple values, a boolean and one of HR, YR, SEC, MIN, DAY."
(cond ((string= token "HR" :end1 2) (values t "HR"))
((string= token "YR" :end1 2) (values t "YR"))
((string= token "SEC" :end1 3) (values t "SEC"))
((string= token "MIN" :end1 3) (values t "MIN"))
((string= token "DAY" :end1 3) (values t "DAY"))
(t (values nil nil))))
(defun period-string->keyword (s)
(cond
((string= s "SEC") :second)
((string= s "MIN") :minute)
((string= s "HR") :hour)
((string= s "DAY") :date)
((string= s "YR") :year)
(t (error "unknown period string ~A" s))))
;;; This seems like more faffing about than should be necessary. However,
;;; it has to cope with numeric ranges (Hr4-9) as well as named ranges
;;; for months and days of the week. The addition of an exclusive range,
;;; notation (Hr9->17) adds to the mess.
(defun parse-range-expr (expr)
(multiple-value-bind (numeric-range-p range-string) (numeric-period-p expr)
(let* ((dash-idx (search "-" expr))
(exclusive-range-p
(handler-case (char= #\> (char expr (1+ dash-idx)))
(error () (syntax-error "malformed range: ~A" expr))))
(start1 (if numeric-range-p
(length range-string)
0))
(end1 dash-idx)
(start2 (if exclusive-range-p
(+ 2 dash-idx)
(+ 1 dash-idx))))
(labels ((get-range ()
(list
(subseq expr start1 end1)
(subseq expr start2)))
(get-range-numbers ()
(handler-case (mapcar #'parse-integer (get-range))
(parse-error () (syntax-error "cannot parse range: ~A" expr))))
(rangeify-name (kw)
(string->keyword
(concatenate 'string (symbol-name kw) "-RANGE")))
(make-numeric-range (op range)
(let ((start (first range))
(end (second range)))
(sanity-check-args op (list start))
(sanity-check-args op (list end))
(if exclusive-range-p
(list (rangeify-name op)
start
(make-exclusive-range op end))
(list (rangeify-name op) start end))))
(make-named-range (range)
(let* ((start (string->keyword (first range)))
(end (string->keyword (second range))))
(cond ((and (getf +months+ start) (getf +months+ end))
(if exclusive-range-p
(list :month-range start (make-exclusive-range :month end))
(list :month-range start end)))
((and (getf +days-of-week+ start)
(getf +days-of-week+ end))
(if exclusive-range-p
(list :day-of-week-range start (make-exclusive-range :days-of-week end))
(list :day-of-week-range start end)))
(t (syntax-error "indecipherable range: ~A" range))))))
(if numeric-range-p
(make-numeric-range (period-string->keyword range-string)
(get-range-numbers))
(make-named-range (get-range)))))))
(eval-when (compile load eval) ; for yacc:define-parser
(defun period-expr (e)
(let ((k (string->keyword e)))
(if (find #\- e)
;; ranges
(parse-range-expr e)
;; simple single periods, checked for sanity
(labels ((check-and-return (op n)
(sanity-check-args op (list n))
(list op n)))
(cond ((getf +days-of-week+ k) (list :day-of-week k))
((getf +months+ k) (list :month k))
((string= e "HR" :end1 2)
(check-and-return :hour (parse-integer e :start 2)))
((string= e "YR" :end1 2)
(check-and-return :year (parse-integer e :start 2)))
((string= e "MIN" :end1 3)
(check-and-return :minute (parse-integer e :start 3)))
((string= e "SEC" :end1 3)
(check-and-return :second (parse-integer e :start 3)))
((string= e "DAY" :end1 3)
(check-and-return :date (parse-integer e :start 3)))
(t (check-and-return :class k))))))) )
(defun token-list-lexer (list)
#'(lambda ()
(let ((value (pop list)))
(if (null value)
(values nil nil)
(let ((term
(cond ((member value '(:and :or :not :period :oparen :cparen)) value)
((stringp value) :period)
(t (syntax-error "ghastly error in token stream - ~S - please report this as a bug" value)))))
(values term value))))))
;;; If any of the functions referred to below (#'or-expr, etc) is changed
;;; during a development session this form has to be evaluated, too. It
;;; keeps hold of the old definitions since it's not keeping their names
;;; but their function values.
(yacc:define-parser *period-class-parser*
(:start-symbol expr)
(:terminals (:and :or :tok :not :period :oparen :cparen))
(:precedence ((:right :not) (:left :and) (:left :or)))
(expr
(expr :or expr #'or-expr)
(expr :and expr #'and-expr)
(:not expr #'not-expr)
term)
(term
(:period #'period-expr)
(:oparen expr :cparen #'group-expr)))
;;; period.lisp ends here