Add pretty printing
[wolf] / src / main / checker.lisp
index bb057fb6fb8cf863a1f3552d1a55b6657e7f329a..f811ee35c2dd0ecfa88261735bb7d9e03e630674 100644 (file)
@@ -18,8 +18,8 @@
 ; * No hanging close parens
 ;
 ; Exceptions
-; - comments
-; - multiline strings
+; * comments
+; * multiline strings
 
 ; Some thoughts
 ; - form starting reader macros will have to be hand added to this code
     :beginning-of-line-with-separator ; empty space in there
     :beginning-of-symbols
     :beginning-of-symbols-with-separator
+    :comment-with-separator ; weird edge case for pre-function comments
+    :beginning-of-line-with-comment-and-separator ; weird edge case part 2
     :first-symbol ; first symbol of form/line
     :all ; matches everything
+    :in-string
    )))
 
 (defun set-state (state)
  (setf *col-no* 0)
  (setf *form-stack* nil)
  (setf *form-ended-on-same-line* nil)
- (format t "~%File: ~A~%" file)
  (handler-case
-  (progn (evaluate (slurp-file file)) t)
+  (progn
+   (evaluate (slurp-file file))
+   (list :success file))
   (check-failure (cf)
-   (format t " - Had an error: ~S at ~A:~A~%" (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf))
-   nil)))
+   (list :failure file (check-failure-msg cf) (check-failure-line-no cf) (check-failure-col-no cf)))))
 
 (defun check-directory (dir)
- (every #'identity (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir)))))
+ (mapcar #'check-file (directory (format nil "~A/**/*.lisp" dir))))
+
+(defun any-failures (checks)
+ (find :failure checks :key #'car))
+
+(defun print-failure (failure)
+ (format nil
+  "Style error in ~A at ~A:~A: ~A~%- ~A~%~VT^"
+  (second failure)
+  (1+ (fourth failure))
+  (1+ (fifth failure))
+  (third failure)
+  (with-open-file (str (second failure)) (loop :repeat (fourth failure) :do (read-line str)) (read-line str))
+  (+ (fifth failure) 2)))
+
+(defun pretty-print-check-directory (dir)
+ (let
+  ((checks (check-directory dir)))
+  (format t "In ~A: Checked ~A files with ~A failures~%~%" dir (length checks) (length (remove :success checks :key #'car)))
+  (format t "~{~A~%~}" (mapcar #'print-failure (remove :success checks :key #'car)))
+  (sb-ext:exit :code (if (any-failures checks) 1 0))))
 
 ; These are in reverse order
 (progn
  (setf *evaluators* nil)
+ (defevaluator :beginning-of-symbols " *;[^\\n]*"
+  (lambda () (set-state :normal)))
+ (defevaluator :beginning-of-symbols-with-separator " *;[^\\n]*"
+  (lambda () (set-state :comment-with-separator)))
+ (defevaluator :normal " *;[^\\n]*"
+  (lambda () (set-state :normal)))
  (defevaluator :normal "\\("
   (lambda ()
    (push (list *line-no* *col-no*) *form-stack*)
      (set-state :first-symbol)))))
  (defevaluator :all "\\t" (constantly "Must not use tabs"))
  (defevaluator :begin "\\(in-package[^\\)]*\\)" (lambda () (set-state :normal)))
- (defevaluator :beginning-of-line-with-separator :eof (constantly "Must not end with empty line"))
+ (defevaluator :beginning-of-line-with-separator :eof
+  (lambda ()
+   (incf *line-no* -1)
+   "Must not end with empty line"))
  (defevaluator :beginning-of-line-with-separator "\\n" (constantly "Must not have two empty lines in a row"))
  (defevaluator :begin ".*" (constantly "Must begin with in-package form"))
  (defevaluator :all "\\( *in-package " (constantly "Only one in-package per file"))
    (incf *line-no*)
    (setf *col-no* -1)
    (set-state :beginning-of-line)))
+ (defevaluator :comment-with-separator "\\n"
+  (lambda ()
+   (incf *line-no*)
+   (setf *col-no* -1)
+   (set-state :beginning-of-line-with-comment-and-separator)
+   nil))
  (defevaluator :normal " +\\n" (constantly "No whitespace at end of line"))
  (defevaluator :beginning-of-line " *" (lambda () (set-state :beginning-of-symbols)))
  (defevaluator :beginning-of-line-with-separator " *" (lambda () (set-state :beginning-of-symbols-with-separator)))
+ (defevaluator :beginning-of-line-with-comment-and-separator "\\n"
+  (lambda ()
+   (progn
+    (incf *line-no*)
+    (setf *col-no* -1)
+    (set-state :beginning-of-line-with-separator))))
+ (defevaluator :beginning-of-line-with-comment-and-separator " *" (lambda () (set-state :beginning-of-symbols-with-separator)))
  (defevaluator :beginning-of-symbols "\\n"
   (lambda ()
    (if
      ((< 50 (- *line-no* (car form))) "Forms can't be over 50 lines long")
      (t (setf *form-ended-on-same-line* (= *line-no* (car form))) nil)))))
  (defevaluator :normal "::" (constantly "No internal symbols from other packages"))
+ (defevaluator :in-string "\\\\\"" (constantly nil))
+ (defevaluator :normal "\"" (lambda () (set-state :in-string)))
+ (defevaluator :in-string "\"" (lambda () (set-state :normal)))
+ (defevaluator :in-string "\\n"
+  (lambda ()
+   (incf *line-no*)
+   (setf *col-no* -1)
+   nil))
+ (defevaluator :in-string "." (constantly nil))
  (defevaluator :first-symbol "\\n" (constantly "No new line after opening form"))
  (defevaluator :first-symbol " " (constantly "No space after opening parens"))
  (defevaluator :first-symbol ""
   (lambda ()
-   ;(format t "HMMM: ~A ~A ~A~%" *form-stack* *line-no* *col-no*)
    (cond
     ((and *form-stack* (/= (1+ (cadr (car *form-stack*))) *col-no*))
      "All form elements must be indented equally")