chiark / gitweb /
src/method-aggregate.lisp: Give aggregating combinations their own file.
[sod] / src / parser / scanner-charbuf-impl.lisp
index 2d7a4ae4ed8caf3aee99e8527a643a269e179bbc..65f6e1ea4bd76bbc3fcffaf4b771a65c074f93e2 100644 (file)
@@ -28,8 +28,9 @@ (cl:in-package #:sod-parser)
 ;;;--------------------------------------------------------------------------
 ;;; 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,18 +60,6 @@ (defstruct charbuf-chain-link
   (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.
 
@@ -121,6 +110,25 @@ (defclass charbuf-scanner (character-scanner)
    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.
 
@@ -190,10 +198,10 @@ (defgeneric charbuf-scanner-map (scanner func &optional fail)
    and wishes to read more.  If DONEP is true then the condition (<= START
    USED END) must hold; the FUNC has consumed the buffer as far as USED
    (exclusive) and has completed successfully; the values DONEP and `t' are
-   returned as the result of CHARBUF-SCANNER-MAP.
+   returned as the result of `charbuf-scanner-map'.
 
    If end-of-file is encountered before FUNC completes successfully then FAIL
-   is called with no arguments, and CHARBUF-SCANNER-MAP returns whatever
+   is called with no arguments, and `charbuf-scanner-map' returns whatever
    FAIL returns.
 
    Observe that, if FAIL returns a second value of nil, then
@@ -316,7 +324,7 @@ (defmethod scanner-capture-place ((scanner charbuf-scanner))
     (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 @@ (defmethod scanner-restore-place ((scanner charbuf-scanner) place)
            tail link))))
 
 (defmethod scanner-release-place ((scanner charbuf-scanner) place)
+  (declare (ignore place))
   (with-slots (captures) scanner
     (decf captures)))
 
@@ -365,11 +374,12 @@ (defmethod scanner-interval
   (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 ()
-            (error "Incorrect places ~S and ~S to SCANNER-INTERVAL."
+            (error "Incorrect places ~S and ~S to `scanner-interval'."
                    place-a place-b)))
       (do ((link (charbuf-scanner-place-link place-a)
                 (charbuf-chain-link-next link))
@@ -399,7 +409,8 @@ (defmethod make-scanner-stream ((scanner charbuf-scanner))
   (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)))