[plt-scheme] Improving the error messages from unit syntax?

From: Danny Yoo (dyoo at hkn.eecs.berkeley.edu)
Date: Mon Jul 11 17:13:26 EDT 2005

Hi everyone,

I'm starting to write a tutorial to units for complete newbies like
myself.  Here's what I have so far:

http://aztec.stanford.edu/svn/repos/personal/dyoo/scratch/plt/python-to-plt/unit-notes.text

One part of my tutorial grips about unit syntax error messages. I'd like
to do something so that I can get rid of that subsection.  *grin*


I'm still very newbie-ish to Scheme's macro system, but I think I
understand enough to put in a few error rules.  Would something like this
be suitable?

######
[dyoo at shoebox plt]$ svn diff collects/mzlib/unit.ss
Index: collects/mzlib/unit.ss
===================================================================
--- collects/mzlib/unit.ss	(revision 371)
+++ collects/mzlib/unit.ss	(working copy)
@@ -360,7 +360,33 @@
 						      (stx-defn ... .
redirections)
 						      ([(intname)
undefined] ...)
 						      (void) ; in case the
body would be empty
-						      defn&expr
...))))))))))))))))))]))])
+						      defn&expr
...))))))))))))))))))]
+
+               ;; Syntax checks; if the first pattern doesn't match,
+               ;; let's inform the user of the syntax error:
+               [(_ (export ivar ...) (import evar ...) defn&expr ...)
+                (raise-syntax-error
+                 #f
+                 "export and import definitions out of order"
+                 stx)]
+               [(_ (export evar ...) defn&expr ...)
+                (raise-syntax-error
+                 #f
+                 "expected import definition"
+                 stx)]
+               [(_ (import evar ...) defn&expr ...)
+                (raise-syntax-error
+                 #f
+                 "expected export definition"
+                 stx)]
+               [(_)
+                (raise-syntax-error
+                 #f
+                 "expected import and export definitions"
+                 stx)]
+
+
+               ))])
       (values (lambda (stx) (do-unit stx #t))
 	      (lambda (stx) (do-unit stx #f)))))

######



For example:

######
> (require (lib "unit.ss"))
> (unit)
repl-2:1:0: unit: expected import and export definitions in: (unit)

> (unit (import x y) (define x 42) (define y "hola"))
repl-3:1:0: unit: expected export definition in: (unit (import x y)
(define x 42) (define y "hola"))

> (unit (export) (import x y) (define x 42) (define y "hola"))
repl-4:1:0: unit: export and import definitions out of order in: (unit
(export) (import x y) (define x 42) (define y "hola"))

> (define d (unit (import) (export) (display "ok, done!") (newline)))
> (invoke-unit d)
ok, done!
######


Having error rules like this will make it easier for the casual Scheme
programmer to play with units without bumping into as many mysterious
error messages.  And if this is ok, would doing something similar for the
unit/sig syntax be acceptable?

Best of wishes!



Posted on the users mailing list.