say which macro call an error came from

A wrong argument count was CHICKEN's to report, about a procedure with
no name, and an error raised in a macro's body said nothing of the call.
This commit is contained in:
2026-10-19 10:23:59 +03:00
parent 63e6cec8e1
commit 45ef052f43
2 changed files with 47 additions and 4 deletions

View File

@@ -2,9 +2,12 @@
scheme
(only fmt fmt)
(chicken base)
(chicken condition)
(chicken irregex)
(chicken module)
(chicken plist)
(chicken string))
(chicken string)
utils)
(export
register-macro
@@ -57,11 +60,36 @@
(symbol? (car form))
(get (car form) 'sex-macro)))
;;; CHICKEN would report a wrong argument count about a procedure with
;;; no name, and an error raised in a macro's body without the call: both
;;; are told here, where the call is known
(define (apply-macro form)
(assert (macro? form)
(fmt #f (car form) " is not a macro"))
(apply (get-macro (car form))
(cdr form)))
(let ((name (car form)))
(check-macro-arity form name (cadr (get name 'sex-macro)) (length (cdr form)))
(handle-exceptions e
(let ((message ((condition-property-accessor 'exn 'message #f) e)))
;; one raised about a form of its own already says where
(if (and message (not (irregex-search "^[^ ]+:[0-9]+: " message)))
(apply sex-error form
(string-append "expanding " (symbol->string name)
": " message)
((condition-property-accessor 'exn 'arguments (list)) e))
(abort e)))
(apply (get-macro name) (cdr form)))))
;;; (defmacro (one x) ...) takes one; (defmacro (many x . more) ...) one
;;; or more
(define (check-macro-arity form name arglist given)
(let count ((args arglist) (required 0))
(cond ((pair? args) (count (cdr args) (+ required 1)))
((null? args)
(unless (= given required)
(sex-error form "macro takes this many arguments" name required)))
((< given required)
(sex-error form "macro takes at least this many arguments"
name required)))))
(define (defmacro form)
(let ((arglist (car form))