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) (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 %read-list (stream)
92 (skip-whitespaces-and-comments stream)
93 (let ((ch (%peek-char stream)))
96 (error "Unspected EOF"))
101 (let ((car (ls-read-1 stream)))
102 (skip-whitespaces-and-comments stream)
104 (if (char= (%peek-char stream) #\.)
107 (if (terminalp (%peek-char stream))
108 (ls-read-1 stream) ; Dotted pair notation
109 (cons (let ((string (concat "." (read-escaped-until stream #'terminalp))))
110 (or (values (!parse-integer string nil))
112 (read-symbol string)))
113 (%read-list stream))))
114 (%read-list stream))))))))
116 (defun read-string (stream)
119 (setq ch (%read-char stream))
120 (while (not (eql ch #\"))
122 (error "Unexpected EOF"))
124 (setq ch (%read-char stream)))
125 (setq string (concat string (string ch)))
126 (setq ch (%read-char stream)))
129 (defun read-sharp (stream)
131 (ecase (%read-char stream)
133 (list 'function (ls-read-1 stream)))
134 (#\( (list-to-vector (%read-list stream)))
137 (string-upcase-noescaped
138 (read-escaped-until stream #'terminalp)))))
141 (concat (string (%read-char stream))
142 (read-until stream #'terminalp))))
144 ((string= cname "space") #\space)
145 ((string= cname "tab") #\tab)
146 ((string= cname "newline") #\newline)
147 (t (char cname 0)))))
149 (let ((feature (read-until stream #'terminalp)))
151 ((string= feature "common-lisp")
152 (ls-read-1 stream) ;ignore
154 ((string= feature "jscl")
157 (error "Unknown reader form.")))))))
161 (dotimes (i (length x))
162 (unless (char= (char x i) #\\)
163 (setq result (concat result (string (char x i))))))
166 (defun escape-all (x)
168 (dotimes (i (length x))
169 (setq result (concat result "\\"))
170 (setq result (concat result (string (char x i)))))
173 (defun string-upcase-noescaped (s)
176 (dotimes (i (length s))
177 (let ((ch (char s i)))
180 (setf last-escape nil)
181 (setf result (concat result (string ch))))
184 (setf result (concat result (string-upcase (string ch))))))))
187 ;;; Parse a string of the form NAME, PACKAGE:NAME or
188 ;;; PACKAGE::NAME and return the name. If the string is of the
189 ;;; form 1) or 3), but the symbol does not exist, it will be created
190 ;;; and interned in that package.
191 (defun read-symbol (string)
192 (let ((size (length string))
193 package name internalp index)
195 (while (and (< index size)
196 (not (char= (char string index) #\:)))
197 (when (char= (char string index) #\\)
204 (setq package *package*)
209 (setq package "KEYWORD")
210 (setq package (string-upcase-noescaped (subseq string 0 index))))
212 (when (char= (char string index) #\:)
215 (setq name (subseq string index))))
216 ;; Canonalize symbol name and package
217 (setq name (if (equal package "JS")
218 (setq name (unescape name))
219 (setq name (string-upcase-noescaped name))))
220 (setq package (find-package package))
222 (eq package (find-package "KEYWORD"))
223 (eq package (find-package "JS")))
224 (intern name package)
225 (multiple-value-bind (symbol external)
226 (find-symbol name package)
227 (if (eq external :external)
229 (error "The symbol `~S' is not external in the package ~S." name package))))))
231 (defun read-integer (string)
234 (size (length string)))
236 (let ((elt (char string i)))
239 (setq number (+ (* (or number 0) 10) (digit-char-p elt))))
244 (t (return-from read-integer))))
245 ((and (= i (1- size)) (char= elt #\.)) nil)
246 (t (return-from read-integer)))))
247 (and number (* sign number))))
249 (defun read-float (string)
253 (fractional-part nil)
258 (size (length string))
260 (when (zerop size) (return))
262 (case (char string index)
266 (unless (< index size) (return))
267 ;; Optional integer part
268 (awhen (digit-char-p (char string index))
269 (setq integer-part t)
270 (while (and (< index size)
271 (setq it (digit-char-p (char string index))))
272 (setq number (+ (* number 10) it))
274 (unless (< index size) (return))
275 ;; Decimal point is mandatory if there's no integer part
276 (unless (or integer-part (char= #\. (char string index))) (return))
277 ;; Optional fractional part
278 (when (char= #\. (char string index))
280 (unless (< index size) (return))
281 (awhen (digit-char-p (char string index))
282 (setq fractional-part t)
283 (while (and (< index size)
284 (setq it (digit-char-p (char string index))))
285 (setq number (+ (* number 10) it))
286 (setq divisor (* divisor 10))
288 ;; Either left or right part of the dot must be present
289 (unless (or integer-part fractional-part) (return))
290 ;; Exponent is mandatory if there is no fractional part
291 (when (and (= index size) (not fractional-part)) (return))
292 ;; Optional exponent part
295 (unless (member (string-upcase (string (char string index)))
296 '("E" "S" "F" "D" "L"))
299 (unless (< index size) (return))
300 ;; Optional exponent sign
301 (case (char string index)
303 (#\- (setq exponent-sign -1)
305 (unless (< index size) (return))
307 (let ((value (digit-char-p (char string index))))
308 (unless value (return))
309 (while (and (< index size)
310 (setq value (digit-char-p (char string index))))
311 (setq exponent (+ (* exponent 10) value))
313 (unless (= index size) (return))
314 ;; Everything went ok, we have a float
315 ;; XXX: Use FLOAT when implemented.
316 (/ (* sign (expt 10.0 (* exponent-sign exponent)) number) divisor))))
318 (defun !parse-integer (string junk-allow)
322 (size (length string))
324 ;; Leading whitespace
325 (while (and (< index size)
326 (whitespacep (char string index)))
328 (unless (< index size) (return (values nil 0)))
330 (case (char string 0)
335 (unless (and (< index size)
336 (setq value (digit-char-p (char string index))))
337 (return (values nil index)))
340 (while (< index size)
341 (let ((digit (digit-char-p (char string index))))
342 (unless digit (return))
343 (setq value (+ (* value 10) digit))
345 ;; Trailing whitespace
346 (do ((i index (1+ i)))
347 ((or (= i size) (not (whitespacep (char string i))))
348 (and (= i size) (setq index i))))
351 (values (* sign value) index)
352 (values nil index)))))
355 (defun parse-integer (string &key junk-allowed)
356 (multiple-value-bind (num index)
357 (!parse-integer string junk-allowed)
360 (error "Junk detected."))))
362 (defvar *eof* (gensym))
363 (defun ls-read-1 (stream)
364 (skip-whitespaces-and-comments stream)
365 (let ((ch (%peek-char stream)))
367 ((or (null ch) (char= ch #\)))
374 (list 'quote (ls-read-1 stream)))
377 (list 'backquote (ls-read-1 stream)))
380 (read-string stream))
383 (if (eql (%peek-char stream) #\@)
384 (progn (%read-char stream) (list 'unquote-splicing (ls-read-1 stream)))
385 (list 'unquote (ls-read-1 stream))))
389 (let ((string (read-escaped-until stream #'terminalp)))
390 (or (read-integer string)
392 (read-symbol string)))))))
394 (defun ls-read (stream &optional (eof-error-p t) eof-value)
395 (let ((x (ls-read-1 stream)))
398 (error "End of file")
402 (defun ls-read-from-string (string &optional (eof-error-p t) eof-value)
403 (ls-read (make-string-stream string) eof-error-p eof-value))
406 (defun read-from-string (string &optional (eof-errorp t) eof-value)
407 (ls-read-from-string string eof-errorp eof-value))