Migrate from travis to candle
[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  "CHECK-DIRECTORY DIR => RESULTS
130
131   RESULTS: RESULT*
132
133 ARGUMENTS AND VALUES:
134
135   DIR: A directory to recurse into and check files
136   RESULT: A result as returned by check-file
137
138 DESCRIPTION:
139
140   CHECK-DIRECTORY grabs all .lisp files in the tree under DIR, and loads
141   checks them all.
142
143   The results are then put together into a list which can be programatically
144   evaluated.  As opposed to pretty-print-check-directory, this function doesn't
145   clutter up your standard out."
146  (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir))))
147
148 (defun any-failures (checks)
149  (find :failure checks :key #'car))
150
151 (defun print-failure (failure)
152  (format nil
153   "Style error in ~A at ~A:~A: ~A~%- ~A~%~VT^"
154   (second failure)
155   (1+ (fourth failure))
156   (1+ (fifth failure))
157   (third failure)
158   (with-open-file (str (second failure)) (loop :repeat (fourth failure) :do (read-line str)) (read-line str))
159   (+ (fifth failure) 2)))
160
161 (defun pretty-print-check-directory (dir)
162  "PRETTY-PRINT-CHECK-DIRECTORY DIR => SUCCESS
163
164 ARGUMENTS AND VALUES:
165
166   DIR: A directory to recurse into and check files
167   SUCCESS: T if there were no failures
168
169 DESCRIPTION:
170
171   PRETTY-PRINT-CHECK-DIRECTORY checks DIR for any errors, dumping them to output
172   and returning a single flag.
173
174   Unlike check-directory, PRETTY-PRINT-CHECK-DIRECTORY is built for continuous
175   integration, dumping errors to standard out and returning a singular result.
176
177 EXAMPLES:
178
179   (pretty-print-check-directory \"src\") => nil"
180  (let
181   ((checks (check-directory dir)))
182   (format t "In ~A: Checked ~A files with ~A failures~%~%"
183    dir (length checks) (length (remove :success checks :key #'car)))
184   (format t "~{~A~%~}" (mapcar #'print-failure (remove :success checks :key #'car)))
185   (not (any-failures checks))))
186
187 ; These are in reverse order
188 (defevaluator :beginning-of-symbols " *;[^\\n]*"
189  (lambda () (set-state :normal)))
190
191 (defevaluator :beginning-of-symbols-with-separator " *;[^\\n]*"
192  (lambda () (set-state :comment-with-separator)))
193
194 (defevaluator :normal " *;[^\\n]*"
195  (lambda () (set-state :normal)))
196
197 (defevaluator :normal "\\("
198  (lambda ()
199   (push (list *line-no* *col-no*) *form-stack*)
200   (set-state :first-symbol)))
201
202 (defevaluator :first-symbol "\\("
203  (lambda ()
204   (cond
205    ((and (not *form-stack*) (not (zerop *col-no*))) "Top level forms must begin on first column")
206    ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
207     "All form elements must be indented equally")
208    (t
209     (push (list *line-no* *col-no*) *form-stack*)
210     (set-state :first-symbol)))))
211
212 (defevaluator :all "\\t" (constantly "Must not use tabs"))
213
214 (defevaluator :begin "\\(in-package[^\\)]*\\)" (lambda () (set-state :normal)))
215
216 (defevaluator :beginning-of-line-with-separator :eof
217  (lambda ()
218   (incf *line-no* -1)
219   "Must not end with empty line"))
220
221 (defevaluator :beginning-of-line-with-separator "\\n" (constantly "Must not have two empty lines in a row"))
222
223 (defevaluator :begin ".*" (constantly "Must begin with in-package form"))
224
225 (defevaluator :all "\\( *in-package " (constantly "Only one in-package per file"))
226
227 (defevaluator :normal "\\n"
228  (lambda ()
229   (incf *line-no*)
230   (setf *col-no* -1)
231   (set-state :beginning-of-line)))
232
233 (defevaluator :comment-with-separator "\\n"
234  (lambda ()
235   (incf *line-no*)
236   (setf *col-no* -1)
237   (set-state :beginning-of-line-with-comment-and-separator)
238   nil))
239
240 (defevaluator :normal " +\\n" (constantly "No whitespace at end of line"))
241
242 (defevaluator :beginning-of-line " *" (lambda () (set-state :beginning-of-symbols)))
243
244 (defevaluator :beginning-of-line-with-separator " *" (lambda () (set-state :beginning-of-symbols-with-separator)))
245
246 (defevaluator :beginning-of-line-with-comment-and-separator "\\n"
247  (lambda ()
248   (progn
249    (incf *line-no*)
250    (setf *col-no* -1)
251    (set-state :beginning-of-line-with-separator))))
252
253 (defevaluator :beginning-of-line-with-comment-and-separator " *"
254  (lambda () (set-state :beginning-of-symbols-with-separator)))
255
256 (defevaluator :beginning-of-symbols "\\n"
257  (lambda ()
258   (if
259    (< 0 *col-no*)
260    "No whitespace only lines"
261    (progn
262     (incf *line-no*)
263     (setf *col-no* -1)
264     (set-state :beginning-of-line-with-separator)))))
265
266 (defevaluator :beginning-of-symbols "\\)" (constantly "No hanging close parens"))
267
268 (defevaluator :beginning-of-symbols-with-separator "\\)" (constantly "No hanging close parens"))
269
270 (defevaluator :beginning-of-symbols ""
271  (lambda ()
272   (if
273    (and (not *form-stack*) (not *form-ended-on-same-line*))
274    "Multiline top level forms must be separated by a space"
275    (set-state :first-symbol))))
276
277 (defevaluator :beginning-of-symbols-with-separator ""
278  (lambda ()
279   (set-state :first-symbol)))
280
281 (defevaluator :normal "\\)"
282  (lambda ()
283   (let
284    ((form (pop *form-stack*)))
285    (cond
286     ((not form) "Unmatched ending paren")
287     ((< 50 (- *line-no* (car form))) "Forms can't be over 50 lines long")
288     (t (setf *form-ended-on-same-line* (= *line-no* (car form))) nil)))))
289
290 (defevaluator :normal "::" (constantly "No internal symbols from other packages"))
291
292 (defevaluator :in-string "\\\\\"" (constantly nil))
293
294 (defevaluator :normal "\"" (lambda () (set-state :in-string)))
295
296 (defevaluator :in-string "\"" (lambda () (set-state :normal)))
297
298 (defevaluator :in-string "\\n"
299  (lambda ()
300   (incf *line-no*)
301   (setf *col-no* -1)
302   nil))
303
304 (defevaluator :in-string "." (constantly nil))
305
306 (defevaluator :first-symbol "\\n" (constantly "No new line after opening form"))
307
308 (defevaluator :first-symbol " " (constantly "No space after opening parens"))
309
310 (defevaluator :first-symbol ""
311  (lambda ()
312   (cond
313    ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
314     "All form elements must be indented equally")
315    (t (set-state :normal)))))
316
317 (defevaluator :normal "  " (constantly "Only one space between items of a form"))
318
319 (defevaluator :normal "." (constantly nil))