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 %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)))
135 (#\: (make-symbol (string-upcase (read-until stream #'terminalp))))
138 (concat (string (%read-char stream))
139 (read-until stream #'terminalp))))
141 ((string= cname "space") #\space)
142 ((string= cname "tab") #\tab)
143 ((string= cname "newline") #\newline)
144 (t (char cname 0)))))
146 (let ((feature (read-until stream #'terminalp)))
148 ((string= feature "common-lisp")
149 (ls-read-1 stream) ;ignore
151 ((string= feature "jscl")
154 (error "Unknown reader form.")))))))
158 (dotimes (i (length x))
159 (unless (char= (char x i) #\\)
160 (setq result (concat result (string (char x i))))))
163 (defun escape-all (x)
165 (dotimes (i (length x))
166 (setq result (concat result "\\"))
167 (setq result (concat result (string (char x i)))))
170 (defun string-upcase-noescaped (s)
173 (dotimes (i (length s))
174 (let ((ch (char s i)))
177 (setf last-escape nil)
178 (setf result (concat result (string ch))))
181 (setf result (concat result (string-upcase (string ch))))))))
184 ;;; Parse a string of the form NAME, PACKAGE:NAME or
185 ;;; PACKAGE::NAME and return the name. If the string is of the
186 ;;; form 1) or 3), but the symbol does not exist, it will be created
187 ;;; and interned in that package.
188 (defun read-symbol (string)
189 (let ((size (length string))
190 package name internalp index)
192 (while (and (< index size)
193 (not (char= (char string index) #\:)))
194 (when (char= (char string index) #\\)
201 (setq package *package*)
206 (setq package "KEYWORD")
207 (setq package (string-upcase-noescaped (subseq string 0 index))))
209 (when (char= (char string index) #\:)
212 (setq name (subseq string index))))
213 ;; Canonalize symbol name and package
214 (setq name (if (equal package "JS")
215 (setq name (unescape name))
216 (setq name (string-upcase-noescaped name))))
217 (setq package (find-package package))
219 (eq package (find-package "KEYWORD"))
220 (eq package (find-package "JS")))
221 (intern name package)
222 (multiple-value-bind (symbol external)
223 (find-symbol name package)
224 (if (eq external :external)
226 (error "The symbol `~S' is not external in the package ~S." name package))))))
228 (defun read-integer (string)
231 (size (length string)))
233 (let ((elt (char string i)))
236 (setq number (+ (* (or number 0) 10) (digit-char-p elt))))
241 (t (return-from read-integer))))
242 ((and (= i (1- size)) (char= elt #\.)) nil)
243 (t (return-from read-integer)))))
244 (and number (* sign number))))
246 (defun read-float (string)
250 (fractional-part nil)
255 (size (length string))
257 (when (zerop size) (return))
259 (case (char string index)
263 (unless (< index size) (return))
264 ;; Optional integer part
265 (awhen (digit-char-p (char string index))
266 (setq integer-part t)
267 (while (and (< index size)
268 (setq it (digit-char-p (char string index))))
269 (setq number (+ (* number 10) it))
271 (unless (< index size) (return))
272 ;; Decimal point is mandatory if there's no integer part
273 (unless (or integer-part (char= #\. (char string index))) (return))
274 ;; Optional fractional part
275 (when (char= #\. (char string index))
277 (unless (< index size) (return))
278 (awhen (digit-char-p (char string index))
279 (setq fractional-part t)
280 (while (and (< index size)
281 (setq it (digit-char-p (char string index))))
282 (setq number (+ (* number 10) it))
283 (setq divisor (* divisor 10))
285 ;; Either left or right part of the dot must be present
286 (unless (or integer-part fractional-part) (return))
287 ;; Exponent is mandatory if there is no fractional part
288 (when (and (= index size) (not fractional-part)) (return))
289 ;; Optional exponent part
292 (unless (member (string-upcase (string (char string index)))
293 '("E" "S" "F" "D" "L"))
296 (unless (< index size) (return))
297 ;; Optional exponent sign
298 (case (char string index)
300 (#\- (setq exponent-sign -1)
302 (unless (< index size) (return))
304 (let ((value (digit-char-p (char string index))))
305 (unless value (return))
306 (while (and (< index size)
307 (setq value (digit-char-p (char string index))))
308 (setq exponent (+ (* exponent 10) value))
310 (unless (= index size) (return))
311 ;; Everything went ok, we have a float
312 ;; XXX: Use FLOAT when implemented.
313 (/ (* sign (expt 10.0 (* exponent-sign exponent)) number) divisor))))
315 (defun !parse-integer (string junk-allow)
319 (size (length string))
321 ;; Leading whitespace
322 (while (and (< index size)
323 (whitespacep (char string index)))
325 (unless (< index size) (return (values nil 0)))
327 (case (char string 0)
332 (unless (and (< index size)
333 (setq value (digit-char-p (char string index))))
334 (return (values nil index)))
337 (while (< index size)
338 (let ((digit (digit-char-p (char string index))))
339 (unless digit (return))
340 (setq value (+ (* value 10) digit))
342 ;; Trailing whitespace
343 (do ((i index (1+ i)))
344 ((or (= i size) (not (whitespacep (char string i))))
345 (and (= i size) (setq index i))))
348 (values (* sign value) index)
349 (values nil index)))))
352 (defun parse-integer (string &key junk-allowed)
353 (multiple-value-bind (num index)
354 (!parse-integer string junk-allowed)
357 (error "Junk detected."))))
359 (defvar *eof* (gensym))
360 (defun ls-read-1 (stream)
361 (skip-whitespaces-and-comments stream)
362 (let ((ch (%peek-char stream)))
364 ((or (null ch) (char= ch #\)))
371 (list 'quote (ls-read-1 stream)))
374 (list 'backquote (ls-read-1 stream)))
377 (read-string stream))
380 (if (eql (%peek-char stream) #\@)
381 (progn (%read-char stream) (list 'unquote-splicing (ls-read-1 stream)))
382 (list 'unquote (ls-read-1 stream))))
386 (let ((string (read-escaped-until stream #'terminalp)))
387 (or (read-integer string)
389 (read-symbol string)))))))
391 (defun ls-read (stream &optional (eof-error-p t) eof-value)
392 (let ((x (ls-read-1 stream)))
395 (error "End of file")
399 (defun ls-read-from-string (string &optional (eof-error-p t) eof-value)
400 (ls-read (make-string-stream string) eof-error-p eof-value))
403 (defun read-from-string (string &optional (eof-errorp t) eof-value)
404 (ls-read-from-string string eof-errorp eof-value))