aboutsummaryrefslogtreecommitdiff
path: root/tools/skribebibtex/bigloo/skribebibtex.scm
blob: 25f3e1625694fba01d1eb050a8add116c8b08b70 (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
;*=====================================================================*/
;*    .../skribe/tools/skribebibtex/bigloo/skribebibtex.scm            */
;*    -------------------------------------------------------------    */
;*    Author      :  Manuel Serrano                                    */
;*    Creation    :  Fri Oct 12 14:57:58 2001                          */
;*    Last change :  Sun Apr 10 09:10:02 2005 (serrano)                */
;*    Copyright   :  2001-05 Manuel Serrano                            */
;*    -------------------------------------------------------------    */
;*    The bibtex->skribe translator                                    */
;*=====================================================================*/

;*---------------------------------------------------------------------*/
;*    The module                                                       */
;*---------------------------------------------------------------------*/
(module skribebibtex
   (export (skribebibtex in)))

;*---------------------------------------------------------------------*/
;*    skribebibtex ...                                                 */
;*---------------------------------------------------------------------*/
(define (skribebibtex in)
   (let* ((port (if (string? in)
		    (let ((p (open-input-file in)))
		       (if (not (input-port? p))
			   (error "skribebibtext"
				  "Can't read input file"
				  in)
			   p))
		    (current-input-port)))
	  (sexp (parse-bibtex port)))
      (for-each (lambda (e)
		   (match-case e
		      ((?kind ?ident . ?fields)
		       (display* "("
				 (string-downcase (symbol->string kind))
				 ident)
		       (for-each (lambda (f)
				    (display* "\n   (" (car f) " ")
				    (write (cdr f))
				    (display ")"))
				 fields)
		       (print ")\n"))))
		sexp)))

;*---------------------------------------------------------------------*/
;*    *bibtex-string-table* ...                                        */
;*---------------------------------------------------------------------*/
(define *bibtex-string-table* #unspecified)

;*---------------------------------------------------------------------*/
;*    make-bibtex-hashtable ...                                        */
;*---------------------------------------------------------------------*/
(define (make-bibtex-hashtable)
   (let ((table (make-hashtable)))
      (for-each (lambda (k)
		   (let ((cp (string-capitalize k)))
		      (hashtable-put! table k cp)
		      (hashtable-put! table cp cp)))
		'("jan" "feb" "mar" "apr" "may" "jun" "jul"
			"aug" "sep" "oct" "nov" "dec"))
      table))

;*---------------------------------------------------------------------*/
;*    parse-bibtex ...                                                 */
;*---------------------------------------------------------------------*/
(define (parse-bibtex port::input-port)
   (set! *bibtex-string-table* (make-bibtex-hashtable))
   (cond-expand
      (bigloo2.6
       (try (read/lalrp bibtex-parser bibtex-lexer port)
	    (lambda (escape proc mes obj)
	       (match-case obj
		  ((?token (?fname . ?pos) . ?val)
		   (error/location proc "bibtex parse error" token fname pos))
		  (else
		   (notify-error proc mes obj)
		   (error proc mes obj))))))
      (else
       (with-exception-handler
	  (lambda (e)
	     (if (&io-parse-error? e)
		 (let ((o (&error-obj e)))
		    (match-case o
		       ((?token (?fname . ?pos) . ?val)
			(error/location (&error-proc e)
					"bibtex parse error"
					token
					fname
					pos))
		       (else
			(raise e))))
		 (raise e)))
	  (lambda ()
	     (read/lalrp bibtex-parser bibtex-lexer port))))))

;*---------------------------------------------------------------------*/
;*    the-coord ...                                                    */
;*---------------------------------------------------------------------*/
(define (the-coord port)
   (cons (input-port-name port) (input-port-position port)))

;*---------------------------------------------------------------------*/
;*    bibtex-lexer ...                                                 */
;*---------------------------------------------------------------------*/
(define bibtex-lexer
   (regular-grammar ((blank (in " \t\n")))
      ;; separators
      ((+ blank)
       (list 'BLANK (the-coord (the-port))))
      ;; comments
      ((: "%" (* all))
       (ignore))
      ;; egal sign
      (#\=
       (list 'EGAL (the-coord (the-port))))
      ;; sharp sign
      ((: (* blank) #\# (* blank))
       (list 'SHARP (the-coord (the-port))))
      ;; open bracket
      (#\{
       (list 'BRA-OPEN (the-coord (the-port))))
      ;; close bracket
      (#\}
       (list 'BRA-CLO (the-coord (the-port))))
      ;; comma
      (#\,
       (list 'COMMA (the-coord (the-port))))
      ;; double quote
      ((: #\\ (in "\"\\_"))
       (list 'CHAR (the-coord (the-port)) (the-character)))
      ;; optional linebreak
      ((: #\\ #\-)
       (ignore))
      ;; special latin characters
      ((or "{\\'e}" "\\'e")
       (list 'CHAR (the-coord (the-port)) "�"))
      ((or "{\\o}" "\\o")
       (list 'CHAR (the-coord (the-port)) "�"))
      ((or "{\\~{n}}" "\\~{n}")
       (list 'CHAR (the-coord (the-port)) "�"))
      ((or "{\\~{N}}" "\\~{N}")
       (list 'CHAR (the-coord (the-port)) "�"))
      ((or "{\\^{o}}" "\\^{o}")
       (list 'CHAR (the-coord (the-port)) "�"))
      ((or "{\\^{O}}" "\\^{O}")
       (list 'CHAR (the-coord (the-port)) "�"))
      ((or "{\\\"{o}}" "\\\"{o}")
       (list 'CHAR (the-coord (the-port)) "�"))
      ((or "{\\\"{O}}" "\\\"{O}")
       (list 'CHAR (the-coord (the-port)) "�"))
      ((or "{\\`e}" "\\`e")
       (list 'CHAR (the-coord (the-port)) "�"))
      ((or "{\\`a}" "\\`a")
       (list 'CHAR (the-coord (the-port)) "�"))
      ((or "{\\\"i}" "{\\\"{i}}" "\\\"i" "\\\"{i}")
       (list 'CHAR (the-coord (the-port)) "�"))
      ((or "{\\\"u}" "\\\"u")
       (list 'CHAR (the-coord (the-port)) "�"))
      ((or "{\\`u}" "\\`u")
       (list 'CHAR (the-coord (the-port)) "�"))
      ;; latex commands
      ((: #\\ alpha (+ (or alpha digit)))
       (let ((s (the-substring 1 (the-length))))
	  (cond
	     ((member s '("pi" "Pi" "lambda" "Lambda"))
	      (list 'IDENT (the-coord (the-port)) s))
	     (else
	      (ignore)))))
      ;; strings
      ((: "\"" (* (or (out #a000 #\\ #\") (: #\\ all))) "\"")
       (list 'STRING
	     (the-coord (the-port))
	     (the-substring 1 (-fx (the-length) 1))))
      ;; commands
      ((: "@" (+ alpha))
       (let* ((str (string-upcase (the-substring 1 (the-length))))
	      (sym (string->symbol str)))
	  (case sym
	     ((STRING)
	      (list 'BIBSTRING (the-coord (the-port))))
	     (else
	      (list 'BIBITEM (the-coord (the-port)) sym)))))
      ;; digit
      ((+ digit)
       (list 'NUMBER (the-coord (the-port)) (the-string)))
      ;; ident
      ((+ (or alpha digit (in ".:-&/?+*")))
       (list 'IDENT (the-coord (the-port)) (the-string)))
      ;; default
      (else
       (let ((c (the-failure)))
	  (if (eof-object? c)
	      c
	      (list 'CHAR (the-coord (the-port)) c))))))

;*---------------------------------------------------------------------*/
;*    bibtex-parser ...                                                */
;*---------------------------------------------------------------------*/
(define bibtex-parser
   (lalr-grammar
      ;; tokens
      (CHAR IDENT STRING COMMA BRA-OPEN BRA-CLO SHARP BLANK NUMBER EGAL
	    BIBSTRING BIBITEM)
      
      ;; bibtex
      (bibtex
       (()
	'())
       ((bibtex string-def)
	bibtex)
       ((bibtex bibtex-entry)
	(cons bibtex-entry bibtex))
       ((bibtex BLANK)
	bibtex))
      
      ;; blank*
      (blank*
       (() '())
       ((blank* BLANK) '()))
      
      ;; string-def
      (string-def
       ((BIBSTRING BRA-OPEN blank* IDENT blank* EGAL blank* bibtex-entry-value BRA-CLO)
	(bibtex-string-def! (cadr IDENT) bibtex-entry-value)))
      
      ;; bibtex-entry
      (bibtex-entry
       ((BIBITEM blank* BRA-OPEN blank* IDENT blank* COMMA
		 bibtex-entry-item* BRA-CLO)
	(make-bibtex-entry (cadr BIBITEM)
			   (cadr IDENT)
			   bibtex-entry-item*)))
      
      ;; bibtex-entry-item*
      (bibtex-entry-item*
       ((blank*)
	'())
       ((bibtex-entry-item)
	(list bibtex-entry-item))
       ((bibtex-entry-item COMMA bibtex-entry-item*)
	(cons bibtex-entry-item bibtex-entry-item*)))
      
      ;; bibtex-entry-item
      (bibtex-entry-item
       ((blank* IDENT blank* EGAL blank* bibtex-entry-value blank*)
	(cons (cadr IDENT) bibtex-entry-value)))
      
      ;; bibtex-entry-value
      (bibtex-entry-value
       ((NUMBER)
	(list (cadr NUMBER)))
       ((bibtex-entry-value-string)
	bibtex-entry-value-string)
       ((BRA-OPEN bibtex-entry-value-block* BRA-CLO)
	bibtex-entry-value-block*))
      
      ;; bibtex-entry-value-string
      (bibtex-entry-value-string
       ((bibtex-entry-value-string-simple)
	(list bibtex-entry-value-string-simple))
       ((bibtex-entry-value-string SHARP bibtex-entry-value-string-simple)
	`(,@bibtex-entry-value-string ,bibtex-entry-value-string-simple)))
      
      ;; bibtex-entry-value-string-simple
      (bibtex-entry-value-string-simple
       ((STRING)
	(cadr STRING))
       ((IDENT)
	`(ref ,(cadr IDENT))))
      
      ;; bibtex-entry-value-block*
      (bibtex-entry-value-block*
       (()
	'())
       ((bibtex-entry-value-block* bibtex-entry-value-block)
	(append bibtex-entry-value-block* bibtex-entry-value-block)))
      
      ;; bibtex-entry-value-block
      (bibtex-entry-value-block
       ((BRA-OPEN bibtex-entry-value-block* BRA-CLO)
	bibtex-entry-value-block*)
       ((COMMA)
	(list ","))
       ((IDENT)
	(list (cadr IDENT)))
       ((BLANK)
	(list " "))
       ((EGAL)
	(list "="))
       ((CHAR)
	(list (cadr CHAR)))
       ((NUMBER)
	(list (cadr NUMBER)))
       ((STRING)
	(list (string-append "\"" (cadr STRING) "\""))))))

;*---------------------------------------------------------------------*/
;*    bibtex-string-def! ...                                           */
;*---------------------------------------------------------------------*/
(define (bibtex-string-def! ident value)
   (define (->string value)
      (if (string? value)
	  value
	  (match-case value
	     (((and ?s (? string?)))
	      s)
	     (((and ?n (? number?)))
	      (number->string n))
	     (else
	      (apply string-append (map ->string value))))))
   (hashtable-put! *bibtex-string-table* ident (->string value)))

;*---------------------------------------------------------------------*/
;*    make-bibtex-entry ...                                            */
;*---------------------------------------------------------------------*/
(define (make-bibtex-entry kind ident value)
   (define (parse-entry-value line)
      (let ((name (car line))
	    (val (cdr line)))
	 (let loop ((val (reverse val))
		    (res ""))
	    (cond
	       ((null? val)
		(cons name (untexify res)))
	       ((char? (car val))
		(loop (cdr val) (string-append (string (car val)) res)))
	       ((string? (car val))
		(loop (cdr val) (string-append (car val) res)))
	       (else
		(match-case (car val)
		   ((ref ?ref)
		    (let ((h (hashtable-get *bibtex-string-table* ref)))
		       (loop (cdr val)
			     (if (string? h)
				 (string-append h res)
				 res))))
		   (else
		    (loop (cdr val) res))))))))
   (let ((fields (map parse-entry-value value)))
      `(,kind ,ident ,@fields)))

;*---------------------------------------------------------------------*/
;*    untexify ...                                                     */
;*---------------------------------------------------------------------*/
(define (untexify val)
   (define (untexify-math-string str)
      (string-case str
	 ((+ (out #\_ #\^ #\space #\Newline #\tab))
	  (let ((s (the-string)))
	     (string-append s (ignore))))
	 ((+ (in "^_"))
	  (ignore))
	 ((+ (in " \n\t"))
	  (string-append " " (ignore)))
	 (else
	  "")))
   (define (untexify-string str)
      (let ((s (pregexp-replace* "C[$]\\^[$]_[+][+][$][$]" str "C++")))
	 (string-case (pregexp-replace* "[{}]" s "")
	    ((+ (out #\\ #\$ #\space #\Newline #\tab #\~))
	     (let ((s (the-string)))
		(string-append s (ignore))))
	    ((: #\\ (+ (or (: "c" (out #\h))
			   (: "ch" (out #\a))
			   (: "cha" (out #\r))
			   (: "char" (out digit))
			   (out #\\ #\space #\c))))
	     (ignore))
	    ((: #\\ "char" (+ digit))
	     (string-append
	      (string
	       (integer->char
		(string->integer
		 (the-substring 5 (the-length)))))
	      (ignore)))
	    ((: #\$ (* (out #\$)) #\$)
	     (let ((s (the-substring 1 (-fx (the-length) 1))))
		(string-append (untexify-math-string s) (ignore))))
	    ((+ (in " \n\t~"))
	     (string-append " " (ignore)))
	    (else
	     ""))))
   (if (string? val)
       (untexify-string val)
       (map untexify val)))