Permalink
Browse files

adding example for exns

  • Loading branch information...
1 parent b00b2e3 commit d4cd2013c75d709bb32e93d3d11b732bc3f4858a Danny Yoo committed Jun 22, 2011
Showing with 52 additions and 0 deletions.
  1. +52 −0 collects/scribblings/reference/exns.scrbl
@@ -576,6 +576,58 @@ value---the structure type instance from which to extract source
locations---and returns a list of @racket[srcloc]s. Some @tech{error
display handlers} use only the first returned location.}
+As an example,
+@codeblock|{
+#lang racket
+
+;; We create a structure that supports the
+;; prop:exn:srcloc protocol. It carries
+;; with it the location of the syntax that
+;; is guilty.
+(define-struct (exn:fail:he-who-shall-not-be-named
+ exn:fail)
+ (a-srcloc)
+ #:property prop:exn:srclocs
+ (lambda (a-struct)
+ (match a-struct
+ [(struct exn:fail:he-who-shall-not-be-named
+ (msg marks a-srcloc))
+ (list a-srcloc)])))
+
+;; We can play with this by creating a form that
+;; looks at identifiers, and only flags specific ones.
+(define-syntax (skeeterize stx)
+ (syntax-case stx ()
+ [(_ expr)
+ (cond
+ [(and (identifier? #'expr)
+ (eq? (syntax-e #'expr) 'voldemort))
+ (quasisyntax/loc stx
+ (raise (make-exn:fail:he-who-shall-not-be-named
+ "oh dear don't say his name"
+ (current-continuation-marks)
+ (srcloc '#,(syntax-source #'expr)
+ '#,(syntax-line #'expr)
+ '#,(syntax-column #'expr)
+ '#,(syntax-position #'expr)
+ '#,(syntax-span #'expr)))))]
+ [else
+ ;; Otherwise, leave the expression alone.
+ #'expr])]))
+
+(define (f x)
+ (* (skeeterize x) x))
+
+(define (g voldemort)
+ (* (skeeterize voldemort) voldemort))
+
+;; Examples:
+(f 7)
+(g 7)
+;; The error should highlight the use
+;; of the one-who-shall-not-be-named
+;; in g.
+}|
@defproc[(exn:srclocs? [v any/c]) boolean?]{

0 comments on commit d4cd201

Please sign in to comment.