Add input-line operation to input strings.
authorChris Hanson <org/chris-hanson/cph>
Wed, 8 Mar 2017 06:11:03 +0000 (22:11 -0800)
committerChris Hanson <org/chris-hanson/cph>
Wed, 8 Mar 2017 06:11:03 +0000 (22:11 -0800)
src/runtime/stringio.scm

index 775fe2f341f2bd7ab2007c5029ab63c8ae6d3ad6..98afab45d5beea080aebe3c6ec8275f8185cf696 100644 (file)
@@ -42,24 +42,26 @@ USA.
   (let* ((end (fix:end-index end (string-length string) 'open-input-string))
         (start (fix:start-index start end 'open-input-string)))
     (make-textual-port string-input-type
-                      (make-istate string start end start))))
+                      (make-istate string start end start 0))))
 
 (define-structure istate
   (string #f read-only #t)
   (start #f read-only #t)
   (end #f read-only #t)
-  next)
+  next
+  line-number)
 
 (define (make-string-input-type)
   (make-textual-port-type `((char-ready? ,string-in/char-ready?)
                            (eof? ,string-in/eof?)
+                           (input-line ,string-in/input-line)
                            (peek-char ,string-in/peek-char)
                            (read-char ,string-in/read-char)
                            (read-substring ,string-in/read-substring)
                            (unread-char ,string-in/unread-char)
                            (write-self ,string-in/write-self))
                          #f))
-
+\f
 (define (string-in/char-ready? port)
   port
   #t)
@@ -79,9 +81,14 @@ USA.
     (if (fix:< (istate-next ss) (istate-end ss))
        (let ((char (string-ref (istate-string ss) (istate-next ss))))
          (set-istate-next! ss (fix:+ (istate-next ss) 1))
+         (if (char=? char #\newline)
+             (set-istate-line-number! ss (fix:+ 1 (istate-line-number ss))))
          char)
        (make-eof-object port))))
 
+(define (string-in/input-line port)
+  (istate-line-number (textual-port-state port)))
+
 (define (string-in/read-substring port string start end)
   (let ((ss (textual-port-state port)))
     (let ((string* (istate-string ss))