3 ;; Copyright (C) 2012, 2013 David Vazquez
4 ;; Copyright (C) 2012 Raimon Grau
6 ;; This program 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 ;; This program 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 this program. 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 skip-whitespaces-and-comments (stream)
62 (skip-whitespaces stream)
63 (setq ch (%peek-char stream))
64 (while (and ch (char= ch #\;))
65 (read-until stream (lambda (x) (char= x #\newline)))
66 (skip-whitespaces stream)
67 (setq ch (%peek-char stream)))))
69 (defun %read-list (stream)
70 (skip-whitespaces-and-comments stream)
71 (let ((ch (%peek-char stream)))
74 (error "Unspected EOF"))
80 (prog1 (ls-read-1 stream)
81 (skip-whitespaces-and-comments stream)
82 (unless (char= (%read-char stream) #\))
83 (error "')' was expected."))))
85 (cons (ls-read-1 stream) (%read-list stream))))))
87 (defun read-string (stream)
90 (setq ch (%read-char stream))
91 (while (not (eql ch #\"))
93 (error "Unexpected EOF"))
95 (setq ch (%read-char stream)))
96 (setq string (concat string (string ch)))
97 (setq ch (%read-char stream)))
100 (defun read-sharp (stream)
102 (ecase (%read-char stream)
104 (list 'function (ls-read-1 stream)))
105 (#\( (list-to-vector (%read-list stream)))
106 (#\: (make-symbol (string-upcase (read-until stream #'terminalp))))
109 (concat (string (%read-char stream))
110 (read-until stream #'terminalp))))
112 ((string= cname "space") (char-code #\space))
113 ((string= cname "tab") (char-code #\tab))
114 ((string= cname "newline") (char-code #\newline))
115 (t (char-code (char cname 0))))))
117 (let ((feature (read-until stream #'terminalp)))
119 ((string= feature "common-lisp")
120 (ls-read-1 stream) ;ignore
122 ((string= feature "jscl")
125 (error "Unknown reader form.")))))))
127 ;;; Parse a string of the form NAME, PACKAGE:NAME or
128 ;;; PACKAGE::NAME and return the name. If the string is of the
129 ;;; form 1) or 3), but the symbol does not exist, it will be created
130 ;;; and interned in that package.
131 (defun read-symbol (string)
132 (let ((size (length string))
133 package name internalp index)
135 (while (and (< index size)
136 (not (char= (char string index) #\:)))
142 (setq package *package*)
147 (setq package "KEYWORD")
148 (setq package (string-upcase (subseq string 0 index))))
150 (when (char= (char string index) #\:)
153 (setq name (subseq string index))))
154 ;; Canonalize symbol name and package
155 (when (not (eq package "JS"))
156 (setq name (string-upcase name)))
157 (setq package (find-package package))
158 ;; TODO: PACKAGE:SYMBOL should signal error if SYMBOL is not an
159 ;; external symbol from PACKAGE.
161 (eq package (find-package "KEYWORD"))
162 (eq package (find-package "JS")))
163 (intern name package)
164 (find-symbol name package))))
166 (defun read-float (string)
170 (fractional-part nil)
175 (size (length string))
177 (when (zerop size) (return))
179 (case (char string index)
183 (unless (< index size) (return))
184 ;; Optional integer part
185 (awhen (digit-char-p (char string index))
186 (setq integer-part t)
187 (while (and (< index size)
188 (setq it (digit-char-p (char string index))))
189 (setq number (+ (* number 10) it))
191 (unless (< index size) (return))
192 ;; Decimal point is mandatory if there's no integer part
193 (unless (or integer-part (char= #\. (char string index))) (return))
194 ;; Optional fractional part
195 (when (char= #\. (char string index))
197 (unless (< index size) (return))
198 (awhen (digit-char-p (char string index))
199 (setq fractional-part t)
200 (while (and (< index size)
201 (setq it (digit-char-p (char string index))))
202 (setq number (+ (* number 10) it))
203 (setq divisor (* divisor 10))
205 ;; Either left or right part of the dot must be present
206 (unless (or integer-part fractional-part) (return))
207 ;; Exponent is mandatory if there is no fractional part
208 (when (and (= index size) (not fractional-part)) (return))
209 ;; Optional exponent part
212 (unless (member (string-upcase (string (char string index)))
213 '("E" "S" "F" "D" "L"))
216 (unless (< index size) (return))
217 ;; Optional exponent sign
218 (case (char string index)
220 (#\- (setq exponent-sign -1)
222 (unless (< index size) (return))
224 (let ((value (digit-char-p (char string index))))
225 (unless value (return))
226 (while (and (< index size)
227 (setq value (digit-char-p (char string index))))
228 (setq exponent (+ (* exponent 10) value))
230 (unless (= index size) (return))
231 ;; Everything went ok, we have a float
232 ;; XXX: Use FLOAT when implemented.
233 (/ (* sign (expt 10.0d0 (* exponent-sign exponent)) number) divisor))))
236 (defun !parse-integer (string junk-allow)
240 (size (length string))
242 ;; Leading whitespace
243 (while (and (< index size)
244 (whitespacep (char string index)))
246 (unless (< index size) (return (values nil 0)))
248 (case (char string 0)
253 (unless (and (< index size)
254 (setq value (digit-char-p (char string index))))
255 (return (values nil index)))
258 (while (< index size)
259 (let ((digit (digit-char-p (char string index))))
260 (unless digit (return))
261 (setq value (+ (* value 10) digit))
263 ;; Trailing whitespace
264 (do ((i index (1+ i)))
265 ((or (= i size) (not (whitespacep (char string i))))
266 (and (= i size) (setq index i))))
269 (values (* sign value) index)
270 (values nil index)))))
273 (defun parse-integer (string &key junk-allowed)
274 (multiple-value-bind (num index)
275 (!parse-integer string junk-allowed)
278 (error "junk detected."))))
280 (defvar *eof* (gensym))
281 (defun ls-read-1 (stream)
282 (skip-whitespaces-and-comments stream)
283 (let ((ch (%peek-char stream)))
285 ((or (null ch) (char= ch #\)))
292 (list 'quote (ls-read-1 stream)))
295 (list 'backquote (ls-read-1 stream)))
298 (read-string stream))
301 (if (eql (%peek-char stream) #\@)
302 (progn (%read-char stream) (list 'unquote-splicing (ls-read-1 stream)))
303 (list 'unquote (ls-read-1 stream))))
307 (let ((string (read-until stream #'terminalp)))
308 (or (values (!parse-integer string nil))
310 (read-symbol string)))))))
312 (defun ls-read (stream &optional (eof-error-p t) eof-value)
313 (let ((x (ls-read-1 stream)))
315 (if eof-error-p (error "EOF") eof-value)
318 (defun ls-read-from-string (string &optional (eof-error-p t) eof-value)
319 (ls-read (make-string-stream string) eof-error-p eof-value))
322 (defun read-from-string (string &optional (eof-errorp t) eof-value)
323 (ls-read-from-string string eof-errorp eof-value))