From da008e637b5bff56fed8dfbacc2adabc4bca18b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Tue, 10 Jun 2025 13:57:22 -0300 Subject: Add better reporting and new features Units report expected results on end of input. Opposite parser helps with the complexity explosion on unit parsers. Input and error printing has context. --- core.lisp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'core.lisp') diff --git a/core.lisp b/core.lisp index 2cd0120..3b06936 100644 --- a/core.lisp +++ b/core.lisp @@ -1,5 +1,14 @@ (in-package #:monparser) +(defun opposite (p) + (lambda (input &key lazy) + (let ((result (funcall p input))) + (cond ((parsing-p result) + (make-failure :place input :message "Unexpected match.")) + ((failure-p result) + (make-parsing :tree nil :left input)) + (t (error "Unexpected result type.")))))) + (defun fail (message) (lambda (input &key lazy) (make-failure :place input :message message))) @@ -28,7 +37,8 @@ (make-parsing :tree it :left (advance input)) (make-failure :place input :message (format nil "Expected: ~a, Got: ~a" ',predicate it)))) - (make-failure :place input :message "Reached end of input.")))) + (make-failure :place input + :message (format nil "Reached end of input. Expected: ~a" ',predicate))))) (defun lazily-select-parser (input parsers) (let ((intermediate-parsers '()) @@ -46,8 +56,7 @@ (input-cursor (parsing-left result)))) (setf result r))) ((failure-p r) - (when (or (failure-p result) - (= (length parsers) 1)) + (when (failure-p result) (setf result r))) (t (error (format nil "Invalid return value: ~a" r)))))) (if intermediate-parsers -- cgit v1.2.3