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 ; * Only one in-package per file
18 ; - form starting reader macros will have to be hand added to this code
19 ; - exceptions will eventually arise, and the rule file will have to be changed
20 ; - the proper formatting of "loop" is weird
22 (define-condition check-failure nil ((msg :initarg :msg :reader check-failure-msg)
23 (line-no :initarg :line-no :reader check-failure-line-no)
24 (col-no :initarg :col-no :reader check-failure-col-no)))
27 (defvar *line-no* nil)
29 (defvar *evaluators* nil)
30 (defvar *form-stack* nil)
32 (eval-when (:compile-toplevel :load-toplevel :execute)
33 (defparameter *possible-states*
34 '(:begin ; start of file
35 :normal ; normal processing
38 :all ; matches everything
42 (defun set-state (state)
43 (when (not (find state *possible-states*))
44 (error "Can't set state to ~A" state))
48 (defmacro defevaluator (state match func)
49 (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
53 ((,scanner (when (stringp ,match) (cl-ppcre:create-scanner ,match))))
58 (or (eql :all ,state) (eql ,state state))
60 (and (symbolp text) (eql text ,match))
63 (multiple-value-bind (start end) (cl-ppcre:scan ,scanner text)
64 (and start end (= 0 start)))))))
65 (lambda (text) (second (multiple-value-list (cl-ppcre:scan ,scanner text))))
69 (defun evaluate (text)
72 ((evaluator (find-if (lambda (f) (funcall f *state* :eof)) *evaluators* :from-end t :key #'car))
73 (problem (when evaluator (funcall (third evaluator)))))
74 (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*))))
76 ((evaluator (find-if (lambda (f) (funcall f *state* text)) *evaluators* :from-end t :key #'car)))
77 (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*)))
79 ((problem (funcall (third evaluator))))
80 (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
82 ((length-of-match (funcall (cadr evaluator) text)))
83 (incf *col-no* length-of-match)
84 (when (< 120 *col-no*) (error (make-condition 'check-failure :msg "Line longer than 120 characters" :line-no *line-no* :col-no 0)))
85 (evaluate (subseq text length-of-match)))))))
87 (defun slurp-file (filename &key (element-type 'character) (sequence-type 'string))
88 (with-open-file (str filename :element-type element-type)
89 (let ((seq (make-sequence sequence-type (file-length str)))) (read-sequence seq str) seq)))
91 (defun check-file (file)
95 (format t "~%File: ~A~%" file)
97 (progn (evaluate (slurp-file file)) t)
99 (format t " - Had an error: ~S at ~A:~A~%" (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf))
102 (defun check-directory (dir)
103 (every #'identity (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir)))))
105 ; These are in reverse order
107 (setf *evaluators* nil)
108 (defevaluator :all "\\t"
109 (constantly "Must not use tabs"))
110 (defevaluator :begin "\\(in-package[^\\)]*\\)"
112 (set-state :normal) nil))
113 (defevaluator :begin ".*"
114 (constantly "Must begin with in-package form"))
115 (defevaluator :normal "\\( *in-package "
116 (constantly "Only one in-package per file"))
117 (defevaluator :normal "\\n"
119 (set-state :beginning-of-line)
123 (defevaluator :normal " +\\n"
125 "No whitespace at end of line"))
126 (defevaluator :beginning-of-line " *"
128 (set-state :beginning-of-symbols)
130 (defevaluator :beginning-of-symbols "\\n"
131 (lambda () (when (< 0 *col-no*) "No whitespace only lines")))
132 (defevaluator :beginning-of-symbols ""
136 (defevaluator :normal "\\("
139 (list *line-no* *col-no*)
142 (defevaluator :normal "\\)"
145 ((form (pop *form-stack*)))
147 ((not form) "Unmatched ending paren")
148 ((< 50 (- *line-no* (car form))) "Forms can't be over 50 lines long")))))
150 (cl-ppcre:scan (cl-ppcre:create-scanner " *") "
153 (defevaluator :normal "." (constantly nil))