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 (if (string= "package" (pathname-name file))
95 (setf *form-stack* nil)
96 (setf *form-ended-on-same-line* nil)
99 (evaluate (slurp-file file))
100 (list :success file))
102 (list :failure file (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf)))))
104 (defun check-directory (dir)
105 (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir))))
107 (defun any-failures (checks)
108 (find :failure checks :key #'car))
110 (defun print-failure (failure)
112 "Style error in ~A at ~A:~A: ~A~%- ~A~%~VT^"
114 (1+ (fourth failure))
117 (with-open-file (str (second failure)) (loop :repeat (fourth failure) :do (read-line str)) (read-line str))
118 (+ (fifth failure) 2)))
120 (defun pretty-print-check-directory (dir)
122 ((checks (check-directory dir)))
123 (format t "In ~A: Checked ~A files with ~A failures~%~%"
124 dir (length checks) (length (remove :success checks :key #'car)))
125 (format t "~{~A~%~}" (mapcar #'print-failure (remove :success checks :key #'car)))
126 (not (any-failures checks))))
128 ; These are in reverse order
129 (defevaluator :beginning-of-symbols " *;[^\\n]*"
130 (lambda () (set-state :normal)))
132 (defevaluator :beginning-of-symbols-with-separator " *;[^\\n]*"
133 (lambda () (set-state :comment-with-separator)))
135 (defevaluator :normal " *;[^\\n]*"
136 (lambda () (set-state :normal)))
138 (defevaluator :normal "\\("
140 (push (list *line-no* *col-no*) *form-stack*)
141 (set-state :first-symbol)))
143 (defevaluator :first-symbol "\\("
146 ((and (not *form-stack*) (not (zerop *col-no*))) "Top level forms must begin on first column")
147 ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
148 "All form elements must be indented equally")
150 (push (list *line-no* *col-no*) *form-stack*)
151 (set-state :first-symbol)))))
153 (defevaluator :all "\\t" (constantly "Must not use tabs"))
155 (defevaluator :begin "\\(in-package[^\\)]*\\)" (lambda () (set-state :normal)))
157 (defevaluator :beginning-of-line-with-separator :eof
160 "Must not end with empty line"))
162 (defevaluator :beginning-of-line-with-separator "\\n" (constantly "Must not have two empty lines in a row"))
164 (defevaluator :begin ".*" (constantly "Must begin with in-package form"))
166 (defevaluator :all "\\( *in-package " (constantly "Only one in-package per file"))
168 (defevaluator :normal "\\n"
172 (set-state :beginning-of-line)))
174 (defevaluator :comment-with-separator "\\n"
178 (set-state :beginning-of-line-with-comment-and-separator)
181 (defevaluator :normal " +\\n" (constantly "No whitespace at end of line"))
183 (defevaluator :beginning-of-line " *" (lambda () (set-state :beginning-of-symbols)))
185 (defevaluator :beginning-of-line-with-separator " *" (lambda () (set-state :beginning-of-symbols-with-separator)))
187 (defevaluator :beginning-of-line-with-comment-and-separator "\\n"
192 (set-state :beginning-of-line-with-separator))))
194 (defevaluator :beginning-of-line-with-comment-and-separator " *"
195 (lambda () (set-state :beginning-of-symbols-with-separator)))
197 (defevaluator :beginning-of-symbols "\\n"
201 "No whitespace only lines"
205 (set-state :beginning-of-line-with-separator)))))
207 (defevaluator :beginning-of-symbols "\\)" (constantly "No hanging close parens"))
209 (defevaluator :beginning-of-symbols-with-separator "\\)" (constantly "No hanging close parens"))
211 (defevaluator :beginning-of-symbols ""
214 (and (not *form-stack*) (not *form-ended-on-same-line*))
215 "Multiline top level forms must be separated by a space"
216 (set-state :first-symbol))))
218 (defevaluator :beginning-of-symbols-with-separator ""
220 (set-state :first-symbol)))
222 (defevaluator :normal "\\)"
225 ((form (pop *form-stack*)))
227 ((not form) "Unmatched ending paren")
228 ((< 50 (- *line-no* (car form))) "Forms can't be over 50 lines long")
229 (t (setf *form-ended-on-same-line* (= *line-no* (car form))) nil)))))
231 (defevaluator :normal "::" (constantly "No internal symbols from other packages"))
233 (defevaluator :in-string "\\\\\"" (constantly nil))
235 (defevaluator :normal "\"" (lambda () (set-state :in-string)))
237 (defevaluator :in-string "\"" (lambda () (set-state :normal)))
239 (defevaluator :in-string "\\n"
245 (defevaluator :in-string "." (constantly nil))
247 (defevaluator :first-symbol "\\n" (constantly "No new line after opening form"))
249 (defevaluator :first-symbol " " (constantly "No space after opening parens"))
251 (defevaluator :first-symbol ""
254 ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
255 "All form elements must be indented equally")
256 (t (set-state :normal)))))
258 (defevaluator :normal " " (constantly "Only one space between items of a form"))
260 (defevaluator :normal "." (constantly nil))