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 4fa8a021de
commit 8e3e6fcdd1
2 changed files with 47 additions and 4 deletions

View File

@@ -2,9 +2,12 @@
scheme scheme
(only fmt fmt) (only fmt fmt)
(chicken base) (chicken base)
(chicken condition)
(chicken irregex)
(chicken module) (chicken module)
(chicken plist) (chicken plist)
(chicken string)) (chicken string)
utils)
(export (export
cat cat
@@ -55,11 +58,36 @@
(symbol? (car form)) (symbol? (car form))
(get (car form) 'sex-macro))) (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) (define (apply-macro form)
(assert (macro? form) (assert (macro? form)
(fmt #f (car form) " is not a macro")) (fmt #f (car form) " is not a macro"))
(apply (get-macro (car form)) (let ((name (car form)))
(cdr 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) (define (defmacro form)
(let ((arglist (car form)) (let ((arglist (car form))

View File

@@ -315,7 +315,22 @@
(test-assert "the offending form is shown too" (test-assert "the offending form is shown too"
(reports? "(include stdio.h)\n(wat 1 2)" "(wat 1 2)")) (reports? "(include stdio.h)\n(wat 1 2)" "(wat 1 2)"))
(test-assert "pub with nothing to define" (test-assert "pub with nothing to define"
(reports? "(pub 1)" "codegen.sex:1:"))) (reports? "(pub 1)" "codegen.sex:1:"))
;; a macro's arity was CHICKEN's to report, about a procedure with
;; no name; and an error in its body said nothing of the call
(test-assert "a macro given the wrong number of arguments"
(reports? "(defmacro (one x) x)\n(fn f () int\n (return (one 1 2)))"
"codegen.sex:3: macro takes this many arguments one 1"))
(test-assert "at toplevel too"
(reports? "(defmacro (one x) x)\n(one)" "codegen.sex:2: macro takes this many arguments"))
(test-assert "and one taking the rest, too few"
(reports? "(defmacro (many x . more) x)\n(fn f () int (return (many)))"
"macro takes at least this many arguments many 1"))
(test-assert "while it may take any more"
(emits? "(defmacro (many x . more) x) (fn f () int (return (many 1 2 3)))" "return 1;"))
(test-assert "an error in a macro's body names the call"
(reports? "(defmacro (boom x) (error \"no good\" x))\n(fn f () int (return (boom 1)))"
"codegen.sex:2: expanding boom: no good")))
;; A grouped header, C's parens carried over, died in the writer with ;; A grouped header, C's parens carried over, died in the writer with
;; `(caddr) bad argument type' and no location ;; `(caddr) bad argument type' and no location