diff options
Diffstat (limited to 'cursor.lisp')
| -rw-r--r-- | cursor.lisp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/cursor.lisp b/cursor.lisp new file mode 100644 index 0000000..321ec3b --- /dev/null +++ b/cursor.lisp @@ -0,0 +1,40 @@ +(in-package #:monparser) + +(defclass text () + ((index :type (unsigned-byte 44) :initarg :index :accessor index :initform 0) + (data :type simple-string :initarg :data :reader data :initform ""))) + +(defun has-data? (cursor) + (< (index cursor) (length (data cursor)))) + +(defun peek (cursor) + (char (data cursor) + (index cursor))) + +(defun advance (cursor) + (make-instance 'text + :data (data cursor) + :index (+ (index cursor) 1))) + +(defun distance (from to) + (- (index to) + (index from))) + +(defun context-window (str index &key (side-length 20)) + (let ((begin (max (- index side-length) 0)) + (end (min (+ index side-length) (length str))) + (result '())) + (push (subseq str (1+ index) end) result) + (push (elt str index) result) + (push (subseq str begin index) result) + result)) + +(defmethod print-object ((obj text) stream) + (print-unreadable-object (obj stream :type t) + (let ((str (if (has-data? obj) + (format nil "~{~a[4;33m~a[m~a~}" + (context-window (data obj) + (index obj) + :side-length 10)) + "END OF DATA"))) + (format stream "~s" (substitute #\~ #\Newline str))))) |
