aboutsummaryrefslogtreecommitdiff
path: root/legacy/bigloo/types.scm
blob: b8babd44e1f6f06539c243cf092a0395e1876e1e (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
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
;*=====================================================================*/
;*    serrano/prgm/project/skribe/src/bigloo/types.scm                 */
;*    -------------------------------------------------------------    */
;*    Author      :  Manuel Serrano                                    */
;*    Creation    :  Tue Jul 22 16:40:42 2003                          */
;*    Last change :  Thu Oct 21 13:23:17 2004 (serrano)                */
;*    Copyright   :  2003-04 Manuel Serrano                            */
;*    -------------------------------------------------------------    */
;*    The definition of the Skribe classes                             */
;*=====================================================================*/

;*---------------------------------------------------------------------*/
;*    The module                                                       */
;*---------------------------------------------------------------------*/
(module skribe_types

   (export (abstract-class %ast
	      (parent (default #unspecified))
	      (loc (default (evmeaning-location))))

	   (class %command::%ast
	      (fmt::bstring read-only)
	      (body (default #f)))
	   
	   (class %unresolved::%ast
	      (proc::procedure read-only))
	   
	   (class %handle::%ast
	      (ast (default #f)))

	   (abstract-class %node::%ast
	      (required-options::pair-nil read-only (default '()))
	      (options::pair-nil (default '()))
	      (body (default #f)))
	   
	   (class %processor::%node
	      (combinator (default (lambda (e1 e2) e1)))
	      (procedure::procedure (default (lambda (n e) n)))
	      engine)
	   
	   (class %markup::%node
	      (markup-init)
	      (ident (default #f))
	      (class (default #f))
	      (markup::symbol read-only))

	   (class %container::%markup
	      (env::pair-nil (default '())))
	   
	   (class %document::%container)
	   
	   (class %engine
	      (ident::symbol read-only)
	      (format::bstring (default "raw"))
	      (info::pair-nil (default '()))
	      (version::obj read-only (default #unspecified))
	      (delegate read-only (default #f))
	      (writers::pair-nil (default '()))
	      (filter::obj (default #f))
	      (customs::pair-nil (default '()))
	      (symbol-table::pair-nil (default '())))

	   (class %writer
	      (ident::symbol read-only)
	      (class read-only)
	      (pred::procedure read-only)
	      (upred read-only)
	      (options::obj read-only)
	      (verified?::bool (default #f))
	      (validate (default #f))
	      (before read-only)
	      (action read-only)
	      (after read-only))

	   (class %language
	      (name::bstring read-only)
	      (fontifier read-only (default #f))
	      (extractor read-only (default #f)))
	      
	   (markup-init ::%markup)
	   (find-markups ::bstring)
	   
	   (inline ast?::bool ::obj)
	   (inline ast-parent::obj ::%ast)
	   (inline ast-loc::obj ::%ast)
	   (inline ast-loc-set!::obj ::%ast ::obj)
	   (ast-location::bstring ::%ast)

	   (new-command . inits)
	   (inline command?::bool ::obj)
	   (inline command-fmt::bstring ::%command)
	   (inline command-body::obj ::%command)
	   
	   (new-unresolved . inits)
	   (inline unresolved?::bool ::obj)
	   (inline unresolved-proc::procedure ::%unresolved)
	   
	   (new-handle . inits)
	   (inline handle?::bool ::obj)
	   (inline handle-ast::obj ::%handle)

	   (inline node?::bool ::obj)
	   (inline node-body::obj ::%node)
	   (inline node-options::pair-nil ::%node)
	   (inline node-loc::obj ::%node)

	   (new-processor . inits)
	   (inline processor?::bool ::obj)
	   (inline processor-combinator::obj ::%processor)
	   (inline processor-engine::obj ::%processor)
	   
	   (new-markup . inits)
	   (inline markup?::bool ::obj)
	   (inline is-markup?::bool ::obj ::symbol)
	   (inline markup-markup::obj ::%markup)
	   (inline markup-ident::obj ::%markup)
	   (inline markup-body::obj ::%markup)
	   (inline markup-options::pair-nil ::%markup)

	   (new-container . inits)
	   (inline container?::bool ::obj)
	   (inline container-ident::obj ::%container)
	   (inline container-body::obj ::%container)
	   (inline container-options::pair-nil ::%container)

	   (new-document . inits)
	   (inline document?::bool ::obj)
	   (inline document-ident::bool ::%document)
	   (inline document-body::bool ::%document)
	   (inline document-options::pair-nil ::%document)
	   (inline document-env::pair-nil ::%document)
	   
	   (inline engine?::bool ::obj)
	   (inline engine-ident::obj ::obj)
	   (inline engine-format::obj ::obj)
	   (inline engine-customs::pair-nil ::obj)
	   (inline engine-filter::obj ::obj)
	   (inline engine-symbol-table::pair-nil ::%engine)

	   (inline writer?::bool ::obj)
	   (inline writer-before::obj ::%writer)
	   (inline writer-action::obj ::%writer)
	   (inline writer-after::obj ::%writer)
	   (inline writer-options::obj ::%writer)

	   (inline language?::bool ::obj)
	   (inline language-name::obj ::obj)
	   (inline language-fontifier::obj ::obj)
	   (inline language-extractor::obj ::obj)
			  
	   (new-language . inits)
	   
	   (location?::bool ::obj)
	   (location-file::bstring ::pair)
	   (location-pos::int ::pair)))

;*---------------------------------------------------------------------*/
;*    skribe-instantiate ...                                           */
;*---------------------------------------------------------------------*/
(define-macro (skribe-instantiate type values . slots)
   `(begin
       (skribe-instantiate-check-values ',type ,values ',slots)
       (,(symbol-append 'instantiate::% type)
	,@(map (lambda (slot)
		  (let ((id (if (pair? slot) (car slot) slot))
			(def (if (pair? slot) (cadr slot) #f)))
		     `(,id (new-get-value ',id ,values ,def))))
	       slots))))

;*---------------------------------------------------------------------*/
;*    skribe-instantiate-check-values ...                              */
;*---------------------------------------------------------------------*/
(define (skribe-instantiate-check-values id values slots)
   (let ((bs (every (lambda (v) (not (memq (car v) slots))) values)))
      (when (pair? bs)
	 (for-each (lambda (b)
		      (error (symbol-append '|new | id)
			     "Illegal field"
			     b))
		   bs))))

;*---------------------------------------------------------------------*/
;*    object-print ...                                                 */
;*---------------------------------------------------------------------*/
(define-method (object-print obj::%ast port print-slot::procedure)
   (let* ((class      (object-class obj))
	  (class-name (class-name class)))
      (display "#|" port)
      (display class-name port)
      (display #\| port)))
      
;*---------------------------------------------------------------------*/
;*    object-display ::%ast ...                                        */
;*---------------------------------------------------------------------*/
(define-method (object-display n::%ast . port)
   (fprintf (if (pair? port) (car port) (current-output-port))
	    "<#~a>"
	    (find-runtime-type n)))

;*---------------------------------------------------------------------*/
;*    object-display ::%markup ...                                     */
;*---------------------------------------------------------------------*/
(define-method (object-display n::%markup . port)
   (fprintf (if (pair? port) (car port) (current-output-port))
	    "<#~a:~a>"
	    (find-runtime-type n)
	    (markup-markup n)))

;*---------------------------------------------------------------------*/
;*    object-write ::%markup ...                                       */
;*---------------------------------------------------------------------*/
(define-method (object-write n::%markup . port)
   (fprintf (if (pair? port) (car port) (current-output-port))
	    "<#~a:~a:~a>"
	    (find-runtime-type n)
	    (markup-markup n)
	    (find-runtime-type (markup-body n))))

;*---------------------------------------------------------------------*/
;*    *node-table*                                                     */
;*    -------------------------------------------------------------    */
;*    A private hashtable that stores all the nodes of an ast. It      */
;*    is used for retreiving a node from its identifier.               */
;*---------------------------------------------------------------------*/
(define *node-table* (make-hashtable))

;*---------------------------------------------------------------------*/
;*    ast? ...                                                         */
;*---------------------------------------------------------------------*/
(define-inline (ast? obj)
   (%ast? obj))

;*---------------------------------------------------------------------*/
;*    ast-parent ...                                                   */
;*---------------------------------------------------------------------*/
(define-inline (ast-parent obj)
   (%ast-parent obj))

;*---------------------------------------------------------------------*/
;*    ast-loc ...                                                      */
;*---------------------------------------------------------------------*/
(define-inline (ast-loc obj)
   (%ast-loc obj))

;*---------------------------------------------------------------------*/
;*    ast-loc-set! ...                                                 */
;*---------------------------------------------------------------------*/
(define-inline (ast-loc-set! obj loc)
   (%ast-loc-set! obj loc))

;*---------------------------------------------------------------------*/
;*    ast-location ...                                                 */
;*---------------------------------------------------------------------*/
(define (ast-location obj)
   (with-access::%ast obj (loc)
      (if (location? loc)
	  (let* ((fname (location-file loc))
		 (char (location-pos loc))
		 (pwd (pwd))
		 (len (string-length pwd))
		 (lenf (string-length fname))
		 (file (if (and (substring=? pwd fname len)
				(and (>fx lenf len)))
			   (substring fname len (+fx 1 (string-length fname)))
			   fname)))
	     (format "~a, char ~a" file char))
	  "no source location")))

;*---------------------------------------------------------------------*/
;*    new-command ...                                                  */
;*---------------------------------------------------------------------*/
(define (new-command . init)
   (skribe-instantiate command init
		       (parent #unspecified)
		       (loc #f)
		       fmt
		       (body #f)))

;*---------------------------------------------------------------------*/
;*    command? ...                                                     */
;*---------------------------------------------------------------------*/
(define-inline (command? obj)
   (%command? obj))

;*---------------------------------------------------------------------*/
;*    command-fmt ...                                                  */
;*---------------------------------------------------------------------*/
(define-inline (command-fmt cmd)
   (%command-fmt cmd))

;*---------------------------------------------------------------------*/
;*    command-body ...                                                 */
;*---------------------------------------------------------------------*/
(define-inline (command-body cmd)
   (%command-body cmd))

;*---------------------------------------------------------------------*/
;*    new-unresolved ...                                               */
;*---------------------------------------------------------------------*/
(define (new-unresolved . init)
   (skribe-instantiate unresolved init
		       (parent #unspecified)
		       loc
		       proc))

;*---------------------------------------------------------------------*/
;*    unresolved? ...                                                  */
;*---------------------------------------------------------------------*/
(define-inline (unresolved? obj)
   (%unresolved? obj))

;*---------------------------------------------------------------------*/
;*    unresolved-proc ...                                              */
;*---------------------------------------------------------------------*/
(define-inline (unresolved-proc unr)
   (%unresolved-proc unr))

;*---------------------------------------------------------------------*/
;*    new-handle ...                                                   */
;*---------------------------------------------------------------------*/
(define (new-handle . init)
   (skribe-instantiate handle init
		       (parent #unspecified)
		       loc
		       (ast #f)))

;*---------------------------------------------------------------------*/
;*    handle? ...                                                      */
;*---------------------------------------------------------------------*/
(define-inline (handle? obj)
   (%handle? obj))

;*---------------------------------------------------------------------*/
;*    handle-ast ...                                                   */
;*---------------------------------------------------------------------*/
(define-inline (handle-ast obj)
   (%handle-ast obj))

;*---------------------------------------------------------------------*/
;*    node? ...                                                        */
;*---------------------------------------------------------------------*/
(define-inline (node? obj)
   (%node? obj))

;*---------------------------------------------------------------------*/
;*    node-body ...                                                    */
;*---------------------------------------------------------------------*/
(define-inline (node-body obj)
   (%node-body obj))

;*---------------------------------------------------------------------*/
;*    node-options ...                                                 */
;*---------------------------------------------------------------------*/
(define-inline (node-options obj)
   (%node-options obj))

;*---------------------------------------------------------------------*/
;*    node-loc ...                                                     */
;*---------------------------------------------------------------------*/
(define-inline (node-loc obj)
   (%node-loc obj))

;*---------------------------------------------------------------------*/
;*    new-processor ...                                                */
;*---------------------------------------------------------------------*/
(define (new-processor . init)
   (skribe-instantiate processor init
		       (parent #unspecified)
		       loc
		       (combinator (lambda (e1 e2) e1))
		       engine
		       (body #f)))

;*---------------------------------------------------------------------*/
;*    processor? ...                                                   */
;*---------------------------------------------------------------------*/
(define-inline (processor? obj)
   (%processor? obj))

;*---------------------------------------------------------------------*/
;*    processor-combinator ...                                         */
;*---------------------------------------------------------------------*/
(define-inline (processor-combinator proc)
   (%processor-combinator proc))
   
;*---------------------------------------------------------------------*/
;*    processor-engine ...                                             */
;*---------------------------------------------------------------------*/
(define-inline (processor-engine proc)
   (%processor-engine proc))
   
;*---------------------------------------------------------------------*/
;*    new-markup ...                                                   */
;*---------------------------------------------------------------------*/
(define (new-markup . init)
   (skribe-instantiate markup init
		       (parent #unspecified)
		       (loc #f)
		       markup
		       ident
		       (class #f)
		       (body #f)
		       (options '())
		       (required-options '())))

;*---------------------------------------------------------------------*/
;*    markup? ...                                                      */
;*---------------------------------------------------------------------*/
(define-inline (markup? obj)
   (%markup? obj))

;*---------------------------------------------------------------------*/
;*    is-markup? ...                                                   */
;*---------------------------------------------------------------------*/
(define-inline (is-markup? obj markup)
   (and (markup? obj) (eq? (markup-markup obj) markup)))

;*---------------------------------------------------------------------*/
;*    markup-init ...                                                  */
;*    -------------------------------------------------------------    */
;*    The markup constructor simply stores in the markup table the     */
;*    news markups.                                                    */
;*---------------------------------------------------------------------*/
(define (markup-init markup)
   (bind-markup! markup))

;*---------------------------------------------------------------------*/
;*    bind-markup! ...                                                 */
;*---------------------------------------------------------------------*/
(define (bind-markup! node)
   (hashtable-update! *node-table*
		      (markup-ident node)
		      (lambda (cur) (cons node cur))
		      (list node)))

;*---------------------------------------------------------------------*/
;*    find-markups ...                                                 */
;*---------------------------------------------------------------------*/
(define (find-markups ident)
   (hashtable-get *node-table* ident))

;*---------------------------------------------------------------------*/
;*    markup-markup ...                                                */
;*---------------------------------------------------------------------*/
(define-inline (markup-markup obj)
   (%markup-markup obj))

;*---------------------------------------------------------------------*/
;*    markup-ident ...                                                 */
;*---------------------------------------------------------------------*/
(define-inline (markup-ident obj)
   (%markup-ident obj))

;*---------------------------------------------------------------------*/
;*    markup-body ...                                                  */
;*---------------------------------------------------------------------*/
(define-inline (markup-body obj)
   (%markup-body obj))

;*---------------------------------------------------------------------*/
;*    markup-options ...                                               */
;*---------------------------------------------------------------------*/
(define-inline (markup-options obj)
   (%markup-options obj))

;*---------------------------------------------------------------------*/
;*    new-container ...                                                */
;*---------------------------------------------------------------------*/
(define (new-container . init)
   (skribe-instantiate container init
		       (parent #unspecified)
		       loc
		       markup
		       ident
		       (class #f)
		       (body #f)
		       (options '())
		       (required-options '())
		       (env '())))

;*---------------------------------------------------------------------*/
;*    container? ...                                                   */
;*---------------------------------------------------------------------*/
(define-inline (container? obj)
   (%container? obj))

;*---------------------------------------------------------------------*/
;*    container-ident ...                                              */
;*---------------------------------------------------------------------*/
(define-inline (container-ident obj)
   (%container-ident obj))

;*---------------------------------------------------------------------*/
;*    container-body ...                                               */
;*---------------------------------------------------------------------*/
(define-inline (container-body obj)
   (%container-body obj))

;*---------------------------------------------------------------------*/
;*    container-options ...                                            */
;*---------------------------------------------------------------------*/
(define-inline (container-options obj)
   (%container-options obj))

;*---------------------------------------------------------------------*/
;*    new-document ...                                                 */
;*---------------------------------------------------------------------*/
(define (new-document . init)
   (skribe-instantiate document init
		       (parent #unspecified)
		       loc
		       markup
		       ident
		       (class #f)
		       (body #f)
		       (options '())
		       (required-options '())
		       (env '())))

;*---------------------------------------------------------------------*/
;*    document? ...                                                    */
;*---------------------------------------------------------------------*/
(define-inline (document? obj)
   (%document? obj))

;*---------------------------------------------------------------------*/
;*    document-options ...                                             */
;*---------------------------------------------------------------------*/
(define-inline (document-options doc)
   (%document-options doc))

;*---------------------------------------------------------------------*/
;*    document-env ...                                                 */
;*---------------------------------------------------------------------*/
(define-inline (document-env doc)
   (%document-env doc))

;*---------------------------------------------------------------------*/
;*    document-ident ...                                               */
;*---------------------------------------------------------------------*/
(define-inline (document-ident doc)
   (%document-ident doc))

;*---------------------------------------------------------------------*/
;*    document-body ...                                                */
;*---------------------------------------------------------------------*/
(define-inline (document-body doc)
   (%document-body doc))

;*---------------------------------------------------------------------*/
;*    engine? ...                                                      */
;*---------------------------------------------------------------------*/
(define-inline (engine? obj)
   (%engine? obj))

;*---------------------------------------------------------------------*/
;*    engine-ident ...                                                 */
;*---------------------------------------------------------------------*/
(define-inline (engine-ident obj)
   (%engine-ident obj))

;*---------------------------------------------------------------------*/
;*    engine-format ...                                                */
;*---------------------------------------------------------------------*/
(define-inline (engine-format obj)
   (%engine-format obj))

;*---------------------------------------------------------------------*/
;*    engine-customs ...                                               */
;*---------------------------------------------------------------------*/
(define-inline (engine-customs obj)
   (%engine-customs obj))

;*---------------------------------------------------------------------*/
;*    engine-filter ...                                                */
;*---------------------------------------------------------------------*/
(define-inline (engine-filter obj)
   (%engine-filter obj))

;*---------------------------------------------------------------------*/
;*    engine-symbol-table ...                                          */
;*---------------------------------------------------------------------*/
(define-inline (engine-symbol-table obj)
   (%engine-symbol-table obj))

;*---------------------------------------------------------------------*/
;*    writer? ...                                                      */
;*---------------------------------------------------------------------*/
(define-inline (writer? obj)
   (%writer? obj))

;*---------------------------------------------------------------------*/
;*    writer-before ...                                                */
;*---------------------------------------------------------------------*/
(define-inline (writer-before obj)
   (%writer-before obj))

;*---------------------------------------------------------------------*/
;*    writer-action ...                                                */
;*---------------------------------------------------------------------*/
(define-inline (writer-action obj)
   (%writer-action obj))

;*---------------------------------------------------------------------*/
;*    writer-after ...                                                 */
;*---------------------------------------------------------------------*/
(define-inline (writer-after obj)
   (%writer-after obj))

;*---------------------------------------------------------------------*/
;*    writer-options ...                                               */
;*---------------------------------------------------------------------*/
(define-inline (writer-options obj)
   (%writer-options obj))

;*---------------------------------------------------------------------*/
;*    language? ...                                                    */
;*---------------------------------------------------------------------*/
(define-inline (language? obj)
   (%language? obj))

;*---------------------------------------------------------------------*/
;*    language-name ...                                                */
;*---------------------------------------------------------------------*/
(define-inline (language-name lg)
   (%language-name lg))

;*---------------------------------------------------------------------*/
;*    language-fontifier ...                                           */
;*---------------------------------------------------------------------*/
(define-inline (language-fontifier lg)
   (%language-fontifier lg))

;*---------------------------------------------------------------------*/
;*    language-extractor ...                                           */
;*---------------------------------------------------------------------*/
(define-inline (language-extractor lg)
   (%language-extractor lg))

;*---------------------------------------------------------------------*/
;*    new-get-value ...                                                */
;*---------------------------------------------------------------------*/
(define (new-get-value key init def)
   (let ((c (assq key init)))
      (match-case c
	 ((?- ?v)
	  v)
	 (else
	  def))))

;*---------------------------------------------------------------------*/
;*    new-language ...                                                 */
;*---------------------------------------------------------------------*/
(define (new-language . init)
   (skribe-instantiate language init name fontifier extractor))

;*---------------------------------------------------------------------*/
;*    location? ...                                                    */
;*---------------------------------------------------------------------*/
(define (location? o)
   (match-case o
      ((at ?- ?-)
       #t)
      (else
       #f)))

;*---------------------------------------------------------------------*/
;*    location-file ...                                                */
;*---------------------------------------------------------------------*/
(define (location-file o)
   (match-case o
      ((at ?fname ?-)
       fname)
      (else
       (error 'location-file "Illegal location" o))))

;*---------------------------------------------------------------------*/
;*    location-pos ...                                                 */
;*---------------------------------------------------------------------*/
(define (location-pos o)
   (match-case o
      ((at ?- ?loc)
       loc)
      (else
       (error 'location-pos "Illegal location" o))))