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 only lines
13 ; - No empty lines at end of file
16 ; - form starting reader macros will have to be hand added to this code
17 ; - exceptions will eventually arise, and the rule file will have to be changed
18 ; - the proper formatting of "loop" is weird
20 (define-condition check-failure nil ((msg :initarg :msg :reader check-failure-msg)
21 (line-no :initarg :line-no :reader check-failure-line-no)
22 (col-no :initarg :col-no :reader check-failure-col-no)))
25 (defvar *line-no* nil)
27 (defvar *evaluators* nil)
28 (defvar *form-stack* nil)
30 (eval-when (:compile-toplevel :load-toplevel :execute)
31 (defparameter *possible-states*
32 '(:begin ; start of file
33 :normal ; normal processing
37 (defun set-state (state)
38 (when (not (find state *possible-states*))
39 (error "Can't set state to ~A" state))
43 (defmacro defevaluator (state match func)
44 (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
48 ((,scanner (when (stringp ,match) (cl-ppcre:create-scanner ,match))))
55 (and (symbolp text) (eql text ,match))
58 (multiple-value-bind (start end) (cl-ppcre:scan ,scanner text)
59 (and start end (= 0 start) (/= 0 end)))))))
60 (lambda (text) (second (multiple-value-list (cl-ppcre:scan ,scanner text))))
64 (defun evaluate (text)
67 ((evaluator (find-if (lambda (f) (funcall f *state* :eof)) *evaluators* :from-end t :key #'car))
68 (problem (when evaluator (funcall (third evaluator)))))
69 (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*))))
71 ((evaluator (find-if (lambda (f) (funcall f *state* text)) *evaluators* :from-end t :key #'car)))
72 (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*)))
74 ((problem (funcall (third evaluator))))
75 (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
77 ((length-of-match (funcall (cadr evaluator) text)))
78 (incf *col-no* length-of-match)
79 (when (< 120 *col-no*) (error (make-condition 'check-failure :msg "Line longer than 120 characters" :line-no *line-no* :col-no 0)))
80 (evaluate (subseq text length-of-match)))))))
82 (defun slurp-file (filename &key (element-type 'character) (sequence-type 'string))
83 (with-open-file (str filename :element-type element-type)
84 (let ((seq (make-sequence sequence-type (file-length str)))) (read-sequence seq str) seq)))
86 (defun check-file (file)
90 (format t "~%File: ~A~%" file)
92 (progn (evaluate (slurp-file file)) t)
94 (format t " - Had an error: ~S at ~A:~A~%" (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf))
97 (defun check-directory (dir)
98 (every #'identity (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir)))))
100 ; These are in reverse order
102 (setf *evaluators* nil)
103 (defevaluator :begin "\\(in-package[^\\)]*\\)"
105 (set-state :normal) nil))
106 (defevaluator :begin ".*"
108 "Must begin with in-package form"))
109 (defevaluator :normal "\\n"
114 (defevaluator :normal "\\("
117 (list *line-no* *col-no*)
120 (defevaluator :normal "\\)"
123 ((form (pop *form-stack*)))
125 (< 50 (- *line-no* (car form)))
126 "Forms can't be over 50 lines long"))))
128 (defevaluator :normal "." (constantly nil))