1 (in-package #:syntax-checker)
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*
21 '(:begin ; start of file
22 :normal ; normal processing
24 :beginning-of-line-with-separator ; empty space in there
26 :beginning-of-symbols-with-separator
27 :comment-with-separator ; weird edge case for pre-function comments
28 :beginning-of-line-with-comment-and-separator ; weird edge case part 2
29 :first-symbol ; first symbol of form/line
30 :all ; matches everything
33 (defun set-state (state)
34 (when (not (find state *possible-states*))
35 (error "Can't set state to ~A" state))
39 (defmacro defevaluator (state match func)
40 (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
44 ((,scanner (when (stringp ,match) (cl-ppcre:create-scanner ,match))))
49 (or (eql :all ,state) (eql ,state state))
51 (and (symbolp text) (eql text ,match))
55 (multiple-value-bind (start end) (cl-ppcre:scan ,scanner text)
56 (and start end (= 0 start)))))))
57 (lambda (text) (second (multiple-value-list (cl-ppcre:scan ,scanner text))))
61 (defun evaluate (text)
64 ((evaluator (find-if (lambda (f) (funcall f *state* :eof)) *evaluators* :from-end t :key #'car))
65 (problem (when evaluator (funcall (third evaluator)))))
66 (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*))))
68 ((evaluator (find-if (lambda (f) (funcall f *state* text)) *evaluators* :from-end t :key #'car)))
71 (make-condition 'check-failure
72 :msg (format nil "Can't check in state ~S: ~S..."
73 *state* (subseq text 0 (min (length text) 10))) :line-no *line-no* :col-no *col-no*)))
75 ((problem (funcall (third evaluator))))
77 (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
79 ((length-of-match (funcall (cadr evaluator) text)))
80 (incf *col-no* length-of-match)
81 (when (< 120 *col-no*)
82 (error (make-condition 'check-failure :msg "Line longer than 120 characters" :line-no *line-no* :col-no 0)))
83 (evaluate (subseq text length-of-match)))))))
85 (defun slurp-file (filename &key (element-type 'character) (sequence-type 'string))
86 (with-open-file (str filename :element-type element-type)
87 (let ((seq (make-sequence sequence-type (file-length str)))) (read-sequence seq str) seq)))
89 (defun check-file (file)
90 "CHECK-FILE FILE => RESULT
92 RESULT: SUCCESS-RESULT | FAILURE-RESULT
93 SUCCESS-RESULT: (:success FILENAME)
94 FAILURE-RESULT: (:success FILENAME MSG LINE-NO COL-NO)
99 FILENAME: the file this check was run on
100 MSG: a string containing the failure message
101 LINE-NO: an integer, the line number on which the failure appeared
102 COL-NO: an integer, the column number on which the failure appeared
106 CHECK-FILE runs all the checks against a file and returns
107 as soon as the first style error is found.
111 (check-file #P\"path/to/file.lisp\") => (:success \"path/to/file.lisp\")
112 (check-file #P\"path/to/error.lisp\") => (:failure \"path/to/error.lisp\" \"File cannot end with empty line\" 20 0)"
114 (if (string= "package" (pathname-name file))
119 (setf *form-stack* nil)
120 (setf *form-ended-on-same-line* nil)
123 (evaluate (slurp-file file))
124 (list :success file))
126 (list :failure file (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf)))))
128 (defun check-directory (dir)
129 "CHECK-DIRECTORY DIR => RESULTS
133 ARGUMENTS AND VALUES:
135 DIR: A directory to recurse into and check files
136 RESULT: A result as returned by check-file
140 CHECK-DIRECTORY grabs all .lisp files in the tree under DIR, and loads
143 The results are then put together into a list which can be programatically
144 evaluated. As opposed to pretty-print-check-directory, this function doesn't
145 clutter up your standard out."
146 (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir))))
148 (defun any-failures (checks)
149 (find :failure checks :key #'car))
151 (defun print-failure (failure)
153 "Style error in ~A at ~A:~A: ~A~%- ~A~%~VT^"
155 (1+ (fourth failure))
158 (with-open-file (str (second failure)) (loop :repeat (fourth failure) :do (read-line str)) (read-line str))
159 (+ (fifth failure) 2)))
161 (defun pretty-print-check-directory (dir)
162 "PRETTY-PRINT-CHECK-DIRECTORY DIR => SUCCESS
164 ARGUMENTS AND VALUES:
166 DIR: A directory to recurse into and check files
167 SUCCESS: T if there were no failures
171 PRETTY-PRINT-CHECK-DIRECTORY checks DIR for any errors, dumping them to output
172 and returning a single flag.
174 Unlike check-directory, PRETTY-PRINT-CHECK-DIRECTORY is built for continuous
175 integration, dumping errors to standard out and returning a singular result.
179 (pretty-print-check-directory \"src\") => nil"
181 ((checks (check-directory dir)))
182 (format t "In ~A: Checked ~A files with ~A failures~%~%"
183 dir (length checks) (length (remove :success checks :key #'car)))
184 (format t "~{~A~%~}" (mapcar #'print-failure (remove :success checks :key #'car)))
185 (not (any-failures checks))))
187 ; These are in reverse order
188 (defevaluator :beginning-of-symbols " *;[^\\n]*"
189 (lambda () (set-state :normal)))
191 (defevaluator :beginning-of-symbols-with-separator " *;[^\\n]*"
192 (lambda () (set-state :comment-with-separator)))
194 (defevaluator :normal " *;[^\\n]*"
195 (lambda () (set-state :normal)))
197 (defevaluator :normal "\\("
199 (push (list *line-no* *col-no*) *form-stack*)
200 (set-state :first-symbol)))
202 (defevaluator :first-symbol "\\("
205 ((and (not *form-stack*) (not (zerop *col-no*))) "Top level forms must begin on first column")
206 ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
207 "All form elements must be indented equally")
209 (push (list *line-no* *col-no*) *form-stack*)
210 (set-state :first-symbol)))))
212 (defevaluator :all "\\t" (constantly "Must not use tabs"))
214 (defevaluator :begin "\\(in-package[^\\)]*\\)" (lambda () (set-state :normal)))
216 (defevaluator :beginning-of-line-with-separator :eof
219 "Must not end with empty line"))
221 (defevaluator :beginning-of-line-with-separator "\\n" (constantly "Must not have two empty lines in a row"))
223 (defevaluator :begin ".*" (constantly "Must begin with in-package form"))
225 (defevaluator :all "\\( *in-package " (constantly "Only one in-package per file"))
227 (defevaluator :normal "\\n"
231 (set-state :beginning-of-line)))
233 (defevaluator :comment-with-separator "\\n"
237 (set-state :beginning-of-line-with-comment-and-separator)
240 (defevaluator :normal " +\\n" (constantly "No whitespace at end of line"))
242 (defevaluator :beginning-of-line " *" (lambda () (set-state :beginning-of-symbols)))
244 (defevaluator :beginning-of-line-with-separator " *" (lambda () (set-state :beginning-of-symbols-with-separator)))
246 (defevaluator :beginning-of-line-with-comment-and-separator "\\n"
251 (set-state :beginning-of-line-with-separator))))
253 (defevaluator :beginning-of-line-with-comment-and-separator " *"
254 (lambda () (set-state :beginning-of-symbols-with-separator)))
256 (defevaluator :beginning-of-symbols "\\n"
260 "No whitespace only lines"
264 (set-state :beginning-of-line-with-separator)))))
266 (defevaluator :beginning-of-symbols "\\)" (constantly "No hanging close parens"))
268 (defevaluator :beginning-of-symbols-with-separator "\\)" (constantly "No hanging close parens"))
270 (defevaluator :beginning-of-symbols ""
273 (and (not *form-stack*) (not *form-ended-on-same-line*))
274 "Multiline top level forms must be separated by a space"
275 (set-state :first-symbol))))
277 (defevaluator :beginning-of-symbols-with-separator ""
279 (set-state :first-symbol)))
281 (defevaluator :normal "\\)"
284 ((form (pop *form-stack*)))
286 ((not form) "Unmatched ending paren")
287 ((< 50 (- *line-no* (car form))) "Forms can't be over 50 lines long")
288 (t (setf *form-ended-on-same-line* (= *line-no* (car form))) nil)))))
290 (defevaluator :normal "::" (constantly "No internal symbols from other packages"))
292 (defevaluator :in-string "\\\\\"" (constantly nil))
294 (defevaluator :normal "\"" (lambda () (set-state :in-string)))
296 (defevaluator :in-string "\"" (lambda () (set-state :normal)))
298 (defevaluator :in-string "\\n"
304 (defevaluator :in-string "." (constantly nil))
306 (defevaluator :first-symbol "\\n" (constantly "No new line after opening form"))
308 (defevaluator :first-symbol " " (constantly "No space after opening parens"))
310 (defevaluator :first-symbol ""
313 ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
314 "All form elements must be indented equally")
315 (t (set-state :normal)))))
317 (defevaluator :normal " " (constantly "Only one space between items of a form"))
319 (defevaluator :normal "." (constantly nil))