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
19 ; - form starting reader macros will have to be hand added to this code
20 ; - exceptions will eventually arise, and the rule file will have to be changed
21 ; - the proper formatting of "loop" is weird
23 (define-condition check-failure nil ((msg :initarg :msg :reader check-failure-msg)
24 (line-no :initarg :line-no :reader check-failure-line-no)
25 (col-no :initarg :col-no :reader check-failure-col-no)))
28 (defvar *line-no* nil)
30 (defvar *evaluators* nil)
31 (defvar *form-stack* nil)
33 (eval-when (:compile-toplevel :load-toplevel :execute)
34 (defparameter *possible-states*
35 '(:begin ; start of file
36 :normal ; normal processing
38 :beginning-of-separators-with-space ; empty space in there
40 :all ; matches everything
44 (defun set-state (state)
45 (when (not (find state *possible-states*))
46 (error "Can't set state to ~A" state))
50 (defmacro defevaluator (state match func)
51 (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
55 ((,scanner (when (stringp ,match) (cl-ppcre:create-scanner ,match))))
60 (or (eql :all ,state) (eql ,state state))
62 (and (symbolp text) (eql text ,match))
65 (multiple-value-bind (start end) (cl-ppcre:scan ,scanner text)
66 (and start end (= 0 start)))))))
67 (lambda (text) (second (multiple-value-list (cl-ppcre:scan ,scanner text))))
71 (defun evaluate (text)
74 ((evaluator (find-if (lambda (f) (funcall f *state* :eof)) *evaluators* :from-end t :key #'car))
75 (problem (when evaluator (funcall (third evaluator)))))
76 (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*))))
78 ((evaluator (find-if (lambda (f) (funcall f *state* text)) *evaluators* :from-end t :key #'car)))
79 (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*)))
81 ((problem (funcall (third evaluator))))
82 (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
84 ((length-of-match (funcall (cadr evaluator) text)))
85 (incf *col-no* length-of-match)
86 (when (< 120 *col-no*) (error (make-condition 'check-failure :msg "Line longer than 120 characters" :line-no *line-no* :col-no 0)))
87 (evaluate (subseq text length-of-match)))))))
89 (defun slurp-file (filename &key (element-type 'character) (sequence-type 'string))
90 (with-open-file (str filename :element-type element-type)
91 (let ((seq (make-sequence sequence-type (file-length str)))) (read-sequence seq str) seq)))
93 (defun check-file (file)
97 (format t "~%File: ~A~%" file)
99 (progn (evaluate (slurp-file file)) t)
101 (format t " - Had an error: ~S at ~A:~A~%" (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf))
104 (defun check-directory (dir)
105 (every #'identity (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir)))))
107 ; These are in reverse order
109 (setf *evaluators* nil)
110 (defevaluator :all "\\t"
111 (constantly "Must not use tabs"))
112 (defevaluator :begin "\\(in-package[^\\)]*\\)"
114 (set-state :normal) nil))
115 (defevaluator :beginning-of-separators-with-space :eof
116 (constantly "Must not end with empty line"))
117 (defevaluator :beginning-of-separators-with-space "\\n"
118 (constantly "Must not have two empty lines in a row"))
119 (defevaluator :begin ".*"
120 (constantly "Must begin with in-package form"))
121 (defevaluator :normal "\\( *in-package "
122 (constantly "Only one in-package per file"))
123 (defevaluator :normal "\\n"
125 (set-state :beginning-of-line)
129 (defevaluator :normal " +\\n"
131 "No whitespace at end of line"))
132 (defevaluator :beginning-of-line " *"
134 (set-state :beginning-of-symbols)
136 (defevaluator :beginning-of-separators-with-space " *"
138 (set-state :beginning-of-symbols)
140 (defevaluator :beginning-of-symbols "\\n"
144 "No whitespace only lines"
145 (set-state :beginning-of-separators-with-space))))
146 (defevaluator :beginning-of-symbols ""
150 (defevaluator :normal "\\("
153 (list *line-no* *col-no*)
156 (defevaluator :normal "\\)"
159 ((form (pop *form-stack*)))
161 ((not form) "Unmatched ending paren")
162 ((< 50 (- *line-no* (car form))) "Forms can't be over 50 lines long")))))
164 (cl-ppcre:scan (cl-ppcre:create-scanner " *") "
167 (defevaluator :normal "." (constantly nil))