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)
29 (defun set-state (state)
30 (when (not (find state (list :begin ; start of file
33 (error "Can't set state to ~A" state))
37 (defmacro defevaluator (state match func)
41 ((,scanner (when (stringp ,match) (cl-ppcre:create-scanner ,match))))
48 (and (symbolp text) (eql text ,match))
51 (multiple-value-bind (start end) (cl-ppcre:scan ,scanner text)
52 (and start end (= 0 start) (/= 0 end)))))))
53 (lambda (text) (second (multiple-value-list (cl-ppcre:scan ,scanner text))))
57 (defun evaluate (text)
58 ; (if (string= "" text)
60 ((evaluator (find-if (lambda (f) (funcall f *state* text)) *evaluators* :from-end t :key #'car)))
61 (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*)))
63 ((problem (funcall (third evaluator))))
64 (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
66 ((length-of-match (funcall (cadr evaluator) text)))
67 (incf *col-no* length-of-match)
68 (when (< 120 *col-no*) (error (make-condition 'check-failure :msg "Line longer than 120 characters" :line-no *line-no* :col-no *col-no*)))
69 (evaluate (subseq text length-of-match))))));)
71 (defun slurp-file (filename &key (element-type 'character) (sequence-type 'string))
72 (with-open-file (str filename :element-type element-type)
73 (let ((seq (make-sequence sequence-type (file-length str)))) (read-sequence seq str) seq)))
75 (defun check-file (file)
80 (progn (evaluate (slurp-file file)) t)
82 (format t "In file ~A, Had an error: ~S at ~A:~A~%" (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf))
85 (defun check-directory (dir)
86 (every #'identity (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir)))))
89 (setf *evaluators* nil)
90 (defevaluator :begin "\\(in-package[^\\)]*\\)"
92 (set-state :normal) nil))
93 (defevaluator :begin ".*"
95 "Must begin with in-package form")))