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