From 1af10ddc10e35805c6723d7a2a85d7e3768497f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Sun, 13 Oct 2024 00:51:57 -0300 Subject: Error out on parser failure --- input.lisp | 11 +++++++++++ main.lisp | 6 +++++- package.lisp | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/input.lisp b/input.lisp index e28e73a..100baa5 100644 --- a/input.lisp +++ b/input.lisp @@ -18,3 +18,14 @@ (defun cursor-distance (input1 input2) (- (input-cursor input1) (input-cursor input2))) + +(defun line-and-column (input) + (let ((line 1) (column 1)) + (dotimes (i (input-cursor input)) + (let ((c (char (input-data input) i))) + (case c + (#\Newline + (incf line) + (setf column 1)) + (t (incf column))))) + (values line column))) diff --git a/main.lisp b/main.lisp index 6357cc4..0d890a0 100644 --- a/main.lisp +++ b/main.lisp @@ -8,4 +8,8 @@ (if (parsing-p result) (let ((finished? (not (has-data? (parsing-left result))))) (values (parsing-tree result) finished?)) - result))) + (error (format nil "~a" result))))) + +(defmethod print-object ((obj failure) stream) + (multiple-value-bind (line column) (line-and-column (failure-place obj)) + (format stream "~a:~a: ~a" line column (failure-message obj)))) diff --git a/package.lisp b/package.lisp index c59f997..62846f2 100644 --- a/package.lisp +++ b/package.lisp @@ -5,6 +5,7 @@ #:failure-message #:input-cursor #:input-data + #:line-and-column #:comp #:one-of #:unit -- cgit v1.2.3