Check for multiple spaces in 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 ; 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     :first-symbol ; first symbol of form/line
49     :all ; matches everything
50    )))
51
52 (defun set-state (state)
53  (when (not (find state *possible-states*))
54   (error "Can't set state to ~A" state))
55  (setf *state* state)
56  nil)
57
58 (defmacro defevaluator (state match func)
59  (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
60  (let
61   ((scanner (gensym)))
62  `(let
63    ((,scanner (when (stringp ,match) (cl-ppcre:create-scanner ,match))))
64    (pushnew
65     (list
66      (lambda (state text)
67       (and
68        (or (eql :all ,state) (eql ,state state))
69        (or
70         (and (symbolp text) (eql text ,match))
71         (and ,scanner
72              (stringp text)
73              (multiple-value-bind (start end) (cl-ppcre:scan ,scanner text)
74               (and start end (= 0 start)))))))
75      (lambda (text) (second (multiple-value-list (cl-ppcre:scan ,scanner text))))
76      ,func)
77     *evaluators*))))
78
79 (defun evaluate (text)
80  (if (string= "" text)
81      (let*
82       ((evaluator (find-if (lambda (f) (funcall f *state* :eof)) *evaluators* :from-end t :key #'car))
83        (problem (when evaluator (funcall (third evaluator)))))
84       (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*))))
85      (let
86       ((evaluator (find-if (lambda (f) (funcall f *state* text)) *evaluators* :from-end t :key #'car)))
87       (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*)))
88       (let
89        ((problem (funcall (third evaluator))))
90        (when problem
91         (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
92        (let
93         ((length-of-match (funcall (cadr evaluator) text)))
94         (incf *col-no* length-of-match)
95         (when (< 120 *col-no*) (error (make-condition 'check-failure :msg "Line longer than 120 characters" :line-no *line-no* :col-no 0)))
96         (evaluate (subseq text length-of-match)))))))
97
98 (defun slurp-file (filename &key (element-type 'character) (sequence-type 'string))
99  (with-open-file (str filename :element-type element-type)
100   (let ((seq (make-sequence sequence-type (file-length str)))) (read-sequence seq str) seq)))
101
102 (defun check-file (file)
103  (set-state :begin)
104  (setf *line-no* 0)
105  (setf *col-no* 0)
106  (setf *form-stack* nil)
107  (setf *form-ended-on-same-line* nil)
108  (format t "~%File: ~A~%" file)
109  (handler-case
110   (progn (evaluate (slurp-file file)) t)
111   (check-failure (cf)
112    (format t " - Had an error: ~S at ~A:~A~%" (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf))
113    nil)))
114
115 (defun check-directory (dir)
116  (every #'identity (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir)))))
117
118 ; These are in reverse order
119 (progn
120  (setf *evaluators* nil)
121  (defevaluator :normal "\\("
122   (lambda ()
123    (push (list *line-no* *col-no*) *form-stack*)
124    (set-state :first-symbol)))
125  (defevaluator :first-symbol "\\("
126   (lambda ()
127    (cond
128     ((and (not *form-stack*) (not (zerop *col-no*))) "Top level forms must begin on first column")
129     ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
130      "All form elements must be indented equally")
131     (t
132      (push (list *line-no* *col-no*) *form-stack*)
133      (set-state :first-symbol)))))
134  (defevaluator :all "\\t" (constantly "Must not use tabs"))
135  (defevaluator :begin "\\(in-package[^\\)]*\\)" (lambda () (set-state :normal)))
136  (defevaluator :beginning-of-line-with-separator :eof (constantly "Must not end with empty line"))
137  (defevaluator :beginning-of-line-with-separator "\\n" (constantly "Must not have two empty lines in a row"))
138  (defevaluator :begin ".*" (constantly "Must begin with in-package form"))
139  (defevaluator :all "\\( *in-package " (constantly "Only one in-package per file"))
140  (defevaluator :normal "\\n"
141   (lambda ()
142    (incf *line-no*)
143    (setf *col-no* -1)
144    (set-state :beginning-of-line)))
145  (defevaluator :normal " +\\n" (constantly "No whitespace at end of line"))
146  (defevaluator :beginning-of-line " *" (lambda () (set-state :beginning-of-symbols)))
147  (defevaluator :beginning-of-line-with-separator " *" (lambda () (set-state :beginning-of-symbols-with-separator)))
148  (defevaluator :beginning-of-symbols "\\n"
149   (lambda ()
150    (if
151     (< 0 *col-no*)
152     "No whitespace only lines"
153     (progn
154      (incf *line-no*)
155      (setf *col-no* -1)
156      (set-state :beginning-of-line-with-separator)))))
157  (defevaluator :beginning-of-symbols "\\)" (constantly "No hanging close parens"))
158  (defevaluator :beginning-of-symbols-with-separator "\\)" (constantly "No hanging close parens"))
159  (defevaluator :beginning-of-symbols ""
160   (lambda ()
161    (if
162     (and (not *form-stack*) (not *form-ended-on-same-line*))
163     "Multiline top level forms must be separated by a space"
164     (set-state :first-symbol))))
165  (defevaluator :beginning-of-symbols-with-separator ""
166   (lambda ()
167    (set-state :first-symbol)))
168  (defevaluator :normal "\\)"
169   (lambda ()
170    (let
171     ((form (pop *form-stack*)))
172     (cond
173      ((not form) "Unmatched ending paren")
174      ((< 50 (- *line-no* (car form))) "Forms can't be over 50 lines long")
175      (t (setf *form-ended-on-same-line* (= *line-no* (car form))) nil)))))
176  (defevaluator :normal "::" (constantly "No internal symbols from other packages"))
177  (defevaluator :first-symbol "\\n" (constantly "No new line after opening form"))
178  (defevaluator :first-symbol " " (constantly "No space after opening parens"))
179  (defevaluator :first-symbol ""
180   (lambda ()
181    ;(format t "HMMM: ~A ~A ~A~%" *form-stack* *line-no* *col-no*)
182    (cond
183     ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
184      "All form elements must be indented equally")
185     (t (set-state :normal)))))
186  (defevaluator :normal "  " (constantly "Only one space between items of a form"))
187  (defevaluator :normal "." (constantly nil)))