3 ;; Copyright (C) 2012, 2013 David Vazquez
4 ;; Copyright (C) 2012 Raimon Grau
6 ;; JSCL is free software: you can redistribute it and/or
7 ;; modify it under the terms of the GNU General Public License as
8 ;; published by the Free Software Foundation, either version 3 of the
9 ;; License, or (at your option) any later version.
11 ;; JSCL is distributed in the hope that it will be useful, but
12 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;; General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with JSCL. If not, see <http://www.gnu.org/licenses/>.
22 ;;; The Lisp reader, parse strings and return Lisp objects. The main
23 ;;; entry points are `ls-read' and `ls-read-from-string'.
25 ;;; #= / ## implementation
27 ;; For now associations label->object are kept in a plist
28 ;; May be it makes sense to use a vector instead if speed
29 ;; is considered a problem with many labelled objects
30 (defvar *labelled-objects* nil)
32 (defun new-labelled-objects-table ()
33 (setf *labelled-objects* nil))
35 (defun find-labelled-object (id)
36 (assoc id *labelled-objects*))
38 (defun add-labelled-object (id value)
39 (push (cons id value) *labelled-objects*))
41 ;; A unique value used to mark in the labelled objects
42 ;; table an object that is being constructed
43 ;; (e.g. #1# while reading elements of "#1=(#1# #1# #1#)")
44 (defvar *future-value* (make-symbol "future"))
46 ;; A unique value used to mark temporary values that will
47 ;; be replaced when fixups are run.
48 (defvar *fixup-value* (make-symbol "fixup"))
50 ;; Fixup locations keeps a list of conses where the CAR
51 ;; is a callable to be called with the value of the object
52 ;; associated to label stored in CDR once reading is completed
53 (defvar *fixup-locations* nil)
55 (defun fixup-backrefs ()
56 (while *fixup-locations*
57 (let* ((fixup (pop *fixup-locations*))
58 (callable (car fixup))
59 (cell (find-labelled-object (cdr fixup))))
61 (funcall callable (cdr cell))
62 (error "Internal error in fixup-backrefs: object #~S# not found"
65 ;; A function that will need to return a fixup callback
66 ;; for the object that is being read. The returned callback will
67 ;; be called with the result of reading.
68 (defvar *make-fixup-function*
70 (error "Internal error in fixup creation during read")))
72 (defun make-string-stream (string)
75 (defun %peek-char (stream)
76 (and (< (cdr stream) (length (car stream)))
77 (char (car stream) (cdr stream))))
79 (defun %read-char (stream)
80 (and (< (cdr stream) (length (car stream)))
81 (prog1 (char (car stream) (cdr stream))
82 (rplacd stream (1+ (cdr stream))))))
84 (defun whitespacep (ch)
85 (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
87 (defun skip-whitespaces (stream)
89 (setq ch (%peek-char stream))
90 (while (and ch (whitespacep ch))
92 (setq ch (%peek-char stream)))))
95 (or (null ch) (whitespacep ch) (char= #\" ch) (char= #\) ch) (char= #\( ch)))
97 (defun read-until (stream func)
100 (setq ch (%peek-char stream))
101 (while (and ch (not (funcall func ch)))
102 (setq string (concat string (string ch)))
104 (setq ch (%peek-char stream)))
107 (defun read-escaped-until (stream func)
109 (ch (%peek-char stream))
111 (while (and ch (or multi-escape (not (funcall func ch))))
115 (setf multi-escape nil)
116 (setf multi-escape t)))
119 (setf ch (%peek-char stream))
120 (setf string (concat string "\\" (string ch))))
123 (setf string (concat string "\\" (string ch)))
124 (setf string (concat string (string ch))))))
126 (setf ch (%peek-char stream)))
129 (defun skip-whitespaces-and-comments (stream)
131 (skip-whitespaces stream)
132 (setq ch (%peek-char stream))
133 (while (and ch (char= ch #\;))
134 (read-until stream (lambda (x) (char= x #\newline)))
135 (skip-whitespaces stream)
136 (setq ch (%peek-char stream)))))
138 (defun discard-char (stream expected)
139 (let ((ch (%read-char stream)))
141 (error "End of file when character ~S was expected." expected))
142 (unless (char= ch expected)
143 (error "Character ~S was found but ~S was expected." ch expected))))
145 (defun %read-list (stream &optional (eof-error-p t) eof-value)
146 (skip-whitespaces-and-comments stream)
147 (let ((ch (%peek-char stream)))
150 (error "Unspected EOF"))
152 (discard-char stream #\))
155 (let* ((cell (cons nil nil))
156 (*make-fixup-function* (lambda ()
160 (next (ls-read stream nil eof t)))
162 (skip-whitespaces-and-comments stream)
165 (discard-char stream #\))
168 (if (char= (%peek-char stream) #\.)
170 (discard-char stream #\.)
171 (if (terminalp (%peek-char stream))
172 (let ((*make-fixup-function* (lambda ()
174 (rplacd cell obj)))))
175 ;; Dotted pair notation
176 (rplacd cell (ls-read stream eof-error-p eof-value t))
177 (skip-whitespaces-and-comments stream)
178 (let ((ch (%peek-char stream)))
179 (if (or (null ch) (char= #\) ch))
180 (discard-char stream #\))
181 (error "Multiple objects following . in a list"))))
182 (let ((token (concat "." (read-escaped-until stream #'terminalp))))
183 (rplacd cell (cons (interpret-token token)
184 (%read-list stream eof-error-p eof-value))))))
185 (rplacd cell (%read-list stream eof-error-p eof-value)))
188 (defun read-string (stream)
191 (setq ch (%read-char stream))
192 (while (not (eql ch #\"))
194 (error "Unexpected EOF"))
196 (setq ch (%read-char stream)))
197 (setq string (concat string (string ch)))
198 (setq ch (%read-char stream)))
201 (defun read-sharp (stream &optional eof-error-p eof-value)
203 (let ((ch (%read-char stream)))
206 (list 'function (ls-read stream eof-error-p eof-value t)))
210 (index 0 (1+ index)))
211 ((progn (skip-whitespaces-and-comments stream)
212 (or (null (%peek-char stream))
213 (char= (%peek-char stream) #\))))
214 (discard-char stream #\))
215 (setf result (make-array index))
217 (aset result (decf index) (pop elements)))
219 (let* ((ix index) ; Can't just use index: the same var would be captured in all fixups
220 (*make-fixup-function* (lambda ()
222 (aset result ix obj))))
224 (value (ls-read stream nil eof t)))
225 (push value elements))))
229 (string-upcase-noescaped
230 (read-escaped-until stream #'terminalp)))))
233 (concat (string (%read-char stream))
234 (read-until stream #'terminalp))))
236 ((string= cname "space") #\space)
237 ((string= cname "tab") #\tab)
238 ((string= cname "newline") #\newline)
239 (t (char cname 0)))))
242 (let ((feature (let ((symbol (ls-read stream eof-error-p eof-value t)))
243 (unless (symbolp symbol)
244 (error "Invalid feature ~S" symbol))
245 (intern (string symbol) "KEYWORD"))))
246 (if (eql (char= ch #\+)
247 (and (find feature *features*) t))
248 (ls-read stream eof-error-p eof-value t)
249 (prog2 (ls-read stream)
250 (ls-read stream eof-error-p eof-value t)))))
251 ((and ch (digit-char-p ch))
252 (let ((id (digit-char-p ch)))
253 (while (and (%peek-char stream)
254 (digit-char-p (%peek-char stream)))
255 (setf id (+ (* id 10) (digit-char-p (%read-char stream)))))
256 (ecase (%peek-char stream)
259 (if (find-labelled-object id)
260 (error "Duplicated label #~S=" id)
262 (add-labelled-object id *future-value*)
263 (let ((obj (ls-read stream eof-error-p eof-value t)))
264 ;; FIXME: somehow the more natural
265 ;; (setf (cdr (find-labelled-object id)) obj)
267 (rplacd (find-labelled-object id) obj)
271 (let ((cell (find-labelled-object id)))
273 (if (eq (cdr cell) *future-value*)
275 (push (cons (funcall *make-fixup-function*)
280 (error "Invalid labelled object #~S#" id)))))))
282 (error "Invalid dispatch character after #")))))
284 (defun unescape-token (x)
286 (dotimes (i (length x))
287 (unless (char= (char x i) #\\)
288 (setq result (concat result (string (char x i))))))
291 (defun string-upcase-noescaped (s)
294 (dotimes (i (length s))
295 (let ((ch (char s i)))
298 (setf last-escape nil)
299 (setf result (concat result (string ch))))
302 (setf result (concat result (string-upcase (string ch))))))))
305 ;;; Parse a string of the form NAME, PACKAGE:NAME or
306 ;;; PACKAGE::NAME and return the name. If the string is of the
307 ;;; form 1) or 3), but the symbol does not exist, it will be created
308 ;;; and interned in that package.
309 (defun read-symbol (string)
310 (let ((size (length string))
311 package name internalp index)
313 (while (and (< index size)
314 (not (char= (char string index) #\:)))
315 (when (char= (char string index) #\\)
322 (setq package *package*)
327 (setq package "KEYWORD")
328 (setq package (string-upcase-noescaped (subseq string 0 index))))
330 (when (char= (char string index) #\:)
333 (setq name (subseq string index))))
334 ;; Canonalize symbol name and package
335 (setq name (if (equal package "JS")
336 (setq name (unescape-token name))
337 (setq name (string-upcase-noescaped name))))
338 (setq package (find-package package))
340 (eq package (find-package "KEYWORD"))
341 (eq package (find-package "JS")))
342 (intern name package)
343 (multiple-value-bind (symbol external)
344 (find-symbol name package)
345 (if (eq external :external)
347 (error "The symbol `~S' is not external in the package ~S." name package))))))
349 (defun read-integer (string)
352 (size (length string)))
354 (let ((elt (char string i)))
357 (setq number (+ (* (or number 0) 10) (digit-char-p elt))))
362 (t (return-from read-integer))))
363 ((and (= i (1- size)) (char= elt #\.)) nil)
364 (t (return-from read-integer)))))
365 (and number (* sign number))))
367 (defun read-float (string)
371 (fractional-part nil)
376 (size (length string))
378 (when (zerop size) (return))
380 (case (char string index)
384 (unless (< index size) (return))
385 ;; Optional integer part
386 (awhen (digit-char-p (char string index))
387 (setq integer-part t)
388 (while (and (< index size)
389 (setq it (digit-char-p (char string index))))
390 (setq number (+ (* number 10) it))
392 (unless (< index size) (return))
393 ;; Decimal point is mandatory if there's no integer part
394 (unless (or integer-part (char= #\. (char string index))) (return))
395 ;; Optional fractional part
396 (when (char= #\. (char string index))
398 (unless (< index size) (return))
399 (awhen (digit-char-p (char string index))
400 (setq fractional-part t)
401 (while (and (< index size)
402 (setq it (digit-char-p (char string index))))
403 (setq number (+ (* number 10) it))
404 (setq divisor (* divisor 10))
406 ;; Either left or right part of the dot must be present
407 (unless (or integer-part fractional-part) (return))
408 ;; Exponent is mandatory if there is no fractional part
409 (when (and (= index size) (not fractional-part)) (return))
410 ;; Optional exponent part
413 (unless (find (char-upcase (char string index)) "ESFDL")
416 (unless (< index size) (return))
417 ;; Optional exponent sign
418 (case (char string index)
420 (#\- (setq exponent-sign -1)
422 (unless (< index size) (return))
424 (let ((value (digit-char-p (char string index))))
425 (unless value (return))
426 (while (and (< index size)
427 (setq value (digit-char-p (char string index))))
428 (setq exponent (+ (* exponent 10) value))
430 (unless (= index size) (return))
431 ;; Everything went ok, we have a float
432 ;; XXX: Use FLOAT when implemented.
433 (/ (* sign (expt 10.0 (* exponent-sign exponent)) number) divisor 1.0))))
435 (defun !parse-integer (string junk-allow)
439 (size (length string))
441 ;; Leading whitespace
442 (while (and (< index size)
443 (whitespacep (char string index)))
445 (unless (< index size) (return (values nil 0)))
447 (case (char string 0)
452 (unless (and (< index size)
453 (setq value (digit-char-p (char string index))))
454 (return (values nil index)))
457 (while (< index size)
458 (let ((digit (digit-char-p (char string index))))
459 (unless digit (return))
460 (setq value (+ (* value 10) digit))
462 ;; Trailing whitespace
463 (do ((i index (1+ i)))
464 ((or (= i size) (not (whitespacep (char string i))))
465 (and (= i size) (setq index i))))
468 (values (* sign value) index)
469 (values nil index)))))
472 (defun parse-integer (string &key junk-allowed)
473 (multiple-value-bind (num index)
474 (!parse-integer string junk-allowed)
477 (error "Junk detected."))))
480 (defun interpret-token (string)
481 (or (read-integer string)
483 (read-symbol string)))
485 (defun ls-read (stream &optional (eof-error-p t) eof-value recursive-p)
486 (let ((save-labelled-objects *labelled-objects*)
487 (save-fixup-locations *fixup-locations*))
489 (setf *fixup-locations* nil)
490 (setf *labelled-objects* (new-labelled-objects-table)))
493 (skip-whitespaces-and-comments stream)
494 (let ((ch (%peek-char stream)))
496 ((or (null ch) (char= ch #\)))
498 (error "End of file")
502 (%read-list stream eof-error-p eof-value))
505 (list 'quote (ls-read stream eof-error-p eof-value t)))
508 (list 'backquote (ls-read stream eof-error-p eof-value t)))
511 (read-string stream))
514 (if (eql (%peek-char stream) #\@)
515 (progn (%read-char stream) (list 'unquote-splicing
516 (ls-read stream eof-error-p eof-value t)))
517 (list 'unquote (ls-read stream eof-error-p eof-value t))))
519 (read-sharp stream eof-error-p eof-value))
521 (let ((string (read-escaped-until stream #'terminalp)))
522 (interpret-token string))))))
525 (setf *labelled-objects* save-labelled-objects)
526 (setf *fixup-locations* save-fixup-locations)))))
528 (defun ls-read-from-string (string &optional (eof-error-p t) eof-value)
529 (ls-read (make-string-stream string) eof-error-p eof-value))
532 (defun read-from-string (string &optional (eof-errorp t) eof-value)
533 (ls-read-from-string string eof-errorp eof-value))