Add exception for package.lisp
[wolf] / src / main / checker.lisp
1 (in-package #:style-checker)
2
3 ; Rules
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
10 ; * No tabs
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
19 ;
20 ; Exceptions
21 ; * comments
22 ; * multiline strings
23 ; * exclude in-package check from package.lisp
24
25 ; Some thoughts
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
29
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)))
33
34 (defvar *state* nil)
35 (defvar *line-no* nil)
36 (defvar *col-no* nil)
37 (defvar *evaluators* nil)
38 (defvar *form-stack* nil)
39 (defvar *form-ended-on-same-line* nil)
40
41 (eval-when (:compile-toplevel :load-toplevel :execute)
42  (defparameter *possible-states*
43   '(:begin ; start of file
44     :normal ; normal processing
45     :beginning-of-line
46     :beginning-of-line-with-separator ; empty space in there
47     :beginning-of-symbols
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
53     :in-string
54    )))
55
56 (defun set-state (state)
57  (when (not (find state *possible-states*))
58   (error "Can't set state to ~A" state))
59  (setf *state* state)
60  nil)
61
62 (defmacro defevaluator (state match func)
63  (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
64  (let
65   ((scanner (gensym)))
66  `(let
67    ((,scanner (when (stringp ,match) (cl-ppcre:create-scanner ,match))))
68    (pushnew
69     (list
70      (lambda (state text)
71       (and
72        (or (eql :all ,state) (eql ,state state))
73        (or
74         (and (symbolp text) (eql text ,match))
75         (and ,scanner
76              (stringp text)
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))))
80      ,func)
81     *evaluators*))))
82
83 (defun evaluate (text)
84  (if (string= "" text)
85      (let*
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*))))
89      (let
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*)))
92       (let
93        ((problem (funcall (third evaluator))))
94        (when problem
95         (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
96        (let
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)))))))
101
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)))
105
106 (defun check-file (file)
107  (if (string= "package" (pathname-name file))
108   (set-state :normal)
109   (set-state :begin))
110  (setf *line-no* 0)
111  (setf *col-no* 0)
112  (setf *form-stack* nil)
113  (setf *form-ended-on-same-line* nil)
114  (handler-case
115   (progn
116    (evaluate (slurp-file file))
117    (list :success file))
118   (check-failure (cf)
119    (list :failure file (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf)))))
120
121 (defun check-directory (dir)
122  (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir))))
123
124 (defun any-failures (checks)
125  (find :failure checks :key #'car))
126
127 (defun print-failure (failure)
128  (format nil
129   "Style error in ~A at ~A:~A: ~A~%- ~A~%~VT^"
130   (second failure)
131   (1+ (fourth failure))
132   (1+ (fifth failure))
133   (third failure)
134   (with-open-file (str (second failure)) (loop :repeat (fourth failure) :do (read-line str)) (read-line str))
135   (+ (fifth failure) 2)))
136
137 (defun pretty-print-check-directory (dir)
138  (let
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))))
143
144 ; These are in reverse order
145 (progn
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 "\\("
154   (lambda ()
155    (push (list *line-no* *col-no*) *form-stack*)
156    (set-state :first-symbol)))
157  (defevaluator :first-symbol "\\("
158   (lambda ()
159    (cond
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")
163     (t
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
169   (lambda ()
170    (incf *line-no* -1)
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"
176   (lambda ()
177    (incf *line-no*)
178    (setf *col-no* -1)
179    (set-state :beginning-of-line)))
180  (defevaluator :comment-with-separator "\\n"
181   (lambda ()
182    (incf *line-no*)
183    (setf *col-no* -1)
184    (set-state :beginning-of-line-with-comment-and-separator)
185    nil))
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"
190   (lambda ()
191    (progn
192     (incf *line-no*)
193     (setf *col-no* -1)
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"
197   (lambda ()
198    (if
199     (< 0 *col-no*)
200     "No whitespace only lines"
201     (progn
202      (incf *line-no*)
203      (setf *col-no* -1)
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 ""
208   (lambda ()
209    (if
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 ""
214   (lambda ()
215    (set-state :first-symbol)))
216  (defevaluator :normal "\\)"
217   (lambda ()
218    (let
219     ((form (pop *form-stack*)))
220     (cond
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"
229   (lambda ()
230    (incf *line-no*)
231    (setf *col-no* -1)
232    nil))
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 ""
237   (lambda ()
238    (cond
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)))