Add ability to specify first line copyright notices
[wolf] / src / main / wolf.lisp
1 (in-package #:wolf)
2
3 ; Some thoughts
4 ; - form starting reader macros will have to be hand added to this code
5 ; - exceptions will eventually arise, and the rule file will have to be changed
6 ; - the proper formatting of "loop" is weird
7
8 (define-condition check-failure nil ((msg :initarg :msg :reader check-failure-msg)
9                                      (line-no :initarg :line-no :reader check-failure-line-no)
10                                      (col-no :initarg :col-no :reader check-failure-col-no)))
11
12 (defvar *state* nil)
13 (defvar *line-no* nil)
14 (defvar *col-no* nil)
15 (defvar *evaluators* nil)
16 (defvar *form-stack* nil)
17 (defvar *form-ended-on-same-line* nil)
18
19 (eval-when (:compile-toplevel :load-toplevel :execute)
20  (defparameter *possible-states*
21   '(:copyright-required
22     :begin ; start of file
23     :normal ; normal processing
24     :beginning-of-line
25     :beginning-of-line-with-separator ; empty space in there
26     :beginning-of-symbols
27     :beginning-of-symbols-with-separator
28     :comment-with-separator ; weird edge case for pre-function comments
29     :beginning-of-line-with-comment-and-separator ; weird edge case part 2
30     :first-symbol ; first symbol of form/line
31     :all ; matches everything
32     :in-string)))
33
34 (defun set-state (state)
35  (when (not (find state *possible-states*))
36   (error "Can't set state to ~A" state))
37  (setf *state* state)
38  nil)
39
40 (defun make-evaluator (eval-state match func)
41  (let
42   ((scanner (when (stringp match) (cl-ppcre:create-scanner match))))
43   (list
44    (lambda (state text)
45     (and
46      (or (eql :all eval-state) (eql eval-state state))
47      (or
48       (and (symbolp text) (eql text match))
49       (and
50        scanner
51        (stringp text)
52        (multiple-value-bind (start end) (cl-ppcre:scan scanner text)
53         (and start end (= 0 start)))))))
54    (lambda (text) (nth-value 1 (cl-ppcre:scan scanner text)))
55    func)))
56
57 (defmacro defevaluator (state match func)
58  (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
59  (let
60   ((scanner (gensym)))
61   `(pushnew (make-evaluator ,state ,match ,func) *evaluators*)))
62
63 (defun evaluate (text)
64  (if (string= "" text)
65   (let*
66    ((evaluator (find-if (lambda (f) (funcall f *state* :eof)) *evaluators* :from-end t :key #'car))
67     (problem (when evaluator (funcall (third evaluator)))))
68    (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*))))
69   (let
70    ((evaluator (find-if (lambda (f) (funcall f *state* text)) *evaluators* :from-end t :key #'car)))
71    (when (not evaluator)
72     (error
73      (make-condition 'check-failure
74       :msg (format nil "Can't check in state ~S: ~S..."
75             *state* (subseq text 0 (min (length text) 10))) :line-no *line-no* :col-no *col-no*)))
76    (let
77     ((problem (funcall (third evaluator))))
78     (when problem
79      (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
80     (let
81      ((length-of-match (funcall (cadr evaluator) text)))
82      (if
83       (eql *col-no* :reset)
84       (setf *col-no* 0)
85       (incf *col-no* length-of-match))
86      (when (< 120 *col-no*)
87       (error (make-condition 'check-failure :msg "Line longer than 120 characters" :line-no *line-no* :col-no 0)))
88      (evaluate (subseq text length-of-match)))))))
89
90 (defun slurp-file (filename &key (element-type 'character) (sequence-type 'string))
91  (with-open-file (str filename :element-type element-type)
92   (let ((seq (make-sequence sequence-type (file-length str)))) (read-sequence seq str) seq)))
93
94 (defun check-file (file &key copyright-notice)
95  (cond
96   (copyright-notice (set-state :copyright-required))
97   ((string= "package" (pathname-name file)) (set-state :normal))
98   (t (set-state :begin)))
99  (setf *line-no* 0)
100  (setf *col-no* 0)
101  (setf *form-stack* nil)
102  (setf *form-ended-on-same-line* nil)
103  (when copyright-notice
104   (setf *evaluators*
105    (append *evaluators*
106     (list
107      (make-evaluator :copyright-required (format nil "~A\\n" copyright-notice)
108       (lambda ()
109        (incf *line-no*)
110        (setf *col-no* :reset)
111        (if (string= "package" (pathname-name file))
112         (set-state :normal)
113         (set-state :begin))))))))
114  (handler-case
115   (progn
116    (evaluate (slurp-file file))
117    (list :success file))
118   (check-failure (cf)
119    (list :failure file (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf)))))
120
121 (setf (documentation 'check-file 'function)
122  "CHECK-FILE FILE &key COPYRIGHT-NOTICE => RESULT
123
124   RESULT: SUCCESS-RESULT | FAILURE-RESULT
125   SUCCESS-RESULT: (:success FILENAME)
126   FAILURE-RESULT: (:success FILENAME MSG LINE-NO COL-NO)
127
128 ARGUMENTS AND VALUES:
129
130   FILE: a pathname
131   COPYRIGHT-NOTICE: A regex string
132   FILENAME: the file this check was run on
133   MSG: a string containing the failure message
134   LINE-NO: an integer, the line number on which the failure appeared
135   COL-NO: an integer, the column number on which the failure appeared
136
137 DESCRIPTION:
138
139   CHECK-FILE runs all the checks against a file and returns
140   as soon as the first style error is found.
141
142   If COPYRIGHT-NOTICE is included, then the first line must match the
143   regular expression passed.
144
145 EXAMPLES:
146
147   (check-file #P\"path/to/file.lisp\") => (:success \"path/to/file.lisp\")
148   (check-file #P\"path/to/error.lisp\") => (:failure \"path/to/error.lisp\" \"File cannot end with empty line\" 20 0)
149   (check-file #P\"path/to/error.lisp\" :copyright-notice \"; Copyright .* AGPL\") => (:failure ...)")
150
151 (defun check-directory (dir &key copyright-notice)
152  "CHECK-DIRECTORY DIR &key COPYRIGHT-NOTICE => RESULTS
153
154   RESULTS: RESULT*
155
156 ARGUMENTS AND VALUES:
157
158   DIR: A directory to recurse into and check files
159   COPYRIGHT-NOTICE: A regex string
160   RESULT: A result as returned by check-file
161
162 DESCRIPTION:
163
164   CHECK-DIRECTORY grabs all .lisp files in the tree under DIR, and loads
165   checks them all.
166
167   If COPYRIGHT-NOTICE is included, then the first line must match the
168   regular expression passed.
169
170   The results are then put together into a list which can be programatically
171   evaluated.  As opposed to pretty-print-check-directory, this function doesn't
172   clutter up your standard out."
173  (mapcar
174   (lambda (file) (check-file file :copyright-notice copyright-notice))
175   (directory (format nil "~A/**/*.lisp" dir))))
176
177 (defun any-failures (checks)
178  (find :failure checks :key #'car))
179
180 (defun print-failure (failure)
181  (format nil
182   "Style error in ~A at ~A:~A: ~A~%- ~A~%~VT^"
183   (second failure)
184   (1+ (fourth failure))
185   (1+ (fifth failure))
186   (third failure)
187   (with-open-file (str (second failure)) (loop :repeat (fourth failure) :do (read-line str)) (read-line str))
188   (+ (fifth failure) 2)))
189
190 (defun pretty-print-check-directory (dir &key copyright-notice)
191  "PRETTY-PRINT-CHECK-DIRECTORY DIR &key COPYRIGHT-NOTICE => SUCCESS
192
193 ARGUMENTS AND VALUES:
194
195   DIR: A directory to recurse into and check files
196   COPYRIGHT-NOTICE: A regex string
197   SUCCESS: T if there were no failures
198
199 DESCRIPTION:
200
201   PRETTY-PRINT-CHECK-DIRECTORY checks DIR for any errors, dumping them to output
202   and returning a single flag.
203
204   If COPYRIGHT-NOTICE is included, then the first line must match the
205   regular expression passed.
206
207   Unlike check-directory, PRETTY-PRINT-CHECK-DIRECTORY is built for continuous
208   integration, dumping errors to standard out and returning a singular result.
209
210 EXAMPLES:
211
212   (pretty-print-check-directory \"src\") => nil"
213  (let
214   ((checks (check-directory dir :copyright-notice copyright-notice)))
215   (format t "In ~A: Checked ~A files with ~A failures~%~%"
216    dir (length checks) (length (remove :success checks :key #'car)))
217   (format t "~{~A~%~}" (mapcar #'print-failure (remove :success checks :key #'car)))
218   (not (any-failures checks))))
219
220 ; These are in reverse order
221 (defevaluator :beginning-of-symbols " *;[^\\n]*"
222  (lambda () (set-state :normal)))
223
224 (defevaluator :beginning-of-symbols-with-separator " *;[^\\n]*"
225  (lambda () (set-state :comment-with-separator)))
226
227 (defevaluator :normal " *;[^\\n]*"
228  (lambda () (set-state :normal)))
229
230 (defevaluator :normal "\\("
231  (lambda ()
232   (push (list *line-no* *col-no*) *form-stack*)
233   (set-state :first-symbol)))
234
235 (defevaluator :first-symbol "\\("
236  (lambda ()
237   (cond
238    ((and (not *form-stack*) (not (zerop *col-no*))) "Top level forms must begin on first column")
239    ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
240     "All form elements must be indented equally")
241    (t
242     (push (list *line-no* *col-no*) *form-stack*)
243     (set-state :first-symbol)))))
244
245 (defevaluator :all "\\t" (constantly "Must not use tabs"))
246
247 (defevaluator :begin "\\(in-package[^\\)]*\\)" (lambda () (set-state :normal)))
248 (defevaluator :copyright-required "\\(in-package[^\\)]*\\)" (constantly "Must begin with specified copyright notice"))
249
250 (defevaluator :beginning-of-line-with-separator :eof
251  (lambda ()
252   (incf *line-no* -1)
253   "Must not end with empty line"))
254
255 (defevaluator :beginning-of-line-with-separator "\\n" (constantly "Must not have two empty lines in a row"))
256
257 (defevaluator :copyright-required ".*" (constantly "Must begin with specified copyright notice"))
258
259 (defevaluator :begin ".*" (constantly "Must begin with in-package form"))
260
261 (defevaluator :all "\\( *in-package " (constantly "Only one in-package per file"))
262
263 (defevaluator :normal "\\n"
264  (lambda ()
265   (incf *line-no*)
266   (setf *col-no* :reset)
267   (set-state :beginning-of-line)))
268
269 (defevaluator :comment-with-separator "\\n"
270  (lambda ()
271   (incf *line-no*)
272   (setf *col-no* :reset)
273   (set-state :beginning-of-line-with-comment-and-separator)
274   nil))
275
276 (defevaluator :normal " +\\n" (constantly "No whitespace at end of line"))
277
278 (defevaluator :beginning-of-line " *" (lambda () (set-state :beginning-of-symbols)))
279
280 (defevaluator :beginning-of-line-with-separator " *" (lambda () (set-state :beginning-of-symbols-with-separator)))
281
282 (defevaluator :beginning-of-line-with-comment-and-separator "\\n"
283  (lambda ()
284   (progn
285    (incf *line-no*)
286    (setf *col-no* :reset)
287    (set-state :beginning-of-line-with-separator))))
288
289 (defevaluator :beginning-of-line-with-comment-and-separator " *"
290  (lambda () (set-state :beginning-of-symbols-with-separator)))
291
292 (defevaluator :beginning-of-symbols "\\n"
293  (lambda ()
294   (if
295    (< 0 *col-no*)
296    "No whitespace only lines"
297    (progn
298     (incf *line-no*)
299     (setf *col-no* :reset)
300     (set-state :beginning-of-line-with-separator)))))
301
302 (defevaluator :beginning-of-symbols "\\)" (constantly "No hanging close parens"))
303
304 (defevaluator :beginning-of-symbols-with-separator "\\)" (constantly "No hanging close parens"))
305
306 (defevaluator :beginning-of-symbols ""
307  (lambda ()
308   (if
309    (and (not *form-stack*) (not *form-ended-on-same-line*))
310    "Multiline top level forms must be separated by a space"
311    (set-state :first-symbol))))
312
313 (defevaluator :beginning-of-symbols-with-separator ""
314  (lambda ()
315   (set-state :first-symbol)))
316
317 (defevaluator :normal "\\)"
318  (lambda ()
319   (let
320    ((form (pop *form-stack*)))
321    (cond
322     ((not form) "Unmatched ending paren")
323     ((< 50 (- *line-no* (car form))) "Forms can't be over 50 lines long")
324     (t (setf *form-ended-on-same-line* (= *line-no* (car form))) nil)))))
325
326 (defevaluator :normal "::" (constantly "No internal symbols from other packages"))
327
328 (defevaluator :in-string "\\\\\"" (constantly nil))
329
330 (defevaluator :normal "\"" (lambda () (set-state :in-string)))
331
332 (defevaluator :in-string "\"" (lambda () (set-state :normal)))
333
334 (defevaluator :in-string "\\n"
335  (lambda ()
336   (incf *line-no*)
337   (setf *col-no* :reset)
338   nil))
339
340 (defevaluator :in-string "." (constantly nil))
341
342 (defevaluator :first-symbol "\\n" (constantly "No new line after opening form"))
343
344 (defevaluator :first-symbol " " (constantly "No space after opening parens"))
345
346 (defevaluator :first-symbol ""
347  (lambda ()
348   (cond
349    ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
350     "All form elements must be indented equally")
351    (t (set-state :normal)))))
352
353 (defevaluator :normal "  " (constantly "Only one space between items of a form"))
354
355 (defevaluator :normal "." (constantly nil))