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 *form-stack* nil)
17 (defvar *form-ended-on-same-line* nil)
18 (defparameter *evaluators* 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))
60 `(pushnew (make-evaluator ,state ,match ,func) *evaluators*))
62 (defun evaluate (text)
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*))))
69 ((evaluator (find-if (lambda (f) (funcall f *state* text)) *evaluators* :from-end t :key #'car)))
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*)))
76 ((problem (funcall (third evaluator))))
78 (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
80 ((length-of-match (funcall (cadr evaluator) text)))
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)))))))
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)))
93 (defun check-file (file &key copyright-notice)
95 (copyright-notice (set-state :copyright-required))
96 ((string= "package" (pathname-name file)) (set-state :normal))
97 (t (set-state :begin)))
100 (setf *form-stack* nil)
101 (setf *form-ended-on-same-line* nil)
102 (when copyright-notice
106 (make-evaluator :copyright-required (format nil "~A\\n" copyright-notice)
109 (setf *col-no* :reset)
110 (if (string= "package" (pathname-name file))
112 (set-state :begin))))))))
115 (evaluate (slurp-file file))
116 (list :success file))
118 (list :failure file (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf)))))
120 (setf (documentation 'check-file 'function)
121 "CHECK-FILE FILE &key COPYRIGHT-NOTICE => RESULT
123 RESULT: SUCCESS-RESULT | FAILURE-RESULT
124 SUCCESS-RESULT: (:success FILENAME)
125 FAILURE-RESULT: (:success FILENAME MSG LINE-NO COL-NO)
127 ARGUMENTS AND VALUES:
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
138 CHECK-FILE runs all the checks against a file and returns
139 as soon as the first style error is found.
141 If COPYRIGHT-NOTICE is included, then the first line must match the
142 regular expression passed.
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 ...)")
150 (defun check-directory (dir &key copyright-notice)
151 "CHECK-DIRECTORY DIR &key COPYRIGHT-NOTICE => RESULTS
155 ARGUMENTS AND VALUES:
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
163 CHECK-DIRECTORY grabs all .lisp files in the tree under DIR, and loads
166 If COPYRIGHT-NOTICE is included, then the first line must match the
167 regular expression passed.
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."
173 (lambda (file) (check-file file :copyright-notice copyright-notice))
174 (directory (format nil "~A/**/*.lisp" dir))))
176 (defun any-failures (checks)
177 (find :failure checks :key #'car))
179 (defun print-failure (failure)
181 "Style error in ~A at ~A:~A: ~A~%- ~A~%~VT^"
183 (1+ (fourth failure))
186 (with-open-file (str (second failure)) (loop :repeat (fourth failure) :do (read-line str)) (read-line str))
187 (+ (fifth failure) 2)))
189 (defun pretty-print-check-directory (dir &key copyright-notice)
190 "PRETTY-PRINT-CHECK-DIRECTORY DIR &key COPYRIGHT-NOTICE => SUCCESS
192 ARGUMENTS AND VALUES:
194 DIR: A directory to recurse into and check files
195 COPYRIGHT-NOTICE: A regex string
196 SUCCESS: T if there were no failures
200 PRETTY-PRINT-CHECK-DIRECTORY checks DIR for any errors, dumping them to output
201 and returning a single flag.
203 If COPYRIGHT-NOTICE is included, then the first line must match the
204 regular expression passed.
206 Unlike check-directory, PRETTY-PRINT-CHECK-DIRECTORY is built for continuous
207 integration, dumping errors to standard out and returning a singular result.
211 (pretty-print-check-directory \"src\") => nil"
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))))
219 ; These are in reverse order
220 (defevaluator :beginning-of-symbols " *;[^\\n]*"
221 (lambda () (set-state :normal)))
223 (defevaluator :beginning-of-symbols-with-separator " *;[^\\n]*"
224 (lambda () (set-state :comment-with-separator)))
226 (defevaluator :normal " *;[^\\n]*"
227 (lambda () (set-state :normal)))
229 (defevaluator :normal "\\("
231 (push (list *line-no* *col-no*) *form-stack*)
232 (set-state :first-symbol)))
234 (defevaluator :first-symbol "\\("
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")
241 (push (list *line-no* *col-no*) *form-stack*)
242 (set-state :first-symbol)))))
244 (defevaluator :all "\\t" (constantly "Must not use tabs"))
246 (defevaluator :begin "\\(in-package[^\\)]*\\)" (lambda () (set-state :normal)))
247 (defevaluator :copyright-required "\\(in-package[^\\)]*\\)" (constantly "Must begin with specified copyright notice"))
249 (defevaluator :beginning-of-line-with-separator :eof
252 "Must not end with empty line"))
254 (defevaluator :beginning-of-line-with-separator "\\n" (constantly "Must not have two empty lines in a row"))
256 (defevaluator :copyright-required ".*" (constantly "Must begin with specified copyright notice"))
258 (defevaluator :begin ".*" (constantly "Must begin with in-package form"))
260 (defevaluator :all "\\( *in-package " (constantly "Only one in-package per file"))
262 (defevaluator :normal "\\n"
265 (setf *col-no* :reset)
266 (set-state :beginning-of-line)))
268 (defevaluator :comment-with-separator "\\n"
271 (setf *col-no* :reset)
272 (set-state :beginning-of-line-with-comment-and-separator)
275 (defevaluator :normal " +\\n" (constantly "No whitespace at end of line"))
277 (defevaluator :beginning-of-line " *" (lambda () (set-state :beginning-of-symbols)))
279 (defevaluator :beginning-of-line-with-separator " *" (lambda () (set-state :beginning-of-symbols-with-separator)))
281 (defevaluator :beginning-of-line-with-comment-and-separator "\\n"
285 (setf *col-no* :reset)
286 (set-state :beginning-of-line-with-separator))))
288 (defevaluator :beginning-of-line-with-comment-and-separator " *"
289 (lambda () (set-state :beginning-of-symbols-with-separator)))
291 (defevaluator :beginning-of-symbols "\\n"
295 "No whitespace only lines"
298 (setf *col-no* :reset)
299 (set-state :beginning-of-line-with-separator)))))
301 (defevaluator :beginning-of-symbols "\\)" (constantly "No hanging close parens"))
303 (defevaluator :beginning-of-symbols-with-separator "\\)" (constantly "No hanging close parens"))
305 (defevaluator :beginning-of-symbols ""
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))))
312 (defevaluator :beginning-of-symbols-with-separator ""
314 (set-state :first-symbol)))
316 (defevaluator :normal "\\)"
319 ((form (pop *form-stack*)))
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)))))
325 (defevaluator :normal "::" (constantly "No internal symbols from other packages"))
327 (defevaluator :in-string "\\\\\"" (constantly nil))
329 (defevaluator :normal "\"" (lambda () (set-state :in-string)))
331 (defevaluator :in-string "\"" (lambda () (set-state :normal)))
333 (defevaluator :in-string "\\n"
336 (setf *col-no* :reset)
339 (defevaluator :in-string "." (constantly nil))
341 (defevaluator :first-symbol "\\n" (constantly "No new line after opening form"))
343 (defevaluator :first-symbol " " (constantly "No space after opening parens"))
345 (defevaluator :first-symbol ""
348 ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
349 "All form elements must be indented equally")
350 (t (set-state :normal)))))
352 (defevaluator :normal " " (constantly "Only one space between items of a form"))
354 (defevaluator :normal "." (constantly nil))