summaryrefslogtreecommitdiff
path: root/input.lisp
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2022-10-30 05:58:46 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2022-10-30 05:58:46 -0300
commit0250b445054766cef04b15cc05912272c0524ee4 (patch)
treebe56a5ca8a79612f82ec7d273928af7c4b35e480 /input.lisp
downloadmonparser-0250b445054766cef04b15cc05912272c0524ee4.tar.gz
monparser-0250b445054766cef04b15cc05912272c0524ee4.zip
Initial Commit
This first revision has a working parser generator, with support for custom error reporting and handling of string and file inputs.
Diffstat (limited to 'input.lisp')
-rw-r--r--input.lisp40
1 files changed, 40 insertions, 0 deletions
diff --git a/input.lisp b/input.lisp
new file mode 100644
index 0000000..d6dd4b3
--- /dev/null
+++ b/input.lisp
@@ -0,0 +1,40 @@
+(in-package #:input)
+
+(defstruct input
+ (cursor 0)
+ (file nil :read-only t)
+ (data nil :read-only t))
+
+(defun has-data? (input)
+ (< (input-cursor input)
+ (length (input-data input))))
+
+(defun element (input)
+ (char (input-data input)
+ (input-cursor input)))
+
+(defun advance (input)
+ (let ((new-input (copy-structure input)))
+ (incf (input-cursor new-input))
+ new-input))
+
+(declaim (ftype (function (simple-string) (values input &optional)) from-string))
+(defun from-string (str)
+ (make-input :data str))
+
+(declaim (ftype (function (simple-string) (values input &optional)) from-file))
+(defun from-file (filename)
+ (make-input :file filename :data (str:read-file filename)))
+
+(defun generate-report (input message)
+ (let ((line 0) (column 0))
+ (dotimes (i (input-cursor input))
+ (let ((c (char (input-data input) i)))
+ (case c
+ (#\Newline
+ (incf line)
+ (setf column 1))
+ (t (incf column)))))
+ (if (input-file input)
+ (format nil "~a:~a:~a: ~a" (input-file input) line column message)
+ (format nil "~a:~a: ~a" line column message))))