1 (in-package #:style-checker)
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
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
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
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)))
29 (defvar *line-no* nil)
31 (defvar *evaluators* nil)
32 (defvar *form-stack* nil)
33 (defvar *form-ended-on-same-line* nil)
35 (eval-when (:compile-toplevel :load-toplevel :execute)
36 (defparameter *possible-states*
37 '(:begin ; start of file
38 :normal ; normal processing
40 :beginning-of-line-with-separator ; empty space in there
42 :beginning-of-symbols-with-separator
43 :all ; matches everything
47 (defun set-state (state)
48 (when (not (find state *possible-states*))
49 (error "Can't set state to ~A" state))
53 (defmacro defevaluator (state match func)
54 (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
58 ((,scanner (when (stringp ,match) (cl-ppcre:create-scanner ,match))))
63 (or (eql :all ,state) (eql ,state state))
65 (and (symbolp text) (eql text ,match))
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))))
74 (defun evaluate (text)
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*))))
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*)))
84 ((problem (funcall (third evaluator))))
85 (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
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)))))))
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)))
96 (defun check-file (file)
100 (setf *form-stack* nil)
101 (setf *form-ended-on-same-line* nil)
102 (format t "~%File: ~A~%" file)
104 (progn (evaluate (slurp-file file)) t)
106 (format t " - Had an error: ~S at ~A:~A~%" (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf))
109 (defun check-directory (dir)
110 (every #'identity (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir)))))
112 ; These are in reverse order
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"
129 (set-state :beginning-of-line)
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"
143 "No whitespace only lines"
144 (set-state :beginning-of-line-with-separator))))
145 (defevaluator :beginning-of-symbols ""
148 (and (not *form-stack*) (not *form-ended-on-same-line*))
149 "Multiline top level forms must be separated by a space"
150 (set-state :normal))))
151 (defevaluator :beginning-of-symbols-with-separator ""
153 (set-state :normal)))
154 (defevaluator :normal "\\("
157 (list *line-no* *col-no*)
160 (defevaluator :normal "\\)"
163 ((form (pop *form-stack*)))
165 ((not form) "Unmatched ending paren")
166 ((< 50 (- *line-no* (car form))) "Forms can't be over 50 lines long")
167 (t (setf *form-ended-on-same-line* (= *line-no* (car form))) nil)))))
168 (defevaluator :normal "::"
169 (constantly "No internal symbols from other packages"))
170 (defevaluator :normal "." (constantly nil)))