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 (discard-char stream #\)))
122 (let ((token (concat "." (read-escaped-until stream #'terminalp))))
123 (cons (interpret-token token)
124 (%read-list stream)))))
125 (%read-list stream))))))))))
127 (defun read-string (stream)
130 (setq ch (%read-char stream))
131 (while (not (eql ch #\"))
133 (error "Unexpected EOF"))
135 (setq ch (%read-char stream)))
136 (setq string (concat string (string ch)))
137 (setq ch (%read-char stream)))
140 (defun read-sharp (stream &optional eof-error-p eof-value)
142 (ecase (%read-char stream)
144 (list 'function (ls-read stream)))
145 (#\( (list-to-vector (%read-list stream)))
146 (#\: (make-symbol (string-upcase (read-until stream #'terminalp))))
149 (concat (string (%read-char stream))
150 (read-until stream #'terminalp))))
152 ((string= cname "space") #\space)
153 ((string= cname "tab") #\tab)
154 ((string= cname "newline") #\newline)
155 (t (char cname 0)))))
157 (let ((feature (let ((symbol (ls-read stream)))
158 (unless (symbolp symbol)
159 (error "Invalid feature ~S" symbol))
160 (intern (string symbol) "KEYWORD"))))
164 (ls-read stream eof-error-p eof-value))
166 (ls-read stream eof-error-p eof-value)))))))
170 (dotimes (i (length x))
171 (unless (char= (char x i) #\\)
172 (setq result (concat result (string (char x i))))))
175 (defun escape-all (x)
177 (dotimes (i (length x))
178 (setq result (concat result "\\"))
179 (setq result (concat result (string (char x i)))))
182 (defun string-upcase-noescaped (s)
185 (dotimes (i (length s))
186 (let ((ch (char s i)))
189 (setf last-escape nil)
190 (setf result (concat result (string ch))))
193 (setf result (concat result (string-upcase (string ch))))))))
196 ;;; Parse a string of the form NAME, PACKAGE:NAME or
197 ;;; PACKAGE::NAME and return the name. If the string is of the
198 ;;; form 1) or 3), but the symbol does not exist, it will be created
199 ;;; and interned in that package.
200 (defun read-symbol (string)
201 (let ((size (length string))
202 package name internalp index)
204 (while (and (< index size)
205 (not (char= (char string index) #\:)))
206 (when (char= (char string index) #\\)
213 (setq package *package*)
218 (setq package "KEYWORD")
219 (setq package (string-upcase-noescaped (subseq string 0 index))))
221 (when (char= (char string index) #\:)
224 (setq name (subseq string index))))
225 ;; Canonalize symbol name and package
226 (setq name (if (equal package "JS")
227 (setq name (unescape name))
228 (setq name (string-upcase-noescaped name))))
229 (setq package (find-package package))
231 (eq package (find-package "KEYWORD"))
232 (eq package (find-package "JS")))
233 (intern name package)
234 (multiple-value-bind (symbol external)
235 (find-symbol name package)
236 (if (eq external :external)
238 (error "The symbol `~S' is not external in the package ~S." name package))))))
240 (defun read-integer (string)
243 (size (length string)))
245 (let ((elt (char string i)))
248 (setq number (+ (* (or number 0) 10) (digit-char-p elt))))
253 (t (return-from read-integer))))
254 ((and (= i (1- size)) (char= elt #\.)) nil)
255 (t (return-from read-integer)))))
256 (and number (* sign number))))
258 (defun read-float (string)
262 (fractional-part nil)
267 (size (length string))
269 (when (zerop size) (return))
271 (case (char string index)
275 (unless (< index size) (return))
276 ;; Optional integer part
277 (awhen (digit-char-p (char string index))
278 (setq integer-part t)
279 (while (and (< index size)
280 (setq it (digit-char-p (char string index))))
281 (setq number (+ (* number 10) it))
283 (unless (< index size) (return))
284 ;; Decimal point is mandatory if there's no integer part
285 (unless (or integer-part (char= #\. (char string index))) (return))
286 ;; Optional fractional part
287 (when (char= #\. (char string index))
289 (unless (< index size) (return))
290 (awhen (digit-char-p (char string index))
291 (setq fractional-part t)
292 (while (and (< index size)
293 (setq it (digit-char-p (char string index))))
294 (setq number (+ (* number 10) it))
295 (setq divisor (* divisor 10))
297 ;; Either left or right part of the dot must be present
298 (unless (or integer-part fractional-part) (return))
299 ;; Exponent is mandatory if there is no fractional part
300 (when (and (= index size) (not fractional-part)) (return))
301 ;; Optional exponent part
304 (unless (member (string-upcase (string (char string index)))
305 '("E" "S" "F" "D" "L"))
308 (unless (< index size) (return))
309 ;; Optional exponent sign
310 (case (char string index)
312 (#\- (setq exponent-sign -1)
314 (unless (< index size) (return))
316 (let ((value (digit-char-p (char string index))))
317 (unless value (return))
318 (while (and (< index size)
319 (setq value (digit-char-p (char string index))))
320 (setq exponent (+ (* exponent 10) value))
322 (unless (= index size) (return))
323 ;; Everything went ok, we have a float
324 ;; XXX: Use FLOAT when implemented.
325 (/ (* sign (expt 10.0 (* exponent-sign exponent)) number) divisor))))
327 (defun !parse-integer (string junk-allow)
331 (size (length string))
333 ;; Leading whitespace
334 (while (and (< index size)
335 (whitespacep (char string index)))
337 (unless (< index size) (return (values nil 0)))
339 (case (char string 0)
344 (unless (and (< index size)
345 (setq value (digit-char-p (char string index))))
346 (return (values nil index)))
349 (while (< index size)
350 (let ((digit (digit-char-p (char string index))))
351 (unless digit (return))
352 (setq value (+ (* value 10) digit))
354 ;; Trailing whitespace
355 (do ((i index (1+ i)))
356 ((or (= i size) (not (whitespacep (char string i))))
357 (and (= i size) (setq index i))))
360 (values (* sign value) index)
361 (values nil index)))))
364 (defun parse-integer (string &key junk-allowed)
365 (multiple-value-bind (num index)
366 (!parse-integer string junk-allowed)
369 (error "Junk detected."))))
372 (defun interpret-token (string)
373 (or (read-integer string)
375 (read-symbol string)))
377 (defun ls-read (stream &optional (eof-error-p t) eof-value)
378 (skip-whitespaces-and-comments stream)
379 (let ((ch (%peek-char stream)))
381 ((or (null ch) (char= ch #\)))
383 (error "End of file")
390 (list 'quote (ls-read stream)))
393 (list 'backquote (ls-read stream)))
396 (read-string stream))
399 (if (eql (%peek-char stream) #\@)
400 (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
401 (list 'unquote (ls-read stream))))
405 (let ((string (read-escaped-until stream #'terminalp)))
406 (interpret-token string))))))
408 (defun ls-read-from-string (string &optional (eof-error-p t) eof-value)
409 (ls-read (make-string-stream string) eof-error-p eof-value))
412 (defun read-from-string (string &optional (eof-errorp t) eof-value)
413 (ls-read-from-string string eof-errorp eof-value))