1 (in-package #:style-checker)
4 ; * Elements on new line in each form must be indented the same amount
5 ; * No space/newline after open parens
6 ; * No form longer than 50 lines
7 ; * Top level multiline forms must be separated by exactly one space
8 ; * No line longer than 120 characters
9 ; * No use of unexported symbols in other packages
11 ; * Only one space between elements in a form on a single line
12 ; * in-package must be first line in file unless file is package.lisp
13 ; * No whitespace at end of line
14 ; * No lines that are only whitespace
15 ; * No empty lines at end of file
16 ; * Never have two empty lines in a row
17 ; * Only one in-package per file
18 ; * No hanging close parens
23 ; * exclude in-package check from package.lisp
26 ; - form starting reader macros will have to be hand added to this code
27 ; - exceptions will eventually arise, and the rule file will have to be changed
28 ; - the proper formatting of "loop" is weird
30 (define-condition check-failure nil ((msg :initarg :msg :reader check-failure-msg)
31 (line-no :initarg :line-no :reader check-failure-line-no)
32 (col-no :initarg :col-no :reader check-failure-col-no)))
35 (defvar *line-no* nil)
37 (defvar *evaluators* nil)
38 (defvar *form-stack* nil)
39 (defvar *form-ended-on-same-line* nil)
41 (eval-when (:compile-toplevel :load-toplevel :execute)
42 (defparameter *possible-states*
43 '(:begin ; start of file
44 :normal ; normal processing
46 :beginning-of-line-with-separator ; empty space in there
48 :beginning-of-symbols-with-separator
49 :comment-with-separator ; weird edge case for pre-function comments
50 :beginning-of-line-with-comment-and-separator ; weird edge case part 2
51 :first-symbol ; first symbol of form/line
52 :all ; matches everything
56 (defun set-state (state)
57 (when (not (find state *possible-states*))
58 (error "Can't set state to ~A" state))
62 (defmacro defevaluator (state match func)
63 (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
67 ((,scanner (when (stringp ,match) (cl-ppcre:create-scanner ,match))))
72 (or (eql :all ,state) (eql ,state state))
74 (and (symbolp text) (eql text ,match))
77 (multiple-value-bind (start end) (cl-ppcre:scan ,scanner text)
78 (and start end (= 0 start)))))))
79 (lambda (text) (second (multiple-value-list (cl-ppcre:scan ,scanner text))))
83 (defun evaluate (text)
86 ((evaluator (find-if (lambda (f) (funcall f *state* :eof)) *evaluators* :from-end t :key #'car))
87 (problem (when evaluator (funcall (third evaluator)))))
88 (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*))))
90 ((evaluator (find-if (lambda (f) (funcall f *state* text)) *evaluators* :from-end t :key #'car)))
91 (when (not evaluator) (error (make-condition 'check-failure :msg (format nil "Can't check in state ~S: ~S..." *state* (subseq text 0 (min (length text) 10))) :line-no *line-no* :col-no *col-no*)))
93 ((problem (funcall (third evaluator))))
95 (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
97 ((length-of-match (funcall (cadr evaluator) text)))
98 (incf *col-no* length-of-match)
99 (when (< 120 *col-no*) (error (make-condition 'check-failure :msg "Line longer than 120 characters" :line-no *line-no* :col-no 0)))
100 (evaluate (subseq text length-of-match)))))))
102 (defun slurp-file (filename &key (element-type 'character) (sequence-type 'string))
103 (with-open-file (str filename :element-type element-type)
104 (let ((seq (make-sequence sequence-type (file-length str)))) (read-sequence seq str) seq)))
106 (defun check-file (file)
107 (if (string= "package" (pathname-name file))
112 (setf *form-stack* nil)
113 (setf *form-ended-on-same-line* nil)
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 (defun check-directory (dir)
122 (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir))))
124 (defun any-failures (checks)
125 (find :failure checks :key #'car))
127 (defun print-failure (failure)
129 "Style error in ~A at ~A:~A: ~A~%- ~A~%~VT^"
131 (1+ (fourth failure))
134 (with-open-file (str (second failure)) (loop :repeat (fourth failure) :do (read-line str)) (read-line str))
135 (+ (fifth failure) 2)))
137 (defun pretty-print-check-directory (dir)
139 ((checks (check-directory dir)))
140 (format t "In ~A: Checked ~A files with ~A failures~%~%" dir (length checks) (length (remove :success checks :key #'car)))
141 (format t "~{~A~%~}" (mapcar #'print-failure (remove :success checks :key #'car)))
142 (sb-ext:exit :code (if (any-failures checks) 1 0))))
144 ; These are in reverse order
146 (setf *evaluators* nil)
147 (defevaluator :beginning-of-symbols " *;[^\\n]*"
148 (lambda () (set-state :normal)))
149 (defevaluator :beginning-of-symbols-with-separator " *;[^\\n]*"
150 (lambda () (set-state :comment-with-separator)))
151 (defevaluator :normal " *;[^\\n]*"
152 (lambda () (set-state :normal)))
153 (defevaluator :normal "\\("
155 (push (list *line-no* *col-no*) *form-stack*)
156 (set-state :first-symbol)))
157 (defevaluator :first-symbol "\\("
160 ((and (not *form-stack*) (not (zerop *col-no*))) "Top level forms must begin on first column")
161 ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
162 "All form elements must be indented equally")
164 (push (list *line-no* *col-no*) *form-stack*)
165 (set-state :first-symbol)))))
166 (defevaluator :all "\\t" (constantly "Must not use tabs"))
167 (defevaluator :begin "\\(in-package[^\\)]*\\)" (lambda () (set-state :normal)))
168 (defevaluator :beginning-of-line-with-separator :eof
171 "Must not end with empty line"))
172 (defevaluator :beginning-of-line-with-separator "\\n" (constantly "Must not have two empty lines in a row"))
173 (defevaluator :begin ".*" (constantly "Must begin with in-package form"))
174 (defevaluator :all "\\( *in-package " (constantly "Only one in-package per file"))
175 (defevaluator :normal "\\n"
179 (set-state :beginning-of-line)))
180 (defevaluator :comment-with-separator "\\n"
184 (set-state :beginning-of-line-with-comment-and-separator)
186 (defevaluator :normal " +\\n" (constantly "No whitespace at end of line"))
187 (defevaluator :beginning-of-line " *" (lambda () (set-state :beginning-of-symbols)))
188 (defevaluator :beginning-of-line-with-separator " *" (lambda () (set-state :beginning-of-symbols-with-separator)))
189 (defevaluator :beginning-of-line-with-comment-and-separator "\\n"
194 (set-state :beginning-of-line-with-separator))))
195 (defevaluator :beginning-of-line-with-comment-and-separator " *" (lambda () (set-state :beginning-of-symbols-with-separator)))
196 (defevaluator :beginning-of-symbols "\\n"
200 "No whitespace only lines"
204 (set-state :beginning-of-line-with-separator)))))
205 (defevaluator :beginning-of-symbols "\\)" (constantly "No hanging close parens"))
206 (defevaluator :beginning-of-symbols-with-separator "\\)" (constantly "No hanging close parens"))
207 (defevaluator :beginning-of-symbols ""
210 (and (not *form-stack*) (not *form-ended-on-same-line*))
211 "Multiline top level forms must be separated by a space"
212 (set-state :first-symbol))))
213 (defevaluator :beginning-of-symbols-with-separator ""
215 (set-state :first-symbol)))
216 (defevaluator :normal "\\)"
219 ((form (pop *form-stack*)))
221 ((not form) "Unmatched ending paren")
222 ((< 50 (- *line-no* (car form))) "Forms can't be over 50 lines long")
223 (t (setf *form-ended-on-same-line* (= *line-no* (car form))) nil)))))
224 (defevaluator :normal "::" (constantly "No internal symbols from other packages"))
225 (defevaluator :in-string "\\\\\"" (constantly nil))
226 (defevaluator :normal "\"" (lambda () (set-state :in-string)))
227 (defevaluator :in-string "\"" (lambda () (set-state :normal)))
228 (defevaluator :in-string "\\n"
233 (defevaluator :in-string "." (constantly nil))
234 (defevaluator :first-symbol "\\n" (constantly "No new line after opening form"))
235 (defevaluator :first-symbol " " (constantly "No space after opening parens"))
236 (defevaluator :first-symbol ""
239 ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
240 "All form elements must be indented equally")
241 (t (set-state :normal)))))
242 (defevaluator :normal " " (constantly "Only one space between items of a form"))
243 (defevaluator :normal "." (constantly nil)))