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
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)))
13 (defvar *line-no* nil)
15 (defvar *evaluators* nil)
16 (defvar *form-stack* nil)
17 (defvar *form-ended-on-same-line* nil)
19 (eval-when (:compile-toplevel :load-toplevel :execute)
20 (defparameter *possible-states*
22 :begin ; start of file
23 :normal ; normal processing
25 :beginning-of-line-with-separator ; empty space in there
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
34 (defun set-state (state)
35 (when (not (find state *possible-states*))
36 (error "Can't set state to ~A" state))
40 (defun make-evaluator (eval-state match func)
42 ((scanner (when (stringp match) (cl-ppcre:create-scanner match))))
46 (or (eql :all eval-state) (eql eval-state state))
48 (and (symbolp text) (eql text match))
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)))
57 (defmacro defevaluator (state match func)
58 (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
61 `(pushnew (make-evaluator ,state ,match ,func) *evaluators*)))
63 (defun evaluate (text)
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*))))
70 ((evaluator (find-if (lambda (f) (funcall f *state* text)) *evaluators* :from-end t :key #'car)))
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*)))
77 ((problem (funcall (third evaluator))))
79 (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
81 ((length-of-match (funcall (cadr evaluator) text)))
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)))))))
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)))
94 (defun check-file (file &key copyright-notice)
96 (copyright-notice (set-state :copyright-required))
97 ((string= "package" (pathname-name file)) (set-state :normal))
98 (t (set-state :begin)))
101 (setf *form-stack* nil)
102 (setf *form-ended-on-same-line* nil)
103 (when copyright-notice
107 (make-evaluator :copyright-required (format nil "~A\\n" copyright-notice)
110 (setf *col-no* :reset)
111 (if (string= "package" (pathname-name file))
113 (set-state :begin))))))))
116 (evaluate (slurp-file file))
117 (list :success file))
119 (list :failure file (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf)))))
121 (setf (documentation 'check-file 'function)
122 "CHECK-FILE FILE &key COPYRIGHT-NOTICE => RESULT
124 RESULT: SUCCESS-RESULT | FAILURE-RESULT
125 SUCCESS-RESULT: (:success FILENAME)
126 FAILURE-RESULT: (:success FILENAME MSG LINE-NO COL-NO)
128 ARGUMENTS AND VALUES:
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
139 CHECK-FILE runs all the checks against a file and returns
140 as soon as the first style error is found.
142 If COPYRIGHT-NOTICE is included, then the first line must match the
143 regular expression passed.
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 ...)")
151 (defun check-directory (dir &key copyright-notice)
152 "CHECK-DIRECTORY DIR &key COPYRIGHT-NOTICE => RESULTS
156 ARGUMENTS AND VALUES:
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
164 CHECK-DIRECTORY grabs all .lisp files in the tree under DIR, and loads
167 If COPYRIGHT-NOTICE is included, then the first line must match the
168 regular expression passed.
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."
174 (lambda (file) (check-file file :copyright-notice copyright-notice))
175 (directory (format nil "~A/**/*.lisp" dir))))
177 (defun any-failures (checks)
178 (find :failure checks :key #'car))
180 (defun print-failure (failure)
182 "Style error in ~A at ~A:~A: ~A~%- ~A~%~VT^"
184 (1+ (fourth failure))
187 (with-open-file (str (second failure)) (loop :repeat (fourth failure) :do (read-line str)) (read-line str))
188 (+ (fifth failure) 2)))
190 (defun pretty-print-check-directory (dir &key copyright-notice)
191 "PRETTY-PRINT-CHECK-DIRECTORY DIR &key COPYRIGHT-NOTICE => SUCCESS
193 ARGUMENTS AND VALUES:
195 DIR: A directory to recurse into and check files
196 COPYRIGHT-NOTICE: A regex string
197 SUCCESS: T if there were no failures
201 PRETTY-PRINT-CHECK-DIRECTORY checks DIR for any errors, dumping them to output
202 and returning a single flag.
204 If COPYRIGHT-NOTICE is included, then the first line must match the
205 regular expression passed.
207 Unlike check-directory, PRETTY-PRINT-CHECK-DIRECTORY is built for continuous
208 integration, dumping errors to standard out and returning a singular result.
212 (pretty-print-check-directory \"src\") => nil"
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))))
220 ; These are in reverse order
221 (defevaluator :beginning-of-symbols " *;[^\\n]*"
222 (lambda () (set-state :normal)))
224 (defevaluator :beginning-of-symbols-with-separator " *;[^\\n]*"
225 (lambda () (set-state :comment-with-separator)))
227 (defevaluator :normal " *;[^\\n]*"
228 (lambda () (set-state :normal)))
230 (defevaluator :normal "\\("
232 (push (list *line-no* *col-no*) *form-stack*)
233 (set-state :first-symbol)))
235 (defevaluator :first-symbol "\\("
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")
242 (push (list *line-no* *col-no*) *form-stack*)
243 (set-state :first-symbol)))))
245 (defevaluator :all "\\t" (constantly "Must not use tabs"))
247 (defevaluator :begin "\\(in-package[^\\)]*\\)" (lambda () (set-state :normal)))
248 (defevaluator :copyright-required "\\(in-package[^\\)]*\\)" (constantly "Must begin with specified copyright notice"))
250 (defevaluator :beginning-of-line-with-separator :eof
253 "Must not end with empty line"))
255 (defevaluator :beginning-of-line-with-separator "\\n" (constantly "Must not have two empty lines in a row"))
257 (defevaluator :copyright-required ".*" (constantly "Must begin with specified copyright notice"))
259 (defevaluator :begin ".*" (constantly "Must begin with in-package form"))
261 (defevaluator :all "\\( *in-package " (constantly "Only one in-package per file"))
263 (defevaluator :normal "\\n"
266 (setf *col-no* :reset)
267 (set-state :beginning-of-line)))
269 (defevaluator :comment-with-separator "\\n"
272 (setf *col-no* :reset)
273 (set-state :beginning-of-line-with-comment-and-separator)
276 (defevaluator :normal " +\\n" (constantly "No whitespace at end of line"))
278 (defevaluator :beginning-of-line " *" (lambda () (set-state :beginning-of-symbols)))
280 (defevaluator :beginning-of-line-with-separator " *" (lambda () (set-state :beginning-of-symbols-with-separator)))
282 (defevaluator :beginning-of-line-with-comment-and-separator "\\n"
286 (setf *col-no* :reset)
287 (set-state :beginning-of-line-with-separator))))
289 (defevaluator :beginning-of-line-with-comment-and-separator " *"
290 (lambda () (set-state :beginning-of-symbols-with-separator)))
292 (defevaluator :beginning-of-symbols "\\n"
296 "No whitespace only lines"
299 (setf *col-no* :reset)
300 (set-state :beginning-of-line-with-separator)))))
302 (defevaluator :beginning-of-symbols "\\)" (constantly "No hanging close parens"))
304 (defevaluator :beginning-of-symbols-with-separator "\\)" (constantly "No hanging close parens"))
306 (defevaluator :beginning-of-symbols ""
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))))
313 (defevaluator :beginning-of-symbols-with-separator ""
315 (set-state :first-symbol)))
317 (defevaluator :normal "\\)"
320 ((form (pop *form-stack*)))
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)))))
326 (defevaluator :normal "::" (constantly "No internal symbols from other packages"))
328 (defevaluator :in-string "\\\\\"" (constantly nil))
330 (defevaluator :normal "\"" (lambda () (set-state :in-string)))
332 (defevaluator :in-string "\"" (lambda () (set-state :normal)))
334 (defevaluator :in-string "\\n"
337 (setf *col-no* :reset)
340 (defevaluator :in-string "." (constantly nil))
342 (defevaluator :first-symbol "\\n" (constantly "No new line after opening form"))
344 (defevaluator :first-symbol " " (constantly "No space after opening parens"))
346 (defevaluator :first-symbol ""
349 ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
350 "All form elements must be indented equally")
351 (t (set-state :normal)))))
353 (defevaluator :normal " " (constantly "Only one space between items of a form"))
355 (defevaluator :normal "." (constantly nil))