1 (in-package #:style-checker)
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
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
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
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)))
30 (defvar *line-no* nil)
32 (defvar *evaluators* nil)
33 (defvar *form-stack* nil)
34 (defvar *form-ended-on-same-line* nil)
36 (eval-when (:compile-toplevel :load-toplevel :execute)
37 (defparameter *possible-states*
38 '(:begin ; start of file
39 :normal ; normal processing
41 :beginning-of-line-with-separator ; empty space in there
43 :beginning-of-symbols-with-separator
44 :first-symbol-of-form ; After an open parens
45 :all ; matches everything
49 (defun set-state (state)
50 (when (not (find state *possible-states*))
51 (error "Can't set state to ~A" state))
55 (defmacro defevaluator (state match func)
56 (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
60 ((,scanner (when (stringp ,match) (cl-ppcre:create-scanner ,match))))
65 (or (eql :all ,state) (eql ,state state))
67 (and (symbolp text) (eql text ,match))
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))))
76 (defun evaluate (text)
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*))))
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*)))
86 ((problem (funcall (third evaluator))))
87 (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
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)))))))
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)))
98 (defun check-file (file)
102 (setf *form-stack* nil)
103 (setf *form-ended-on-same-line* nil)
104 (format t "~%File: ~A~%" file)
106 (progn (evaluate (slurp-file file)) t)
108 (format t " - Had an error: ~S at ~A:~A~%" (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf))
111 (defun check-directory (dir)
112 (every #'identity (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir)))))
114 ; These are in reverse order
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"
125 (set-state :beginning-of-line)
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"
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 ""
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 ""
148 (set-state :normal)))
149 (defevaluator :normal "\\("
152 (list *line-no* *col-no*)
154 (set-state :first-symbol-of-form)))
155 (defevaluator :normal "\\)"
158 ((form (pop *form-stack*)))
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)))