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 (defun make-string-stream (string)
28 (defun %peek-char (stream)
29 (and (< (cdr stream) (length (car stream)))
30 (char (car stream) (cdr stream))))
32 (defun %read-char (stream)
33 (and (< (cdr stream) (length (car stream)))
34 (prog1 (char (car stream) (cdr stream))
35 (rplacd stream (1+ (cdr stream))))))
37 (defun whitespacep (ch)
38 (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
40 (defun skip-whitespaces (stream)
42 (setq ch (%peek-char stream))
43 (while (and ch (whitespacep ch))
45 (setq ch (%peek-char stream)))))
48 (or (null ch) (whitespacep ch) (char= #\) ch) (char= #\( ch)))
50 (defun read-until (stream func)
53 (setq ch (%peek-char stream))
54 (while (and ch (not (funcall func ch)))
55 (setq string (concat string (string ch)))
57 (setq ch (%peek-char stream)))
60 (defun read-escaped-until (stream func)
62 (ch (%peek-char stream))
64 (while (and ch (or multi-escape (not (funcall func ch))))
68 (setf multi-escape nil)
69 (setf multi-escape t)))
72 (setf ch (%peek-char stream))
73 (setf string (concat string "\\" (string ch))))
76 (setf string (concat string "\\" (string ch)))
77 (setf string (concat string (string ch))))))
79 (setf ch (%peek-char stream)))
82 (defun skip-whitespaces-and-comments (stream)
84 (skip-whitespaces stream)
85 (setq ch (%peek-char stream))
86 (while (and ch (char= ch #\;))
87 (read-until stream (lambda (x) (char= x #\newline)))
88 (skip-whitespaces stream)
89 (setq ch (%peek-char stream)))))
91 (defun discard-char (stream expected)
92 (let ((ch (%read-char stream)))
94 (error "End of file when character ~S was expected." expected))
95 (unless (char= ch expected)
96 (error "Character ~S was found but ~S was expected." ch expected))))
98 (defun %read-list (stream)
99 (skip-whitespaces-and-comments stream)
100 (let ((ch (%peek-char stream)))
103 (error "Unspected EOF"))
105 (discard-char stream #\))
108 (let* ((eof (gensym))
109 (next (ls-read stream nil eof)))
110 (skip-whitespaces-and-comments stream)
113 (discard-char stream #\)))
116 (if (char= (%peek-char stream) #\.)
118 (discard-char stream #\.)
119 (if (terminalp (%peek-char stream))
120 (prog1 (ls-read stream) ; Dotted pair notation
121 (skip-whitespaces-and-comments stream)
122 (if (char= (%peek-char stream) #\) )
123 (discard-char stream #\))
124 (error "More than one object follows . in list")))
125 (let ((token (concat "." (read-escaped-until stream #'terminalp))))
126 (cons (interpret-token token)
127 (%read-list stream)))))
128 (%read-list stream))))))))))
130 (defun read-string (stream)
133 (setq ch (%read-char stream))
134 (while (not (eql ch #\"))
136 (error "Unexpected EOF"))
138 (setq ch (%read-char stream)))
139 (setq string (concat string (string ch)))
140 (setq ch (%read-char stream)))
143 (defun read-sharp (stream &optional eof-error-p eof-value)
145 (ecase (%read-char stream)
147 (list 'function (ls-read stream)))
148 (#\( (list-to-vector (%read-list stream)))
149 (#\: (make-symbol (string-upcase (read-until stream #'terminalp))))
152 (concat (string (%read-char stream))
153 (read-until stream #'terminalp))))
155 ((string= cname "space") #\space)
156 ((string= cname "tab") #\tab)
157 ((string= cname "newline") #\newline)
158 (t (char cname 0)))))
160 (let ((feature (let ((symbol (ls-read stream)))
161 (unless (symbolp symbol)
162 (error "Invalid feature ~S" symbol))
163 (intern (string symbol) "KEYWORD"))))
167 (ls-read stream eof-error-p eof-value))
169 (ls-read stream eof-error-p eof-value)))))))
173 (dotimes (i (length x))
174 (unless (char= (char x i) #\\)
175 (setq result (concat result (string (char x i))))))
178 (defun string-upcase-noescaped (s)
181 (dotimes (i (length s))
182 (let ((ch (char s i)))
185 (setf last-escape nil)
186 (setf result (concat result (string ch))))
189 (setf result (concat result (string-upcase (string ch))))))))
192 ;;; Parse a string of the form NAME, PACKAGE:NAME or
193 ;;; PACKAGE::NAME and return the name. If the string is of the
194 ;;; form 1) or 3), but the symbol does not exist, it will be created
195 ;;; and interned in that package.
196 (defun read-symbol (string)
197 (let ((size (length string))
198 package name internalp index)
200 (while (and (< index size)
201 (not (char= (char string index) #\:)))
202 (when (char= (char string index) #\\)
209 (setq package *package*)
214 (setq package "KEYWORD")
215 (setq package (string-upcase-noescaped (subseq string 0 index))))
217 (when (char= (char string index) #\:)
220 (setq name (subseq string index))))
221 ;; Canonalize symbol name and package
222 (setq name (if (equal package "JS")
223 (setq name (unescape name))
224 (setq name (string-upcase-noescaped name))))
225 (setq package (find-package package))
227 (eq package (find-package "KEYWORD"))
228 (eq package (find-package "JS")))
229 (intern name package)
230 (multiple-value-bind (symbol external)
231 (find-symbol name package)
232 (if (eq external :external)
234 (error "The symbol `~S' is not external in the package ~S." name package))))))
236 (defun read-integer (string)
239 (size (length string)))
241 (let ((elt (char string i)))
244 (setq number (+ (* (or number 0) 10) (digit-char-p elt))))
249 (t (return-from read-integer))))
250 ((and (= i (1- size)) (char= elt #\.)) nil)
251 (t (return-from read-integer)))))
252 (and number (* sign number))))
254 (defun read-float (string)
258 (fractional-part nil)
263 (size (length string))
265 (when (zerop size) (return))
267 (case (char string index)
271 (unless (< index size) (return))
272 ;; Optional integer part
273 (awhen (digit-char-p (char string index))
274 (setq integer-part t)
275 (while (and (< index size)
276 (setq it (digit-char-p (char string index))))
277 (setq number (+ (* number 10) it))
279 (unless (< index size) (return))
280 ;; Decimal point is mandatory if there's no integer part
281 (unless (or integer-part (char= #\. (char string index))) (return))
282 ;; Optional fractional part
283 (when (char= #\. (char string index))
285 (unless (< index size) (return))
286 (awhen (digit-char-p (char string index))
287 (setq fractional-part t)
288 (while (and (< index size)
289 (setq it (digit-char-p (char string index))))
290 (setq number (+ (* number 10) it))
291 (setq divisor (* divisor 10))
293 ;; Either left or right part of the dot must be present
294 (unless (or integer-part fractional-part) (return))
295 ;; Exponent is mandatory if there is no fractional part
296 (when (and (= index size) (not fractional-part)) (return))
297 ;; Optional exponent part
300 (unless (member (string-upcase (string (char string index)))
301 '("E" "S" "F" "D" "L"))
304 (unless (< index size) (return))
305 ;; Optional exponent sign
306 (case (char string index)
308 (#\- (setq exponent-sign -1)
310 (unless (< index size) (return))
312 (let ((value (digit-char-p (char string index))))
313 (unless value (return))
314 (while (and (< index size)
315 (setq value (digit-char-p (char string index))))
316 (setq exponent (+ (* exponent 10) value))
318 (unless (= index size) (return))
319 ;; Everything went ok, we have a float
320 ;; XXX: Use FLOAT when implemented.
321 (/ (* sign (expt 10.0 (* exponent-sign exponent)) number) divisor))))
323 (defun !parse-integer (string junk-allow)
327 (size (length string))
329 ;; Leading whitespace
330 (while (and (< index size)
331 (whitespacep (char string index)))
333 (unless (< index size) (return (values nil 0)))
335 (case (char string 0)
340 (unless (and (< index size)
341 (setq value (digit-char-p (char string index))))
342 (return (values nil index)))
345 (while (< index size)
346 (let ((digit (digit-char-p (char string index))))
347 (unless digit (return))
348 (setq value (+ (* value 10) digit))
350 ;; Trailing whitespace
351 (do ((i index (1+ i)))
352 ((or (= i size) (not (whitespacep (char string i))))
353 (and (= i size) (setq index i))))
356 (values (* sign value) index)
357 (values nil index)))))
360 (defun parse-integer (string &key junk-allowed)
361 (multiple-value-bind (num index)
362 (!parse-integer string junk-allowed)
365 (error "Junk detected."))))
368 (defun interpret-token (string)
369 (or (read-integer string)
371 (read-symbol string)))
373 (defun ls-read (stream &optional (eof-error-p t) eof-value)
374 (skip-whitespaces-and-comments stream)
375 (let ((ch (%peek-char stream)))
377 ((or (null ch) (char= ch #\)))
379 (error "End of file")
386 (list 'quote (ls-read stream)))
389 (list 'backquote (ls-read stream)))
392 (read-string stream))
395 (if (eql (%peek-char stream) #\@)
396 (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
397 (list 'unquote (ls-read stream))))
401 (let ((string (read-escaped-until stream #'terminalp)))
402 (interpret-token string))))))
404 (defun ls-read-from-string (string &optional (eof-error-p t) eof-value)
405 (ls-read (make-string-stream string) eof-error-p eof-value))
408 (defun read-from-string (string &optional (eof-errorp t) eof-value)
409 (ls-read-from-string string eof-errorp eof-value))