9d0ec4615625a75de11c582a1b249ca72f74a0ce
[jscl.git] / src / read.lisp
1 ;;; read.lisp --- 
2
3 ;; Copyright (C) 2012, 2013 David Vazquez
4 ;; Copyright (C) 2012 Raimon Grau
5
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.
10 ;;
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.
15 ;;
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/>.
18
19
20 ;;;; Reader
21
22 ;;; The Lisp reader, parse strings and return Lisp objects. The main
23 ;;; entry points are `ls-read' and `ls-read-from-string'.
24
25 (defun make-string-stream (string)
26   (cons string 0))
27
28 (defun %peek-char (stream)
29   (and (< (cdr stream) (length (car stream)))
30        (char (car stream) (cdr stream))))
31
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))))))
36
37 (defun whitespacep (ch)
38   (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
39
40 (defun skip-whitespaces (stream)
41   (let (ch)
42     (setq ch (%peek-char stream))
43     (while (and ch (whitespacep ch))
44       (%read-char stream)
45       (setq ch (%peek-char stream)))))
46
47 (defun terminalp (ch)
48   (or (null ch) (whitespacep ch) (char= #\" ch) (char= #\) ch) (char= #\( ch)))
49
50 (defun read-until (stream func)
51   (let ((string "")
52         (ch))
53     (setq ch (%peek-char stream))
54     (while (and ch (not (funcall func ch)))
55       (setq string (concat string (string ch)))
56       (%read-char stream)
57       (setq ch (%peek-char stream)))
58     string))
59
60 (defun read-escaped-until (stream func)
61   (let ((string "")
62         (ch (%peek-char stream))
63         (multi-escape nil))
64     (while (and ch (or multi-escape (not (funcall func ch))))
65       (cond
66         ((char= ch #\|)
67          (if multi-escape
68              (setf multi-escape nil)
69              (setf multi-escape t)))
70         ((char= ch #\\)
71          (%read-char stream)
72          (setf ch (%peek-char stream))
73          (setf string (concat string "\\" (string ch))))
74         (t
75          (if multi-escape
76              (setf string (concat string "\\" (string ch)))
77              (setf string (concat string (string ch))))))
78       (%read-char stream)
79       (setf ch (%peek-char stream)))
80     string))
81
82 (defun skip-whitespaces-and-comments (stream)
83   (let (ch)
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)))))
90
91 (defun discard-char (stream expected)
92   (let ((ch (%read-char stream)))
93     (when (null ch)
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))))
97
98 (defun %read-list (stream)
99   (skip-whitespaces-and-comments stream)
100   (let ((ch (%peek-char stream)))
101     (cond
102       ((null ch)
103        (error "Unspected EOF"))
104       ((char= ch #\))
105        (discard-char stream #\))
106        nil)
107       (t
108        (let* ((eof (gensym))
109               (next (ls-read stream nil eof)))
110          (skip-whitespaces-and-comments stream)
111          (cond
112            ((eq next eof)
113             (discard-char stream #\)))
114            (t
115             (cons next
116                   (if (char= (%peek-char stream) #\.)
117                       (progn
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                               (let ((ch (%peek-char stream)))
123                                 (if (or (null ch) (char= #\) ch))
124                                     (discard-char stream #\))
125                                     (error "Multiple objects following . in a list"))))
126                             (let ((token (concat "." (read-escaped-until stream #'terminalp))))
127                               (cons (interpret-token token)
128                                     (%read-list stream)))))
129                       (%read-list stream))))))))))
130
131 (defun read-string (stream)
132   (let ((string "")
133         (ch nil))
134     (setq ch (%read-char stream))
135     (while (not (eql ch #\"))
136       (when (null ch)
137         (error "Unexpected EOF"))
138       (when (eql ch #\\)
139         (setq ch (%read-char stream)))
140       (setq string (concat string (string ch)))
141       (setq ch (%read-char stream)))
142     string))
143
144 (defun read-sharp (stream &optional eof-error-p eof-value)
145   (%read-char stream)
146   (ecase (%read-char stream)
147     (#\'
148      (list 'function (ls-read stream)))
149     (#\( (list-to-vector (%read-list stream)))
150     (#\: (make-symbol
151           (unescape-token
152            (string-upcase-noescaped
153             (read-escaped-until stream #'terminalp)))))
154     (#\\
155      (let ((cname
156             (concat (string (%read-char stream))
157                     (read-until stream #'terminalp))))
158        (cond
159          ((string= cname "space") #\space)
160          ((string= cname "tab") #\tab)
161          ((string= cname "newline") #\newline)
162          (t (char cname 0)))))
163     (#\+
164      (let ((feature (let ((symbol (ls-read stream)))
165                       (unless (symbolp symbol)
166                         (error "Invalid feature ~S" symbol))
167                       (intern (string symbol) "KEYWORD"))))
168        (ecase feature
169          (:common-lisp
170           (ls-read stream)
171           (ls-read stream eof-error-p eof-value))
172          (:jscl
173           (ls-read stream eof-error-p eof-value))
174          (:nil
175           (ls-read stream)
176           (ls-read stream eof-error-p eof-value)))))))
177
178 (defun unescape-token (x)
179   (let ((result ""))
180     (dotimes (i (length x))
181       (unless (char= (char x i) #\\)
182         (setq result (concat result (string (char x i))))))
183     result))
184
185 (defun string-upcase-noescaped (s)
186   (let ((result "")
187         (last-escape nil))
188     (dotimes (i (length s))
189       (let ((ch (char s i)))
190         (if last-escape
191            (progn
192               (setf last-escape nil)
193               (setf result (concat result (string ch))))
194             (if (char= ch #\\)
195                 (setf last-escape t)
196                 (setf result (concat result (string-upcase (string ch))))))))
197     result))
198
199 ;;; Parse a string of the form NAME, PACKAGE:NAME or
200 ;;; PACKAGE::NAME and return the name. If the string is of the
201 ;;; form 1) or 3), but the symbol does not exist, it will be created
202 ;;; and interned in that package.
203 (defun read-symbol (string)
204   (let ((size (length string))
205         package name internalp index)
206     (setq index 0)
207     (while (and (< index size)
208                 (not (char= (char string index) #\:)))
209       (when (char= (char string index) #\\)
210         (incf index))
211       (incf index))
212     (cond
213       ;; No package prefix
214       ((= index size)
215        (setq name string)
216        (setq package *package*)
217        (setq internalp t))
218       (t
219        ;; Package prefix
220        (if (zerop index)
221            (setq package "KEYWORD")
222            (setq package (string-upcase-noescaped (subseq string 0 index))))
223        (incf index)
224        (when (char= (char string index) #\:)
225          (setq internalp t)
226          (incf index))
227        (setq name (subseq string index))))
228     ;; Canonalize symbol name and package
229     (setq name (if (equal package "JS")
230                    (setq name (unescape-token name))
231                    (setq name (string-upcase-noescaped name))))
232     (setq package (find-package package))
233     (if (or internalp
234             (eq package (find-package "KEYWORD"))
235             (eq package (find-package "JS")))
236         (intern name package)
237         (multiple-value-bind (symbol external)
238             (find-symbol name package)
239           (if (eq external :external)
240               symbol
241               (error "The symbol `~S' is not external in the package ~S." name package))))))
242
243 (defun read-integer (string)
244   (let ((sign 1)
245         (number nil)
246         (size (length string)))
247     (dotimes (i size)
248       (let ((elt (char string i)))
249         (cond
250           ((digit-char-p elt)
251            (setq number (+ (* (or number 0) 10) (digit-char-p elt))))
252           ((zerop i)
253            (case elt
254              (#\+ nil)
255              (#\- (setq sign -1))
256              (t (return-from read-integer))))
257           ((and (= i (1- size)) (char= elt #\.)) nil)
258           (t (return-from read-integer)))))
259     (and number (* sign number))))
260
261 (defun read-float (string)
262   (block nil
263     (let ((sign 1)
264           (integer-part nil)
265           (fractional-part nil)
266           (number 0)
267           (divisor 1)
268           (exponent-sign 1)
269           (exponent 0)
270           (size (length string))
271           (index 0))
272       (when (zerop size) (return))
273       ;; Optional sign
274       (case (char string index)
275         (#\+ (incf index))
276         (#\- (setq sign -1)
277              (incf index)))
278       (unless (< index size) (return))
279       ;; Optional integer part
280       (awhen (digit-char-p (char string index))
281         (setq integer-part t)
282         (while (and (< index size)
283                     (setq it (digit-char-p (char string index))))
284           (setq number (+ (* number 10) it))
285           (incf index)))
286       (unless (< index size) (return))
287       ;; Decimal point is mandatory if there's no integer part
288       (unless (or integer-part (char= #\. (char string index))) (return))
289       ;; Optional fractional part
290       (when (char= #\. (char string index))
291         (incf index)
292         (unless (< index size) (return))
293         (awhen (digit-char-p (char string index))
294           (setq fractional-part t)
295           (while (and (< index size)
296                       (setq it (digit-char-p (char string index))))
297             (setq number (+ (* number 10) it))
298             (setq divisor (* divisor 10))
299             (incf index))))
300       ;; Either left or right part of the dot must be present
301       (unless (or integer-part fractional-part) (return))
302       ;; Exponent is mandatory if there is no fractional part
303       (when (and (= index size) (not fractional-part)) (return))
304       ;; Optional exponent part
305       (when (< index size)
306         ;; Exponent-marker
307         (unless (find (char-upcase (char string index)) "ESFDL")
308           (return))
309         (incf index)
310         (unless (< index size) (return))
311         ;; Optional exponent sign
312         (case (char string index)
313           (#\+ (incf index))
314           (#\- (setq exponent-sign -1)
315                (incf index)))
316         (unless (< index size) (return))
317         ;; Exponent digits
318         (let ((value (digit-char-p (char string index))))
319           (unless value (return))
320           (while (and (< index size)
321                       (setq value (digit-char-p (char string index))))
322             (setq exponent (+ (* exponent 10) value))
323             (incf index))))
324       (unless (= index size) (return))
325       ;; Everything went ok, we have a float
326       ;; XXX: Use FLOAT when implemented.
327       (/ (* sign (expt 10.0 (* exponent-sign exponent)) number) divisor))))
328
329 (defun !parse-integer (string junk-allow)
330   (block nil
331     (let ((value 0)
332           (index 0)
333           (size (length string))
334           (sign 1))
335       ;; Leading whitespace
336       (while (and (< index size)
337                   (whitespacep (char string index)))
338         (incf index))
339       (unless (< index size) (return (values nil 0)))
340       ;; Optional sign
341       (case (char string 0)
342         (#\+ (incf index))
343         (#\- (setq sign -1)
344              (incf index)))
345       ;; First digit
346       (unless (and (< index size)
347                    (setq value (digit-char-p (char string index))))
348         (return (values nil index)))
349       (incf index)
350       ;; Other digits
351       (while (< index size)
352         (let ((digit (digit-char-p (char string index))))
353           (unless digit (return))
354           (setq value (+ (* value 10) digit))
355           (incf index)))
356       ;; Trailing whitespace
357       (do ((i index (1+ i)))
358           ((or (= i size) (not (whitespacep (char string i))))
359            (and (= i size) (setq index i))))
360       (if (or junk-allow
361               (= index size))
362           (values (* sign value) index)
363           (values nil index)))))
364
365 #+jscl
366 (defun parse-integer (string &key junk-allowed)
367   (multiple-value-bind (num index)
368       (!parse-integer string junk-allowed)
369     (if num
370         (values num index)
371         (error "Junk detected."))))
372
373
374 (defun interpret-token (string)
375   (or (read-integer string)
376       (read-float string)
377       (read-symbol string)))
378
379 (defun ls-read (stream  &optional (eof-error-p t) eof-value)
380   (skip-whitespaces-and-comments stream)
381   (let ((ch (%peek-char stream)))
382     (cond
383       ((or (null ch) (char= ch #\)))
384        (if eof-error-p
385            (error "End of file")
386            eof-value))
387       ((char= ch #\()
388        (%read-char stream)
389        (%read-list stream))
390       ((char= ch #\')
391        (%read-char stream)
392        (list 'quote (ls-read stream)))
393       ((char= ch #\`)
394        (%read-char stream)
395        (list 'backquote (ls-read stream)))
396       ((char= ch #\")
397        (%read-char stream)
398        (read-string stream))
399       ((char= ch #\,)
400        (%read-char stream)
401        (if (eql (%peek-char stream) #\@)
402            (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
403            (list 'unquote (ls-read stream))))
404       ((char= ch #\#)
405        (read-sharp stream))
406       (t
407        (let ((string (read-escaped-until stream #'terminalp)))
408          (interpret-token string))))))
409
410 (defun ls-read-from-string (string &optional (eof-error-p t) eof-value)
411   (ls-read (make-string-stream string) eof-error-p eof-value))
412
413 #+jscl
414 (defun read-from-string (string &optional (eof-errorp t) eof-value)
415   (ls-read-from-string string eof-errorp eof-value))