From 2ebab36f8c689fa3e6f88cfc25cecd83848ca129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Mon, 24 Nov 2025 07:01:47 -0300 Subject: Big update --- cursor.lisp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 cursor.lisp (limited to 'cursor.lisp') 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~a~a~}" + (context-window (data obj) + (index obj) + :side-length 10)) + "END OF DATA"))) + (format stream "~s" (substitute #\~ #\Newline str))))) -- cgit v1.2.3