| 1 |
;;;-*- Mode:LISP; Package:CHAOS; Base:10; Syntax:Common-lisp -*- |
| 2 |
;;; |
| 3 |
;;; Copyright (c) 2000-2017, Toshimi Sawada. All rights reserved. |
| 4 |
;;; |
| 5 |
;;; Redistribution and use in source and binary forms, with or without |
| 6 |
;;; modification, are permitted provided that the following conditions |
| 7 |
;;; are met: |
| 8 |
;;; |
| 9 |
;;; * Redistributions of source code must retain the above copyright |
| 10 |
;;; notice, this list of conditions and the following disclaimer. |
| 11 |
;;; |
| 12 |
;;; * Redistributions in binary form must reproduce the above |
| 13 |
;;; copyright notice, this list of conditions and the following |
| 14 |
;;; disclaimer in the documentation and/or other materials |
| 15 |
;;; provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED |
| 18 |
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 |
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 21 |
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 23 |
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 |
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 |
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
;;; |
| 29 |
(in-package :chaos) |
| 30 |
#|============================================================================== |
| 31 |
System:Chaos |
| 32 |
Module:comlib |
| 33 |
File: lex.lisp |
| 34 |
==============================================================================|# |
| 35 |
#-:chaos-debug |
| 36 |
(declaim (optimize (speed 3) (safety 0) #-GCL (debug 0))) |
| 37 |
#+:chaos-debug |
| 38 |
(declaim (optimize (speed 1) (safety 3) #-GCL (debug 3))) |
| 39 |
|
| 40 |
;;; ************ |
| 41 |
;;; TOKEN READER________________________________________________________________ |
| 42 |
;;; ************ |
| 43 |
|
| 44 |
;;; ** NOTES ON GENERAL ASSUMPTIONS AND LIMITATIONS.**************************** |
| 45 |
;;; <TODO> |
| 46 |
;;; |
| 47 |
|
| 48 |
;;;============================================================================= |
| 49 |
;;; *** CHAOS BUILTIN LEXICAL CATEGORIES *** |
| 50 |
;;;============================================================================= |
| 51 |
|
| 52 |
(eval-when (:execute :compile-toplevel :load-toplevel) |
| 53 |
(defvar *builtin-cats* (make-hash-table :test #'eq :size 50)) |
| 54 |
(defmacro define-builtin-token (token-name id-value) |
| 55 |
` (eval-when (:execute :compile-toplevel :load-toplevel) |
| 56 |
(defparameter ,token-name ,id-value) |
| 57 |
(setf (gethash ,id-value *builtin-cats*) t))) |
| 58 |
) |
| 59 |
|
| 60 |
;;; Chaos BUILTIN LEXICAL CATEGORIES. |
| 61 |
;;;----------------------------------------------------------------------------- |
| 62 |
|
| 63 |
(defmacro declare-bi-token (sym) |
| 64 |
`(setf (get ,sym ':bi-token) t)) |
| 65 |
|
| 66 |
;;; The followings are builtin lexical units of Chaos system. |
| 67 |
|
| 68 |
;;; Lisp expression. |
| 69 |
(defparameter *lisp-escape-char* '|!|) |
| 70 |
(defparameter .lisp-simple-sexpr. '%SLisp) |
| 71 |
(defparameter .lisp-general-sexpr. '%GLisp) |
| 72 |
;;; Chaos Value |
| 73 |
(defparameter *chaos-escape-char* '|%|) |
| 74 |
(defparameter .chaos-value-sexpr. '|%Chaos|) |
| 75 |
|
| 76 |
;;; String&Character |
| 77 |
(defparameter .String-token. '|String|) |
| 78 |
;; (defparameter .Char-token. '|Character|) |
| 79 |
|
| 80 |
(eval-when (:execute :load-toplevel) |
| 81 |
(declare-bi-token '%SLisp) |
| 82 |
(declare-bi-token '%GLisp) |
| 83 |
(declare-bi-token '|String|) |
| 84 |
(declare-bi-token '|%Chaos|) |
| 85 |
(declare-bi-token '|#\||) |
| 86 |
(declare-bi-token '|\|#|) |
| 87 |
) |
| 88 |
|
| 89 |
;;;============================================================================= |
| 90 |
;;; LOW LEVEL READER |
| 91 |
;;;============================================================================= |
| 92 |
|
| 93 |
;;; Syntactic properties of characters:_________________________________________ |
| 94 |
;;; At the lowest level, each input character has a syntactic property which is |
| 95 |
;;; one of the followings: |
| 96 |
;;; - 'space : means white space, i.e., normaly the chars chars except |
| 97 |
;;; cr/newline have this property. |
| 98 |
;;; - 'return : cr & newline. |
| 99 |
;;; ** 'white and 'return are always treated as token separators. |
| 100 |
;;; There are some chars which are treated as they stand, i.e., each of which |
| 101 |
;;; construct a token by itself. it is a resposibility of higher procs to treate |
| 102 |
;;; these chars in different ways. In such case, the value of the syntactic |
| 103 |
;;; property is set to a symbol other than 'space, 'return, or 'nil. |
| 104 |
;;; The property 'nil means that a sequnce of these char construct a token. |
| 105 |
;;; The global *reader-read-table* holds the above infos, `!init-read-table' |
| 106 |
;;; sets the property for each chars according to its arguments. |
| 107 |
;;; |
| 108 |
(declaim (type (integer 0 256) .reader-char-code-limit.)) |
| 109 |
(defparameter .reader-char-code-limit. 255) |
| 110 |
#-GCL (declaim (type simple-vector *reader-read-table*)) |
| 111 |
#+GCL (declaim (type vector *reader-read-table*)) |
| 112 |
(defvar *reader-read-table*) |
| 113 |
(eval-when (:execute :load-toplevel) |
| 114 |
(setf *reader-read-table* (make-array (list .reader-char-code-limit.) |
| 115 |
:initial-element nil))) |
| 116 |
|
| 117 |
(defmacro !set-syntax (ch val) |
| 118 |
`(setf (aref *reader-read-table* (the fixnum (char-code ,ch))) ,val)) |
| 119 |
|
| 120 |
(defun lex-show-delimiters (stream) |
| 121 |
(dotimes (x .reader-char-code-limit.) |
| 122 |
(let ((syntax (aref *reader-read-table* x))) |
| 123 |
(when syntax |
| 124 |
(format stream "~%~S : ~S" (code-char x) syntax))))) |
| 125 |
|
| 126 |
;;; !INIT-READ-TABLE : List[Char] List[Char] List[Char] -> Void |
| 127 |
;;; initialize Chaos read table. |
| 128 |
;;; space : list of `space' characters |
| 129 |
;;; return : list of `return' characters |
| 130 |
;;; single : list of self terminatig characters |
| 131 |
;;; |
| 132 |
(defun !init-read-table (space return single) |
| 133 |
(declare (type list space return single) |
| 134 |
(values t)) |
| 135 |
#|| |
| 136 |
(do ((i 0 (1+ i))) |
| 137 |
((= i .reader-char-code-limit.)) |
| 138 |
(declare (type (integer 0 256) i)) |
| 139 |
(setf (aref *reader-read-table* i) nil)) |
| 140 |
||# |
| 141 |
(dolist (char space) |
| 142 |
(!set-syntax char 'space)) |
| 143 |
(dolist (char return) |
| 144 |
(!set-syntax char 'return)) |
| 145 |
(dolist (c-c single) |
| 146 |
(!set-syntax (car c-c) (cdr c-c)))) |
| 147 |
|
| 148 |
(defmacro reader-get-syntax (ch) |
| 149 |
`(if (< (char-code ,ch) .reader-char-code-limit.) |
| 150 |
(aref *reader-read-table* (the fixnum (char-code ,ch))) |
| 151 |
nil)) |
| 152 |
|
| 153 |
(defmacro reader-valid-char-code (n) |
| 154 |
(once-only (n) |
| 155 |
`(and (<= 0 ,n) (<= ,n .reader-char-code-limit.)))) |
| 156 |
|
| 157 |
;;; !SET-SINGLE-READER list-of-chars |
| 158 |
;;; make a list of characters be single character symbols (self terminating) |
| 159 |
;;; in the reader. |
| 160 |
;;; returns list of original status. the return valu can be used for an argument |
| 161 |
;;; of `!set-reader' for recovering the modifications. |
| 162 |
;;; |
| 163 |
(defun !set-single-reader (l) |
| 164 |
(declare (type list l) |
| 165 |
(values t)) |
| 166 |
(mapcar #'(lambda (x) |
| 167 |
(declare (type (or simple-string character) x)) |
| 168 |
(let ((chr (if (and (stringp x) |
| 169 |
(= (length x) 1)) |
| 170 |
(char (the string x) 0) |
| 171 |
(if (characterp x) |
| 172 |
x |
| 173 |
(with-output-chaos-error ('invalid-str) |
| 174 |
(format t "delimiter must be a single character, but ~a is given" x)))))) |
| 175 |
(prog1 |
| 176 |
(cons chr (reader-get-syntax chr)) |
| 177 |
;; (print chr) |
| 178 |
(!set-syntax chr (intern (string x)))))) |
| 179 |
l)) |
| 180 |
|
| 181 |
;;; !SET-READER list-of-chars |
| 182 |
;;; modify a sequence of characters for syntax as given by associated values. |
| 183 |
;;; useful for restoring the old properties of chars modified by !set-single-reader. |
| 184 |
;;; |
| 185 |
(defun !set-reader (l) |
| 186 |
(declare (type list l) |
| 187 |
(values t)) |
| 188 |
(mapc #'(lambda (x) |
| 189 |
(declare (type list x)) |
| 190 |
(let ((s (car x))) |
| 191 |
(declare (type (or simple-string character) s)) |
| 192 |
(!set-syntax |
| 193 |
(if (stringp s) |
| 194 |
(char (the string s) 0) |
| 195 |
s) |
| 196 |
(cdr x)))) |
| 197 |
l)) |
| 198 |
|
| 199 |
;;; !READ-IN |
| 200 |
;;; read a token iff the last input is not processed yet, |
| 201 |
;;; i.e. *reader-input* == *reader-void*. |
| 202 |
;;; the token is set to *reader-input*. |
| 203 |
;;; |
| 204 |
;;; *reader-input* : token buffer. |
| 205 |
;;; |
| 206 |
(defvar *reader-input* nil) |
| 207 |
|
| 208 |
;;; *reder-void* is the marker that indicates the buffered token is |
| 209 |
;;; consumed, thus we should read a token. |
| 210 |
;;; |
| 211 |
(defparameter *reader-void* '(void)) |
| 212 |
(defvar *token-buf* nil) |
| 213 |
(defvar *last-token* *reader-void*) |
| 214 |
|
| 215 |
;;; The eof value. |
| 216 |
(eval-when (:execute :compile-toplevel :load-toplevel) |
| 217 |
(defparameter *lex-eof* (cons nil nil)) |
| 218 |
) |
| 219 |
|
| 220 |
;;; |
| 221 |
(defmacro !read-in () |
| 222 |
` (when (eq *reader-input* *reader-void*) |
| 223 |
(setq *reader-input* (read-sym)))) |
| 224 |
|
| 225 |
;;; !READ-DISCARD |
| 226 |
;;; discard the last input token. |
| 227 |
;;; |
| 228 |
(defmacro !read-discard () |
| 229 |
`(progn ;; (clear-input) |
| 230 |
;; (setq *token-buf* nil) |
| 231 |
(setq *reader-input* *reader-void*))) |
| 232 |
|
| 233 |
;;; !READ-SYM |
| 234 |
;;; read a token. |
| 235 |
;;; |
| 236 |
(defun !read-sym () |
| 237 |
(cond ((eq *reader-input* *reader-void*) (read-sym)) |
| 238 |
(t (prog1 *reader-input* |
| 239 |
(!read-discard))))) |
| 240 |
|
| 241 |
;;; |
| 242 |
(defun test-lex (file) |
| 243 |
(!lex-read-init) |
| 244 |
(with-open-file (str file :direction :input) |
| 245 |
(let ((tok nil) |
| 246 |
(*standard-input* str)) |
| 247 |
(while-not (eq tok *lex-eof*) |
| 248 |
(setf tok (!read-sym)) |
| 249 |
(print tok))))) |
| 250 |
|
| 251 |
;;; SIMPLE READER_______________________________________________________________ |
| 252 |
;;; |
| 253 |
|
| 254 |
;;; READ-LINES (stream) |
| 255 |
;;; |
| 256 |
(defparameter newline-string (string #\newline)) |
| 257 |
(defparameter line-continue-char #\;) |
| 258 |
|
| 259 |
(defparameter .read-line-eof. "") |
| 260 |
|
| 261 |
(declaim (special *live-newline*)) |
| 262 |
(defvar *live-newline* nil) |
| 263 |
|
| 264 |
(defmacro add-new-line (str) |
| 265 |
`(concatenate 'string ,str newline-string)) |
| 266 |
|
| 267 |
(defun read-lines (&optional (stream *standard-input*)) |
| 268 |
(declare (type stream stream) |
| 269 |
(values simple-string fixnum)) |
| 270 |
(let (res |
| 271 |
line |
| 272 |
(ll 0) |
| 273 |
l-char |
| 274 |
(l-total 0)) |
| 275 |
(declare (type fixnum l-total ll)) |
| 276 |
(loop (setq line (read-line stream nil .read-line-eof.)) |
| 277 |
(when (eq line .read-line-eof.) (return)) |
| 278 |
(when (<= (setq ll (length (the simple-string line))) 0) |
| 279 |
(return)) |
| 280 |
(incf l-total ll) |
| 281 |
(decf ll) |
| 282 |
(setq l-char (char line ll)) |
| 283 |
(if (char= line-continue-char (the character l-char)) |
| 284 |
(progn |
| 285 |
(setq res (concatenate 'string res |
| 286 |
(setq line (subseq (the simple-string line) |
| 287 |
0 ll)) |
| 288 |
newline-string |
| 289 |
)) |
| 290 |
;; (decf l-total) |
| 291 |
(when (at-top-level) |
| 292 |
(princ "> ") |
| 293 |
(force-output))) |
| 294 |
(progn |
| 295 |
(setq res (concatenate 'string |
| 296 |
res |
| 297 |
(if (char= #\. (the character l-char)) |
| 298 |
(progn |
| 299 |
;; (decf l-total) |
| 300 |
(subseq line 0 ll)) |
| 301 |
line))) |
| 302 |
(return)))) |
| 303 |
(if (eq line .read-line-eof.) |
| 304 |
(values *lex-eof* 0) |
| 305 |
(let ((str (if res |
| 306 |
(if *live-newline* |
| 307 |
(add-new-line res) |
| 308 |
res) |
| 309 |
""))) |
| 310 |
(values str (length str)))))) |
| 311 |
|
| 312 |
;;; the global .reader-ch. holds the last char read. |
| 313 |
;;; if the character has a property other than 'nil, the property value is set, |
| 314 |
;;; otherwise the character itself is set. |
| 315 |
;;; |
| 316 |
(defvar .reader-ch. 'space) |
| 317 |
|
| 318 |
;;; the special .escape-char. holds a character which is used as escape |
| 319 |
;;; character, i.e., the preceding char is treated as is. |
| 320 |
;;; |
| 321 |
(declaim (special .escape-char.)) |
| 322 |
(defvar .default-escape-char. #\\) |
| 323 |
|
| 324 |
;;; |
| 325 |
(defparameter control-d #\Eot) |
| 326 |
(defparameter control-d-string "") |
| 327 |
(defparameter input-escape #\esc) |
| 328 |
(defparameter input-escape-string "") |
| 329 |
|
| 330 |
(defmacro see-ctrl-d () |
| 331 |
`(eq .reader-ch. control-d)) |
| 332 |
|
| 333 |
(defmacro reader-is-at-eof () |
| 334 |
`(eq *lex-eof* *reader-input*)) |
| 335 |
|
| 336 |
(defmacro at-eof () `(or (see-ctrl-d) (eq *lex-eof* .reader-ch.))) |
| 337 |
|
| 338 |
(defmacro at-eof-or-control-d () |
| 339 |
`(or (at-eof) |
| 340 |
(equal *reader-input* control-d-string))) |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
(defmacro see-input-escape () |
| 345 |
`(eq .reader-ch. input-escape)) |
| 346 |
|
| 347 |
(defun str-match? (x y) |
| 348 |
(declare (type t x) |
| 349 |
(type (or symbol simple-string) y) |
| 350 |
(values (or null t))) |
| 351 |
(or (eq x y) |
| 352 |
(and (stringp x) |
| 353 |
(string= (the simple-string x) |
| 354 |
(if (stringp y) |
| 355 |
(the simple-string y) |
| 356 |
(string-downcase (string (the symbol y)))))))) |
| 357 |
|
| 358 |
(defun lex-string-match(x y) |
| 359 |
(declare (type t x) |
| 360 |
(type (or atom list) y) |
| 361 |
(values (or null t))) |
| 362 |
(if (atom y) |
| 363 |
(str-match? x y) |
| 364 |
(member x y :test #'str-match?))) |
| 365 |
|
| 366 |
;;; READER-GET-CHAR : STREAM |
| 367 |
;;; reads a one character from stream, set .reader-ch. handling ESCAPE sequence. |
| 368 |
;;; |
| 369 |
(declaim (special .reader-escape.)) |
| 370 |
(defvar .reader-escape. nil) ; flags indicating we are now in `escaped' |
| 371 |
; status. |
| 372 |
|
| 373 |
;; (defvar .read-buffer. nil) |
| 374 |
;; (defvar .read-pos. 0) |
| 375 |
(defvar .newline-count. 0) |
| 376 |
(defvar *last-newline* nil) |
| 377 |
|
| 378 |
(defparameter eof-char control-d) |
| 379 |
|
| 380 |
(defun reader-get-char (stream) |
| 381 |
(declare (type stream stream) |
| 382 |
(values t)) |
| 383 |
(let ((inch (read-char stream nil *lex-eof*))) |
| 384 |
(cond ((eq inch *lex-eof*) |
| 385 |
(setf .reader-ch. *lex-eof*)) |
| 386 |
#|| |
| 387 |
(.reader-escape. |
| 388 |
(setf .reader-ch. inch)) |
| 389 |
((char= .escape-char. inch) |
| 390 |
(let ((.reader-escape. t)) |
| 391 |
(setf .reader-ch. 'space) |
| 392 |
(reader-get-char stream))) |
| 393 |
||# |
| 394 |
(t (unless *chaos-input-source* |
| 395 |
;; interactive session |
| 396 |
(if (and (char= inch #\newline) |
| 397 |
*last-newline*) |
| 398 |
(incf .newline-count.) |
| 399 |
(if (char= inch #\newline) |
| 400 |
(setq *last-newline* t) |
| 401 |
(setf .newline-count. 0 |
| 402 |
*last-newline* nil))) |
| 403 |
(when (> .newline-count. 2) |
| 404 |
(!read-discard) |
| 405 |
(clear-input) |
| 406 |
(setq *last-newline* nil) |
| 407 |
(setq .newline-count. 0) |
| 408 |
(throw :aborting-read :aborting-read))) |
| 409 |
;; |
| 410 |
(let ((val (reader-get-syntax inch))) |
| 411 |
(setf .reader-ch. (if val val inch))))))) |
| 412 |
|
| 413 |
; (defun reader-get-char (stream) |
| 414 |
; (declare (type stream stream) |
| 415 |
; (values t)) |
| 416 |
; (let ((inch (read-char stream nil *lex-eof*))) |
| 417 |
; (cond ((eq inch *lex-eof*) |
| 418 |
; (setf .reader-ch. *lex-eof*)) |
| 419 |
; #|| |
| 420 |
; (.reader-escape. |
| 421 |
; (setf .reader-ch. inch)) |
| 422 |
; ((char= .escape-char. inch) |
| 423 |
; (let ((.reader-escape. t)) |
| 424 |
; (setf .reader-ch. 'space) |
| 425 |
; (reader-get-char stream))) |
| 426 |
; ||# |
| 427 |
; (t (let ((val (reader-get-syntax inch))) |
| 428 |
; (setf .reader-ch. (if val val inch))))))) |
| 429 |
|
| 430 |
;;; READ-LEXICON : STREAM -> TOKEN |
| 431 |
;;; read a lexicon. |
| 432 |
;;; |
| 433 |
;;; implementation limit: a lexicon must be of length less than or equal to 256. |
| 434 |
;;; |
| 435 |
(declaim (type simple-string .reader-buf.)) |
| 436 |
(defvar .reader-buf. (make-string 256)) |
| 437 |
(defparameter .chaos-simple-LISP-keyword. "#!") |
| 438 |
(defparameter .chaos-general-LISP-keyword. "#!!") |
| 439 |
(defparameter .chaos-value-keyword. "#%") |
| 440 |
(defparameter .ml-begin-char. #\#) |
| 441 |
(defparameter .ml-end-char. #\|) |
| 442 |
|
| 443 |
(defvar .lex-inner-multi-comment. nil) |
| 444 |
|
| 445 |
(defun read-lexicon (&optional (stream *standard-input*)) |
| 446 |
(declare (type stream stream)) |
| 447 |
(let ((p -1) |
| 448 |
res) |
| 449 |
(declare (type fixnum p) |
| 450 |
(type (or symbol list simple-string) res)) |
| 451 |
(setq res |
| 452 |
(loop (cond ((member .reader-ch. '(#\Rubout #\Backspace)) |
| 453 |
(if (<= 0 p) |
| 454 |
(decf p 1))) |
| 455 |
((characterp .reader-ch.) |
| 456 |
(incf p) |
| 457 |
(setf (aref .reader-buf. p) .reader-ch.)) |
| 458 |
(t (let ((c (string .reader-ch.))) |
| 459 |
(setq .reader-ch. 'space) |
| 460 |
(return c)))) |
| 461 |
(reader-get-char stream) |
| 462 |
(when (at-eof) |
| 463 |
(if (<= 0 p) |
| 464 |
(progn (setq .reader-ch. 'space) |
| 465 |
(return (subseq .reader-buf. 0 (1+ p)))) |
| 466 |
(return *lex-eof*))) |
| 467 |
(when (symbolp .reader-ch.) |
| 468 |
(return (subseq .reader-buf. 0 (1+ p)))) |
| 469 |
)) |
| 470 |
;; |
| 471 |
(lex-consider-token res))) |
| 472 |
|
| 473 |
(defun lex-consider-token (tok) |
| 474 |
(declare (type t tok)) |
| 475 |
(if (equal .chaos-simple-LISP-keyword. tok) |
| 476 |
(progn (reader-suppress-ch tok) (list .lisp-simple-sexpr. (read))) |
| 477 |
(if (equal .chaos-general-lisp-keyword. tok) |
| 478 |
(progn (reader-suppress-ch tok) (list .lisp-general-sexpr. (read))) |
| 479 |
(if (equal .chaos-value-keyword. tok) |
| 480 |
(progn (reader-suppress-ch tok) (list .chaos-value-sexpr. (read))) |
| 481 |
tok)))) |
| 482 |
|
| 483 |
(defun reader-suppress-ch (context &optional (stream *standard-input*)) |
| 484 |
(declare (ignore context) |
| 485 |
(type stream stream) |
| 486 |
(values t)) |
| 487 |
(unless (at-eof) |
| 488 |
(unless (memq .reader-ch. '(space return)) |
| 489 |
(unread-char (if (characterp .reader-ch.) |
| 490 |
.reader-ch. |
| 491 |
(char (the simple-string (string .reader-ch.)) 0)) |
| 492 |
stream) |
| 493 |
(setq .reader-ch. 'space)))) |
| 494 |
|
| 495 |
(defun reader-unread (ch stream) |
| 496 |
(declare (type (or symbol character) ch) |
| 497 |
(type stream stream) |
| 498 |
(values t)) |
| 499 |
(unless (memq ch '(space return)) |
| 500 |
(unread-char (if (characterp ch) |
| 501 |
ch |
| 502 |
(char (the simple-string (string (the symbol ch))) 0)) |
| 503 |
stream) |
| 504 |
ch)) |
| 505 |
|
| 506 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 507 |
|
| 508 |
(defun skip-multi-comment (stream) |
| 509 |
(loop |
| 510 |
(reader-get-char stream) |
| 511 |
(cond ((at-eof) |
| 512 |
(setf .reader-ch. 'space) |
| 513 |
(!read-discard) |
| 514 |
(return-from skip-multi-comment *lex-eof*)) |
| 515 |
(t (case .reader-ch. |
| 516 |
(#\| |
| 517 |
(reader-get-char stream) |
| 518 |
(when (equal .reader-ch. #\#) |
| 519 |
(!read-discard) |
| 520 |
(setq .reader-ch. 'space) |
| 521 |
(return-from skip-multi-comment nil)) |
| 522 |
(when (at-eof) |
| 523 |
(setf .reader-ch. 'space) |
| 524 |
(!read-discard) |
| 525 |
(return-from skip-multi-comment *lex-eof*)) |
| 526 |
(reader-unread .reader-ch. stream))))))) |
| 527 |
|
| 528 |
;;; READ-SYM : STREAM -> TOKEN |
| 529 |
;;; read characters considered to be constructs of a token, returns |
| 530 |
;;; the token recognized. |
| 531 |
;;; The followings are treated specially: |
| 532 |
;;; (...) : read a parenthesized unit. |
| 533 |
;;; "..." : read as string constant. |
| 534 |
;;; 'c : read as character constant. |
| 535 |
;;; |
| 536 |
(defun unread-token (&rest ignore) |
| 537 |
(declare (ignore ignore)) |
| 538 |
(unless (eq *last-token* *reader-void*) |
| 539 |
(push *last-token* *token-buf*) |
| 540 |
(setq *last-token* *reader-void*))) |
| 541 |
|
| 542 |
(defun read-sym (&optional (stream *standard-input*) (parse-list nil)) |
| 543 |
(declare (type stream stream) |
| 544 |
(type (or null t) parse-list)) |
| 545 |
(flet ((skip-whites () |
| 546 |
;; skip white chars. |
| 547 |
(while (memq .reader-ch. (if *live-newline* '(space) |
| 548 |
'(space return))) |
| 549 |
(reader-get-char stream)))) |
| 550 |
(when *token-buf* |
| 551 |
(return-from read-sym (pop *token-buf*))) |
| 552 |
;; skip white chars. |
| 553 |
(skip-whites) |
| 554 |
;; get token |
| 555 |
;; (setq *last-token* nil) |
| 556 |
(tagbody retry |
| 557 |
(cond ((at-eof) (setf .reader-ch. 'space) |
| 558 |
(!read-discard) |
| 559 |
(return-from read-sym (progn (setq *last-token* *reader-void*) *lex-eof*))) |
| 560 |
((see-input-escape) |
| 561 |
;; user forces aborting reading process. |
| 562 |
(setq .reader-ch. 'space) |
| 563 |
(!read-discard) |
| 564 |
(clear-input) |
| 565 |
(throw :aborting-read :aborting-read)) |
| 566 |
(t (case .reader-ch. |
| 567 |
(\( (if parse-list |
| 568 |
(setq *last-token* (lex-read-list stream)) |
| 569 |
(progn |
| 570 |
(setq .reader-ch. 'space) |
| 571 |
(setq *last-token* "("))) |
| 572 |
(return-from read-sym *last-token*)) |
| 573 |
(return |
| 574 |
(setq .reader-ch. 'space) |
| 575 |
(setq *last-token* (if *live-newline* |
| 576 |
'(return) |
| 577 |
*reader-void*)) |
| 578 |
(return-from read-sym *last-token*)) |
| 579 |
(#\" ; string |
| 580 |
(return-from read-sym (setq *last-token* (list (lex-read-string stream))))) |
| 581 |
(#\# ; #! or #!! |
| 582 |
(reader-get-char stream) |
| 583 |
(cond ((memq .reader-ch. '(space return)) |
| 584 |
(return-from read-sym (setq *last-token* '("#")))) |
| 585 |
((eq .reader-ch. *lisp-escape-char*) |
| 586 |
(return-from read-sym (setq *last-token* (lex-read-lisp-escape stream)))) |
| 587 |
((eq .reader-ch. *chaos-escape-char*) |
| 588 |
(return-from read-sym (setq *last-token* (lex-read-chaos-value stream)))) |
| 589 |
((equal .reader-ch. #\|) ; begin multi comment |
| 590 |
(skip-multi-comment stream) |
| 591 |
(skip-whites) |
| 592 |
(go retry)) |
| 593 |
(t (reader-unread .reader-ch. stream) |
| 594 |
(setq .reader-ch. #\#) |
| 595 |
(let ((tok (read-lexicon stream))) |
| 596 |
(if (equal tok *lex-eof*) |
| 597 |
(return-from read-sym |
| 598 |
(progn (setq *last-token* *reader-void*) |
| 599 |
*lex-eof*)) |
| 600 |
(return-from read-sym (setq *last-token* tok)))))))) |
| 601 |
;; |
| 602 |
(if (symbolp .reader-ch.) |
| 603 |
(let ((str (string .reader-ch.))) |
| 604 |
(setq .reader-ch. 'space) |
| 605 |
(return-from read-sym (setq *last-token* (lex-consider-token str)))) |
| 606 |
(let ((tok (read-lexicon stream))) |
| 607 |
(if (eq tok *lex-eof*) |
| 608 |
(return-from read-sym |
| 609 |
(progn (setq *last-token* *reader-void*) |
| 610 |
*lex-eof*)) |
| 611 |
(return-from read-sym (setq *last-token* tok)))))))))) |
| 612 |
|
| 613 |
;;; builtin string reader |
| 614 |
(defun lex-read-string (stream) |
| 615 |
(declare (type stream stream) |
| 616 |
(values t)) |
| 617 |
(reader-unread .reader-ch. stream) |
| 618 |
(let ((str (read stream nil *lex-eof*))) |
| 619 |
(if (eq str *lex-eof*) |
| 620 |
*lex-eof* |
| 621 |
(prog1 |
| 622 |
(list .String-token. str) |
| 623 |
(setf .reader-ch. 'space))))) |
| 624 |
|
| 625 |
;; builtin lisp expression |
| 626 |
(defun lex-read-lisp-escape (stream) |
| 627 |
(declare (type stream stream) |
| 628 |
(values list)) |
| 629 |
(let ((nx nil)) |
| 630 |
(setq nx (reader-get-char stream)) |
| 631 |
(while (memq .reader-ch. '(space return)) |
| 632 |
(setq nx (reader-get-char stream))) |
| 633 |
(case nx |
| 634 |
((*lisp-escape-char* *chaos-escape-char*) |
| 635 |
;; #!! |
| 636 |
(let ((expr (read stream nil *lex-eof*))) |
| 637 |
(setq .reader-ch. 'space) |
| 638 |
(if (equal expr *lex-eof*) |
| 639 |
(progn (setq *last-token* *reader-void*) |
| 640 |
(setq .reader-ch. 'space) |
| 641 |
*lex-eof*) |
| 642 |
(list .lisp-general-sexpr. expr)))) |
| 643 |
(otherwise |
| 644 |
;; #! |
| 645 |
(let ((expr nil)) |
| 646 |
(setq .reader-ch. 'space) |
| 647 |
(reader-unread nx stream) |
| 648 |
(setq expr (read stream nil *lex-eof*)) |
| 649 |
(if (equal expr *lex-eof*) |
| 650 |
(progn (setq *last-token* *reader-void*) |
| 651 |
*lex-eof*) |
| 652 |
(list .lisp-simple-sexpr. expr)))))) |
| 653 |
) |
| 654 |
|
| 655 |
(defun lex-read-chaos-value (stream) |
| 656 |
(declare (type stream stream) |
| 657 |
(values list)) |
| 658 |
(let ((expr (read stream nil *lex-eof*))) |
| 659 |
(setq .reader-ch. 'space) |
| 660 |
(if (equal expr *lex-eof*) |
| 661 |
(progn (setq *last-token* *reader-void*) |
| 662 |
*lex-eof*) |
| 663 |
(list .chaos-value-sexpr. expr)))) |
| 664 |
|
| 665 |
;;; builtin character reader : obsolate |
| 666 |
#|| |
| 667 |
(defun lex-read-character (stream) |
| 668 |
(let ((char (read-char stream nil *lex-eof*))) |
| 669 |
(if (eq char *lex-eof*) |
| 670 |
*lex-eof* |
| 671 |
(progn |
| 672 |
(when (eql char #\\) ; escape char |
| 673 |
(let ((echar (read-char stream nil *lex-eof*))) |
| 674 |
(if (eq echar *lex-eof*) |
| 675 |
(return-from lex-read-character *lex-eof*) |
| 676 |
(setf char |
| 677 |
(case echar |
| 678 |
(#\n #\Newline) |
| 679 |
(#\r #\Return) |
| 680 |
(#\t #\Tab) |
| 681 |
(#\s #\Space) |
| 682 |
(#\l #\LineFeed) |
| 683 |
(#\p #\Page) |
| 684 |
(otherwise echar)))))) |
| 685 |
(setf .reader-ch. 'space) |
| 686 |
(list .Char-token. char))))) |
| 687 |
||# |
| 688 |
|
| 689 |
;;; read up to matching close parenthesis |
| 690 |
;;; |
| 691 |
(defun lex-read-list (&optional (stream *standard-input*)) |
| 692 |
(declare (type stream stream)) |
| 693 |
(reader-get-char stream) |
| 694 |
(lex-read-rest-of-list stream)) |
| 695 |
|
| 696 |
(defun lex-read-rest-of-list (&optional (stream *standard-input*)) |
| 697 |
(declare (type stream stream) |
| 698 |
(values list)) |
| 699 |
(while (memq .reader-ch. '(space return)) |
| 700 |
(reader-get-char stream)) |
| 701 |
(if (at-eof) |
| 702 |
*lex-eof* |
| 703 |
(if (eq '\) .reader-ch.) |
| 704 |
(progn |
| 705 |
(reader-get-char stream) |
| 706 |
(list "(" ")")) |
| 707 |
(let ((res (list "(")) |
| 708 |
x) |
| 709 |
(loop (setq x (lex-read stream)) |
| 710 |
(when (eq *lex-eof* x) |
| 711 |
(return *lex-eof*)) |
| 712 |
(setq res (append res x)) |
| 713 |
;; (wait-until-non-white stream) |
| 714 |
(while (memq .reader-ch. '(space return)) |
| 715 |
(reader-get-char stream)) |
| 716 |
(when (eq '|)| .reader-ch.) |
| 717 |
(reader-get-char stream) |
| 718 |
(return (nconc res (list ")")))) |
| 719 |
(when (at-eof) |
| 720 |
(return *lex-eof*))) |
| 721 |
)))) |
| 722 |
|
| 723 |
;;; LEX-READ : STREAM -> List[Token] |
| 724 |
;;; standard routine to get token from stream. |
| 725 |
;;; |
| 726 |
(defun bi-token? (tok) |
| 727 |
(declare (type t tok) |
| 728 |
(values (or null t))) |
| 729 |
(and (consp tok) |
| 730 |
(let ((tm (car tok))) |
| 731 |
(and (symbolp tm) |
| 732 |
(get tm ':bi-token))))) |
| 733 |
|
| 734 |
(defun lex-read (&optional (stream *standard-input*)) |
| 735 |
(declare (type stream stream) |
| 736 |
(values t)) |
| 737 |
(let ((tok (read-sym stream t))) |
| 738 |
(if (eq *lex-eof* tok) |
| 739 |
*lex-eof* |
| 740 |
(cond ((atom tok) |
| 741 |
(if tok |
| 742 |
(list tok) |
| 743 |
nil)) |
| 744 |
(t (if (bi-token? tok) |
| 745 |
(list tok) |
| 746 |
tok)))))) |
| 747 |
|
| 748 |
;;; returns t iff the characters in the string are all digit char. |
| 749 |
|
| 750 |
(defmacro all-digit? (string start end) |
| 751 |
(once-only (string) |
| 752 |
` (the (or null t) |
| 753 |
(do ((s (the fixnum ,start) (1+ s))) |
| 754 |
((>= s ,end) t) |
| 755 |
(declare (type fixnum s end)) |
| 756 |
(if (not (digit-char-p (schar ,string s))) (return nil)))))) |
| 757 |
|
| 758 |
;;; BUFFERED-INPUT______________________________________________________________ |
| 759 |
;;; one token is bufferd. |
| 760 |
|
| 761 |
;;; Input Buffering for general reader |
| 762 |
;;; the following routines create a single token buffer, which |
| 763 |
;;; allows look-ahead. |
| 764 |
;;; |
| 765 |
|
| 766 |
;;; !LEX-READ-INIT |
| 767 |
;;; initialize read table, .escape-char., and .reader-ch.. |
| 768 |
;;; |
| 769 |
(defparameter .default-space-chars. |
| 770 |
'(#\Space #\Tab #\Page #\Linefeed)) |
| 771 |
|
| 772 |
(defparameter .default-return-chars. |
| 773 |
'(#\Return #\Newline)) |
| 774 |
|
| 775 |
(defparameter .default-single-chars. |
| 776 |
`((#\( . \() |
| 777 |
(#\) . \)) |
| 778 |
(#\, . |,|) |
| 779 |
(#\[ . |[|) |
| 780 |
(#\] . |]|) |
| 781 |
(#\{ . |{|) |
| 782 |
(#\} . |}|) |
| 783 |
(#\; . \;) |
| 784 |
;; (#\_ . |_|) |
| 785 |
;; (#\% . "%") |
| 786 |
;; (#\! . |!|) |
| 787 |
(,control-d . ,control-d) |
| 788 |
)) |
| 789 |
|
| 790 |
(defun !force-single-reader (l) |
| 791 |
(declare (type list l) |
| 792 |
(values t)) |
| 793 |
(dolist (x l) |
| 794 |
(let* ((chr (if (and (stringp x) |
| 795 |
(= (length x) 1)) |
| 796 |
(char (the string x) 0) |
| 797 |
(if (characterp x) |
| 798 |
x |
| 799 |
(with-output-chaos-error ('invalid-str) |
| 800 |
(format t "delimiter must be a single character, but ~a is given" x))))) |
| 801 |
(sym (intern (string x)))) |
| 802 |
(format t "~&setting delimiters ~S : ~S" chr sym) |
| 803 |
(!set-syntax chr sym)))) |
| 804 |
|
| 805 |
(defun !unset-single-reader (l) |
| 806 |
(declare (type list l) |
| 807 |
(values t)) |
| 808 |
(dolist (x l) |
| 809 |
(let ((chr (if (and (stringp x) |
| 810 |
(= (length x) 1)) |
| 811 |
(char (the string x) 0) |
| 812 |
(if (characterp x) |
| 813 |
x |
| 814 |
(with-output-chaos-error ('invalid-str) |
| 815 |
(format t "Delimiter must be a single character, but ~a is given" x)))))) |
| 816 |
(if (assoc chr .default-single-chars.) |
| 817 |
(warn "Character '~A' is a hardwired self delimiting charcter, ignored." |
| 818 |
chr) |
| 819 |
(progn |
| 820 |
(format t "~&unsetting delimiters ~S" chr) |
| 821 |
(!set-syntax chr nil)))))) |
| 822 |
;;; |
| 823 |
;;; |
| 824 |
;;; |
| 825 |
(defun !lex-read-init (&key (space .default-space-chars.) |
| 826 |
(return .default-return-chars.) |
| 827 |
(single .default-single-chars.) |
| 828 |
(escape .default-escape-char.)) |
| 829 |
(!init-read-table space return single) |
| 830 |
(setq .escape-char. escape) |
| 831 |
(setq .reader-ch. 'space) |
| 832 |
(setq *reader-input* *reader-void* |
| 833 |
*last-token* *reader-void* |
| 834 |
*token-buf* nil)) |
| 835 |
|
| 836 |
;;; EOF |