Check for internal symbols
[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 ; * Never have two empty lines in a row
16 ; * Only one in-package per file
17 ;
18 ; Some thoughts
19 ; - form starting reader macros will have to be hand added to this code
20 ; - exceptions will eventually arise, and the rule file will have to be changed
21 ; - the proper formatting of "loop" is weird
22
23 (define-condition check-failure nil ((msg :initarg :msg :reader check-failure-msg)
24                                      (line-no :initarg :line-no :reader check-failure-line-no)
25                                      (col-no :initarg :col-no :reader check-failure-col-no)))
26
27 (defvar *state* nil)
28 (defvar *line-no* nil)
29 (defvar *col-no* nil)
30 (defvar *evaluators* nil)
31 (defvar *form-stack* nil)
32
33 (eval-when (:compile-toplevel :load-toplevel :execute)
34  (defparameter *possible-states*
35   '(:begin ; start of file
36     :normal ; normal processing
37     :beginning-of-line
38     :beginning-of-separators-with-space ; empty space in there
39     :beginning-of-symbols
40     :all ; matches everything
41    )))
42
43
44 (defun set-state (state)
45  (when (not (find state *possible-states*))
46   (error "Can't set state to ~A" state))
47  (setf *state* state)
48  nil)
49
50 (defmacro defevaluator (state match func)
51  (when (not (find state *possible-states*)) (error "~A is an invalid state" state))
52  (let
53   ((scanner (gensym)))
54  `(let
55    ((,scanner (when (stringp ,match) (cl-ppcre:create-scanner ,match))))
56    (pushnew
57     (list
58      (lambda (state text)
59       (and
60        (or (eql :all ,state) (eql ,state state))
61        (or
62         (and (symbolp text) (eql text ,match))
63         (and ,scanner
64              (stringp text)
65              (multiple-value-bind (start end) (cl-ppcre:scan ,scanner text)
66               (and start end (= 0 start)))))))
67      (lambda (text) (second (multiple-value-list (cl-ppcre:scan ,scanner text))))
68      ,func)
69     *evaluators*))))
70
71 (defun evaluate (text)
72  (if (string= "" text)
73      (let*
74       ((evaluator (find-if (lambda (f) (funcall f *state* :eof)) *evaluators* :from-end t :key #'car))
75        (problem (when evaluator (funcall (third evaluator)))))
76       (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*))))
77      (let
78       ((evaluator (find-if (lambda (f) (funcall f *state* text)) *evaluators* :from-end t :key #'car)))
79       (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*)))
80       (let
81        ((problem (funcall (third evaluator))))
82        (when problem (error (make-condition 'check-failure :msg problem :line-no *line-no* :col-no *col-no*)))
83        (let
84         ((length-of-match (funcall (cadr evaluator) text)))
85         (incf *col-no* length-of-match)
86         (when (< 120 *col-no*) (error (make-condition 'check-failure :msg "Line longer than 120 characters" :line-no *line-no* :col-no 0)))
87         (evaluate (subseq text length-of-match)))))))
88
89 (defun slurp-file (filename &key (element-type 'character) (sequence-type 'string))
90  (with-open-file (str filename :element-type element-type)
91   (let ((seq (make-sequence sequence-type (file-length str)))) (read-sequence seq str) seq)))
92
93 (defun check-file (file)
94  (set-state :begin)
95  (setf *line-no* 0)
96  (setf *col-no* 0)
97  (format t "~%File: ~A~%" file)
98  (handler-case
99   (progn (evaluate (slurp-file file)) t)
100   (check-failure (cf)
101    (format t " - Had an error: ~S at ~A:~A~%" (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf))
102    nil)))
103
104 (defun check-directory (dir)
105  (every #'identity (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir)))))
106
107 ; These are in reverse order
108 (progn
109  (setf *evaluators* nil)
110  (defevaluator :all "\\t"
111   (constantly "Must not use tabs"))
112  (defevaluator :begin "\\(in-package[^\\)]*\\)"
113   (lambda ()
114    (set-state :normal) nil))
115  (defevaluator :beginning-of-separators-with-space :eof
116   (constantly "Must not end with empty line"))
117  (defevaluator :beginning-of-separators-with-space "\\n"
118   (constantly "Must not have two empty lines in a row"))
119  (defevaluator :begin ".*"
120   (constantly "Must begin with in-package form"))
121  (defevaluator :normal "\\( *in-package "
122   (constantly "Only one in-package per file"))
123  (defevaluator :normal "\\n"
124   (lambda ()
125    (set-state :beginning-of-line)
126    (incf *line-no*)
127    (setf *col-no* -1)
128    nil))
129  (defevaluator :normal " +\\n"
130   (lambda ()
131    "No whitespace at end of line"))
132  (defevaluator :beginning-of-line " *"
133   (lambda ()
134    (set-state :beginning-of-symbols)
135    nil))
136  (defevaluator :beginning-of-separators-with-space " *"
137   (lambda ()
138    (set-state :beginning-of-symbols)
139    nil))
140  (defevaluator :beginning-of-symbols "\\n"
141   (lambda ()
142    (if
143     (< 0 *col-no*)
144     "No whitespace only lines"
145     (set-state :beginning-of-separators-with-space))))
146  (defevaluator :beginning-of-symbols ""
147   (lambda ()
148    (set-state :normal)
149    nil))
150  (defevaluator :normal "\\("
151   (lambda ()
152    (push
153     (list *line-no* *col-no*)
154     *form-stack*)
155    nil))
156  (defevaluator :normal "\\)"
157   (lambda ()
158    (let
159     ((form (pop *form-stack*)))
160     (cond
161      ((not form) "Unmatched ending paren")
162      ((< 50 (- *line-no* (car form))) "Forms can't be over 50 lines long")))))
163  (defevaluator :normal "::"
164   (constantly "No internal symbols from other packages"))
165
166  (defevaluator :normal "." (constantly nil))
167  )