| 1 |
;;;-*- Mode:LISP; Package:CHAOS; Base:10; Syntax:Common-lisp -*- |
| 2 |
;;; |
| 3 |
;;; Copyright (c) 2000-2015, 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: reader.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 |
;;; SCHEMA BASED Genral Reader. |
| 41 |
;;; |
| 42 |
;;; BASED ON OBJ3 READER ROUTINES. |
| 43 |
;;; ;;;; Copyright 1988,1991 SRI International. |
| 44 |
;;; |
| 45 |
|
| 46 |
;;; SCHEMA |
| 47 |
;;;============================================================================= |
| 48 |
;;; Schemas match patterns |
| 49 |
;;; choices must be determined by first symbol of context |
| 50 |
;;; e.g. if an optional element doesn't occur what occurs first |
| 51 |
;;; must be an explicit symbol in the schema |
| 52 |
;;; |
| 53 |
;;;--- BUILT-IN PATTERNS ------------------------------------------------------- |
| 54 |
;;; To parse the OBJ3 syntax more easier, these definitions are just the same |
| 55 |
;;; as of OBJ3 schema. |
| 56 |
;;; |
| 57 |
;;; (:optional PAT) - optional occurrence of pattern |
| 58 |
;;; (:if-present PAT) - optional occurrence of pattern |
| 59 |
;;; *NOTE* |
| 60 |
;;; :optional - omit if next token matches following context |
| 61 |
;;; :if-present - assume if first matches |
| 62 |
;;; (:one-of PAT1 PAT2 ...) - one of patterns determined by first of patterns |
| 63 |
;;; (:one-of-default PAT1 PAT2 ...) ... with a default to PAT1 |
| 64 |
;;; (:seq-of PAT) - some number of repetitions of PAT |
| 65 |
;;; (:many-of PAT1 PAT2 ...) - roughly (:seq-of (:one-of PAT1 ...)) |
| 66 |
;;; :symbol - a symbol |
| 67 |
;;; :symbols - (:seq-of :symbol) |
| 68 |
;;; :int - an integer |
| 69 |
;;; :term - sequence of tokens up to terminator which follows |
| 70 |
;;; :sort - a sort name (possibly qualified) |
| 71 |
;;; :sorts - (:seq-of :sort) |
| 72 |
;;; :comment - string of characters to end of current line |
| 73 |
;;; :commentlong - variation of above with provision for long comments |
| 74 |
;;; (:+ a b c) - any of several symbols |
| 75 |
;;; (:! SCHEMA-NAME) - match the named schema |
| 76 |
;;; (:call LISP-EXPR) - escape to Lisp |
| 77 |
;;; (:rdr ("char1" "char2"...) PAT) - make characters be single-char and |
| 78 |
;;; then match pattern, returning status of characters to previous when done |
| 79 |
;;; :modexp - read a term with balanced (/) [/] view/endv |
| 80 |
;;; particular tricky case: view A to B[view C to D is ... endv] { ... } |
| 81 |
;;; :chaos-item - read a (possibly parenthesized) item |
| 82 |
;;; otherwise: symbol - matches symbol if it is not defined as the schema name. |
| 83 |
;;; (:pred PREDICATE) - matches a token satisfying predicate |
| 84 |
;;; :append PAT - analogous to ,@; incorporate structure from PAT |
| 85 |
;;; into result "removing one set of parentheses" |
| 86 |
;;; :upto - specify ending context for repeated form |
| 87 |
;;; :args - for command arguments. read rest of line till \newline. |
| 88 |
;;; |
| 89 |
;;;--- DEFINING NEW SCHEMA & Its Reader .... |
| 90 |
|
| 91 |
|
| 92 |
(defvar *reader-special-schema-patterns* nil) |
| 93 |
|
| 94 |
;;; READER schema-pattern-name schemas |
| 95 |
;;; read input from *standard-input*, parse according to schemas |
| 96 |
;;; |
| 97 |
(defun reader (name schms) |
| 98 |
(declare (type symbol name) |
| 99 |
(type list schms) |
| 100 |
(values t)) |
| 101 |
(let ((*reader-schema-env* schms)) |
| 102 |
(!lex-read-init) |
| 103 |
(read-named name *reader-void* nil) |
| 104 |
)) |
| 105 |
|
| 106 |
;;; |
| 107 |
;;; ERROR HANDLERS |
| 108 |
;;; |
| 109 |
|
| 110 |
(defun abort-general-reader (msg) |
| 111 |
(with-output-chaos-error ('reader-error) |
| 112 |
(princ msg) |
| 113 |
(when *chaos-input-source* |
| 114 |
(print-next) |
| 115 |
(princ "file: ") |
| 116 |
(princ (namestring *chaos-input-source*))) |
| 117 |
(when (and *reader-current-schema* |
| 118 |
(general-read-is-simple-schema *reader-current-schema*)) |
| 119 |
(print-next) |
| 120 |
(princ "expecting: ") |
| 121 |
(let ((*print-indent-contin* t)) |
| 122 |
;; (break) |
| 123 |
;; (general-read-print-schema *reader-current-schema*) |
| 124 |
(general-read-display-schema *reader-current-schema* :short))) |
| 125 |
(when *reader-starting-position* |
| 126 |
(print-next) |
| 127 |
(princ "starting character position was: ") |
| 128 |
(princ *reader-starting-position*)))) |
| 129 |
|
| 130 |
(defun general-read-eof-error () |
| 131 |
(declare (values t)) |
| 132 |
(abort-general-reader "Unexpected EOF.")) |
| 133 |
|
| 134 |
(defun general-read-abort () |
| 135 |
(declare (values t)) |
| 136 |
(abort-general-reader "Cancel reading input...")) |
| 137 |
|
| 138 |
;;; STRING-MATCH string1 string2 |
| 139 |
;;; basic string matching function. |
| 140 |
;;; |
| 141 |
(defun string-match (x y) |
| 142 |
(declare (values (or null t))) |
| 143 |
(cond ((stringp x) |
| 144 |
(string= (the simple-string x) |
| 145 |
(if (stringp y) |
| 146 |
(the simple-string y) |
| 147 |
(the simple-string |
| 148 |
(string-downcase (string y)))))) |
| 149 |
((characterp x) |
| 150 |
(eql (the character x) (the character y))) |
| 151 |
(t (eq x y)))) |
| 152 |
|
| 153 |
;;; GENERAL-READ-STRING-MATCHES : token pattern -> Bool |
| 154 |
;;; used to match tokens against "patterns" which should be |
| 155 |
;;; either a symbol, string, or of one of the forms |
| 156 |
;;; (:+ a b c ...) or (:pred PRED) |
| 157 |
;;; |
| 158 |
(defun general-read-string-matches (x y) |
| 159 |
(declare (values (or null t))) |
| 160 |
(and (atom x) |
| 161 |
(if (atom y) (string-match x y) |
| 162 |
(or (and (eq ':pred (car y)) (funcall (cadr y) x)) |
| 163 |
(if (eq ':+ (car y)) |
| 164 |
(member x (cdr y) :test #'string-match) |
| 165 |
(member x y :test #'string-match)))))) |
| 166 |
|
| 167 |
;;; GENERAL-READ-NUMBERP token -> Bool |
| 168 |
;;; is a token an integer? |
| 169 |
;;; |
| 170 |
(defun general-read-numberp (str) |
| 171 |
(declare (type simple-string str) |
| 172 |
(values (or null t))) |
| 173 |
(let ((p 0) |
| 174 |
(len (length str))) |
| 175 |
(declare (type fixnum p len)) |
| 176 |
(when (member (char str p) '(#\+ #\-)) |
| 177 |
(incf p) |
| 178 |
(when (= 1 len) (return-from general-read-numberp nil))) |
| 179 |
(loop |
| 180 |
(when (= len p) (return t)) |
| 181 |
(when (not (digit-char-p (char str p))) (return nil)) |
| 182 |
(incf p) |
| 183 |
))) |
| 184 |
|
| 185 |
;;; GENERAL-READ |
| 186 |
;;; the workhorse general read routine |
| 187 |
;;; it dispatches to routines to handle the various cases |
| 188 |
;;; op general-read : {*standard-input*} schema context -> |
| 189 |
;;; {*standard-input*} parse-tree |
| 190 |
|
| 191 |
(defun general-read (schema context &optional (allow-other nil)) |
| 192 |
(declare (type list schema context) |
| 193 |
(type (or null t) allow-other) |
| 194 |
(values t)) |
| 195 |
;; |
| 196 |
(let ((*reader-current-schema* schema) |
| 197 |
(*reader-current-context* context) |
| 198 |
(*reader-starting-position* |
| 199 |
(if (at-top-level) nil (file-position *standard-input*))) |
| 200 |
(result nil)) |
| 201 |
(setq result |
| 202 |
(catch :aborting-read |
| 203 |
(cond ((null schema) nil) |
| 204 |
(t (let ((elt (car schema)) |
| 205 |
(rest (cdr schema))) |
| 206 |
(let ((restcontext (if rest rest context))) |
| 207 |
(cond |
| 208 |
((symbolp elt) |
| 209 |
(case elt |
| 210 |
(:unread (read-continue (unread-token) |
| 211 |
rest context)) |
| 212 |
(:optional (read-optional rest context)) |
| 213 |
(:if-present (read-if-present rest context)) |
| 214 |
(:one-of (read-one-of rest context)) |
| 215 |
(:one-of-default (read-one-of-default rest context)) |
| 216 |
(:many-of ;like one-of but with repetitions |
| 217 |
(read-many-of rest context)) |
| 218 |
(:seq-of (read-seq-of rest context)) |
| 219 |
(:symbol (read-continue (!read-sym) rest context)) |
| 220 |
(:symbols (read-continue (read-seq-of '(:symbol) |
| 221 |
restcontext) |
| 222 |
rest context)) |
| 223 |
(:int (let ((val (!read-sym))) |
| 224 |
(cond |
| 225 |
((general-read-numberp val) |
| 226 |
(read-continue val rest context)) |
| 227 |
(t (with-output-chaos-error ('reader-error) |
| 228 |
(princ "was expecting an integer not ") |
| 229 |
(princ val) |
| 230 |
(print-next) |
| 231 |
(general-read-show-context) |
| 232 |
(clear-input) |
| 233 |
))))) |
| 234 |
(:top-term (read-continue (read-term-at-top restcontext) |
| 235 |
rest context)) |
| 236 |
(:term (read-continue (read-term restcontext) |
| 237 |
rest context)) |
| 238 |
;; (:term-to (read-continue (read-term-to restcontext) |
| 239 |
;; rest context)) |
| 240 |
(:top-opname (read-continue (read-opname-at-top restcontext) |
| 241 |
rest context)) |
| 242 |
(:opname (read-continue (read-opname restcontext) |
| 243 |
rest context)) |
| 244 |
(:sort (read-continue (read-sort restcontext) |
| 245 |
rest context)) |
| 246 |
(:sorts (read-continue (read-sorts restcontext) |
| 247 |
rest context)) |
| 248 |
(:chars (read-continue (read-chars restcontext) |
| 249 |
rest context)) |
| 250 |
(:optattr (read-continue (read-opattr restcontext) |
| 251 |
rest context)) |
| 252 |
(:comment (read-continue (read-comment-line) rest context)) |
| 253 |
(:commentlong |
| 254 |
(read-continue (general-read-commentlong) rest context)) |
| 255 |
(:+ (read-any-one rest)) |
| 256 |
(:! ; use named description |
| 257 |
(read-named (car rest) context)) |
| 258 |
(:call (eval (car rest))) |
| 259 |
(:append |
| 260 |
(let* ((rr (cdr rest)) |
| 261 |
(rc (if rr rr context))) |
| 262 |
(read-continue-append |
| 263 |
(general-read (car rest) rc) rr context))) |
| 264 |
(:rdr |
| 265 |
(let ((cur (!set-single-reader (car rest)))) |
| 266 |
(prog1 (general-read (cdr rest) context) |
| 267 |
(!set-reader cur)))) |
| 268 |
(:modexp |
| 269 |
(read-continue |
| 270 |
(read-module-exp (car restcontext)) rest context)) |
| 271 |
(:super |
| 272 |
(read-continue |
| 273 |
(read-super-exp (car restcontext)) rest context)) |
| 274 |
(:chaos-item |
| 275 |
(!read-discard) |
| 276 |
(let ((val (lex-read))) |
| 277 |
(let ((a (if (null (cdr val)) (car val) val))) |
| 278 |
(read-continue a rest context)))) |
| 279 |
(:args |
| 280 |
(read-args rest context)) |
| 281 |
(otherwise |
| 282 |
(if allow-other |
| 283 |
;; we read input as a seq-of term |
| 284 |
(general-read '(:seq-of :term) '(void)) |
| 285 |
(progn |
| 286 |
(!read-in) |
| 287 |
(cond |
| 288 |
((string-match *reader-input* elt) |
| 289 |
(let ((inp *reader-input*)) |
| 290 |
(!read-discard) |
| 291 |
(read-continue inp rest context))) |
| 292 |
(t (with-output-chaos-error ('reader-error) |
| 293 |
(princ "was expecting the symbol ") |
| 294 |
(princ "`") |
| 295 |
(princ elt) |
| 296 |
(if (or (equal *reader-input* *lex-eof*) |
| 297 |
(equal *reader-input* control-d-string)) |
| 298 |
(format t "', premature end of input.") |
| 299 |
(format t "' not `~a'." *reader-input*)) |
| 300 |
(print-next) |
| 301 |
(general-read-show-context) |
| 302 |
(clear-input) |
| 303 |
)))))) |
| 304 |
)) |
| 305 |
((member (car elt) '(:! :rdr)) |
| 306 |
(let ((val (general-read elt restcontext))) |
| 307 |
(cond ((eq *reader-void* val) |
| 308 |
(general-read rest context)) |
| 309 |
(t (append val |
| 310 |
(general-read rest context)))))) |
| 311 |
((eq :upto (car elt)) |
| 312 |
(append (general-read (cddr elt) (list (cadr elt))) |
| 313 |
(general-read rest context))) |
| 314 |
(t |
| 315 |
(read-continue (general-read elt restcontext) rest context) |
| 316 |
)))))) |
| 317 |
)) |
| 318 |
(if (eq :aborting-read result) |
| 319 |
(general-read-abort) |
| 320 |
result) |
| 321 |
)) |
| 322 |
|
| 323 |
;;; PATTERN HANDLERS |
| 324 |
;;; |
| 325 |
|
| 326 |
;;; READ-NAMED name context |
| 327 |
;;; |
| 328 |
(defun read-named (name context &optional allow-other) |
| 329 |
(declare (type symbol name) |
| 330 |
(type list context) |
| 331 |
(type (or null t) allow-other)) |
| 332 |
(let ((val (assoc name *reader-schema-env* :test #'eq))) |
| 333 |
(cond (val (general-read (cadr val) context allow-other)) |
| 334 |
(t (error "Undefined name in general reader ~a" name))))) |
| 335 |
|
| 336 |
;;; READ-OPTIONAL |
| 337 |
;;; |
| 338 |
(defun read-optional (s c) |
| 339 |
(!read-in) |
| 340 |
(when (at-eof-or-control-d) |
| 341 |
(general-read-eof-error)) |
| 342 |
(cond ((general-read-string-matches *reader-input* (car c)) |
| 343 |
*reader-void*) |
| 344 |
(t (general-read s c))) |
| 345 |
) |
| 346 |
|
| 347 |
;;; READ-IF-PRESENT |
| 348 |
;;; |
| 349 |
(defun read-if-present (s c) |
| 350 |
(declare (type list s c) |
| 351 |
(values t)) |
| 352 |
(!read-in) |
| 353 |
(when (at-eof-or-control-d) |
| 354 |
(general-read-eof-error)) |
| 355 |
(cond ((general-read-string-matches *reader-input* (car s)) |
| 356 |
(general-read s c)) |
| 357 |
(t *reader-void*))) |
| 358 |
|
| 359 |
;;; READ-ONE-OF |
| 360 |
;;; |
| 361 |
(defun read-one-of (s c) |
| 362 |
(declare (type list s c) |
| 363 |
(values t)) |
| 364 |
(let ((inp (!read-sym))) |
| 365 |
(when (and c (at-eof-or-control-d)) |
| 366 |
(general-read-eof-error)) |
| 367 |
(let ((val (assoc inp s :test #'general-read-string-matches))) |
| 368 |
(cond (val (cons inp (general-read (cdr val) c))) |
| 369 |
((and (consp inp) |
| 370 |
(eq (caar inp) '|String|)) |
| 371 |
(read-one-of s c)) |
| 372 |
((and (eq *lex-eof* inp) |
| 373 |
(assoc 'eof s)) |
| 374 |
(cons 'eof (general-read (cdr (assoc 'eof s)) c))) |
| 375 |
((eq *lex-eof* inp) (general-read-eof-error)) |
| 376 |
(*allow-general-term-input* |
| 377 |
(unread-token) |
| 378 |
(read-term '(|.|))) |
| 379 |
(t (let ((top-level (assoc 'eof s))) |
| 380 |
(when (equal inp ".") |
| 381 |
(chaos-error 'reader-error)) |
| 382 |
(with-output-chaos-error ('reader-error) |
| 383 |
(princ "expecting one of followings:") |
| 384 |
(print-next) |
| 385 |
(let ((*print-indent-contin* t)) |
| 386 |
(general-read-print-schema (mapcar #'car s) :short)) |
| 387 |
(print-next) |
| 388 |
(princ "* NOT: ") |
| 389 |
(princ inp) |
| 390 |
(general-read-show-context) |
| 391 |
(when top-level |
| 392 |
(setq *chaos-print-errors* nil)) |
| 393 |
(clear-input) |
| 394 |
))) |
| 395 |
)))) |
| 396 |
|
| 397 |
|
| 398 |
;;; READ-ONE-OF-DEFAULT |
| 399 |
;;; first alternative is a default |
| 400 |
;;; |
| 401 |
(defun read-one-of-default (s c) |
| 402 |
(declare (type list s c) |
| 403 |
(values t)) |
| 404 |
(!read-in) |
| 405 |
(let ((val (assoc *reader-input* (cdr s) |
| 406 |
:test #'general-read-string-matches))) |
| 407 |
(cond (val (let ((inp *reader-input*)) |
| 408 |
(!read-discard) |
| 409 |
(cons inp (general-read (cdr val) c)))) |
| 410 |
((and (reader-is-at-eof) (assoc 'eof s)) |
| 411 |
(cons 'eof (general-read (cdr (assoc 'eof s)) c))) |
| 412 |
((reader-is-at-eof) ; !!! |
| 413 |
(general-read-eof-error)) |
| 414 |
(t (general-read (car s) c))))) |
| 415 |
|
| 416 |
;;; READ-MANY-OF |
| 417 |
;;; |
| 418 |
(defun read-many-of (s c) |
| 419 |
(declare (type list s c) |
| 420 |
(values t)) |
| 421 |
(let ((res nil) (close (car c))) |
| 422 |
(loop (!read-in) |
| 423 |
(when (at-eof-or-control-d) |
| 424 |
(general-read-eof-error)) |
| 425 |
(when (general-read-string-matches *reader-input* close) |
| 426 |
(return (if (null res) |
| 427 |
*reader-void* |
| 428 |
(nreverse res)))) |
| 429 |
(if (and (consp *reader-input*) |
| 430 |
(eq (caar *reader-input*) '|String|)) |
| 431 |
(setq *reader-input* *reader-void*) |
| 432 |
(setq res (cons (read-one-of s c) res)))))) |
| 433 |
|
| 434 |
;;; READ-SEQ-OF |
| 435 |
;;; |
| 436 |
(defun read-seq-of (s c) |
| 437 |
(declare (type list s c) |
| 438 |
(values t)) |
| 439 |
(cond ((equal '(:term) s) (read-seq-of-term c)) |
| 440 |
((equal '(:opname) s) (read-seq-of-opname c)) |
| 441 |
((equal '(:top-term) s) (read-seq-of-term-at-top c)) |
| 442 |
((equal '(:top-opname) s) (read-seq-of-opname-at-top c)) |
| 443 |
(t (let ((res nil) (close (car c))) |
| 444 |
(loop |
| 445 |
(!read-in) |
| 446 |
(when (at-eof-or-control-d) |
| 447 |
(general-read-eof-error)) |
| 448 |
(when (general-read-string-matches *reader-input* close) |
| 449 |
(return (if (null res) *reader-void* res))) |
| 450 |
(setq res (append res (general-read s c))) |
| 451 |
))))) |
| 452 |
|
| 453 |
;;; READ-ANY-ONE |
| 454 |
;;; |
| 455 |
(defun read-any-one (s) |
| 456 |
(declare (type list s) |
| 457 |
(values t)) |
| 458 |
(!read-in) |
| 459 |
(cond ((member *reader-input* s :test #'string-match) |
| 460 |
(!read-sym)) |
| 461 |
((at-eof-or-control-d) (general-read-eof-error)) |
| 462 |
(t (with-output-chaos-error ('reader-error) |
| 463 |
(princ "expecting one of") |
| 464 |
(print-next) |
| 465 |
(let ((*print-indent-contin* t)) |
| 466 |
(general-read-print-schema s :short)) |
| 467 |
(print-next) |
| 468 |
(format t "NOT ") |
| 469 |
(princ *reader-input*) |
| 470 |
(general-read-show-context) |
| 471 |
(clear-input) |
| 472 |
)))) |
| 473 |
|
| 474 |
;;; READ-CONTINUE : {*standard-input*} value schema context -> |
| 475 |
;;; {*standard-input*} parse-tree |
| 476 |
;;; continues the matching process |
| 477 |
;;; the value is returned as the first component of the resulting tree |
| 478 |
;;; |
| 479 |
(defun read-continue (v s c) |
| 480 |
(declare (type t v) |
| 481 |
(type list s c) |
| 482 |
(values t)) |
| 483 |
(cond ((eq *reader-void* v) (general-read s c)) |
| 484 |
((equal v control-d-string) (general-read-eof-error)) |
| 485 |
(t (cons v (general-read s c))))) |
| 486 |
|
| 487 |
;;; READ-CONTINUE-APPEND : {*standard-input*} value schema context -> |
| 488 |
;;; {*standard-input*} parse-tree |
| 489 |
;;; continues the matching process; appending the value given |
| 490 |
;;; the value is returned as the first component of the resulting tree |
| 491 |
;;; |
| 492 |
(defun read-continue-append (v s c) |
| 493 |
(declare (type t v) |
| 494 |
(type list s c) |
| 495 |
(values t)) |
| 496 |
(cond ((eq *reader-void* v) (general-read s c)) |
| 497 |
((equal v control-d-string) (general-read-eof-error)) |
| 498 |
(t (append v (general-read s c))))) |
| 499 |
|
| 500 |
(defun general-read-show-context () |
| 501 |
(declare (values t)) |
| 502 |
(when (and *chaos-verbose* |
| 503 |
*reader-current-context* |
| 504 |
(not (eq *reader-void* *reader-current-context*))) |
| 505 |
(terpri) |
| 506 |
(princ "-- Expecting context is: ") |
| 507 |
(print-simple-princ-open *reader-current-context*) |
| 508 |
(unless *chaos-input-source* (terpri))) |
| 509 |
(when *chaos-input-source* ; nil means may be from terminal |
| 510 |
(terpri) |
| 511 |
(princ "-- file: ") (princ (namestring *chaos-input-source*)) |
| 512 |
(when (file-position *standard-input*) |
| 513 |
(princ " at character position: ") |
| 514 |
(prin1 (file-position *standard-input*))) |
| 515 |
(terpri) |
| 516 |
(when (and *reader-current-schema* |
| 517 |
(general-read-is-simple-schema *reader-current-schema*)) |
| 518 |
(princ " expecting: ") |
| 519 |
(general-read-print-schema-1 *reader-current-schema* :short) |
| 520 |
(terpri)) |
| 521 |
(when (and *reader-starting-position* |
| 522 |
(not (equal *reader-starting-position* |
| 523 |
(file-position *standard-input*)))) |
| 524 |
(princ " starting character position was: ") |
| 525 |
(prin1 *reader-starting-position*) |
| 526 |
(terpri)) |
| 527 |
(princ "-- Context:") |
| 528 |
(unless (eq *reader-void* *reader-input*) |
| 529 |
(princ *reader-input*)) |
| 530 |
(if (reader-is-at-eof) |
| 531 |
(princ " ... at end of file") |
| 532 |
(dotimes (i 20) |
| 533 |
(print-check) |
| 534 |
(princ #\space) |
| 535 |
(let ((val (read-sym))) |
| 536 |
(when (at-eof) |
| 537 |
(princ " [end of file]") |
| 538 |
(return)) |
| 539 |
(princ val) |
| 540 |
(when (equal "eof" val) (return))))) |
| 541 |
(terpri))) |
| 542 |
|
| 543 |
#|| |
| 544 |
(defun general-read-is-simple-schema (sch) |
| 545 |
(declare (type t sch) |
| 546 |
(values (or null t))) |
| 547 |
(or (atom sch) |
| 548 |
(and (consp sch) |
| 549 |
(every #'atom sch))) |
| 550 |
) |
| 551 |
||# |
| 552 |
|
| 553 |
(defun general-read-is-simple-schema (sch) |
| 554 |
(declare (ignore sch)) |
| 555 |
t) |
| 556 |
|
| 557 |
;;; modify print to certain depth and length transliterating notations |
| 558 |
;;; |
| 559 |
|
| 560 |
(defun general-read-display-schema (sch &optional (short nil)) |
| 561 |
(declare (type list sch) |
| 562 |
(values t)) |
| 563 |
(let ((limit (if short 10 most-positive-fixnum)) |
| 564 |
(count 0) |
| 565 |
(*print-level* (if short 2 nil))) |
| 566 |
(declare (type fixnum limit count)) |
| 567 |
(if (> (length sch) 1) |
| 568 |
(dolist (i (firstn sch 3)) |
| 569 |
(when (>= count limit) (princ " ...") (return)) |
| 570 |
(incf count) |
| 571 |
(print-check) |
| 572 |
(princ #\space) |
| 573 |
(prin1 i)) |
| 574 |
(dolist (i sch) |
| 575 |
(print-check) |
| 576 |
(princ #\space) |
| 577 |
(prin1 i))))) |
| 578 |
|
| 579 |
(defun general-read-print-schema-1 (s &optional (short nil)) |
| 580 |
(declare (type t s) |
| 581 |
(values t)) |
| 582 |
(if (atom s) |
| 583 |
(princ s) |
| 584 |
(let ((flag nil)) |
| 585 |
(declare (ignore flag)) |
| 586 |
(general-read-print-schema (car s) short)))) |
| 587 |
|
| 588 |
(defun general-read-print-schema (s &optional (short nil)) |
| 589 |
(declare (type t s) |
| 590 |
(values t)) |
| 591 |
(let ((limit (if short 10 most-positive-fixnum)) |
| 592 |
(count 0)) |
| 593 |
(declare (type fixnum limit count)) |
| 594 |
(if (atom s) |
| 595 |
(princ s) |
| 596 |
(let ((flag nil)) |
| 597 |
(dolist (i s) |
| 598 |
(when (>= count limit) (princ " ...") (return)) |
| 599 |
(incf count) |
| 600 |
(if (< *print-line-limit* (filecol *standard-output*)) |
| 601 |
(progn |
| 602 |
(print-next) |
| 603 |
(when *print-indent-contin* |
| 604 |
(princ " ") |
| 605 |
(setq flag t))) |
| 606 |
(if flag (princ " ") (setq flag t))) |
| 607 |
(if (atom i) |
| 608 |
(unless (eql control-d i) |
| 609 |
(prin1 i)) |
| 610 |
(if (eq ':+ (car i)) |
| 611 |
(dolist (e (cdr i)) |
| 612 |
(if (< *print-line-limit* (filecol *standard-output*)) |
| 613 |
(progn |
| 614 |
(print-next) |
| 615 |
(when *print-indent-contin* |
| 616 |
(princ " ") |
| 617 |
(setq flag t))) |
| 618 |
(if flag (princ " ") (setq flag t))) |
| 619 |
(prin1 e)) |
| 620 |
(prin1 i)))))))) |
| 621 |
|
| 622 |
(defun read-comment-line () |
| 623 |
(let ((ch (peek-char nil *standard-input* nil nil))) |
| 624 |
(unless ch (return-from read-comment-line " ")) |
| 625 |
(if (eq .reader-ch. 'return) |
| 626 |
(return-from read-comment-line (string #\linefeed)) |
| 627 |
(if (member ch '(#\linefeed #\page #\return #\newline)) |
| 628 |
(progn (read-char) |
| 629 |
(return-from read-comment-line (string #\linefeed))) |
| 630 |
(read-line))))) |
| 631 |
|
| 632 |
;;; an ignored comment (value is "") |
| 633 |
;;; has provision for long case: ** ( ) |
| 634 |
;;; |
| 635 |
(defun general-read-commentlong () |
| 636 |
(declare (values simple-string)) |
| 637 |
(let (ch) |
| 638 |
(unless (eql '\( .reader-ch.) |
| 639 |
(loop |
| 640 |
(setq ch (read-char *standard-input* nil *lex-eof*)) |
| 641 |
(unless (or (eql #\Space ch) |
| 642 |
(eql #\Tab ch)) |
| 643 |
(return))) |
| 644 |
(setq .reader-ch. |
| 645 |
(if (eq ch *lex-eof*) |
| 646 |
*lex-eof* |
| 647 |
(let ((val (reader-get-syntax ch))) |
| 648 |
(if val val ch))))) |
| 649 |
(if (eq '\( .reader-ch.) |
| 650 |
(lex-read) |
| 651 |
(unless (or (eql #\Newline ch) (eql #\Return ch)) (read-line)) |
| 652 |
)) |
| 653 |
(setq .reader-ch. 'space) |
| 654 |
"" |
| 655 |
) |
| 656 |
|
| 657 |
|
| 658 |
;;;============================================================================= |
| 659 |
;;; SPECIAL READERS |
| 660 |
;;; readers specific to external representations of Chaos expressions. |
| 661 |
;;;============================================================================= |
| 662 |
|
| 663 |
|
| 664 |
;;; READ-TERM-FROM-STRING : string -> List(Token) |
| 665 |
;;; |
| 666 |
;;; (declaim (function read-term-from-string (string) list)) |
| 667 |
(declaim (inline read-term-from-string)) |
| 668 |
|
| 669 |
; (eval-when (:execute :compile-toplevel :load-toplevel) |
| 670 |
; (defparameter .term-delimiting-chars. |
| 671 |
; '("[" "]" "{" "}" ";" "@" "%" "~" ))) |
| 672 |
(eval-when (:execute :compile-toplevel :load-toplevel) |
| 673 |
(defparameter .term-delimiting-chars. '("[" "]" "{" "}"))) |
| 674 |
|
| 675 |
(defun !set-term-delim-chars () |
| 676 |
(!set-single-reader .term-delimiting-chars.)) |
| 677 |
|
| 678 |
(defun read-term-from-string (string) |
| 679 |
(declare (type simple-string string)) |
| 680 |
(with-input-from-string (*standard-input* string) |
| 681 |
(let ((cur (!set-term-delim-chars))) |
| 682 |
(let ((res nil) |
| 683 |
(inp nil) |
| 684 |
(inv nil)) |
| 685 |
(loop (setq inp (lex-read)) |
| 686 |
(when #+:CCL-3 (equal *lex-eof* inp) |
| 687 |
#-:CCL-3 (eq *lex-eof* inp) |
| 688 |
(setq *reader-input* inv) |
| 689 |
(return)) |
| 690 |
(cond ((= 1 (length (the list inp))) (setq inv (car inp))) |
| 691 |
(t (setq inv *reader-void*))) |
| 692 |
(setq res (append res inp)) |
| 693 |
) |
| 694 |
(!set-reader cur) |
| 695 |
(clear-input) |
| 696 |
(setq *reader-input* *reader-void*) |
| 697 |
res |
| 698 |
)))) |
| 699 |
|
| 700 |
(defun read-seq-of-term-from-string (string) |
| 701 |
(declare (type simple-string string) |
| 702 |
(values list)) |
| 703 |
(with-input-from-string (*standard-input* string) |
| 704 |
(let ((cur (!set-term-delim-chars))) |
| 705 |
(let ((res nil)) |
| 706 |
(block exit |
| 707 |
;; read in one token. |
| 708 |
(if (eq *reader-input* *reader-void*) |
| 709 |
(setq *reader-input* (lex-read)) |
| 710 |
(when (equal "(" *reader-input*) |
| 711 |
(setq *reader-input* (lex-read-rest-of-list)))) |
| 712 |
(when (reader-is-at-eof) (return-from exit)) |
| 713 |
(when (atom *reader-input*) |
| 714 |
(setq *reader-input* (list *reader-input*))) |
| 715 |
(loop (setq res (append res *reader-input*)) |
| 716 |
(setq *reader-input* (lex-read)) |
| 717 |
(when (reader-is-at-eof) (return-from exit))) |
| 718 |
) |
| 719 |
;; restore read table. |
| 720 |
(!set-reader cur) |
| 721 |
(clear-input) |
| 722 |
(setq *reader-input* *reader-void*) |
| 723 |
res |
| 724 |
)))) |
| 725 |
|
| 726 |
;;; READ-TERM-AT-TOP |
| 727 |
;;; |
| 728 |
(defun read-term-at-top (&rest ignore) |
| 729 |
(declare (ignore ignore)) |
| 730 |
(let ((lines (read-lines))) |
| 731 |
(if (eq lines *lex-eof*) |
| 732 |
*lex-eof* |
| 733 |
(read-term-from-string lines)))) |
| 734 |
|
| 735 |
(defun read-seq-of-term-at-top (&rest ignore) |
| 736 |
(declare (ignore ignore)) |
| 737 |
(let ((lines (read-lines))) |
| 738 |
(if #+:MCL (equal lines *lex-eof*) |
| 739 |
#-:MCL (eq lines *lex-eof*) |
| 740 |
*lex-eof* |
| 741 |
(read-seq-of-term-from-string lines)))) |
| 742 |
|
| 743 |
;;; READ-TERM |
| 744 |
;;; |
| 745 |
(defun read-term (context) |
| 746 |
(declare (type list context) |
| 747 |
(values list)) |
| 748 |
(let ((cur (!set-term-delim-chars))) |
| 749 |
(let ((res nil) |
| 750 |
inp |
| 751 |
inv) |
| 752 |
(loop (setq inp (lex-read)) |
| 753 |
(when #-:ccl-3(eq *lex-eof* inp) |
| 754 |
#+:ccl-3(equal *lex-eof* inp) |
| 755 |
(return-from read-term *lex-eof*)) |
| 756 |
(cond ((= 1 (length (the list inp))) |
| 757 |
(setq inv (car (the list inp)))) |
| 758 |
(t (setq inv *reader-void*))) |
| 759 |
(when (lex-string-match inv (car context)) |
| 760 |
(setq *reader-input* inv) |
| 761 |
(return)) |
| 762 |
(setq res (append res inp)) |
| 763 |
) |
| 764 |
(!set-reader cur) |
| 765 |
res |
| 766 |
))) |
| 767 |
|
| 768 |
;;; READ-SEQ-OF-TERM |
| 769 |
;;; |
| 770 |
(defun read-seq-of-term (context) |
| 771 |
(declare (type list context) |
| 772 |
(values list)) |
| 773 |
(let ((cur (!set-term-delim-chars))) |
| 774 |
(let ((res nil)) |
| 775 |
;; read in one token. |
| 776 |
(if (eq *reader-input* *reader-void*) |
| 777 |
(setq *reader-input* (lex-read)) |
| 778 |
(when (equal "(" *reader-input*) |
| 779 |
(setq *reader-input* (lex-read-rest-of-list)))) |
| 780 |
(when (at-eof-or-control-d) ; was reader-is-at-eof |
| 781 |
(return-from read-seq-of-term *lex-eof*)) |
| 782 |
(when (atom *reader-input*) |
| 783 |
(setq *reader-input* (list *reader-input*))) |
| 784 |
(loop (when (and (null (cdr *reader-input*)) |
| 785 |
(stringp (car *reader-input*)) |
| 786 |
(lex-string-match (car *reader-input*) (car context))) |
| 787 |
(setq *reader-input* (car *reader-input*)) |
| 788 |
(return)) |
| 789 |
(setq res (append res *reader-input*)) |
| 790 |
(setq *reader-input* (lex-read)) |
| 791 |
(when (at-eof-or-control-d) ; was reader-is-at-eof |
| 792 |
(return-from read-seq-of-term *lex-eof*)) |
| 793 |
) |
| 794 |
;; restore read table. |
| 795 |
(!set-reader cur) |
| 796 |
res |
| 797 |
))) |
| 798 |
|
| 799 |
;;; READ-ARGS |
| 800 |
;;; |
| 801 |
(defun read-args (&rest context) |
| 802 |
(declare (ignore context)) |
| 803 |
(let ((*live-newline* t)) |
| 804 |
(let ((res nil) |
| 805 |
(inv nil) |
| 806 |
inp) |
| 807 |
(loop (setq inp (lex-read)) |
| 808 |
(when #-:ccl-3(eq *lex-eof* inp) |
| 809 |
#+:ccl-3(equal *lex-eof* inp) |
| 810 |
(return-from read-args *lex-eof*)) |
| 811 |
(cond ((= 1 (length (the list inp))) |
| 812 |
(setq inv (car (the list inp)))) |
| 813 |
(t (setq inv *reader-void*))) |
| 814 |
(when (and (consp inv) (eq (car inv) .String-token.)) |
| 815 |
(setq res (append res inp)) |
| 816 |
(setq *reader-input* *reader-void*) |
| 817 |
(return)) |
| 818 |
(when (lex-string-match inv 'return) |
| 819 |
(setq *reader-input* *reader-void*) |
| 820 |
(return)) |
| 821 |
(setq res (append res inp))) |
| 822 |
res))) |
| 823 |
|
| 824 |
;;; READ-SEQ-OF-OPNAME |
| 825 |
;;; |
| 826 |
; (defparameter .op-name-delimiting-chars. |
| 827 |
; '("[" "]" "{" "}" "_" ";" "@" "%" "~")) |
| 828 |
(defparameter .op-name-delimiting-chars. '("[" "]" "{" "}" "_")) |
| 829 |
|
| 830 |
(defun read-seq-of-opname (context) |
| 831 |
(declare (type list context) |
| 832 |
(values list)) |
| 833 |
(let ((cur (!set-single-reader .op-name-delimiting-chars.))) |
| 834 |
(let ((res nil)) |
| 835 |
(if (eq *reader-input* *reader-void*) |
| 836 |
(setq *reader-input* (lex-read)) |
| 837 |
(when (equal "(" *reader-input*) |
| 838 |
(setq *reader-input* (lex-read-rest-of-list)))) |
| 839 |
(when ; (reader-is-at-eof) |
| 840 |
(at-eof-or-control-d) |
| 841 |
(return-from read-seq-of-opname *lex-eof*)) |
| 842 |
(when (atom *reader-input*) |
| 843 |
(setq *reader-input* (list *reader-input*))) |
| 844 |
(loop (when (and (null (cdr *reader-input*)) |
| 845 |
(stringp (car *reader-input*)) |
| 846 |
(lex-string-match (car *reader-input*) (car context))) |
| 847 |
(setq *reader-input* (car *reader-input*)) |
| 848 |
(return)) |
| 849 |
(setq res (append res *reader-input*)) |
| 850 |
(setq *reader-input* (lex-read)) |
| 851 |
(when (at-eof-or-control-d) ; (reader-is-at-eof) |
| 852 |
(return-from read-seq-of-opname *lex-eof*)) |
| 853 |
) |
| 854 |
(!set-reader cur) |
| 855 |
res |
| 856 |
))) |
| 857 |
|
| 858 |
;;; READ-OPNAME context |
| 859 |
;;; |
| 860 |
(defun read-opname (context) |
| 861 |
(declare (type list context) |
| 862 |
(values list)) |
| 863 |
(let ((cur (!set-single-reader .op-name-delimiting-chars.))) |
| 864 |
(let ((res nil) |
| 865 |
inp |
| 866 |
inv) |
| 867 |
(loop (setq inp (lex-read)) |
| 868 |
(when (eq *lex-eof* inp) |
| 869 |
(return-from read-opname *lex-eof*)) |
| 870 |
(cond ((= 1 (length (the list inp))) (setq inv (car inp))) |
| 871 |
(t (setq inv *reader-void*))) |
| 872 |
(when (lex-string-match inv (car context)) |
| 873 |
(setq *reader-input* inv) |
| 874 |
(return)) |
| 875 |
(setq res (append res inp)) |
| 876 |
) |
| 877 |
(!set-reader cur) |
| 878 |
res |
| 879 |
))) |
| 880 |
|
| 881 |
(defun read-opname-at-top (&rest ignore) |
| 882 |
(declare (ignore ignore)) |
| 883 |
(let ((line (read-lines))) |
| 884 |
(if (eq line *lex-eof*) |
| 885 |
*lex-eof* |
| 886 |
(read-opname-from-string line)))) |
| 887 |
|
| 888 |
(defun read-seq-of-opname-at-top (&rest ignore) |
| 889 |
(declare (ignore ignore)) |
| 890 |
(let ((line (read-lines))) |
| 891 |
(if (eq line *lex-eof*) |
| 892 |
*lex-eof* |
| 893 |
(read-seq-of-opname-from-string line)))) |
| 894 |
|
| 895 |
(defun read-opname-from-string (string) |
| 896 |
(declare (type simple-string string)) |
| 897 |
(with-input-from-string (*standard-input* string) |
| 898 |
(let ((cur (!set-single-reader .op-name-delimiting-chars.))) |
| 899 |
(let ((res nil) |
| 900 |
(inp nil) |
| 901 |
(inv nil)) |
| 902 |
(loop (setq inp (lex-read)) |
| 903 |
(when (eq *lex-eof* inp) |
| 904 |
(setq *reader-input* inv) |
| 905 |
(return)) |
| 906 |
(cond ((= 1 (length (the list inp))) (setq inv (car inp))) |
| 907 |
(t (setq inv *reader-void*))) |
| 908 |
(setq res (append res inp)) |
| 909 |
) |
| 910 |
(!set-reader cur) |
| 911 |
(clear-input) |
| 912 |
(setq *reader-input* *reader-void*) |
| 913 |
res |
| 914 |
)))) |
| 915 |
|
| 916 |
(defun read-seq-of-opname-from-string (string) |
| 917 |
(declare (type simple-string string) |
| 918 |
(values list)) |
| 919 |
(with-input-from-string (*standard-input* string) |
| 920 |
(let ((cur (!set-single-reader .op-name-delimiting-chars.))) |
| 921 |
(let ((res nil)) |
| 922 |
(block exit |
| 923 |
;; read in one token. |
| 924 |
(if (eq *reader-input* *reader-void*) |
| 925 |
(setq *reader-input* (lex-read)) |
| 926 |
(when (equal "(" *reader-input*) |
| 927 |
(setq *reader-input* (lex-read-rest-of-list)))) |
| 928 |
(when (reader-is-at-eof) (return-from exit)) |
| 929 |
(when (atom *reader-input*) |
| 930 |
(setq *reader-input* (list *reader-input*))) |
| 931 |
(loop (setq res (append res *reader-input*)) |
| 932 |
(setq *reader-input* (lex-read)) |
| 933 |
(when (reader-is-at-eof) (return-from exit))) |
| 934 |
) |
| 935 |
;; restore read table. |
| 936 |
(!set-reader cur) |
| 937 |
(clear-input) |
| 938 |
(setq *reader-input* *reader-void*) |
| 939 |
res |
| 940 |
)))) |
| 941 |
|
| 942 |
;;; READ-SORT |
| 943 |
;;; |
| 944 |
(defun read-sort (c) |
| 945 |
(declare (ignore c)) |
| 946 |
(let ((old-syntax (reader-get-syntax #\!))) |
| 947 |
(unwind-protect |
| 948 |
(let ((inp nil)) |
| 949 |
(!set-syntax #\! nil) |
| 950 |
(setq inp (!read-sym)) |
| 951 |
(cond ((and (stringp inp) |
| 952 |
(eql #\. (char (the simple-string inp) |
| 953 |
(1- (length (the simple-string inp)))))) |
| 954 |
(loop (unless (eq 'space .reader-ch.) (return)) ;a bit ugly |
| 955 |
(setq .reader-ch. (reader-get-char *standard-input*))) |
| 956 |
(when (eq .reader-ch. *lex-eof*) |
| 957 |
(return-from read-sort *lex-eof*)) |
| 958 |
(if (equal '\( .reader-ch.) |
| 959 |
(let ((rest (lex-read))) |
| 960 |
(if (eq rest *lex-eof*) |
| 961 |
*lex-eof* |
| 962 |
(list* inp (lex-read)))) |
| 963 |
inp)) |
| 964 |
(t inp) |
| 965 |
)) |
| 966 |
(!set-syntax #\! old-syntax)))) |
| 967 |
|
| 968 |
;;; READ-SORTS |
| 969 |
;;; |
| 970 |
(defun read-sorts (context) |
| 971 |
(let ((res nil) |
| 972 |
(old-syntax (reader-get-syntax #\!))) |
| 973 |
(unwind-protect |
| 974 |
(progn (!set-syntax #\! nil) |
| 975 |
(loop (!read-in) |
| 976 |
(when (at-eof-or-control-d) |
| 977 |
(return-from read-sorts *lex-eof*)) |
| 978 |
(when (lex-string-match *reader-input* (car context)) |
| 979 |
(return (nreverse res))) |
| 980 |
(push (read-sort context) res))) |
| 981 |
(!set-syntax #\! old-syntax)))) |
| 982 |
|
| 983 |
;;; READ-CHARS |
| 984 |
;;; |
| 985 |
(defun read-chars (context) |
| 986 |
(let ((res nil)) |
| 987 |
(loop (!read-in) |
| 988 |
(when (at-eof-or-control-d) |
| 989 |
(return-from read-chars *lex-eof*)) |
| 990 |
(when (lex-string-match *reader-input* (car context)) |
| 991 |
(return-from read-chars (nreverse res))) |
| 992 |
(let ((c (!read-sym))) |
| 993 |
;;(format t "~%- read-chars: sym=~s, res=~s" c res) |
| 994 |
(if (consp c) |
| 995 |
(push (car c) res) |
| 996 |
(push c res)))))) |
| 997 |
|
| 998 |
;;; SPECIAL READERS NOT Spported by Chaos General Reader |
| 999 |
;;; |
| 1000 |
|
| 1001 |
(defun read-opattr (c) |
| 1002 |
(declare (type list c) |
| 1003 |
(values list)) |
| 1004 |
(!read-in) |
| 1005 |
(when (at-eof-or-control-d) (return-from read-opattr *lex-eof*)) |
| 1006 |
(if (lex-string-match *reader-input* #\[) |
| 1007 |
;; |
| 1008 |
() |
| 1009 |
;; |
| 1010 |
(progn |
| 1011 |
(reader-suppress-ch c) |
| 1012 |
nil))) |
| 1013 |
|
| 1014 |
;;; |
| 1015 |
(defun read-super-exp (c) |
| 1016 |
(declare (type list c) |
| 1017 |
(values list)) |
| 1018 |
(let ((cur (!set-single-reader '(#\( #\))))) |
| 1019 |
(prog1 (read-superexp c) |
| 1020 |
(!set-reader cur)))) |
| 1021 |
|
| 1022 |
(defun read-superexp (c) |
| 1023 |
(declare (type list c) |
| 1024 |
(values list)) |
| 1025 |
(let ((res nil)) |
| 1026 |
(loop (!read-in) |
| 1027 |
(when (at-eof-or-control-d) (general-read-eof-error)) |
| 1028 |
(when (general-read-string-matches *reader-input* c) |
| 1029 |
(return res)) |
| 1030 |
(setq res (nconc res (read-superexpr-delimited)))) |
| 1031 |
res)) |
| 1032 |
|
| 1033 |
(defun read-superexpr-delimited () |
| 1034 |
(declare (values list)) |
| 1035 |
(!read-in) |
| 1036 |
(when (at-eof-or-control-d) (general-read-eof-error)) |
| 1037 |
(let ((pr (assoc *reader-input* '(("(" ")")) |
| 1038 |
:test #'general-read-string-matches))) |
| 1039 |
(cond ((null pr) |
| 1040 |
(prog1 (cons *reader-input* nil) |
| 1041 |
(!read-discard))) |
| 1042 |
(t (let ((sym *reader-input*)) |
| 1043 |
(!read-discard) |
| 1044 |
(let ((lst (read-superexp (cdr pr)))) |
| 1045 |
(prog1 (cons sym (append lst (cons *reader-input* nil))) |
| 1046 |
(!read-discard))))) |
| 1047 |
))) |
| 1048 |
;;; |
| 1049 |
;;; Module Expression Reader |
| 1050 |
;;; |
| 1051 |
;;; *modexp-parse-input* -- binds module expression input (list of tokens). |
| 1052 |
;;; |
| 1053 |
(defvar *modexp-parse-input* 'undef) |
| 1054 |
|
| 1055 |
;;; MODEXP-SKIP : {*modexp-parse-input*} -> {*modexp-parse-input*} |
| 1056 |
;;; skip input one. |
| 1057 |
;;; |
| 1058 |
(defmacro modexp-skip () `(setq *modexp-parse-input* (cdr *modexp-parse-input*))) |
| 1059 |
|
| 1060 |
;;;************ |
| 1061 |
;;; Some utils__________________________________________________________________ |
| 1062 |
;;;************ |
| 1063 |
|
| 1064 |
;;; SCAN-PRENTHESIZED-UNIT : tokens -> LIST[tokens], signal 'unbalanced |
| 1065 |
;;; |
| 1066 |
;;; (scan-parenthesized-unit '( "(" "aho" "(" "baka" "tako" ")" "manuke" ")" "baba")) |
| 1067 |
;;; ==> ("aho" "(" "baka" "tako" ")" "manuke") |
| 1068 |
;;; ("baba") |
| 1069 |
;;; (scan-parenthesized-unit '( "(" "aho" "(" "baka" "tako" ")" "manuke" "baba")) |
| 1070 |
;;; ==> UNBALANCED |
| 1071 |
;;; ("(" "aho" "(" "baka" "tako" ")" "manuke" "baba") |
| 1072 |
;;; |
| 1073 |
(defun scan-parenthesized-unit (tokens) |
| 1074 |
(declare (type list tokens) |
| 1075 |
(values (or symbol list) list)) |
| 1076 |
(if (equal "(" (car tokens)) |
| 1077 |
(let ((count 1) |
| 1078 |
(lst (cdr tokens)) |
| 1079 |
(res nil) |
| 1080 |
tok) |
| 1081 |
(declare (type fixnum count)) |
| 1082 |
(loop (when (null lst) (return (values 'unbalanced tokens))) |
| 1083 |
(setf tok (car lst) |
| 1084 |
lst (cdr lst)) |
| 1085 |
(when (and (= 1 count) (equal ")" tok)) |
| 1086 |
(return (values (nreverse res) lst))) |
| 1087 |
(setf res (cons tok res)) |
| 1088 |
(if (equal "(" tok) |
| 1089 |
(incf count) |
| 1090 |
(if (equal ")" tok) |
| 1091 |
(decf count))) |
| 1092 |
)) |
| 1093 |
(values (list (car tokens)) (cdr tokens)))) |
| 1094 |
|
| 1095 |
(defun group-paren-units (tokens) |
| 1096 |
(declare (type list tokens) |
| 1097 |
(values list)) |
| 1098 |
(let ((res nil) |
| 1099 |
(lst tokens) |
| 1100 |
unit) |
| 1101 |
(loop (multiple-value-setq (unit lst) |
| 1102 |
(scan-parenthesized-unit lst)) |
| 1103 |
(when (eq 'unbalanced unit) |
| 1104 |
(return tokens)) |
| 1105 |
(setq res (cons unit res)) |
| 1106 |
(when (null lst) |
| 1107 |
(return (nreverse res))) |
| 1108 |
))) |
| 1109 |
|
| 1110 |
(defun check-enclosing-parens (tokens) |
| 1111 |
(declare (type t tokens) |
| 1112 |
(values (or null t))) |
| 1113 |
(and (consp tokens) |
| 1114 |
(equal "(" (car tokens)) |
| 1115 |
(multiple-value-bind (par rst) |
| 1116 |
(scan-parenthesized-unit tokens) |
| 1117 |
(declare (ignore par)) |
| 1118 |
(null rst)))) |
| 1119 |
|
| 1120 |
;;; READ-MODULE-EXP |
| 1121 |
|
| 1122 |
(defun read-module-exp (context) |
| 1123 |
(let ((cur (!set-single-reader '(#\[ #\])))) |
| 1124 |
(prog1 (read-modexp context) |
| 1125 |
(!set-reader cur)))) |
| 1126 |
|
| 1127 |
;;; read term with balanced: (/) [/] terminated by c context |
| 1128 |
;;; |
| 1129 |
(defun read-modexp (c) |
| 1130 |
(let ((cur (!set-single-reader '("_")))) |
| 1131 |
(let ((res nil)) |
| 1132 |
(loop (!read-in) |
| 1133 |
(when (at-eof-or-control-d) (general-read-eof-error)) |
| 1134 |
(when (general-read-string-matches *reader-input* c) |
| 1135 |
(return res)) |
| 1136 |
(setq res (nconc res (read-modexp-delimited)))) |
| 1137 |
(!set-reader cur) |
| 1138 |
res |
| 1139 |
))) |
| 1140 |
|
| 1141 |
;;; also used for CafeOBJ view declaration. |
| 1142 |
|
| 1143 |
(defun read-modexp-delimited () |
| 1144 |
(declare (values list)) |
| 1145 |
(!read-in) |
| 1146 |
;; (when (at-eof-or-control-d) (general-read-eof-error)) |
| 1147 |
(let ((pr (assoc *reader-input* |
| 1148 |
'(("view" "}") ("[" "]") ("(" ")") ) |
| 1149 |
:test #'general-read-string-matches))) |
| 1150 |
;; (format t "~&*reader-input* ~s" *reader-input*) |
| 1151 |
(cond ((null pr) |
| 1152 |
(prog1 (cons *reader-input* nil) |
| 1153 |
(!read-discard))) |
| 1154 |
(t (let ((sym *reader-input*)) |
| 1155 |
(!read-discard) |
| 1156 |
(let ((lst (read-modexp (cdr pr)))) |
| 1157 |
(prog1 (cons sym (append lst (cons *reader-input* nil))) |
| 1158 |
(!read-discard))))) |
| 1159 |
))) |
| 1160 |
|
| 1161 |
;;; READ-MODEXP-FROM-STRING |
| 1162 |
|
| 1163 |
(defun read-modexp-from-string (string) |
| 1164 |
(declare (type simple-string string) |
| 1165 |
(values list)) |
| 1166 |
(let ((*live-newline* nil)) |
| 1167 |
(with-input-from-string (*standard-input* string) |
| 1168 |
(let ((cur (!set-single-reader '(#\[ #\] #\_ #\{ #\}))) |
| 1169 |
(res nil)) |
| 1170 |
(loop (!read-in) |
| 1171 |
(when (at-eof-or-control-d) (return)) |
| 1172 |
(setq res (nconc res (read-modexp-delimited)))) |
| 1173 |
(!set-reader cur) |
| 1174 |
(clear-input) |
| 1175 |
(setq *reader-input* *reader-void*) |
| 1176 |
res)))) |
| 1177 |
|
| 1178 |
#|| |
| 1179 |
(defun module-print-top-level-choices () |
| 1180 |
(let ((flag nil)) |
| 1181 |
(dolist (i '( |
| 1182 |
"module" "mod" "view" "reduce" "red" |
| 1183 |
"make" "test" "input" "in" "-->" |
| 1184 |
"**>" "--" "**" "parse" "match" "ev" "lisp" |
| 1185 |
"show" "sh" "set" "do" "select" "open" "close" "eof" |
| 1186 |
"let" "choose" |
| 1187 |
"quit" "q" "start" "apply" "cd" "ls" "pwd" )) |
| 1188 |
(if (< *print-line-limit* (filecol *standard-output*)) |
| 1189 |
(progn |
| 1190 |
(terpri) |
| 1191 |
(when *print-indent-contin* |
| 1192 |
(princ " ") |
| 1193 |
(setq flag t))) |
| 1194 |
(if flag (princ " ") (setq flag t))) |
| 1195 |
(princ i)))) |
| 1196 |
||# |
| 1197 |
|
| 1198 |
;;; |
| 1199 |
(defvar *interactive-session* nil) |
| 1200 |
(defun set-interactive () (setf *interactive-session* t)) |
| 1201 |
(defun off-interactive () (setf *interactive-session* nil)) |
| 1202 |
|
| 1203 |
(defun wait-until-non-white (stream) |
| 1204 |
(declare (type stream) |
| 1205 |
(values t)) |
| 1206 |
(if (at-top-level) |
| 1207 |
(loop (when (not (or (eq 'space .reader-ch.) (eq 'return .reader-ch.))) |
| 1208 |
(setf *sub-prompt* t) (return)) |
| 1209 |
(if (eq 'return .reader-ch.) |
| 1210 |
(progn |
| 1211 |
(when *sub-prompt* |
| 1212 |
(princ "> ") |
| 1213 |
(force-output)) |
| 1214 |
(reader-get-char stream) |
| 1215 |
(when (eq #\? .reader-ch.) |
| 1216 |
(let ((*chaos-verbose* t)) |
| 1217 |
(general-read-show-context) |
| 1218 |
(clear-input) |
| 1219 |
(force-output)) |
| 1220 |
(setf .reader-ch. 'space))) |
| 1221 |
(reader-get-char stream))) |
| 1222 |
(loop (when (not (or (eq 'space .reader-ch.) (eq 'return .reader-ch.))) |
| 1223 |
(return)) |
| 1224 |
(reader-get-char stream)))) |
| 1225 |
;;; EOF |