1 (in-package #:syntax-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
55 (defun set-state (state)
56 (when (not (find state *possible-states*))
57 (error "Can't set state to ~A" state))
61 (defmacro defevaluator (state match func)
62 (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
66 ((,scanner (when (stringp ,match) (cl-ppcre:create-scanner ,match))))
71 (or (eql :all ,state) (eql ,state state))
73 (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)))
93 (make-condition 'check-failure
94 :msg (format nil "Can't check in state ~S: ~S..."
95 *state* (subseq text 0 (min (length text) 10))) :line-no *line-no* :col-no *col-no*)))
97 ((problem (funcall (third evaluator))))
99 (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
101 ((length-of-match (funcall (cadr evaluator) text)))
102 (incf *col-no* length-of-match)
103 (when (< 120 *col-no*)
104 (error (make-condition 'check-failure :msg "Line longer than 120 characters" :line-no *line-no* :col-no 0)))
105 (evaluate (subseq text length-of-match)))))))
107 (defun slurp-file (filename &key (element-type 'character) (sequence-type 'string))
108 (with-open-file (str filename :element-type element-type)
109 (let ((seq (make-sequence sequence-type (file-length str)))) (read-sequence seq str) seq)))
111 (defun check-file (file)
112 (if (string= "package" (pathname-name file))
117 (setf *form-stack* nil)
118 (setf *form-ended-on-same-line* nil)
121 (evaluate (slurp-file file))
122 (list :success file))
124 (list :failure file (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf)))))
126 (defun check-directory (dir)
127 (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir))))
129 (defun any-failures (checks)
130 (find :failure checks :key #'car))
132 (defun print-failure (failure)
134 "Style error in ~A at ~A:~A: ~A~%- ~A~%~VT^"
136 (1+ (fourth failure))
139 (with-open-file (str (second failure)) (loop :repeat (fourth failure) :do (read-line str)) (read-line str))
140 (+ (fifth failure) 2)))
142 (defun pretty-print-check-directory (dir)
144 ((checks (check-directory dir)))
145 (format t "In ~A: Checked ~A files with ~A failures~%~%"
146 dir (length checks) (length (remove :success checks :key #'car)))
147 (format t "~{~A~%~}" (mapcar #'print-failure (remove :success checks :key #'car)))
148 (not (any-failures checks))))
150 ; These are in reverse order
151 (defevaluator :beginning-of-symbols " *;[^\\n]*"
152 (lambda () (set-state :normal)))
154 (defevaluator :beginning-of-symbols-with-separator " *;[^\\n]*"
155 (lambda () (set-state :comment-with-separator)))
157 (defevaluator :normal " *;[^\\n]*"
158 (lambda () (set-state :normal)))
160 (defevaluator :normal "\\("
162 (push (list *line-no* *col-no*) *form-stack*)
163 (set-state :first-symbol)))
165 (defevaluator :first-symbol "\\("
168 ((and (not *form-stack*) (not (zerop *col-no*))) "Top level forms must begin on first column")
169 ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
170 "All form elements must be indented equally")
172 (push (list *line-no* *col-no*) *form-stack*)
173 (set-state :first-symbol)))))
175 (defevaluator :all "\\t" (constantly "Must not use tabs"))
177 (defevaluator :begin "\\(in-package[^\\)]*\\)" (lambda () (set-state :normal)))
179 (defevaluator :beginning-of-line-with-separator :eof
182 "Must not end with empty line"))
184 (defevaluator :beginning-of-line-with-separator "\\n" (constantly "Must not have two empty lines in a row"))
186 (defevaluator :begin ".*" (constantly "Must begin with in-package form"))
188 (defevaluator :all "\\( *in-package " (constantly "Only one in-package per file"))
190 (defevaluator :normal "\\n"
194 (set-state :beginning-of-line)))
196 (defevaluator :comment-with-separator "\\n"
200 (set-state :beginning-of-line-with-comment-and-separator)
203 (defevaluator :normal " +\\n" (constantly "No whitespace at end of line"))
205 (defevaluator :beginning-of-line " *" (lambda () (set-state :beginning-of-symbols)))
207 (defevaluator :beginning-of-line-with-separator " *" (lambda () (set-state :beginning-of-symbols-with-separator)))
209 (defevaluator :beginning-of-line-with-comment-and-separator "\\n"
214 (set-state :beginning-of-line-with-separator))))
216 (defevaluator :beginning-of-line-with-comment-and-separator " *"
217 (lambda () (set-state :beginning-of-symbols-with-separator)))
219 (defevaluator :beginning-of-symbols "\\n"
223 "No whitespace only lines"
227 (set-state :beginning-of-line-with-separator)))))
229 (defevaluator :beginning-of-symbols "\\)" (constantly "No hanging close parens"))
231 (defevaluator :beginning-of-symbols-with-separator "\\)" (constantly "No hanging close parens"))
233 (defevaluator :beginning-of-symbols ""
236 (and (not *form-stack*) (not *form-ended-on-same-line*))
237 "Multiline top level forms must be separated by a space"
238 (set-state :first-symbol))))
240 (defevaluator :beginning-of-symbols-with-separator ""
242 (set-state :first-symbol)))
244 (defevaluator :normal "\\)"
247 ((form (pop *form-stack*)))
249 ((not form) "Unmatched ending paren")
250 ((< 50 (- *line-no* (car form))) "Forms can't be over 50 lines long")
251 (t (setf *form-ended-on-same-line* (= *line-no* (car form))) nil)))))
253 (defevaluator :normal "::" (constantly "No internal symbols from other packages"))
255 (defevaluator :in-string "\\\\\"" (constantly nil))
257 (defevaluator :normal "\"" (lambda () (set-state :in-string)))
259 (defevaluator :in-string "\"" (lambda () (set-state :normal)))
261 (defevaluator :in-string "\\n"
267 (defevaluator :in-string "." (constantly nil))
269 (defevaluator :first-symbol "\\n" (constantly "No new line after opening form"))
271 (defevaluator :first-symbol " " (constantly "No space after opening parens"))
273 (defevaluator :first-symbol ""
276 ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
277 "All form elements must be indented equally")
278 (t (set-state :normal)))))
280 (defevaluator :normal " " (constantly "Only one space between items of a form"))
282 (defevaluator :normal "." (constantly nil))