File: compile.scm

package info (click to toggle)
sdc 1.0.8beta-8
  • links: PTS
  • area: contrib
  • in suites: slink
  • size: 1,400 kB
  • ctags: 874
  • sloc: lisp: 8,120; ansic: 967; makefile: 671; perl: 136; sh: 50
file content (701 lines) | stat: -rwxr-xr-x 18,422 bytes parent folder | download
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
;{{{ module declaration

(module compile
	(include "compile.sch" "stream.sch")
	(export
	 replacement-alist
	 current-args
	 current-sysid
	 current-file
	 (atv sym)			; deliver the val of arg
	 notations
	 ext-entities
	 (add-ext-entity nm def)
	 (define-ext-entity line)
	 (old-apply-notation notation fn sysid)
	 (apply-notation nn notation fn sysid)
	 (old-refer-entity line)
	 compile-structure
	 (compile-structure-to-string)
	 (make-compiler lines)
	 (compile-to-file compiler outfile)
	 (scan-doctype lines)
	 doc-preface-hook
	 doc-postface-hook
	 doc-date
	 (set-date! args)

	 (make-scaner lines)
	 (inline arg-name t)
	 (inline arg-type t)
	 (inline arg-val  t)
	 (parse-arg string)
	 (inline token-type t)
	 (inline token-gi t)
	 (inline token-args t)
	 (inline data-token-data t)
	 (inline ext-type et)
	 (inline ext-def  et)
	 (inline ext-sysid et)
	 (inline ext-file  et)
	 empty-token
	 (empty-token? t)
	 (token-stream scan)

	 (start-gi? gi)
	 (end-gi? gi)
	 (xat token name)
	 (xatt token name)
	 (xatv token name)

	 (external-handler signal-function)

	 )
	(import
	 (message typeset "typeset.scm")
	 (docpath typeset "typeset.scm")
	 (files "files.scm")
	 (strings "strings.scm")
	 (chrproc "chrproc.scm")
	 (control "control.scm")
	 (stream "stream.scm")
	 ))

;}}}

