better error msg for miscalled tag function

pull/218/head
Matthew Butterick 5 years ago
parent 8db9bfadd6
commit ccb6bbebec

@ -1 +1 @@
1576590927
1578421107

@ -1,4 +1,4 @@
#lang pollen/mode racket/base
#lang pollen/mode debug racket/base
(require (for-syntax
racket/base
syntax/parse)
@ -53,7 +53,7 @@
(make-keyword-procedure
(λ (outer-kws outer-kw-args . ids)
(define tag-proc (apply compose1 (for/list ([id (in-list ids)])
(make-one-tag-function outer-kws outer-kw-args id))))
(make-one-tag-function outer-kws outer-kw-args id))))
(define tag-proc-name (string->symbol (format "pollen-tag:~a" (string-join (map symbol->string ids) "+"))))
(procedure-rename tag-proc tag-proc-name))))
@ -78,19 +78,22 @@
(define-syntax (define-tag-function stx)
(syntax-parse stx
#:literals (λ)
[(_ (ID:id ARG:id ...) EXPR:expr ...)
#'(define-tag-function ID (λ (ARG ...) EXPR ...))]
[(THIS (ID:id ARG:id ...) EXPR:expr ...)
#'(THIS ID (λ (ARG ...) EXPR ...))]
[(_ ID:id (λ (ATTRS:id ELEMS:id ARG:id ...) EXPR:expr ...))
#:fail-when (> (length (syntax->list #'(ARG ...))) 0) "tag function must have exactly 2 positional arguments"
#:fail-when (check-duplicate-identifier (list #'ATTRS #'ELEMS)) "duplicate variable name"
#:fail-when (null? (syntax->list #'(EXPR ...))) "body of definition cannot be empty"
#'(define ID
;; the srcloc of the `lambda` expression determines the srcloc of errors raised within its body
#`(define ID
(make-keyword-procedure
(λ (kws kwargs . args)
(define tx-proc (keyword-apply default-tag-function kws kwargs (list 'ID)))
(define tx (apply tx-proc args))
(define-values (_ ATTRS ELEMS) (txexpr->values tx))
EXPR ...)))]))
#,(syntax/loc #'ID (lambda (kws kwargs attrs . elems)
(define tx-proc (keyword-apply default-tag-function kws kwargs (list 'ID)))
(unless (and (list? elems) (andmap txexpr-element? elems))
(raise-argument-error 'ID (format "elements need to be passed to tag function as individual trailing arguments (or, if you want to pass them as a single list, use `(apply ~a ···)` here instead of `(~a ···)`)" 'ID 'ID) (car elems)))
(define tx (apply tx-proc attrs elems))
(define-values (_ ATTRS ELEMS) (txexpr->values tx))
EXPR ...))))]))
(module+ test

Loading…
Cancel
Save