aboutsummaryrefslogtreecommitdiff
path: root/src/guile/skribe/engine.scm
blob: 1cac16864adae24c266dc6a8196d6c127aba94a9 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
;;;;
;;;; engines.stk	-- Skribe Engines Stuff
;;;; 
;;;; Copyright � 2003-2004 Erick Gallesio - I3S-CNRS/ESSI <eg@essi.fr>
;;;; 
;;;; 
;;;; This program 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 2 of the License, or
;;;; (at your option) any later version.
;;;; 
;;;; This program 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 this program; if not, write to the Free Software
;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
;;;; USA.
;;;; 
;;;;           Author: Erick Gallesio [eg@essi.fr]
;;;;    Creation date: 24-Jul-2003 20:33 (eg)
;;;; Last file update: 28-Oct-2004 21:21 (eg)
;;;;

(define-module (skribe engine)
  :use-module (skribe debug)
;  :use-module (skribe eval)
  :use-module (skribe writer)
  :use-module (skribe types)

  :use-module (oop goops)
  :use-module (ice-9 optargs)
 
  :export (default-engine default-engine-set!
	   make-engine copy-engine find-engine
	   engine-custom engine-custom-set!
	   engine-format? engine-add-writer!
	   processor-get-engine
	   push-default-engine pop-default-engine))




;;; Module definition is split here because this file is read by the documentation
;;; Should be changed.
;(select-module SKRIBE-ENGINE-MODULE)

(define *engines*		'())
(define *default-engine* 	#f)
(define *default-engines* 	'())


(define (default-engine)
   *default-engine*)


(define (default-engine-set! e)
  (unless (engine? e)
    (skribe-error 'default-engine-set! "bad engine ~S" e))
  (set! *default-engine* e)
  (set! *default-engines* (cons e *default-engines*))
  e)


(define (push-default-engine e)
   (set! *default-engines* (cons e *default-engines*))
   (default-engine-set! e))

(define (pop-default-engine)
   (if (null? *default-engines*)
       (skribe-error 'pop-default-engine "Empty engine stack" '())
       (begin
	  (set! *default-engines* (cdr *default-engines*))
	  (if (pair? *default-engines*)
	      (default-engine-set! (car *default-engines*))
	      (set! *default-engine* #f)))))


(define (processor-get-engine combinator newe olde)
  (cond
    ((procedure? combinator)
     (combinator newe olde))
    ((engine? newe)
     newe)
    (else
     olde)))


(define (engine-format? fmt . e)
  (let ((e (cond
	     ((pair? e) (car e))
	     ((engine? *skribe-engine*) *skribe-engine*)
	     (else (find-engine *skribe-engine*)))))
    (if (not (engine? e))
	(skribe-error 'engine-format? "No engine" e)
	(string=? fmt (engine-format e)))))

;;;
;;; MAKE-ENGINE
;;; 
(define* (make-engine ident #:key (version 'unspecified)
		     		(format "raw")
				(filter #f)
				(delegate #f)
				(symbol-table '())
				(custom '())
				(info '()))
  (let ((e (make <engine> :ident ident :version version :format format
		 	  :filter filter :delegate delegate
			  :symbol-table symbol-table
			  :custom custom :info info)))
    ;; store the engine in the global table
    (set! *engines* (cons e *engines*))
    ;; return it
    e))


;;;
;;; COPY-ENGINE
;;;
(define* (copy-engine ident e #:key (version 'unspecified)
				  (filter #f)
				  (delegate #f)
				  (symbol-table #f)
				  (custom #f))
  (let ((new (shallow-clone e)))
    (slot-set! new 'ident 	 ident)
    (slot-set! new 'version 	 version)
    (slot-set! new 'filter	 (or filter (slot-ref e 'filter)))
    (slot-set! new 'delegate	 (or delegate (slot-ref e 'delegate)))
    (slot-set! new 'symbol-table (or symbol-table (slot-ref e 'symbol-table)))
    (slot-set! new 'customs	 (or custom (slot-ref e 'customs)))

    (set! *engines* (cons new *engines*))
    new))


;;;
;;; 	FIND-ENGINE
;;;
(define (%find-loaded-engine id version)
  (let Loop ((es *engines*))
    (cond
      ((null? es) #f)
      ((eq? (slot-ref (car es) 'ident) id)
       (cond
	   ((eq? version 'unspecified) 		       (car es))
	   ((eq? version (slot-ref (car es) 'version)) (car es))
	   (else			 	       (Loop (cdr es)))))
      (else (loop (cdr es))))))


(define* (find-engine id #:key (version 'unspecified))
  (with-debug 5 'find-engine
     (debug-item "id=" id " version=" version)

     (or (%find-loaded-engine id version)
	 (let ((c (assq id *skribe-auto-load-alist*)))
	   (debug-item "c=" c)
	   (if (and c (string? (cdr c)))
	       (begin
		 (skribe-load (cdr c) :engine 'base)
		 (%find-loaded-engine id version))
	       #f)))))

;;;
;;; ENGINE-CUSTOM
;;;
(define (engine-custom e id)
  (let* ((customs (slot-ref e 'customs))
	 (c       (assq id customs)))
    (if (pair? c)
	(cadr c)
	'unspecified)))


;;;
;;; ENGINE-CUSTOM-SET!
;;;
(define (engine-custom-set! e id val)
  (let* ((customs (slot-ref e 'customs))
	 (c       (assq id customs)))
    (if (pair? c)
	(set-car! (cdr c) val)
	(slot-set! e 'customs (cons (list id val) customs)))))


;;;
;;; ENGINE-ADD-WRITER!
;;;
(define (engine-add-writer! e ident pred upred opt before action after class valid)
  (define (check-procedure name proc arity)
    (cond
      ((not (procedure? proc))
         (skribe-error ident "Illegal procedure" proc))
      ((not (equal? (%procedure-arity proc) arity))
         (skribe-error ident
		       (format #f "Illegal ~S procedure" name)
		       proc))))

  (define (check-output name proc)
    (and proc (or (string? proc) (check-procedure name proc 2))))

  ;;
  ;; Engine-add-writer! starts here
  ;;
  (unless (is-a? e <engine>)
    (skribe-error ident "Illegal engine" e))
  
  ;; check the options
  (unless (or (eq? opt 'all) (list? opt))
    (skribe-error ident "Illegal options" opt))
  
  ;; check the correctness of the predicate
  (check-procedure "predicate" pred 2)

  ;; check the correctness of the validation proc
  (when valid
    (check-procedure "validate" valid 2))
  
  ;; check the correctness of the three actions
  (check-output "before" before)
  (check-output "action" action)
  (check-output "after" after)

  ;; create a new writer and bind it
  (let ((n (make <writer>
	     :ident (if (symbol? ident) ident 'all)
	     :class class :pred pred :upred upred :options opt
	     :before before :action action :after after
	     :validate valid)))
    (slot-set! e 'writers (cons n (slot-ref e 'writers)))
    n))

;;;; ======================================================================
;;;;
;;;;   				    I N I T S
;;;;
;;;; ======================================================================

;; A base engine must pre-exist before anything is loaded. In
;; particular, this dummy base engine is used to load the actual
;; definition of base. 

(make-engine 'base :version 'bootstrap)