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