2c42714d771c6a3affa1774944e333d70f7dbf22
[wolf] / src / main / checker.lisp
1 (in-package #:style-checker)
2
3 ; Rules
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
9 ; * No tabs
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
16 ;
17 ; Some thoughts
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
21
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)))
25
26 (defvar *state* nil)
27 (defvar *line-no* nil)
28 (defvar *col-no* nil)
29 (defvar *evaluators* nil)
30 (defvar *form-stack* nil)
31
32 (eval-when (:compile-toplevel :load-toplevel :execute)
33  (defparameter *possible-states*
34   '(:begin ; start of file
35     :normal ; normal processing
36     :beginning-of-line
37     :beginning-of-symbols
38     :all ; matches everything
39    )))
40
41
42 (defun set-state (state)
43  (when (not (find state *possible-states*))
44   (error "Can't set state to ~A" state))
45  (setf *state* state)
46  nil)
47
48 (defmacro defevaluator (state match func)
49  (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
50  (let
51   ((scanner (gensym)))
52  `(let
53    ((,scanner (when (stringp ,match) (cl-ppcre:create-scanner ,match))))
54    (pushnew
55     (list
56      (lambda (state text)
57       (and
58        (or (eql :all ,state) (eql ,state state))
59        (or
60         (and (symbolp text) (eql text ,match))
61         (and ,scanner
62              (stringp text)
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))))
66      ,func)
67     *evaluators*))))
68
69 (defun evaluate (text)
70  (if (string= "" text)
71      (let*
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*))))
75      (let
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*)))
78       (let
79        ((problem (funcall (third evaluator))))
80        (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
81        (let
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)))))))
86
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)))
90
91 (defun check-file (file)
92  (set-state :begin)
93  (setf *line-no* 0)
94  (setf *col-no* 0)
95  (format t "~%File: ~A~%" file)
96  (handler-case
97   (progn (evaluate (slurp-file file)) t)
98   (check-failure (cf)
99    (format t " - Had an error: ~S at ~A:~A~%" (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf))
100    nil)))
101
102 (defun check-directory (dir)
103  (every #'identity (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir)))))
104
105 ; These are in reverse order
106 (progn
107  (setf *evaluators* nil)
108  (defevaluator :all "\\t"
109   (constantly "Must not use tabs"))
110  (defevaluator :begin "\\(in-package[^\\)]*\\)"
111   (lambda ()
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"
118   (lambda ()
119    (set-state :beginning-of-line)
120    (incf *line-no*)
121    (setf *col-no* -1)
122    nil))
123  (defevaluator :normal " +\\n"
124   (lambda ()
125    "No whitespace at end of line"))
126  (defevaluator :beginning-of-line " *"
127   (lambda ()
128    (set-state :beginning-of-symbols)
129    nil))
130  (defevaluator :beginning-of-symbols "\\n"
131   (lambda () (when (< 0 *col-no*) "No whitespace only lines")))
132  (defevaluator :beginning-of-symbols ""
133   (lambda ()
134    (set-state :normal)
135    nil))
136  (defevaluator :normal "\\("
137   (lambda ()
138    (push
139     (list *line-no* *col-no*)
140     *form-stack*)
141    nil))
142  (defevaluator :normal "\\)"
143   (lambda ()
144    (let
145     ((form (pop *form-stack*)))
146     (cond
147      ((not form) "Unmatched ending paren")
148      ((< 50 (- *line-no* (car form))) "Forms can't be over 50 lines long")))))
149
150 (cl-ppcre:scan (cl-ppcre:create-scanner " *") "
151 asdf asdf")
152
153  (defevaluator :normal "." (constantly nil))
154  )