Add coverage, get to near 100
[sheep] / src / main / var.lisp
1 ; Copyright 2022 Frank Duncan (frank@consxy.com) under AGPL3.  See distributed LICENSE.txt.
2 (in-package #:sheep-var)
3
4 (defvar *doc*)
5 (defvar *prev-line*)
6 (defun peek () (car *doc*))
7 (defun next () (setf *prev-line* (pop *doc*)))
8 (defun more () (not (not *doc*)))
9 (defun prev-line () *prev-line*)
10
11 (defvar *keywords*)
12
13 (defun add-keyword (type)
14  (setf *keywords* (remove-duplicates (cons type *keywords*) :test #'string=)))
15
16 (defun fire-error (msg) (error (make-instance 'sheep:validation-failure :msg msg)))
17
18 (defun expect-blank-line ()
19  (let
20   ((prev (prev-line)))
21   (when (string/= "" (next)) (fire-error (format nil "Expected blank line after: ~A" prev)))))
22
23 (defun verify-next-line (&key optional)
24  (cond
25   ((and optional (not (more))) t)
26   ((cl-ppcre:scan " $" (peek)) (fire-error (format nil "Can't end line with a space: ~A" (peek))))
27   ((< 120 (length (peek))) (fire-error (format nil "Longer than 120 chars: ~A" (peek))))))
28
29 (defun freeform->paragraphs (next next-optional)
30  (verify-next-line :optional t)
31  (let
32   ((next-line (next)))
33   (cond
34    ((and next-optional (not next-line)) (list ""))
35    ((and (string= "" next-line) (not (more))) (fire-error "Can't end with empty line"))
36    ((cl-ppcre:scan "^  [^ ]+" next-line)
37     (let
38      ((rest-of-freeform (freeform->paragraphs next next-optional)))
39      (cons
40       (format nil "~A~A~A"
41        (subseq next-line 2 (length next-line))
42        (if (string/= "" (car rest-of-freeform)) " " "")
43        (car rest-of-freeform))
44       (cdr rest-of-freeform))))
45    ((string= "" next-line)
46     (if (string= next (peek))
47      (list "")
48      (cons "" (freeform->paragraphs next next-optional))))
49    (t (fire-error (format nil "Got unexpected line, requires blank lines or start with two spaces: ~S" next-line))))))
50
51 (defun parse-freeform (start section next next-optional)
52  (when (string/= start (next)) (fire-error (format nil "Expected ~A instead of: ~A" start (prev-line))))
53  (expect-blank-line)
54  (let
55   ((paragraphs (freeform->paragraphs next next-optional)))
56   (list section (mapcar #'handle-text paragraphs))))
57
58 (defun process-examples ()
59  (when (more)
60   (verify-next-line :optional t)
61   (cons
62    (let
63     ((example-scanner (cl-ppcre:create-scanner "^  ([^ ].+) => (.+)$"))
64      (next-line (next)))
65     (if (not (cl-ppcre:scan example-scanner next-line))
66      (fire-error (format nil "Example line does not match \"  example => result\": ~A" next-line))
67      (cl-ppcre:register-groups-bind (example result) (example-scanner next-line)
68       (list example result))))
69    (process-examples))))
70
71 (defun parse-examples ()
72  ; This shouldn't fire, unless there's a bug in our processing
73  (when (string/= "EXAMPLES:" (next)) (fire-error (format nil "Expected EXAMPLES: instead of: ~A" (prev-line))))
74  (expect-blank-line)
75  (list :examples (process-examples)))
76
77 ; For formatting of things like types in there
78 (defun handle-text (text)
79  (labels
80   ((inject-keywords (text remaining-keywords)
81     (if
82      (not remaining-keywords)
83      (list text)
84      (apply #'append
85       (mapcar
86        (lambda
87         (text-item)
88         (cond
89          ((not (cl-ppcre:scan (cl-ppcre:quote-meta-chars (car remaining-keywords)) text-item)) (list text-item))
90          (t
91           (let
92            ((split-text (cl-ppcre:split (cl-ppcre:quote-meta-chars (car remaining-keywords)) text-item :limit 1000)))
93            (apply #'append
94             (list (car split-text))
95             (mapcar (lambda (ti) (list (list :keyword (car remaining-keywords)) ti)) (cdr split-text)))))))
96        (inject-keywords text (cdr remaining-keywords)))))))
97   (list :text (inject-keywords text *keywords*))))
98
99 (defun parse-header (var)
100  (verify-next-line)
101  (let*
102   ((var-name (symbol-name var)))
103   (when (not (string= var-name (peek)))
104    (fire-error (format nil "First line of ~A did not match: ~A, ~A" var var-name (peek))))
105   (add-keyword var-name)
106   (next)
107   (expect-blank-line)
108   (list :variable var-name)))
109
110 (defun internal-doc->ast (var doc)
111  (let
112   ((*doc* (cl-ppcre:split "\\n" doc :limit 1000))
113    (*prev-line* nil)
114    (*keywords* nil))
115   (cons (parse-header var)
116    (append
117     (list
118      (parse-freeform "VALUE TYPE:" :value-type "INITIAL VALUE:" nil)
119      (parse-freeform "INITIAL VALUE:" :initial-value "DESCRIPTION:" nil)
120      (parse-freeform "DESCRIPTION:" :description "EXAMPLES:" t))
121     (when (more) (list (parse-examples)))))))
122
123 (defun doc->ast (var) (internal-doc->ast var (documentation var 'variable)))
124
125 (defun format-text (text)
126  (format nil "~{~A~}"
127   (mapcar
128    (lambda (text)
129     (cond
130      ((stringp text) text)
131      ((and (listp text) (eql :keyword (car text))) (format nil "_~(~A~)_" (cadr text)))
132      ; This should never fire, unless there's a bug in our processor
133      (t (fire-error (format nil "Don't know how to convert text: ~S" text)))))
134    (cadr text))))
135
136 (defun format-header (header)
137  (format nil "## Variable ~A
138
139 "
140   (cl-ppcre:quote-meta-chars (second header))))
141
142 (defun format-freeform (heading text)
143  (format nil "#### ~A:~%~%~{~A~%~^~%~}" heading (mapcar #'format-text (cadr text))))
144
145 (defun format-examples (examples)
146  (if (not examples)
147   ""
148   (format nil "~%#### Examples:~%~%~{~A~%~}"
149    (mapcar
150     (lambda (example) (format nil "```~A``` => ```~A```  " (car example) (cadr example)))
151     (cadr examples)))))
152
153 (defun ast->md (ast)
154  (flet
155   ((get-section (name) (find name ast :key #'car)))
156   (format nil "~A~A~%~A~%~A~A"
157    (format-header (get-section :variable))
158    (format-freeform "Value Type" (get-section :value-type))
159    (format-freeform "Initial Value" (get-section :initial-value))
160    (format-freeform "Description" (get-section :description))
161    (format-examples (get-section :examples)))))
162
163 (defun ast->category-name (ast)
164  (declare (ignore ast))
165  "variable")
166
167 (defun ast->short-name (ast)
168  (format nil "~(~A~)" (cl-ppcre:quote-meta-chars (second (find :variable ast :key #'car)))))
169
170 (defun ast->link (ast)
171  (format nil "variable-~(~A~)" (cl-ppcre:regex-replace-all "\\*" (second (find :variable ast :key #'car)) "")))
172
173 (defun ast->short-desc (ast)
174  (format-text (car (cadr (find :description ast :key #'car)))))