diff options
| author | Juan Manuel Tomás <jtomas1815@gmail.com> | 2025-11-24 07:01:47 -0300 |
|---|---|---|
| committer | Juan Manuel Tomás <jtomas1815@gmail.com> | 2025-11-24 07:01:47 -0300 |
| commit | 2ebab36f8c689fa3e6f88cfc25cecd83848ca129 (patch) | |
| tree | 63208a8be028b19974b8a5e686470bd4fb3fc657 /cursor.lisp | |
| parent | 1c1162747d8d7e12140329a105c0776d5555a351 (diff) | |
| download | monparser-2ebab36f8c689fa3e6f88cfc25cecd83848ca129.tar.gz monparser-2ebab36f8c689fa3e6f88cfc25cecd83848ca129.zip | |
Big update
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))))) |
