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
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
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)))
34 (defvar *line-no* nil)
36 (defvar *evaluators* nil)
37 (defvar *form-stack* nil)
38 (defvar *form-ended-on-same-line* nil)
40 (eval-when (:compile-toplevel :load-toplevel :execute)
41 (defparameter *possible-states*
42 '(:begin ; start of file
43 :normal ; normal processing
45 :beginning-of-line-with-separator ; empty space in there
47 :beginning-of-symbols-with-separator
48 :first-symbol ; first symbol of form/line
49 :all ; matches everything
53 (defun set-state (state)
54 (when (not (find state *possible-states*))
55 (error "Can't set state to ~A" state))
59 (defmacro defevaluator (state match func)
60 (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
64 ((,scanner (when (stringp ,match) (cl-ppcre:create-scanner ,match))))
69 (or (eql :all ,state) (eql ,state state))
71 (and (symbolp text) (eql text ,match))
74 (multiple-value-bind (start end) (cl-ppcre:scan ,scanner text)
75 (and start end (= 0 start)))))))
76 (lambda (text) (second (multiple-value-list (cl-ppcre:scan ,scanner text))))
80 (defun evaluate (text)
83 ((evaluator (find-if (lambda (f) (funcall f *state* :eof)) *evaluators* :from-end t :key #'car))
84 (problem (when evaluator (funcall (third evaluator)))))
85 (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*))))
87 ((evaluator (find-if (lambda (f) (funcall f *state* text)) *evaluators* :from-end t :key #'car)))
88 (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*)))
90 ((problem (funcall (third evaluator))))
92 (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
94 ((length-of-match (funcall (cadr evaluator) text)))
95 (incf *col-no* length-of-match)
96 (when (< 120 *col-no*) (error (make-condition 'check-failure :msg "Line longer than 120 characters" :line-no *line-no* :col-no 0)))
97 (evaluate (subseq text length-of-match)))))))
99 (defun slurp-file (filename &key (element-type 'character) (sequence-type 'string))
100 (with-open-file (str filename :element-type element-type)
101 (let ((seq (make-sequence sequence-type (file-length str)))) (read-sequence seq str) seq)))
103 (defun check-file (file)
107 (setf *form-stack* nil)
108 (setf *form-ended-on-same-line* nil)
109 (format t "~%File: ~A~%" file)
111 (progn (evaluate (slurp-file file)) t)
113 (format t " - Had an error: ~S at ~A:~A~%" (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf))
116 (defun check-directory (dir)
117 (every #'identity (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir)))))
119 ; These are in reverse order
121 (setf *evaluators* nil)
122 (defevaluator :normal "\\("
124 (push (list *line-no* *col-no*) *form-stack*)
125 (set-state :first-symbol)))
126 (defevaluator :first-symbol "\\("
129 ((and (not *form-stack*) (not (zerop *col-no*))) "Top level forms must begin on first column")
130 ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
131 "All form elements must be indented equally")
133 (push (list *line-no* *col-no*) *form-stack*)
134 (set-state :first-symbol)))))
135 (defevaluator :all "\\t" (constantly "Must not use tabs"))
136 (defevaluator :begin "\\(in-package[^\\)]*\\)" (lambda () (set-state :normal)))
137 (defevaluator :beginning-of-line-with-separator :eof (constantly "Must not end with empty line"))
138 (defevaluator :beginning-of-line-with-separator "\\n" (constantly "Must not have two empty lines in a row"))
139 (defevaluator :begin ".*" (constantly "Must begin with in-package form"))
140 (defevaluator :all "\\( *in-package " (constantly "Only one in-package per file"))
141 (defevaluator :normal "\\n"
145 (set-state :beginning-of-line)))
146 (defevaluator :normal " +\\n" (constantly "No whitespace at end of line"))
147 (defevaluator :beginning-of-line " *" (lambda () (set-state :beginning-of-symbols)))
148 (defevaluator :beginning-of-line-with-separator " *" (lambda () (set-state :beginning-of-symbols-with-separator)))
149 (defevaluator :beginning-of-symbols "\\n"
153 "No whitespace only lines"
157 (set-state :beginning-of-line-with-separator)))))
158 (defevaluator :beginning-of-symbols "\\)" (constantly "No hanging close parens"))
159 (defevaluator :beginning-of-symbols-with-separator "\\)" (constantly "No hanging close parens"))
160 (defevaluator :beginning-of-symbols ""
163 (and (not *form-stack*) (not *form-ended-on-same-line*))
164 "Multiline top level forms must be separated by a space"
165 (set-state :first-symbol))))
166 (defevaluator :beginning-of-symbols-with-separator ""
168 (set-state :first-symbol)))
169 (defevaluator :normal "\\)"
172 ((form (pop *form-stack*)))
174 ((not form) "Unmatched ending paren")
175 ((< 50 (- *line-no* (car form))) "Forms can't be over 50 lines long")
176 (t (setf *form-ended-on-same-line* (= *line-no* (car form))) nil)))))
177 (defevaluator :normal "::" (constantly "No internal symbols from other packages"))
178 (defevaluator :first-symbol "\\n" (constantly "No new line after opening form"))
179 (defevaluator :first-symbol " " (constantly "No space after opening parens"))
180 (defevaluator :first-symbol ""
182 ;(format t "HMMM: ~A ~A ~A~%" *form-stack* *line-no* *col-no*)
184 ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
185 "All form elements must be indented equally")
186 (t (set-state :normal)))))
187 (defevaluator :normal "." (constantly nil)))