From 741361d41328d7df7759a6c6cab64e6d965c6100 Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Wed, 22 Jun 2011 14:33:22 -0400 Subject: [PATCH] adding example for exns Signed-off-by: Sam Tobin-Hochstadt --- collects/scribblings/reference/exns.scrbl | 52 +++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/collects/scribblings/reference/exns.scrbl b/collects/scribblings/reference/exns.scrbl index ea6a07c181..c82487dfaa 100644 --- a/collects/scribblings/reference/exns.scrbl +++ b/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?]{