;{{{ (process-processing-instruction line)

; we need to parse the processing instructions as well:

(define (process-processing-instruction line)
  (let ((len (string-length line)))
    (do ((i 0 (+ i 1)))
	((>= i len) #t)
      (let ((c (process-escape-char (string-ref line i))))
	(if c
	    (write-out c)
	    #t)))))

;}}}
;{{{ ELEMENT parsing

(define replacement-alist '())

(define current-args '())
(define current-sysid "")
(define current-file "")

(define (atv sym)
  (let* ((pp (assv sym current-args))
	 (av (if pp (cdr pp) #f)))
    av))

(define (eval-element compiler line args)
  (let* ((lsym (string->symbol line))
	 (pn (assq  lsym replacement-alist))
	 (proc (lambda (rep s/e)
		 (cond
		  ((vector? rep)
		   (let ((el (vector-ref rep s/e)))
		     (cond
		      ((procedure? el)
		       (set! current-args args)
		       (set! compile-structure (lambda (. dummy)
			     (message 0 "Error: compile-structure called from "
				      (vector-ref '#("open" "close") s/e)
				      " tag of " line ".")))
		       (el args))
		      (else (write-out el)))))))))
    ; translate it
    (if pn
	(let ((rep (cdr pn)))
	  (cond
	   ((procedure? rep ) (set! current-args args) 		      
			      (set! compile-structure compiler)
			      (rep args))
	   (else (proc rep 0)
		 (compiler)
		 (set! pn (assq lsym replacement-alist)) ;might have changed
		 (if pn (proc (cdr pn) 1)))))
	(compiler))))

;}}}

(define notations (list))		; map notation name -> sys id (cmd)

;{{{ (apply-notation nn notation file sysid)

(define (apply-notation nn notation file sysid)
  (letrec ((tmpf (make-tmp-file-name))
	   (output-port (open-output-string))
	   (tr path-to-file-name)
	   (sp (lambda (line)
		 (let ((p (string-find-char line #\% ))
		       (l (string-length line)))
		   (if p
		       (cons (substring line 0 p)
			     (if (= p (- l -1))
				 '("%")
				 (let ((next (string-ref line (+ p 1))))
				   (cons
				    (cond
				     ((char=? next #\f) file)
				     ((char=? next #\F) (tr file))
				     ((char=? next #\s) sysid)
				     ((char=? next #\S) (tr sysid))
				     ((char=? next #\%) "%")
				     (else ""))
				    (sp (substring line (+ p 2) l))))))
		       (list line))))))
    (message 1 "Using notation " nn " for file " file)
    (let* ((cmd (apply string-append (append (sp notation) `(">" ,tmpf))))
	   (nothing (message 3 cmd))
	   (err (system cmd)))
      (if (> err 0)
	  (message 0 "Processing of notation " nn   #\newline
		   " to file " file	         #\newline
		   "SGML system identifier " sysid  #\newline
		   "returned error code " err)))
    (file-cat tmpf output-port)
    (delete-file tmpf)
    (close-output-port output-port)))

(define (old-apply-notation n f s)
  (apply-notation n (cdr (assq n notations)) f s))

;}}}

;{{{ ext-entities (old style)

; map pairs entity name -> #( 'NDATA notation sysid filename )

(define ext-entities (list))

(define (add-ext-entity nm def)
  (set! ext-entities (cons (cons nm def) ext-entities)))

(define (define-ext-entity line)
  (let* ((l (string-length line))
	 (s1 (string-find-char line #\space 0))
	 (s2 (string-find-char line #\space (+ s1 1))))
    (if (not s2) (set! s2 (- l 1)))
    (add-ext-entity (substring line 0 s1)
		    (vector
		     'NDATA
		     (string->symbol
		      (substring line (+ s2 1) l))
		     current-sysid
		     current-file))))

;}}}
;{{{ entity references

; remember the representations of external extities by sysid's

(define entity-cache '())

; Recall the representations of external extities if already known.
; Otherwise apply the notation defined for them.

(define (refer-entity definition notations)
  (if
   (not (vector-ref definition 3)) ; file name
   (begin
     (message 0 "Error: No file found for SYSTEM ID "
	      (vector-ref definition 2) #\.)
     #f)
   (let* ((cached (assoc (vector-ref definition 2) entity-cache))
	  (not-name (vector-ref definition 1))
	  (not-def (assq not-name notations))
	  (file (vector-ref definition 3))
	  (sysid (vector-ref definition 2)))
     (if cached
	 (cdr cached)
	 (if not-def
	     (let ((ext (apply-notation (car not-def) (cdr not-def)
					file sysid)))
	       (set! entity-cache
		     (cons (cons (vector-ref definition 2) ext) entity-cache))
	       ext)
	     (begin
	       (message 0 "Error: Skipping element: file " file #\newline
			"SGML system identifier " sysid
			". Notation " not-name " not defined.")
	       #f))))))

(define (old-refer-entity line)
  (write-out 
   (let ((def (assoc line ext-entities)))
     (if def
	 (refer-entity (cdr def) notations)
	 (message 0 "External entity " line " not defined."))))) 

;}}}

;{{{ old style

;{{{ (make-compiler lines) // old-style

; deliver the list of args with the new pair (name value) prepended

(define (scan-arg args line)
  (let* ((length (string-length line))
	 (s1 (string-find-char line #\space  0))
	 (s2 (string-find-char line #\space (+ s1 1))))
    (if (not s2) (set! s2 (- length 1)))
    (cons (cons 
	   (string->symbol (substring line 0 s1))
	   (process-cdata (substring line (+ s2 1) length)))
    args)))

; parse the whole list made from the input lines

(define (make-compiler lines)
  (letrec
      ((exitf #f)
       (doit (lambda ()
	       (do ((args '()))
		   ((or
		     (null? lines)
		     (let ((cmd (caar lines))
			   (line (cdar lines)))
		       (set! lines (cdr lines))
		       (case cmd
			((#\) #\}) #t)
			((#\-) (process-text line) #f)
			((#\() (eval-element dispatch line args) #f)
			((#\A) (set! args (scan-arg args line)) #f)
			((#\?)
			 (process-processing-instruction line) #f)
			((#\&) (old-refer-entity line) #f)
			((#\N)
			 (set! notations  (cons (cons (string->symbol line)
						      current-sysid)
						notations )) #f)
			((#\E) (define-ext-entity line) #f)
			((#\s) (set! current-sysid line) #f)
			((#\f) (set! current-file
				     (sgmls-entity-file-name line)) #f)
			((#\C) (exitf #t)))))
		    #f))))
       (dispatch (lambda ( . command)
		   (if (pair? command)
		       (cond
			((eq? (car command) 'exit) (exitf (cdr command)))
			(else (message 0 "compile-command " 
				       (car command) "not understood")))
		       (doit)))))
    (lambda ( . command)
      (if exitf (apply dispatch command)
	  (bind-exit (ex)
		     (set! exitf ex)
		     (apply dispatch command))))))

;}}}
;{{{ (compile-...)

(define compile-structure null-proc)

(define (compile-structure-to-string)
  (capture-out #t)
  (compile-structure)
  (capture-out #f))

(define (compile-to-file compiler outfile)
  (let ((oop output-port))
    (set! output-port
	  (if (string=? outfile "-")
	      (current-output-port)
	      (open-output-file outfile)))
    (doc-preface-hook 'run)
    (let ((pres (compiler)))
      (doc-postface-hook 'run)
      (close-output-port output-port)
      (set! output-port oop)
      pres)))

; scan forward to the doctype declaration and deliver the type

(define (scan-doctype lines)
  (if (pair? lines)
      (if (char=? #\( (caar lines))
	  (string-copy (cdar lines))
	  (scan-doctype (cdr lines)))
      #f))

;}}}
;{{{ some standard (date/language) support

(define doc-preface-hook (make-hook))
(define doc-postface-hook (make-hook))

(define doc-date (date))

(define (set-date! args)
  (let* ((pp (assv 'DATE args))
	 (pd (if pp (cdr pp) (date))))
  (if (not (equal? pd ""))
      (set! doc-date pd))))

;}}}

;}}}
;{{{ sgmls special

(define (sgmls-entity-file-name string)
  (if (eqv? (string-ref string 0) #\<)
      (path-find-file
       docpath
       (substring string
		  (+ (string-find-char string #\>) 1)
		  (string-length string)))
      string))

;}}}
; ---------------------------------------------------------------------------
;{{{ (make-scanner lines)

; maybe some day we need some more complicated scaner stuff...

(define (make-scaner lines)
  (if (null? lines)
      empty-stream
      (cons-stream (caar lines)
		   (cons-stream (cdar lines)
				(make-scaner (cdr lines))))))

;}}}
;{{{ token arguments

(define-inline (arg-name arg-token) (vector-ref arg-token 0))
(define-inline (arg-type arg-token) (vector-ref arg-token 1))
(define-inline (arg-val  arg-token) (vector-ref arg-token 2))

(define (parse-arg s)
  (let* ((length (string-length s))
	 (s1 (string-find-char s #\space 0))
	 (s2x (string-find-char s #\space (+ s1 1)))
	 (s2a (if s2x s2x length))
	 (s2b (if s2x (+ s2x 1) length))
	 (name (string->symbol (substring s 0 s1)))
	 (type (string->symbol (substring s (+ s1 1) s2a)))
	 (rawval (substring s s2b length))
	 (val (case type
		((CDATA) rawval)
		((ID)  (string-split-string rawval " "))
		((TOKEN ENTITY) (string-split-string rawval " "))
		((NOTATION) (string->symbol rawval))
		((IMPLIED) #f)
		(else (message 0 "parse-arg: argument type " type " unknown.")
		      rawval))))
    (vector name type val)))

;}}}
;{{{ token predicates

;;; Bigloo 1.8 apears  to restrict inlined procedures *not* to use cond.
  ; We avoid it for now...

;(define-inline (token-type t)
;  (cond
;   ((vector? t) (vector-ref t 0))
;   (else 'OUTPUT)))
(define-inline (token-type t)
  (if (vector? t)
      (vector-ref t 0)
      'OUTPUT))
(define-inline (token-gi t) (vector-ref t 1))
(define-inline (token-args t) (vector-ref t 2))
;(define-inline (data-token-data t)
;  (cond
;   ((vector? t) (vector-ref t 1))
;   (else t)))
(define-inline (data-token-data t)
  (if (vector? t)
      (vector-ref t 1)
      t))
(define-macro (token-size) 3)

;}}}
;{{{ external token predicates

(define-inline (ext-type e) (vector-ref e 0))
(define-inline (ext-def  e) (vector-ref e 1))
(define-inline (ext-sysid e) (vector-ref e 2))
(define-inline (ext-file e) (vector-ref e 3))

;}}}
;{{{ attribute service

(define (start-gi? gi)
  (lambda (t)
    (and (start-tag? t) (memq (token-gi t) gi))))

(define (end-gi? gi)
  (lambda (t)
    (and (end-tag? t) (memq (token-gi t) gi))))

(define (xat t n)
  (if (not (start-tag? t)) (error "xat" "Not a STARTTAG" t))
  (let loop ((a (token-args t)))
    (cond
     ((null? a) #f)
     ((eq? (arg-name (car a)) n) (car a))
     (else (loop (cdr a))))))

(define (xatv t n)
  (let ((a (xat t n)))
    (if a
	(arg-val a)
	(error "xatv" "Attribute not in token" (cons n t)))))

(define (xatt t n)
  (let ((a (xat t n)))
    (if a
	(arg-type a)
	(error "xatt" "Attribute not in token" (cons n t)))))

;}}}
;{{{ (token-stream scan)

(define empty-token (vector 'EMPTY #f #f))
(define (empty-token? t) (eq? t empty-token))

(define (token-stream-from-stream scan)
  (letrec
      ((sysid #f)
       (file #f)
       (args '())
       (loop (lambda (scan)
	       (if
		(stream-empty? scan)
		empty-stream
		(let ((t (head scan)))
		  (case t
		    ((#\-)
		     (cons-stream
		      (vector 'DATA (head (tail scan)))
		      (loop (tail (tail scan)))))
		    ((#\?)
		     (cons-stream (vector 'PI (head (tail scan)))
				  (loop (tail (tail scan)))))
		    ((#\A) (set! args (cons (parse-arg (head (tail scan)))
					    args))
			   (loop (tail (tail scan))))
		    ((#\()
		     (cons-stream
		      (vector 'STARTTAG
			      (string->symbol (head (tail scan)))
			      args)
		      (token-stream-from-stream (tail (tail scan)))))
		    ((#\))
		     (cons-stream (vector 'ENDTAG
					  (string->symbol (head (tail scan))))
				  (loop (tail (tail scan)))))
		    ((#\s) (set! sysid (head (tail scan)))
			   (loop (tail (tail scan))))
		    ((#\f) (set! file (sgmls-entity-file-name
				       (head (tail scan))))
			   (loop (tail (tail scan))))
		    ((#\E)
		     (let* ((line (head (tail scan)))
			    (l (string-length line))
			    (s1 (string-find-char line #\space 0))
			    (s2a (string-find-char line #\space (+ s1 1)))
			    (s2 (if s2a s2a (- l 1))))
		       (cons-stream
			(vector
			 'External-Definition
			 (string->symbol (substring line 0 s1))
			 (vector
			  (string->symbol (substring line (+ s1 1) s2))
			  (string->symbol (substring line (+ s2 1) l))
			  sysid
			  file))
			(token-stream-from-stream (tail (tail scan))))))
		    ((#\&)
		     (cons-stream
		      (vector 'External-Reference
			      (string->symbol (head (tail scan))))
		      (loop (tail (tail scan)))))
		    ((#\N)
		     (cons-stream
		      (vector 'NOTATION
			      (string->symbol (head (tail scan)))
			      sysid)
		      (loop (tail (tail scan)))))
		    ((#\S)		; drop it, #\{ delivers the same
		     (loop (tail (tail scan))))
		    ((#\{)
		     (cons-stream
		      (vector  'STARTSUBDOC
			       (string->symbol (head (tail scan)))
			       (vector 'SUBDOC
				       ""
				       sysid
				       file))
		      (loop (tail (tail scan)))))
		    ((#\})
		     (cons-stream
		      (vector 'ENDSUBDOC
			      (string->symbol (head (tail scan))))
		      (loop (tail (tail scan)))))
		    ((#\#)		; FIXME drop additional sp info
		     (loop (tail (tail scan))))
		    ((#f #\C) #f)
		    (else (cons-stream t (loop (tail scan))))))))))
    (loop scan)))

;; this one does not need a stream but the untouched list instead

;;; HACK
;; a local cons-stream which works ONLY for one stream definition
;; i. e., the one without delay.
;; -- differs from the "token-stream-from-stream as far as that one
;; depends on evaluation order, which in turn is fixed through the
;; use of "delay" or "lambda". Here we must use "let".

(define-macro (tsfll-cons-stream h t) `(cons ,h ,t))

(define (token-stream-from-line-list scan)
  (letrec
      ((sysid #f)
       (file #f)
       (args '())
       (loop (lambda (scan)
	       (if
		(null? scan)
		empty-stream
		(let ((t (caar scan)))
		  (case t
		    ((#\-)
		     (tsfll-cons-stream
		      (vector 'DATA (cdar scan))
		      (loop (cdr scan))))
		    ((#\?)
		     (tsfll-cons-stream (vector 'PI (cdar scan))
					(loop (cdr scan))))
		    ((#\A) (set! args (cons (parse-arg (cdar scan))
					    args))
			   (loop (cdr scan)))
		    ((#\()
		     (let ((token `#(STARTTAG
				     ,(string->symbol (cdar scan))
				     ,args)))
		       (tsfll-cons-stream
			token
			(token-stream-from-line-list (cdr scan)))))
		    ((#\))
		     (tsfll-cons-stream (vector 'ENDTAG
						(string->symbol (cdar scan)))
				  (loop (cdr scan))))
		    ((#\s) (set! sysid (cdar scan))
			   (loop (cdr scan)))
		    ((#\f) (set! file (sgmls-entity-file-name (cdar scan)))
			   (loop (cdr scan)))
		    ((#\E)
		     (let* ((line (cdar scan))
			    (l (string-length line))
			    (s1 (string-find-char line #\space 0))
			    (s2a (string-find-char line #\space (+ s1 1)))
			    (s2 (if s2a s2a (- l 1)))
			    (token `#(External-Definition
				      ,(string->symbol (substring line 0 s1))
				      ,(vector
					(string->symbol
					 (substring line (+ s1 1) s2))
					(string->symbol
					 (substring line (+ s2 1) l))
					sysid
					file))))
		       (tsfll-cons-stream
			token
			(token-stream-from-line-list (cdr scan)))))
		    ((#\&)
		     (tsfll-cons-stream
		      (vector 'External-Reference
			      (string->symbol (cdar scan)))
		      (loop (cdr scan))))
		    ((#\N)
		     (let ((token `#(NOTATION
				    ,(string->symbol (cdar scan))
				    ,sysid)))
		       (tsfll-cons-stream
			token
			(loop (cdr scan)))))
		    ((#\S)		; drop it, #\{ delivers the same
		     (loop (cdr scan)))
		    ((#\{)
		     (let ((token (vector  'STARTSUBDOC
					   (string->symbol (cdar scan))
					   (vector 'SUBDOC
						   ""
						   sysid
						   file))))
		       (tsfll-cons-stream
			token
			(loop (cdr scan)))))
		    ((#\})
		     (tsfll-cons-stream
		      (vector 'ENDSUBDOC
			      (string->symbol (cdar scan)))
		      (loop (cdr scan))))
		    ((#\#)		; FIXME drop additional sp info
		     (loop (cdr scan)))
		    ((#f #\C) empty-stream)
		    (else (tsfll-cons-stream t 
				       (tsfll-cons-stream
					(cdar scan)
					(loop (cdr scan)))))))))))
    (loop scan)))

(define (token-stream s) (token-stream-from-line-list s))

;}}}
;{{{ (external-handler signal-function)

(define (external-handler signal-function)
  (define (handler
	   notations			; ((notation . sysid)...)
	   ext-entities			; ((id . #(def))...)
	   )
    (define (appl-f def) (refer-entity def notations))
    (define (refer-f id)
      (let ((ent (assq id ext-entities)))
	(if ent
	    (refer-entity (cdr ent) notations)
	    (begin
	      (signal-function
	       `#(error external-handler
			("External entity undefined: " ,id)))
	      #f))))
    (lambda (token)
      (case (token-type token)
	((NOTATION)
	 (signal-function (vector 'NOTATION
				  (token-gi token) (token-args token)))
	 (handler
	  `((,(token-gi token) . ,(token-args token)) . ,notations)
	  ext-entities))
	((External-Definition)
	 (signal-function (vector 'External-Definition
				  (token-gi token) (token-args token)))
	 (handler
	  notations
	  `((,(token-gi token) . ,(token-args token)) . ,ext-entities)))
	((External-Reference)
	 (signal-function (vector 'External-Reference
				  (token-gi token)
				  (assq (token-gi token) ext-entities)))
	 (vector 'OUTPUT (refer-f (token-gi token))))
	((STARTSUBDOC)
	 (signal-function token)
	 (external-handler signal-function))
	(else
	 (signal-function token)
	 (handler notations ext-entities)))))

  (handler '() '(())))

;}}}