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
41 (defun set-state (state)
42 (when (not (find state *possible-states*))
43 (error "Can't set state to ~A" state))
47 (defmacro defevaluator (state match func)
48 (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
52 ((,scanner (when (stringp ,match) (cl-ppcre:create-scanner ,match))))
59 (and (symbolp text) (eql text ,match))
62 (multiple-value-bind (start end) (cl-ppcre:scan ,scanner text)
63 (and start end (= 0 start)))))))
64 (lambda (text) (second (multiple-value-list (cl-ppcre:scan ,scanner text))))
68 (defun evaluate (text)
71 ((evaluator (find-if (lambda (f) (funcall f *state* :eof)) *evaluators* :from-end t :key #'car))
72 (problem (when evaluator (funcall (third evaluator)))))
73 (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*))))
75 ((evaluator (find-if (lambda (f) (funcall f *state* text)) *evaluators* :from-end t :key #'car)))
76 (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*)))
78 ((problem (funcall (third evaluator))))
79 (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
81 ((length-of-match (funcall (cadr evaluator) text)))
82 (incf *col-no* length-of-match)
83 (when (< 120 *col-no*) (error (make-condition 'check-failure :msg "Line longer than 120 characters" :line-no *line-no* :col-no 0)))
84 (evaluate (subseq text length-of-match)))))))
86 (defun slurp-file (filename &key (element-type 'character) (sequence-type 'string))
87 (with-open-file (str filename :element-type element-type)
88 (let ((seq (make-sequence sequence-type (file-length str)))) (read-sequence seq str) seq)))
90 (defun check-file (file)
94 (format t "~%File: ~A~%" file)
96 (progn (evaluate (slurp-file file)) t)
98 (format t " - Had an error: ~S at ~A:~A~%" (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf))
101 (defun check-directory (dir)
102 (every #'identity (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir)))))
104 ; These are in reverse order
106 (setf *evaluators* nil)
107 (defevaluator :begin "\\(in-package[^\\)]*\\)"
109 (set-state :normal) nil))
110 (defevaluator :begin ".*"
111 (constantly "Must begin with in-package form"))
112 (defevaluator :normal "\\( *in-package "
113 (constantly "Only one in-package per file"))
114 (defevaluator :normal "\\n"
116 (set-state :beginning-of-line)
120 (defevaluator :normal " +\\n"
122 "No whitespace at end of line"))
123 (defevaluator :beginning-of-line " *"
125 (set-state :beginning-of-symbols)
127 (defevaluator :beginning-of-symbols "\\n"
128 (lambda () (when (< 0 *col-no*) "No whitespace only lines")))
129 (defevaluator :beginning-of-symbols ""
133 (defevaluator :normal "\\("
136 (list *line-no* *col-no*)
139 (defevaluator :normal "\\)"
142 ((form (pop *form-stack*)))
144 ((not form) "Unmatched ending paren")
145 ((< 50 (- *line-no* (car form))) "Forms can't be over 50 lines long")))))
147 (cl-ppcre:scan (cl-ppcre:create-scanner " *") "
150 (defevaluator :normal "." (constantly nil))