Add pretty printing
[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
24 ; Some thoughts
25 ; - form starting reader macros will have to be hand added to this code
26 ; - exceptions will eventually arise, and the rule file will have to be changed
27 ; - the proper formatting of "loop" is weird
28
29 (define-condition check-failure nil ((msg :initarg :msg :reader check-failure-msg)
30                                      (line-no :initarg :line-no :reader check-failure-line-no)
31                                      (col-no :initarg :col-no :reader check-failure-col-no)))
32
33 (defvar *state* nil)
34 (defvar *line-no* nil)
35 (defvar *col-no* nil)
36 (defvar *evaluators* nil)
37 (defvar *form-stack* nil)
38 (defvar *form-ended-on-same-line* nil)
39
40 (eval-when (:compile-toplevel :load-toplevel :execute)
41  (defparameter *possible-states*
42   '(:begin ; start of file
43     :normal ; normal processing
44     :beginning-of-line
45     :beginning-of-line-with-separator ; empty space in there
46     :beginning-of-symbols
47     :beginning-of-symbols-with-separator
48     :comment-with-separator ; weird edge case for pre-function comments
49     :beginning-of-line-with-comment-and-separator ; weird edge case part 2
50     :first-symbol ; first symbol of form/line
51     :all ; matches everything
52     :in-string
53    )))
54
55 (defun set-state (state)
56  (when (not (find state *possible-states*))
57   (error "Can't set state to ~A" state))
58  (setf *state* state)
59  nil)
60
61 (defmacro defevaluator (state match func)
62  (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
63  (let
64   ((scanner (gensym)))
65  `(let
66    ((,scanner (when (stringp ,match) (cl-ppcre:create-scanner ,match))))
67    (pushnew
68     (list
69      (lambda (state text)
70       (and
71        (or (eql :all ,state) (eql ,state state))
72        (or
73         (and (symbolp text) (eql text ,match))
74         (and ,scanner
75              (stringp text)
76              (multiple-value-bind (start end) (cl-ppcre:scan ,scanner text)
77               (and start end (= 0 start)))))))
78      (lambda (text) (second (multiple-value-list (cl-ppcre:scan ,scanner text))))
79      ,func)
80     *evaluators*))))
81
82 (defun evaluate (text)
83  (if (string= "" text)
84      (let*
85       ((evaluator (find-if (lambda (f) (funcall f *state* :eof)) *evaluators* :from-end t :key #'car))
86        (problem (when evaluator (funcall (third evaluator)))))
87       (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*))))
88      (let
89       ((evaluator (find-if (lambda (f) (funcall f *state* text)) *evaluators* :from-end t :key #'car)))
90       (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*)))
91       (let
92        ((problem (funcall (third evaluator))))
93        (when problem
94         (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
95        (let
96         ((length-of-match (funcall (cadr evaluator) text)))
97         (incf *col-no* length-of-match)
98         (when (< 120 *col-no*) (error (make-condition 'check-failure :msg "Line longer than 120 characters" :line-no *line-no* :col-no 0)))
99         (evaluate (subseq text length-of-match)))))))
100
101 (defun slurp-file (filename &key (element-type 'character) (sequence-type 'string))
102  (with-open-file (str filename :element-type element-type)
103   (let ((seq (make-sequence sequence-type (file-length str)))) (read-sequence seq str) seq)))
104
105 (defun check-file (file)
106  (set-state :begin)
107  (setf *line-no* 0)
108  (setf *col-no* 0)
109  (setf *form-stack* nil)
110  (setf *form-ended-on-same-line* nil)
111  (handler-case
112   (progn
113    (evaluate (slurp-file file))
114    (list :success file))
115   (check-failure (cf)
116    (list :failure file (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf)))))
117
118 (defun check-directory (dir)
119  (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir))))
120
121 (defun any-failures (checks)
122  (find :failure checks :key #'car))
123
124 (defun print-failure (failure)
125  (format nil
126   "Style error in ~A at ~A:~A: ~A~%- ~A~%~VT^"
127   (second failure)
128   (1+ (fourth failure))
129   (1+ (fifth failure))
130   (third failure)
131   (with-open-file (str (second failure)) (loop :repeat (fourth failure) :do (read-line str)) (read-line str))
132   (+ (fifth failure) 2)))
133
134 (defun pretty-print-check-directory (dir)
135  (let
136   ((checks (check-directory dir)))
137   (format t "In ~A: Checked ~A files with ~A failures~%~%" dir (length checks) (length (remove :success checks :key #'car)))
138   (format t "~{~A~%~}" (mapcar #'print-failure (remove :success checks :key #'car)))
139   (sb-ext:exit :code (if (any-failures checks) 1 0))))
140
141 ; These are in reverse order
142 (progn
143  (setf *evaluators* nil)
144  (defevaluator :beginning-of-symbols " *;[^\\n]*"
145   (lambda () (set-state :normal)))
146  (defevaluator :beginning-of-symbols-with-separator " *;[^\\n]*"
147   (lambda () (set-state :comment-with-separator)))
148  (defevaluator :normal " *;[^\\n]*"
149   (lambda () (set-state :normal)))
150  (defevaluator :normal "\\("
151   (lambda ()
152    (push (list *line-no* *col-no*) *form-stack*)
153    (set-state :first-symbol)))
154  (defevaluator :first-symbol "\\("
155   (lambda ()
156    (cond
157     ((and (not *form-stack*) (not (zerop *col-no*))) "Top level forms must begin on first column")
158     ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
159      "All form elements must be indented equally")
160     (t
161      (push (list *line-no* *col-no*) *form-stack*)
162      (set-state :first-symbol)))))
163  (defevaluator :all "\\t" (constantly "Must not use tabs"))
164  (defevaluator :begin "\\(in-package[^\\)]*\\)" (lambda () (set-state :normal)))
165  (defevaluator :beginning-of-line-with-separator :eof
166   (lambda ()
167    (incf *line-no* -1)
168    "Must not end with empty line"))
169  (defevaluator :beginning-of-line-with-separator "\\n" (constantly "Must not have two empty lines in a row"))
170  (defevaluator :begin ".*" (constantly "Must begin with in-package form"))
171  (defevaluator :all "\\( *in-package " (constantly "Only one in-package per file"))
172  (defevaluator :normal "\\n"
173   (lambda ()
174    (incf *line-no*)
175    (setf *col-no* -1)
176    (set-state :beginning-of-line)))
177  (defevaluator :comment-with-separator "\\n"
178   (lambda ()
179    (incf *line-no*)
180    (setf *col-no* -1)
181    (set-state :beginning-of-line-with-comment-and-separator)
182    nil))
183  (defevaluator :normal " +\\n" (constantly "No whitespace at end of line"))
184  (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)))
186  (defevaluator :beginning-of-line-with-comment-and-separator "\\n"
187   (lambda ()
188    (progn
189     (incf *line-no*)
190     (setf *col-no* -1)
191     (set-state :beginning-of-line-with-separator))))
192  (defevaluator :beginning-of-line-with-comment-and-separator " *" (lambda () (set-state :beginning-of-symbols-with-separator)))
193  (defevaluator :beginning-of-symbols "\\n"
194   (lambda ()
195    (if
196     (< 0 *col-no*)
197     "No whitespace only lines"
198     (progn
199      (incf *line-no*)
200      (setf *col-no* -1)
201      (set-state :beginning-of-line-with-separator)))))
202  (defevaluator :beginning-of-symbols "\\)" (constantly "No hanging close parens"))
203  (defevaluator :beginning-of-symbols-with-separator "\\)" (constantly "No hanging close parens"))
204  (defevaluator :beginning-of-symbols ""
205   (lambda ()
206    (if
207     (and (not *form-stack*) (not *form-ended-on-same-line*))
208     "Multiline top level forms must be separated by a space"
209     (set-state :first-symbol))))
210  (defevaluator :beginning-of-symbols-with-separator ""
211   (lambda ()
212    (set-state :first-symbol)))
213  (defevaluator :normal "\\)"
214   (lambda ()
215    (let
216     ((form (pop *form-stack*)))
217     (cond
218      ((not form) "Unmatched ending paren")
219      ((< 50 (- *line-no* (car form))) "Forms can't be over 50 lines long")
220      (t (setf *form-ended-on-same-line* (= *line-no* (car form))) nil)))))
221  (defevaluator :normal "::" (constantly "No internal symbols from other packages"))
222  (defevaluator :in-string "\\\\\"" (constantly nil))
223  (defevaluator :normal "\"" (lambda () (set-state :in-string)))
224  (defevaluator :in-string "\"" (lambda () (set-state :normal)))
225  (defevaluator :in-string "\\n"
226   (lambda ()
227    (incf *line-no*)
228    (setf *col-no* -1)
229    nil))
230  (defevaluator :in-string "." (constantly nil))
231  (defevaluator :first-symbol "\\n" (constantly "No new line after opening form"))
232  (defevaluator :first-symbol " " (constantly "No space after opening parens"))
233  (defevaluator :first-symbol ""
234   (lambda ()
235    (cond
236     ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
237      "All form elements must be indented equally")
238     (t (set-state :normal)))))
239  (defevaluator :normal "  " (constantly "Only one space between items of a form"))
240  (defevaluator :normal "." (constantly nil)))