79cf0f53ca41a6bcb9e37fb2f57a0105bc1ab962
[wolf] / src / main / syntax-checker.lisp
1 (in-package #:syntax-checker)
2
3 ; Some thoughts
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
7
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)))
11
12 (defvar *state* nil)
13 (defvar *line-no* nil)
14 (defvar *col-no* nil)
15 (defvar *evaluators* nil)
16 (defvar *form-stack* nil)
17 (defvar *form-ended-on-same-line* nil)
18
19 (eval-when (:compile-toplevel :load-toplevel :execute)
20  (defparameter *possible-states*
21   '(:begin ; start of file
22     :normal ; normal processing
23     :beginning-of-line
24     :beginning-of-line-with-separator ; empty space in there
25     :beginning-of-symbols
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
31     :in-string)))
32
33 (defun set-state (state)
34  (when (not (find state *possible-states*))
35   (error "Can't set state to ~A" state))
36  (setf *state* state)
37  nil)
38
39 (defmacro defevaluator (state match func)
40  (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
41  (let
42   ((scanner (gensym)))
43   `(let
44     ((,scanner (when (stringp ,match) (cl-ppcre:create-scanner ,match))))
45     (pushnew
46      (list
47       (lambda (state text)
48        (and
49         (or (eql :all ,state) (eql ,state state))
50         (or
51          (and (symbolp text) (eql text ,match))
52          (and
53           ,scanner
54           (stringp text)
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))))
58       ,func)
59      *evaluators*))))
60
61 (defun evaluate (text)
62  (if (string= "" text)
63   (let*
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*))))
67   (let
68    ((evaluator (find-if (lambda (f) (funcall f *state* text)) *evaluators* :from-end t :key #'car)))
69    (when (not evaluator)
70     (error
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*)))
74    (let
75     ((problem (funcall (third evaluator))))
76     (when problem
77      (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
78     (let
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)))))))
84
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)))
88
89 (defun check-file (file)
90  "CHECK-FILE FILE => RESULT
91
92   RESULT: SUCCESS-RESULT | FAILURE-RESULT
93   SUCCESS-RESULT: (:success FILENAME)
94   FAILURE-RESULT: (:success FILENAME MSG LINE-NO COL-NO)
95
96 ARGUMENTS AND VALUES
97
98   FILE: a pathname
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
103
104 DESCRIPTION:
105
106   CHECK-FILE runs all the checks against a file and returns
107   as soon as the first style error is found.
108
109 EXAMPLES:
110
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)"
113
114  (if (string= "package" (pathname-name file))
115   (set-state :normal)
116   (set-state :begin))
117  (setf *line-no* 0)
118  (setf *col-no* 0)
119  (setf *form-stack* nil)
120  (setf *form-ended-on-same-line* nil)
121  (handler-case
122   (progn
123    (evaluate (slurp-file file))
124    (list :success file))
125   (check-failure (cf)
126    (list :failure file (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf)))))
127
128 (defun check-directory (dir)
129  (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir))))
130
131 (defun any-failures (checks)
132  (find :failure checks :key #'car))
133
134 (defun print-failure (failure)
135  (format nil
136   "Style error in ~A at ~A:~A: ~A~%- ~A~%~VT^"
137   (second failure)
138   (1+ (fourth failure))
139   (1+ (fifth failure))
140   (third failure)
141   (with-open-file (str (second failure)) (loop :repeat (fourth failure) :do (read-line str)) (read-line str))
142   (+ (fifth failure) 2)))
143
144 (defun pretty-print-check-directory (dir)
145  (let
146   ((checks (check-directory dir)))
147   (format t "In ~A: Checked ~A files with ~A failures~%~%"
148    dir (length checks) (length (remove :success checks :key #'car)))
149   (format t "~{~A~%~}" (mapcar #'print-failure (remove :success checks :key #'car)))
150   (not (any-failures checks))))
151
152 ; These are in reverse order
153 (defevaluator :beginning-of-symbols " *;[^\\n]*"
154  (lambda () (set-state :normal)))
155
156 (defevaluator :beginning-of-symbols-with-separator " *;[^\\n]*"
157  (lambda () (set-state :comment-with-separator)))
158
159 (defevaluator :normal " *;[^\\n]*"
160  (lambda () (set-state :normal)))
161
162 (defevaluator :normal "\\("
163  (lambda ()
164   (push (list *line-no* *col-no*) *form-stack*)
165   (set-state :first-symbol)))
166
167 (defevaluator :first-symbol "\\("
168  (lambda ()
169   (cond
170    ((and (not *form-stack*) (not (zerop *col-no*))) "Top level forms must begin on first column")
171    ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
172     "All form elements must be indented equally")
173    (t
174     (push (list *line-no* *col-no*) *form-stack*)
175     (set-state :first-symbol)))))
176
177 (defevaluator :all "\\t" (constantly "Must not use tabs"))
178
179 (defevaluator :begin "\\(in-package[^\\)]*\\)" (lambda () (set-state :normal)))
180
181 (defevaluator :beginning-of-line-with-separator :eof
182  (lambda ()
183   (incf *line-no* -1)
184   "Must not end with empty line"))
185
186 (defevaluator :beginning-of-line-with-separator "\\n" (constantly "Must not have two empty lines in a row"))
187
188 (defevaluator :begin ".*" (constantly "Must begin with in-package form"))
189
190 (defevaluator :all "\\( *in-package " (constantly "Only one in-package per file"))
191
192 (defevaluator :normal "\\n"
193  (lambda ()
194   (incf *line-no*)
195   (setf *col-no* -1)
196   (set-state :beginning-of-line)))
197
198 (defevaluator :comment-with-separator "\\n"
199  (lambda ()
200   (incf *line-no*)
201   (setf *col-no* -1)
202   (set-state :beginning-of-line-with-comment-and-separator)
203   nil))
204
205 (defevaluator :normal " +\\n" (constantly "No whitespace at end of line"))
206
207 (defevaluator :beginning-of-line " *" (lambda () (set-state :beginning-of-symbols)))
208
209 (defevaluator :beginning-of-line-with-separator " *" (lambda () (set-state :beginning-of-symbols-with-separator)))
210
211 (defevaluator :beginning-of-line-with-comment-and-separator "\\n"
212  (lambda ()
213   (progn
214    (incf *line-no*)
215    (setf *col-no* -1)
216    (set-state :beginning-of-line-with-separator))))
217
218 (defevaluator :beginning-of-line-with-comment-and-separator " *"
219  (lambda () (set-state :beginning-of-symbols-with-separator)))
220
221 (defevaluator :beginning-of-symbols "\\n"
222  (lambda ()
223   (if
224    (< 0 *col-no*)
225    "No whitespace only lines"
226    (progn
227     (incf *line-no*)
228     (setf *col-no* -1)
229     (set-state :beginning-of-line-with-separator)))))
230
231 (defevaluator :beginning-of-symbols "\\)" (constantly "No hanging close parens"))
232
233 (defevaluator :beginning-of-symbols-with-separator "\\)" (constantly "No hanging close parens"))
234
235 (defevaluator :beginning-of-symbols ""
236  (lambda ()
237   (if
238    (and (not *form-stack*) (not *form-ended-on-same-line*))
239    "Multiline top level forms must be separated by a space"
240    (set-state :first-symbol))))
241
242 (defevaluator :beginning-of-symbols-with-separator ""
243  (lambda ()
244   (set-state :first-symbol)))
245
246 (defevaluator :normal "\\)"
247  (lambda ()
248   (let
249    ((form (pop *form-stack*)))
250    (cond
251     ((not form) "Unmatched ending paren")
252     ((< 50 (- *line-no* (car form))) "Forms can't be over 50 lines long")
253     (t (setf *form-ended-on-same-line* (= *line-no* (car form))) nil)))))
254
255 (defevaluator :normal "::" (constantly "No internal symbols from other packages"))
256
257 (defevaluator :in-string "\\\\\"" (constantly nil))
258
259 (defevaluator :normal "\"" (lambda () (set-state :in-string)))
260
261 (defevaluator :in-string "\"" (lambda () (set-state :normal)))
262
263 (defevaluator :in-string "\\n"
264  (lambda ()
265   (incf *line-no*)
266   (setf *col-no* -1)
267   nil))
268
269 (defevaluator :in-string "." (constantly nil))
270
271 (defevaluator :first-symbol "\\n" (constantly "No new line after opening form"))
272
273 (defevaluator :first-symbol " " (constantly "No space after opening parens"))
274
275 (defevaluator :first-symbol ""
276  (lambda ()
277   (cond
278    ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
279     "All form elements must be indented equally")
280    (t (set-state :normal)))))
281
282 (defevaluator :normal "  " (constantly "Only one space between items of a form"))
283
284 (defevaluator :normal "." (constantly nil))