[racket] Syntax parse and not enough forms

From: Eric Dobson (eric.n.dobson at gmail.com)
Date: Sat Jul 20 13:16:19 EDT 2013

I'm trying to improve the error messages for some macros and it seems
like in some cases syntax-parse cannot give a good error message and
just returns "bad syntax". The main issue I'm dealing with is when the
use does not have as many subforms as the macro requires. I think I
understand why it is hard for syntax-parse to automatically generate
the error message because it doesn't want to make error messages be
made from compound parts. But I'm not sure as a macro writer how I
tell syntax-parse the info it doesn't want to synthesize so that I can
have nice error messages when there are not enough arguments. Is this
easy to do/even possible?

My Program:

#lang racket

(require syntax/parse)

(define (parser stx)
  (with-handlers ([exn:fail? exn-message])
    (syntax-parse stx
      [(_ arg1:id arg2:id) "Matched"])))

(parser #'(foo a1)) ;; Bad error message and I want to change this
(parser #'(foo a1 a2))
(parser #'(foo a1 a2 a3))

(define (parser2 stx)
  (with-handlers ([exn:fail? exn-message])
    (syntax-parse stx
      [(_ arg1:id . arg2:id) "Matched"])))

(parser2 #'(foo a1))

/Users/endobson/tmp/tmp2.rkt:10:10: foo: bad syntax\n  in: (foo a1)"
"/Users/endobson/tmp/tmp2.rkt:12:21: foo: unexpected term\n  at: a3\n
in: (foo a1 a2 a3)"
"/Users/endobson/tmp/tmp2.rkt:20:11: foo: expected identifier\n  at:
()\n  in: (foo a1)"

Posted on the users mailing list.