aboutsummaryrefslogtreecommitdiff
path: root/src/guile/skribilo/verify.scm
blob: 0689c2a00fb71455d3bdfe60e6a6428c79158917 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
;;; verify.scm  --  Skribe AST verification.
;;; -*- coding: iso-8859-1 -*-
;;;
;;; Copyright 2005, 2007, 2008, 2018  Ludovic Court�s <ludo@gnu.org>
;;; Copyright 2003, 2004  Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
;;;
;;;
;;; This file is part of Skribilo.
;;;
;;; Skribilo is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Skribilo is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Skribilo.  If not, see <http://www.gnu.org/licenses/>.

(define-module (skribilo verify)
  #:autoload   (skribilo engine) (engine-ident processor-get-engine)
  #:autoload   (skribilo writer) (writer? writer-options lookup-markup-writer)
  #:autoload   (skribilo lib)    (skribe-warning/ast)
  #:use-module (skribilo debug)
  #:use-module (skribilo ast)
  #:use-module (skribilo condition)
  #:use-module (skribilo utils syntax)
  #:autoload   (skribilo location) (location?)

  #:autoload   (srfi srfi-34)    (raise)
  #:use-module (srfi srfi-35)

  #:use-module (oop goops)

  #:export (verify))


(skribilo-module-syntax)



;;;
;;; Error conditions.
;;;

(define-condition-type &verify-error &skribilo-error
  verify-error?)

(define-condition-type &unsupported-markup-option-error &verify-error
  unsupported-markup-option-error?
  (markup  unsupported-markup-option-error:markup)
  (engine  unsupported-markup-option-error:engine)
  (option  unsupported-markup-option-error:option))


(define (handle-verify-error c)
  ;; Issue a user-friendly error message for error condition C.
  (define (show-location obj)
    (let ((location (and (ast? obj) (ast-loc obj))))
      (if (location? location)
          (format (current-error-port) "~a:~a:~a: "
                  (location-file location)
                  (location-line location)
                  (location-column location)))))

  (cond ((unsupported-markup-option-error? c)
	 (let ((node   (unsupported-markup-option-error:markup c))
               (engine (unsupported-markup-option-error:engine c))
               (option (unsupported-markup-option-error:option c)))
           (show-location node)
	   (format (current-error-port)
                   (G_ "option '~a' of markup '~a' not supported by engine '~a'~%")
		   option (and (markup? node)
                               (markup-markup node))
                   (engine-ident engine))))

	(else
	 (format (current-error-port)
                 (G_ "undefined verify error: ~a~%")
		 c))))

(register-error-condition-handler! verify-error? handle-verify-error)


(define-generic verify)

;;;
;;; CHECK-REQUIRED-OPTIONS
;;;
(define (check-required-options markup writer engine)
  (let ((required-options (slot-ref markup 'required-options))
	(options	  (slot-ref writer 'options))
	(verified?	  (slot-ref writer 'verified?)))
    (or verified?
	(eq? options 'all)
	(begin
	  (for-each (lambda (o)
		      (if (not (memq o options))
                          (raise (condition (&unsupported-markup-option-error
                                             (markup markup)
                                             (option o)
                                             (engine engine))))))
		    required-options)
	  (slot-set! writer 'verified? #t)))))

;;;
;;; CHECK-OPTIONS
;;;
(define (check-options lopts markup engine)

  ;;  Only keywords are checked, symbols are voluntary left unchecked. */
  (with-debug 6 'check-options
      (debug-item "markup="  (markup-markup markup))
      (debug-item "options=" (slot-ref markup 'options))
      (debug-item "lopts="   lopts)
      (for-each
	  (lambda (o2)
	    (for-each
		(lambda (o)
		  (if (and (keyword? o)
			   (not (eq? o :&skribe-eval-location))
			   (not (memq o lopts)))
		      (skribe-warning/ast
		       3
		       markup
		       'verify
		       (format #f "engine ~a does not support markup ~a option '~a' -- ~a"
			       (engine-ident engine)
			       (markup-markup markup)
			       o
			       (markup-option markup o)))))
		o2))
	  (slot-ref markup 'options))))


;;; ======================================================================
;;;
;;;				V E R I F Y
;;;
;;; ======================================================================

;;; TOP
(define-method (verify (obj <top>) e)
  obj)

;;; PAIR
(define-method (verify (obj <pair>) e)
  (if (list? obj)
      (for-each (lambda (o) (verify o e)) obj)
      (begin
        (verify (car obj) e)
        (verify (cdr obj) e)))
  obj)

;;; PROCESSOR
(define-method (verify (obj <processor>) e)
  (let ((combinator (slot-ref obj 'combinator))
	(engine     (slot-ref obj 'engine))
	(body       (slot-ref obj 'body)))
    (verify body (processor-get-engine combinator engine e))
    obj))

;;; NODE
(define-method (verify (node <node>) e)
  ;; Verify body
  (verify (slot-ref node 'body) e)
  ;; Verify options
  (for-each (lambda (o) (verify (cadr o) e))
	    (slot-ref node 'options))
  node)

;;; MARKUP
(define-method (verify (node <markup>) e)
  (with-debug 5 'verify::<markup>
     (debug-item "node="    (markup-markup node))
     (debug-item "options=" (slot-ref node 'options))
     (debug-item "e="	    (engine-ident e))

     (next-method)

     (let ((w (lookup-markup-writer node e)))
       (when (writer? w)
	 (check-required-options node w e)
	 (when (pair? (writer-options w))
	   (check-options (slot-ref w 'options) node e))
	 (let ((validate (slot-ref w 'validate)))
	   (when (procedure? validate)
	     (unless (validate node e)
	       (skribe-warning/ast
		     1
		     node
		     (format #f "node '~a' forbidden here by ~a engine"
			     (markup-markup node)
			     (engine-ident e))))))))
     node))


;;; DOCUMENT
(define-method (verify (node <document>) e)
  (next-method)

  ;; verify the engine customs
  (for-each (lambda (c)
	      (let ((a (cadr c)))
		(set-car! (cdr c) (verify a e))))
	    (slot-ref e 'customs))

   node)

;;; verify.scm ends here