Check for spaces/newlines after opening a form
[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 ; Some thoughts
21 ; - form starting reader macros will have to be hand added to this code
22 ; - exceptions will eventually arise, and the rule file will have to be changed
23 ; - the proper formatting of "loop" is weird
24
25 (define-condition check-failure nil ((msg :initarg :msg :reader check-failure-msg)
26                                      (line-no :initarg :line-no :reader check-failure-line-no)
27                                      (col-no :initarg :col-no :reader check-failure-col-no)))
28
29 (defvar *state* nil)
30 (defvar *line-no* nil)
31 (defvar *col-no* nil)
32 (defvar *evaluators* nil)
33 (defvar *form-stack* nil)
34 (defvar *form-ended-on-same-line* nil)
35
36 (eval-when (:compile-toplevel :load-toplevel :execute)
37  (defparameter *possible-states*
38   '(:begin ; start of file
39     :normal ; normal processing
40     :beginning-of-line
41     :beginning-of-line-with-separator ; empty space in there
42     :beginning-of-symbols
43     :beginning-of-symbols-with-separator
44     :first-symbol-of-form ; After an open parens
45     :all ; matches everything
46    )))
47
48
49 (defun set-state (state)
50  (when (not (find state *possible-states*))
51   (error "Can't set state to ~A" state))
52  (setf *state* state)
53  nil)
54
55 (defmacro defevaluator (state match func)
56  (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
57  (let
58   ((scanner (gensym)))
59  `(let
60    ((,scanner (when (stringp ,match) (cl-ppcre:create-scanner ,match))))
61    (pushnew
62     (list
63      (lambda (state text)
64       (and
65        (or (eql :all ,state) (eql ,state state))
66        (or
67         (and (symbolp text) (eql text ,match))
68         (and ,scanner
69              (stringp text)
70              (multiple-value-bind (start end) (cl-ppcre:scan ,scanner text)
71               (and start end (= 0 start)))))))
72      (lambda (text) (second (multiple-value-list (cl-ppcre:scan ,scanner text))))
73      ,func)
74     *evaluators*))))
75
76 (defun evaluate (text)
77  (if (string= "" text)
78      (let*
79       ((evaluator (find-if (lambda (f) (funcall f *state* :eof)) *evaluators* :from-end t :key #'car))
80        (problem (when evaluator (funcall (third evaluator)))))
81       (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*))))
82      (let
83       ((evaluator (find-if (lambda (f) (funcall f *state* text)) *evaluators* :from-end t :key #'car)))
84       (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*)))
85       (let
86        ((problem (funcall (third evaluator))))
87        (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
88        (let
89         ((length-of-match (funcall (cadr evaluator) text)))
90         (incf *col-no* length-of-match)
91         (when (< 120 *col-no*) (error (make-condition 'check-failure :msg "Line longer than 120 characters" :line-no *line-no* :col-no 0)))
92         (evaluate (subseq text length-of-match)))))))
93
94 (defun slurp-file (filename &key (element-type 'character) (sequence-type 'string))
95  (with-open-file (str filename :element-type element-type)
96   (let ((seq (make-sequence sequence-type (file-length str)))) (read-sequence seq str) seq)))
97
98 (defun check-file (file)
99  (set-state :begin)
100  (setf *line-no* 0)
101  (setf *col-no* 0)
102  (setf *form-stack* nil)
103  (setf *form-ended-on-same-line* nil)
104  (format t "~%File: ~A~%" file)
105  (handler-case
106   (progn (evaluate (slurp-file file)) t)
107   (check-failure (cf)
108    (format t " - Had an error: ~S at ~A:~A~%" (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf))
109    nil)))
110
111 (defun check-directory (dir)
112  (every #'identity (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir)))))
113
114 ; These are in reverse order
115 (progn
116  (setf *evaluators* nil)
117  (defevaluator :all "\\t" (constantly "Must not use tabs"))
118  (defevaluator :begin "\\(in-package[^\\)]*\\)" (lambda () (set-state :normal)))
119  (defevaluator :beginning-of-line-with-separator :eof (constantly "Must not end with empty line"))
120  (defevaluator :beginning-of-line-with-separator "\\n" (constantly "Must not have two empty lines in a row"))
121  (defevaluator :begin ".*" (constantly "Must begin with in-package form"))
122  (defevaluator :normal "\\( *in-package " (constantly "Only one in-package per file"))
123  (defevaluator :normal "\\n"
124   (lambda ()
125    (set-state :beginning-of-line)
126    (incf *line-no*)
127    (setf *col-no* -1)
128    nil))
129  (defevaluator :normal " +\\n" (constantly "No whitespace at end of line"))
130  (defevaluator :beginning-of-line " *" (lambda () (set-state :beginning-of-symbols)))
131  (defevaluator :beginning-of-line-with-separator " *" (lambda () (set-state :beginning-of-symbols-with-separator)))
132  (defevaluator :beginning-of-symbols "\\n"
133   (lambda ()
134    (if
135     (< 0 *col-no*)
136     "No whitespace only lines"
137     (set-state :beginning-of-line-with-separator))))
138  (defevaluator :beginning-of-symbols "\\)" (constantly "No hanging close parens"))
139  (defevaluator :beginning-of-symbols-with-separator "\\)" (constantly "No hanging close parens"))
140  (defevaluator :beginning-of-symbols ""
141   (lambda ()
142    (if
143     (and (not *form-stack*) (not *form-ended-on-same-line*))
144     "Multiline top level forms must be separated by a space"
145     (set-state :normal))))
146  (defevaluator :beginning-of-symbols-with-separator ""
147   (lambda ()
148    (set-state :normal)))
149  (defevaluator :normal "\\("
150   (lambda ()
151    (push
152     (list *line-no* *col-no*)
153     *form-stack*)
154    (set-state :first-symbol-of-form)))
155  (defevaluator :normal "\\)"
156   (lambda ()
157    (let
158     ((form (pop *form-stack*)))
159     (cond
160      ((not form) "Unmatched ending paren")
161      ((< 50 (- *line-no* (car form))) "Forms can't be over 50 lines long")
162      (t (setf *form-ended-on-same-line* (= *line-no* (car form))) nil)))))
163  (defevaluator :normal "::" (constantly "No internal symbols from other packages"))
164  (defevaluator :first-symbol-of-form "\\n" (constantly "No new line after opening form"))
165  (defevaluator :first-symbol-of-form " " (constantly "No space after opening parens"))
166  (defevaluator :first-symbol-of-form "" (lambda () (set-state :normal)))
167  (defevaluator :normal "." (constantly nil)))