X-Git-Url: https://git.distorted.org.uk/~mdw/sod/blobdiff_plain/3109662aca9c06495ac22c5c58b46e1c036aca5c..4b8e5c0347115ff30841f1d1e71afe59ecb6c82c:/src/parser/scanner-charbuf-impl.lisp diff --git a/src/parser/scanner-charbuf-impl.lisp b/src/parser/scanner-charbuf-impl.lisp index 9cafc3d..1919b69 100644 --- a/src/parser/scanner-charbuf-impl.lisp +++ b/src/parser/scanner-charbuf-impl.lisp @@ -28,8 +28,9 @@ ;;;-------------------------------------------------------------------------- ;;; Infrastructure types. -(defconstant charbuf-size 4096 - "Number of characters in a character buffer.") +(eval-when (:compile-toplevel :load-toplevel :execute) + (defconstant charbuf-size 4096 + "Number of characters in a character buffer.")) (deftype charbuf () "Type of character buffers." @@ -59,24 +60,12 @@ (buf nil :type (or charbuf (member nil :eof)) :read-only t) (size 0 :type charbuf-index :read-only t)) -(export 'charbuf-scanner-place-p) -(defstruct charbuf-scanner-place - "A captured place we can return to later. - - We remember the buffer-chain link, so that we can retrace our steps up to - the present. We also need the index at which we continue reading - characters; and the line and column numbers to resume from." - (link nil :type charbuf-chain-link :read-only t) - (index 0 :type charbuf-index :read-only t) - (line 0 :type fixnum :read-only t) - (column 0 :type fixnum :read-only t)) - ;;;-------------------------------------------------------------------------- ;;; Main class. (export 'charbuf-scanner) (defclass charbuf-scanner (character-scanner) - ((stream :initarg :stream :type stream) + ((%stream :initarg :stream :type stream) (buf :initform nil :type (or charbuf (member nil :eof))) (size :initform 0 :type (integer 0 #.charbuf-size)) (index :initform 0 :type (integer 0 #.charbuf-size)) @@ -121,6 +110,25 @@ captured places properly when he's finished. In practice, this is usually done using the `peek' parser macro so there isn't a problem.")) +(export 'charbuf-scanner-place-p) +(defstruct charbuf-scanner-place + "A captured place we can return to later. + + We remember the buffer-chain link, so that we can retrace our steps up to + the present. We also need the index at which we continue reading + characters; and the line and column numbers to resume from." + (scanner nil :type charbuf-scanner :read-only t) + (link nil :type charbuf-chain-link :read-only t) + (index 0 :type charbuf-index :read-only t) + (line 0 :type fixnum :read-only t) + (column 0 :type fixnum :read-only t)) + +(defmethod file-location ((place charbuf-scanner-place)) + (make-file-location (scanner-filename + (charbuf-scanner-place-scanner place)) + (charbuf-scanner-place-line place) + (charbuf-scanner-place-column place))) + ;;;-------------------------------------------------------------------------- ;;; Utilities. @@ -135,7 +143,7 @@ (if we're currently rewound) or with a new buffer from the stream.")) (defmethod charbuf-scanner-fetch ((scanner charbuf-scanner)) - (with-slots (stream buf size index tail captures) scanner + (with-slots ((stream %stream) buf size index tail captures) scanner (loop (acond @@ -246,7 +254,7 @@ ;; Grab the filename from the underlying stream if we don't have a better ;; guess. (default-slot (scanner 'filename slot-names) - (with-slots (stream) scanner + (with-slots ((stream %stream)) scanner (aif (stream-pathname stream) (namestring it) nil))) ;; Get ready with the first character. @@ -316,7 +324,7 @@ (incf captures) (unless tail (setf tail (make-charbuf-chain-link :buf buf :size size))) - (make-charbuf-scanner-place :link tail :index index + (make-charbuf-scanner-place :scanner scanner :link tail :index index :line line :column column))) (defmethod scanner-restore-place ((scanner charbuf-scanner) place) @@ -330,6 +338,7 @@ tail link)))) (defmethod scanner-release-place ((scanner charbuf-scanner) place) + (declare (ignore place)) (with-slots (captures) scanner (decf captures))) @@ -365,7 +374,8 @@ (let* ((slices nil) (place-b (or place-b (with-slots (index tail) scanner - (make-charbuf-scanner-place :link tail + (make-charbuf-scanner-place :scanner scanner + :link tail :index index)))) (last-link (charbuf-scanner-place-link place-b))) (flet ((bad () @@ -399,7 +409,8 @@ (make-instance 'charbuf-scanner-stream :scanner scanner)) (defmethod stream-read-sequence - ((stream charbuf-scanner-stream) (seq string) &optional (start 0) end) + ((stream charbuf-scanner-stream) (seq string) + #+clisp &key #-clisp &optional (start 0) end) (with-slots (scanner) stream (unless end (setf end (length seq))) (let ((i start) (n (- end start)))