1 ; Copyright 2022 Frank Duncan (frank@consxy.com) under AGPL3. See distributed LICENSE.txt.
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
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)))
14 (defvar *line-no* nil)
16 (defvar *evaluators* nil)
17 (defvar *form-stack* nil)
18 (defvar *form-ended-on-same-line* nil)
20 (eval-when (:compile-toplevel :load-toplevel :execute)
21 (defparameter *possible-states*
23 :begin ; start of file
24 :normal ; normal processing
26 :beginning-of-line-with-separator ; empty space in there
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
35 (defun set-state (state)
36 (when (not (find state *possible-states*))
37 (error "Can't set state to ~A" state))
41 (defun make-evaluator (eval-state match func)
43 ((scanner (when (stringp match) (cl-ppcre:create-scanner match))))
47 (or (eql :all eval-state) (eql eval-state state))
49 (and (symbolp text) (eql text match))
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)))
58 (defmacro defevaluator (state match func)
59 (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
62 `(pushnew (make-evaluator ,state ,match ,func) *evaluators*)))
64 (defun evaluate (text)
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*))))
71 ((evaluator (find-if (lambda (f) (funcall f *state* text)) *evaluators* :from-end t :key #'car)))
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*)))
78 ((problem (funcall (third evaluator))))
80 (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
82 ((length-of-match (funcall (cadr evaluator) text)))
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)))))))
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)))
95 (defun check-file (file &key copyright-notice)
97 (copyright-notice (set-state :copyright-required))
98 ((string= "package" (pathname-name file)) (set-state :normal))
99 (t (set-state :begin)))
102 (setf *form-stack* nil)
103 (setf *form-ended-on-same-line* nil)
104 (when copyright-notice
108 (make-evaluator :copyright-required (format nil "~A\\n" copyright-notice)
111 (setf *col-no* :reset)
112 (if (string= "package" (pathname-name file))
114 (set-state :begin))))))))
117 (evaluate (slurp-file file))
118 (list :success file))
120 (list :failure file (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf)))))
122 (setf (documentation 'check-file 'function)
123 "CHECK-FILE FILE &key COPYRIGHT-NOTICE => RESULT
125 RESULT: SUCCESS-RESULT | FAILURE-RESULT
126 SUCCESS-RESULT: (:success FILENAME)
127 FAILURE-RESULT: (:success FILENAME MSG LINE-NO COL-NO)
129 ARGUMENTS AND VALUES:
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
140 CHECK-FILE runs all the checks against a file and returns
141 as soon as the first style error is found.
143 If COPYRIGHT-NOTICE is included, then the first line must match the
144 regular expression passed.
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 ...)")
152 (defun check-directory (dir &key copyright-notice)
153 "CHECK-DIRECTORY DIR &key COPYRIGHT-NOTICE => RESULTS
157 ARGUMENTS AND VALUES:
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
165 CHECK-DIRECTORY grabs all .lisp files in the tree under DIR, and loads
168 If COPYRIGHT-NOTICE is included, then the first line must match the
169 regular expression passed.
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."
175 (lambda (file) (check-file file :copyright-notice copyright-notice))
176 (directory (format nil "~A/**/*.lisp" dir))))
178 (defun any-failures (checks)
179 (find :failure checks :key #'car))
181 (defun print-failure (failure)
183 "Style error in ~A at ~A:~A: ~A~%- ~A~%~VT^"
185 (1+ (fourth failure))
188 (with-open-file (str (second failure)) (loop :repeat (fourth failure) :do (read-line str)) (read-line str))
189 (+ (fifth failure) 2)))
191 (defun pretty-print-check-directory (dir &key copyright-notice)
192 "PRETTY-PRINT-CHECK-DIRECTORY DIR &key COPYRIGHT-NOTICE => SUCCESS
194 ARGUMENTS AND VALUES:
196 DIR: A directory to recurse into and check files
197 COPYRIGHT-NOTICE: A regex string
198 SUCCESS: T if there were no failures
202 PRETTY-PRINT-CHECK-DIRECTORY checks DIR for any errors, dumping them to output
203 and returning a single flag.
205 If COPYRIGHT-NOTICE is included, then the first line must match the
206 regular expression passed.
208 Unlike check-directory, PRETTY-PRINT-CHECK-DIRECTORY is built for continuous
209 integration, dumping errors to standard out and returning a singular result.
213 (pretty-print-check-directory \"src\") => nil"
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))))
221 ; These are in reverse order
222 (defevaluator :beginning-of-symbols " *;[^\\n]*"
223 (lambda () (set-state :normal)))
225 (defevaluator :beginning-of-symbols-with-separator " *;[^\\n]*"
226 (lambda () (set-state :comment-with-separator)))
228 (defevaluator :normal " *;[^\\n]*"
229 (lambda () (set-state :normal)))
231 (defevaluator :normal "\\("
233 (push (list *line-no* *col-no*) *form-stack*)
234 (set-state :first-symbol)))
236 (defevaluator :first-symbol "\\("
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")
243 (push (list *line-no* *col-no*) *form-stack*)
244 (set-state :first-symbol)))))
246 (defevaluator :all "\\t" (constantly "Must not use tabs"))
248 (defevaluator :begin "\\(in-package[^\\)]*\\)" (lambda () (set-state :normal)))
249 (defevaluator :copyright-required "\\(in-package[^\\)]*\\)" (constantly "Must begin with specified copyright notice"))
251 (defevaluator :beginning-of-line-with-separator :eof
254 "Must not end with empty line"))
256 (defevaluator :beginning-of-line-with-separator "\\n" (constantly "Must not have two empty lines in a row"))
258 (defevaluator :copyright-required ".*" (constantly "Must begin with specified copyright notice"))
260 (defevaluator :begin ".*" (constantly "Must begin with in-package form"))
262 (defevaluator :all "\\( *in-package " (constantly "Only one in-package per file"))
264 (defevaluator :normal "\\n"
267 (setf *col-no* :reset)
268 (set-state :beginning-of-line)))
270 (defevaluator :comment-with-separator "\\n"
273 (setf *col-no* :reset)
274 (set-state :beginning-of-line-with-comment-and-separator)
277 (defevaluator :normal " +\\n" (constantly "No whitespace at end of line"))
279 (defevaluator :beginning-of-line " *" (lambda () (set-state :beginning-of-symbols)))
281 (defevaluator :beginning-of-line-with-separator " *" (lambda () (set-state :beginning-of-symbols-with-separator)))
283 (defevaluator :beginning-of-line-with-comment-and-separator "\\n"
287 (setf *col-no* :reset)
288 (set-state :beginning-of-line-with-separator))))
290 (defevaluator :beginning-of-line-with-comment-and-separator " *"
291 (lambda () (set-state :beginning-of-symbols-with-separator)))
293 (defevaluator :beginning-of-symbols "\\n"
297 "No whitespace only lines"
300 (setf *col-no* :reset)
301 (set-state :beginning-of-line-with-separator)))))
303 (defevaluator :beginning-of-symbols "\\)" (constantly "No hanging close parens"))
305 (defevaluator :beginning-of-symbols-with-separator "\\)" (constantly "No hanging close parens"))
307 (defevaluator :beginning-of-symbols ""
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))))
314 (defevaluator :beginning-of-symbols-with-separator ""
316 (set-state :first-symbol)))
318 (defevaluator :normal "\\)"
321 ((form (pop *form-stack*)))
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)))))
327 (defevaluator :normal "::" (constantly "No internal symbols from other packages"))
329 (defevaluator :in-string "\\\\\"" (constantly nil))
331 (defevaluator :normal "\"" (lambda () (set-state :in-string)))
333 (defevaluator :in-string "\"" (lambda () (set-state :normal)))
335 (defevaluator :in-string "\\n"
338 (setf *col-no* :reset)
341 (defevaluator :in-string "." (constantly nil))
343 (defevaluator :first-symbol "\\n" (constantly "No new line after opening form"))
345 (defevaluator :first-symbol " " (constantly "No space after opening parens"))
347 (defevaluator :first-symbol ""
350 ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
351 "All form elements must be indented equally")
352 (t (set-state :normal)))))
354 (defevaluator :normal " " (constantly "Only one space between items of a form"))
356 (defevaluator :normal "." (constantly nil))