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