[plt-scheme] slatex and including unit testable code
On Wed, 16 May 2007, Robby Findler wrote:
> Well, you probably wouldn't need to hack the parser to go that route.
> You can use read-syntax to get a version of the file as a value that has
> the source locations in it and then reconstruct things from there. Not
> trivial, but at least you can rely on the manuals for help :)
Hi Grant,
Here's an example that may help you get started:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module example-with-syntax-reprinter mzscheme
(require (lib "etc.ss")
(planet "syntax-reprinter.ss"
("dyoo" "syntax-reprinter.plt" 1)))
(define (make-sample-port)
(local
((define sample-port (open-input-string #<<HERE
(define (hello world)
(printf "Hello, ~a"
world))
(define (goodbye)
(printf "Goodbye!"))
HERE
)))
(port-count-lines! sample-port)
sample-port))
;; find-defn: symbol input-port -> (union syntax #f)
;; Looks for a defined function in defun-style syntax.
(define (find-defn name port)
(let loop ([stx (read-syntax #f port)])
(cond [(eof-object? stx)
#f]
[else
(syntax-case stx ()
[(define (name-stx args ...) body ...)
(and (symbol? (syntax-e #'name-stx))
(symbol=? (syntax-e #'name-stx)
name))
stx]
[else
(loop (read-syntax #f port))])])))
(syntax-reprint (find-defn 'goodbye (make-sample-port)))
(newline)
(newline)
(syntax-reprint (find-defn 'hello (make-sample-port))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Best of wishes!