aboutsummaryrefslogtreecommitdiff
path: root/src/guile/skribilo/resolve.scm
blob: 7058d9010e6f3d738bd56df1262b1cc8bf7c5044 (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
;;; resolve.scm  --  Skribilo reference resolution.
;;;
;;; Copyright 2005, 2006, 2008, 2009, 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 resolve)
  :use-module (skribilo debug)
  :use-module (skribilo ast)
  :use-module (skribilo utils syntax)

  :use-module (oop goops)
  :use-module (srfi srfi-39)

  :use-module (skribilo condition)
  :use-module (srfi srfi-34)
  :use-module (srfi srfi-35)
  :use-module (ice-9 match)

  :export (resolve! resolve-search-parent
	   resolve-counter resolve-parent resolve-ident
	   *document-being-resolved*))

(skribilo-module-syntax)




;;;
;;; Resolving nodes.
;;;

;; The document being resolved.  Note: This is only meant to be used by the
;; compatibility layer in order to implement things like `find-markups'!
(define *document-being-resolved* (make-parameter #f))

(define *unresolved* (make-parameter #f))
(define-generic do-resolve!)


;;;; ======================================================================
;;;;
;;;; RESOLVE!
;;;;
;;;; This function iterates over an ast until all unresolved  references
;;;; are resolved.
;;;;
;;;; ======================================================================
(define (resolve! ast engine env)
  (with-debug 3 'resolve
     (debug-item "ast=" ast)

     (if (document? ast)
	 ;; Bind nodes prior to resolution so that unresolved nodes can
	 ;; lookup nodes by identifier using `document-lookup-node' or
	 ;; `resolve-ident'.
	 (document-bind-nodes! ast))

     (parameterize ((*unresolved* #f))
       (let Loop ((ast ast))
	 (*unresolved* #f)
	 (let ((ast (do-resolve! ast engine env)))
	   (if (*unresolved*)
	       (begin
		 (debug-item "iterating over ast " ast)
		 (Loop ast))
	       ast))))))

;;;; ======================================================================
;;;;
;;;;				D O - R E S O L V E !
;;;;
;;;; ======================================================================

(define-method (do-resolve! ast engine env)
  ast)


(define-method (do-resolve! (ast <pair>) engine env)
  (match ast
    ((? list?)                                    ;proper list
     (map (lambda (elt)
            (do-resolve! elt engine env))
          ast))
    ((head . tail)                                ;pair or improper list
     (cons (do-resolve! head engine env)
           (do-resolve! tail engine env)))))


(define-method (do-resolve! (node <node>) engine env)
  (if (ast-resolved? node)
      node
      (let ((body    (slot-ref node 'body))
	    (options (slot-ref node 'options))
	    (parent  (slot-ref node 'parent))
	    (unresolved? (*unresolved*)))
	(with-debug 5 'do-resolve<body>
	   (debug-item "body=" body)
	   (parameterize ((*unresolved* #f))
	     (when (eq? parent 'unspecified)
	       (let ((p (assq 'parent env)))
		 (slot-set! node 'parent
			    (and (pair? p) (pair? (cdr p)) (cadr p)))))

             (when (pair? options)
               (debug-item "unresolved options=" options)
               (let ((resolved (map (match-lambda
                                      ((option value)
                                       (list option
                                             (do-resolve! value engine env))))
                                    options)))
                 (slot-set! node 'options resolved)
                 (debug-item "resolved options=" options)))

	     (slot-set! node 'body (do-resolve! body engine env))
	     (slot-set! node 'resolved? (not (*unresolved*))))

	   (*unresolved* (or unresolved? (not (ast-resolved? node))))
	   node))))


(define-method (do-resolve! (node <container>) engine env0)
  ;; Similar to the NODE method, except that (i) children will get NODE as
  ;; their parent, and (ii) NODE may extend its environment, through its
  ;; `env' slot.
  (if (ast-resolved? node)
      node
      (let ((body     (slot-ref node 'body))
            (options  (slot-ref node 'options))
            (env      (slot-ref node 'env))
            (parent   (slot-ref node 'parent))
            (unresolved? (*unresolved*)))
        (with-debug 5 'do-resolve<container>
           (debug-item "markup=" (markup-markup node))
           (debug-item "body=" body)
           (debug-item "env0=" env0)
           (debug-item "env=" env)
           (parameterize ((*unresolved* #f))
             (when (eq? parent 'unspecified)
               (let ((p (assq 'parent env0)))
                 (slot-set! node 'parent
                            (and (pair? p) (pair? (cdr p)) (cadr p)))))

             (when (pair? options)
               (let ((e (append `((parent ,node)) env0)))
                 (debug-item "unresolved options=" options)
                 (for-each (lambda (o)
                             (set-car! (cdr o)
                                       (do-resolve! (cadr o)
                                                    engine e)))
                           options)
                 (debug-item "resolved options=" options)))

             (let ((e `((parent ,node) ,@env ,@env0)))
               (slot-set! node 'body (do-resolve! body engine e)))
             (slot-set! node 'resolved? (not (*unresolved*))))

           (*unresolved* (or unresolved? (not (ast-resolved? node))))
           node))))


(define-method (do-resolve! (node <document>) engine env0)
  (parameterize ((*document-being-resolved* node))
    (next-method)
    ;; resolve the engine custom
    (let* ((env (append `((parent ,node)) env0))
           (resolved (map (match-lambda
                            ((i a)
                             (debug-item "custom=" i " " a)
                             (list i (do-resolve! a engine env))))
                          (slot-ref engine 'customs))))
      (slot-set! engine 'customs resolved))
    node))


(define-method (do-resolve! (node <unresolved>) engine env)
  (with-debug 5 'do-resolve<unresolved>
     (debug-item "node=" node)
     (let* ((p      (assq 'parent env))
            (parent (and (pair? p) (pair? (cdr p)) (cadr p))))

       (slot-set! node 'parent parent)

       (let* ((proc (slot-ref node 'proc))
              (res  (proc node engine env))
              (loc  (ast-loc node)))

         ;; Bind non-unresolved children of RES now so that unresolved
         ;; children of RES (if any) can look them up in the next `resolve!'
         ;; run.  (XXX: This largely duplicates `document-bind-nodes!'.)
         (let loop ((node res)
                    (doc  (ast-document node)))
           (if (ast? node)
               (ast-loc-set! node loc))

           (cond ((document? node)
                  ;; Bind NODE in its parent's document.  This is so
                  ;; that (i) a sub-document is bound in its parent
                  ;; document, and (ii) a node within a sub-document
                  ;; is bound in this sub-document.
                  (document-bind-node! doc node)
                  (loop (markup-body node) node))

                 ((markup? node)
                  (document-bind-node! doc node)
                  (loop (markup-body node) doc))

                 ((node? node)
                  (loop (node-body node) doc))

                 ((pair? node)
                  (for-each (lambda (n) (loop n doc)) node))

                 ((command? node)
                  (loop (command-body node) doc))))

         (debug-item "res=" res)
         (*unresolved* #t)
         res))))


(define-method (do-resolve! (node <handle>) engine env)
  node)


(define-method (do-resolve! (node <command>) engine env)
  (with-debug 5 'do-resolve<command>
     (debug-item "node=" node)
     (let ((p (assq 'parent env)))
       (slot-set! node 'parent (and (pair? p) (pair? (cdr p)) (cadr p))))
     (slot-set! node 'body
                (do-resolve! (command-body node) engine env))
     node))



;;;; ======================================================================
;;;;
;;;; RESOLVE-PARENT
;;;;
;;;; ======================================================================
(define (resolve-parent n e)
  (with-debug 5 'resolve-parent
     (debug-item "n=" n)
     (cond
       ((not (is-a? n <ast>))
	(let ((c (assq 'parent e)))
	  (if (pair? c)
	      (cadr c)
	      n)))
       ((eq? (slot-ref n 'parent) 'unspecified)
        (raise (condition (&ast-orphan-error (ast n)))))
       (else
	(slot-ref n 'parent)))))


;;;; ======================================================================
;;;;
;;;; RESOLVE-SEARCH-PARENT
;;;;
;;;; ======================================================================
(define (resolve-search-parent n e pred)
  (with-debug 5 'resolve-search-parent
     (debug-item "node=" n)
     (debug-item "searching=" pred)
     (let ((p (resolve-parent n e)))
       (debug-item "parent=" p " "
		   (if (is-a? p <markup>) (slot-ref p 'markup) "???"))
       (cond
	 ((pred p)		 p)
	 ((is-a? p <unresolved>) p)
	 ((not p)		 #f)
	 (else			 (resolve-search-parent p e pred))))))

;;;; ======================================================================
;;;;
;;;; RESOLVE-COUNTER
;;;;
;;;; ======================================================================
;;FIXME: factoriser
(define (resolve-counter n e cnt val . opt)
  (let ((c (assq (symbol-append cnt '-counter) e)))
    (if (not (pair? c))
	(if (or (null? opt) (not (car opt)) (null? e))
            (raise (condition (&ast-orphan-error (ast n))))
	    (begin
	      (set-cdr! (last-pair e)
			(list (list (symbol-append cnt '-counter) 0)
			      (list (symbol-append cnt '-env) '())))
	      (resolve-counter n e cnt val)))
	(let* ((num (cadr c)))
	  (let ((c2 (assq (symbol-append cnt '-env) e)))
	    (set-car! (cdr c2) (cons (resolve-parent n e) (cadr c2))))
	  (cond
	    ((integer? val)
	     (set-car! (cdr c) val)
	     (car val))
	    ((not val)
	     val)
	    (else
	     (set-car! (cdr c) (+ 1 num))
	     (+ 1 num)))))))


;;;
;;; `resolve-ident'.
;;;
;;; This function kind of sucks because the document where IDENT is to be
;;; searched is not explictly passed.  Thus, using `document-lookup-node' is
;;; recommended instead of using this function.
;;;

(define (resolve-ident ident markup n e)
  ;; Search for a node with identifier IDENT and markup type MARKUP.  N is
  ;; typically an `<unresolved>' node and the node lookup should be performed
  ;; in its parent document.  E is the "environment" (an alist).
  (with-debug 4 'resolve-ident
     (debug-item "ident=" ident)
     (debug-item "markup=" markup)
     (debug-item "n=" (if (markup? n) (markup-markup n) n))
     (if (not (string? ident))
         (raise (condition (&invalid-argument-error ;; type error
                            (proc-name "resolve-ident")
                            (argument  ident))))
	 (let* ((doc (ast-document n))
		(result (and doc (document-lookup-node doc ident))))
	   (if (or (not markup)
		   (and (markup? result) (eq? (markup-markup result) markup)))
	       result
	       #f)))))


;;; Local Variables:
;;; coding: latin-1
;;; End: