-
Notifications
You must be signed in to change notification settings - Fork 2
/
bst-reader.lisp
286 lines (257 loc) · 9.76 KB
/
bst-reader.lisp
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
;; A BibTeX re-implementation in Common Lisp - the BST file reader
;; Copyright 2001, 2002 Matthias Koeppe <[email protected]>
;;
;; This code is free software; you can redistribute it and/or
;; modify it under the terms of version 2.1 of the GNU Lesser
;; General Public License as published by the Free Software
;; Foundation or any later version, as clarified by the preamble
;; found in COPYING-preamble.txt. This preamble is in the style
;; of the Franz Inc. preamble at http://opensource.franz.com/preamble.html
;; with names and copyright holders altered accordingly.
(in-package bibtex-compiler)
(defvar *bst-readtable*
(let ((*readtable* (copy-readtable nil)))
;; comment character
(set-syntax-from-char #\% #\;)
;; CL's package marker : is an ordinary constituent in BST...
;; Here's just a quick fix to make := work
(set-macro-character #\:
#'(lambda (stream char)
(declare (ignore char))
(let ((sym (read stream nil nil t)))
(unless (symbolp sym)
(error "Bad syntax"))
(values
(intern
(string-upcase
(concatenate 'string ":" (symbol-name sym))))))))
;; function lists
(set-macro-character #\{
#'(lambda (stream char)
(declare (ignore char))
(read-delimited-list #\} stream t)))
(set-syntax-from-char #\} #\))
;; quote
(set-macro-character #\'
#'(lambda (stream char)
(declare (ignore char))
(list 'quote (read stream nil nil t))))
;; numbers
(set-macro-character #\#
#'(lambda (stream char)
(declare (ignore char))
(read stream nil nil t)))
;; double-quote; BST strings have no escape
(set-macro-character #\"
#'(lambda (stream char)
(declare (ignore char))
(coerce
(loop as char = (read-char stream nil nil t)
until (char= char #\")
collect char)
'string)))
*readtable*))
;;;
(defvar *bst-stream* nil)
(defun bst-read (&key (eof-ok nil))
(let ((result
(let ((*readtable* *bst-readtable*))
(read *bst-stream* nil '*EOF*))))
(when (and (not eof-ok)
(eq result '*EOF*))
(error "Unexpected end of file"))
result))
;;;
(defvar *read-seen-p* nil "Non-nil if the BST READ command has been seen.")
(defvar *entry-seen-p* nil "Non-nil if the BST ENTRY command has been seen.")
(defstruct bst-command
function
args)
(defvar *bst-commands* (make-hash-table :test 'equalp))
(defmacro define-bst-command (name args &body body)
`(setf (gethash ,(string name) *bst-commands*)
(make-bst-command :function (lambda ,args ,@body)
:args ',args)))
(defun copy-comment (from-stream to-stream)
(terpri to-stream)
(loop while (char= (peek-char t from-stream nil #\x) #\%)
do
(princ ";" to-stream)
(loop while (char= (peek-char nil from-stream nil #\x) #\%)
do (read-char from-stream) (princ ";" to-stream))
(princ (read-line from-stream nil "") to-stream)
(terpri to-stream)))
(defun store-comment ()
"Copy top-level comments; replace N leading % signs with N+1 semicola"
(let ((comment
(with-output-to-string (s)
(copy-comment *bst-stream* s))))
(push comment *bst-definition-sequence*)))
(defun get-bst-commands-and-process (stream)
(let* ((*bst-stream* stream)
(*entry-seen-p* nil)
(*read-seen-p* nil))
(loop
(when (and *bst-compiling*
(char= (peek-char t *bst-stream* nil #\x) #\%))
(store-comment))
(let ((command (bst-read :eof-ok t)))
(when (eql command '*EOF*)
(return))
(let ((bst-command (gethash (string command) *bst-commands*)))
(unless bst-command
(error "~A is an illegal style-file command" command))
(apply (bst-command-function bst-command)
(mapcar (lambda (argname)
(declare (ignore argname))
(bst-read :eof-ok nil))
(bst-command-args bst-command))))))))
(define-bst-command "ENTRY" (fields int-entry-vars str-entry-vars)
(when *entry-seen-p*
(error "Illegal, another entry command"))
(setq *entry-seen-p* t)
(dolist (field fields)
(check-for-already-defined-function field)
(register-bst-entry field 'field '(string missing) nil *bst-functions*))
(dolist (entry int-entry-vars)
(check-for-already-defined-function entry)
(register-bst-entry entry 'int-entry-var '(integer) 0 *bst-functions*))
(dolist (entry str-entry-vars)
(check-for-already-defined-function entry)
(register-bst-entry entry 'str-entry-var '(string) "" *bst-functions*)))
(defun singleton-list-p (arg)
"Non-nil if ARG is a list consisting of one symbol."
(and (consp arg)
(symbolp (car arg))
(null (cdr arg))))
(define-bst-command "EXECUTE" (function-list)
(unless *read-seen-p*
(error "Illegal, execute command before read command"))
(unless (singleton-list-p function-list)
(error "Illegal argument ~A to execute command"
function-list))
(let* ((name (car function-list))
(function (get-bst-function-of-type name '(built-in wiz-defined compiled-wiz-defined))))
(if *bst-compiling*
(progn
(push (bst-compile-thunkcall name) *main-lisp-body*)
(push function *bst-function-call-sequence*))
(bst-execute function))))
(define-bst-command "FUNCTION" (function-list function-definition)
(unless (singleton-list-p function-list)
(error "Illegal argument ~A to function command"
function-list))
(let* ((bst-name (car function-list)))
(unless (check-for-already-defined-function bst-name)
(let ((bst-function (make-bst-function :name (string bst-name)
:type 'wiz-defined
:body function-definition)))
(setf (gethash (string bst-name) *bst-functions*) bst-function)
(when *bst-compiling*
(compile-bst-function bst-function)
(push bst-function *bst-definition-sequence*))))))
(define-bst-command "INTEGERS" (name-list)
(unless (listp name-list)
(error "Illegal argument ~A to integers command"
name-list))
(dolist (bst-name name-list)
(check-for-already-defined-function bst-name)
(let* ((lexical (member bst-name *lexicals* :test 'string-equal))
(lisp-name (and *bst-compiling*
(bst-name-to-lisp-name bst-name
(if lexical :lexical :variable))))
(bst-function
(register-bst-global-var bst-name lisp-name 'int-global-var
'(integer) 0 *bst-functions*)))
(when *bst-compiling*
(push bst-function *bst-definition-sequence*)))))
(define-bst-command "ITERATE" (function-list)
(unless *read-seen-p*
(error "Illegal, iterate command before read command"))
(unless (singleton-list-p function-list)
(error "Illegal argument ~A to iterate command"
function-list))
(let* ((name (car function-list))
(function (get-bst-function-of-type name '(built-in wiz-defined compiled-wiz-defined))))
(if *bst-compiling*
(progn
(push `(dolist (*bib-entry* ,*bib-entries-symbol*)
,(bst-compile-thunkcall name))
*main-lisp-body*)
(push function *bst-function-call-sequence*))
(dolist (*bib-entry* *bib-entries*)
(bst-execute function)))))
(define-bst-command "MACRO" (function-list function-definition)
(when *read-seen-p*
(error "Illegal, macro command after read command"))
(unless (singleton-list-p function-list)
(error "Illegal argument ~A to macro command"
function-list))
(unless (and (consp function-definition)
(stringp (car function-definition))
(null (cdr function-definition)))
(error "Illegal argument ~A to macro command"
function-definition))
(let ((name (car function-list))
(definition (car function-definition)))
(check-for-already-defined-function name)
(setq name (string name))
(make-bst-function :name name
:lisp-name (lambda () (gethash name *bib-macros*))
:lisp-form-maker (lambda ()
`(gethash name *bib-macros*))
:type 'macro
:argument-types '()
:result-types '((string)))
(setf (gethash name *bib-macros*) definition)))
(define-bst-command "READ" ()
(when *read-seen-p*
(error "Illegal, another read command"))
(unless *entry-seen-p*
(error "Illegal, read command before entry command"))
(setq *read-seen-p* t)
(if *bst-compiling*
(push `(setq ,*bib-entries-symbol*
(read-all-bib-files-and-compute-bib-entries))
*main-lisp-body*)
(setq *bib-entries* (read-all-bib-files-and-compute-bib-entries))))
(define-bst-command "REVERSE" (function-list)
(unless *read-seen-p*
(error "Illegal, reverse command before read command"))
(unless (singleton-list-p function-list)
(error "Illegal argument ~A to execute command"
function-list))
(let* ((name (car function-list))
(function (get-bst-function-of-type name '(built-in wiz-defined compiled-wiz-defined))))
(if *bst-compiling*
(progn
(push `(dolist (*bib-entry* (reverse ,*bib-entries-symbol*))
,(bst-compile-thunkcall name))
*main-lisp-body*)
(push function *bst-function-call-sequence*))
(dolist (*bib-entry* (reverse *bib-entries*))
(bst-execute function)))))
(define-bst-command "SORT" ()
(unless *read-seen-p*
(error "Illegal, sort command before read command"))
(if *bst-compiling*
(push `(setq ,*bib-entries-symbol*
(stable-sort ,*bib-entries-symbol* #'cmp<))
*main-lisp-body*)
(setq *bib-entries*
(stable-sort *bib-entries* #'cmp<))))
(define-bst-command "STRINGS" (name-list)
(unless (listp name-list)
(error "Illegal argument ~A to strings command"
name-list))
(dolist (bst-name name-list)
(check-for-already-defined-function bst-name)
(let* ((lexical (member bst-name *lexicals* :test 'string-equal))
(lisp-name (and *bst-compiling*
(bst-name-to-lisp-name bst-name
(if lexical :lexical :variable))))
(bst-function
(register-bst-global-var bst-name lisp-name 'str-global-var
'(string) "" *bst-functions*)))
(when *bst-compiling*
(push bst-function *bst-definition-sequence*)))))