www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

scribble-enhanced-template.lp2.rkt (3289B)


      1 #lang scribble/lp2
      2 @; TODO: use hyper-literate language instead.
      3 @(require scribble-enhanced/doc)
      4 @doc-lib-setup
      5 
      6 @title[#:style manual-doc-style]{Life, the Universe and Everything.}
      7 
      8 @(table-of-contents)
      9 
     10 @section{Introduction}
     11 
     12 @chunk[<lue>
     13        (define lue 42)]
     14 
     15 Here is a macro:
     16 
     17 @CHUNK[<scribble-macro-expansion>
     18        (define-for-syntax mymacro-tmp
     19          (syntax-rules () [(_ a b) (let ((b 1)) a)]))
     20        (define-syntax (mymacro-stx stx) #`'#,(mymacro-tmp stx))
     21        (define-syntax mymacro mymacro-tmp)]
     22 
     23 We can use it like this:
     24 
     25 @chunk[<scribble-macro-expansion-example>
     26        (mymacro (+ x 3) x)]
     27 
     28 @(begin
     29    (require (for-syntax racket/base))
     30    (define-syntax (if<6.4 stx)
     31      (syntax-case stx ()
     32        [(_ lt ge)
     33         (if (or (regexp-match #px"^6(\\.[0123](\\..*|)|)$" (version))
     34                 (regexp-match #px"^[123245]\\..*$" (version)))
     35             #'lt
     36             #'ge)]))
     37    (define-syntax-rule (skip<6.4 . rest) (if<6.4 (begin) (begin . rest))))
     38 
     39 @skip<6.4{
     40  Which expands to (requires Racket ≥ 6.4 and a bit of set-up boilerplate to have
     41  the output in scribble, see
     42  @url{http://lists.racket-lang.org/users/archive/2014-December/065175.html}):
     43  @(begin
     44     (require syntax/location scribble/example)
     45     (define res-mod-name
     46       (resolved-module-path-name
     47        (module-path-index-resolve
     48         (module-path-index-join '(submod ".." main)
     49                                 (variable-reference->module-path-index
     50                                  (#%variable-reference))))))
     51     (define evaluator (make-base-eval #:lang 'typed/racket))
     52     (evaluator
     53      #`(begin
     54          (require/typed racket/enter
     55                         [dynamic-enter! (->* (Module-Path)
     56                                              (#:re-require-enter? Any)
     57                                              Void)])
     58          (dynamic-require (cast '#,(cons 'submod res-mod-name) Module-Path) #f)
     59          (dynamic-enter! (cast '#,(cons 'submod res-mod-name) Module-Path)
     60                          #:re-require-enter? #f))))
     61 
     62  @examples[#:eval evaluator #:result-only
     63            (mymacro-stx (+ x 3) x)
     64            #;(begin
     65                (require (for-syntax racket/pretty))
     66                (begin-for-syntax
     67                  (pretty-write
     68                   (syntax->datum
     69                    #'(mymacro-tmp (+ x 3) x)))))]
     70 
     71  The code above should show the expanded code, i.e:}
     72 
     73 @if<6.4[
     74  @list{With Racket ≥ 6.4, it is possible to automatically compute the expanded
     75   code, and show it. The result would be:}
     76  @list{}]
     77 
     78 @chunk[<expanded-code>
     79        (let ((x 1)) (+ x 3))]
     80 
     81 @chunk[<test-foo>
     82        (check-equal? lue 42)]
     83 
     84 @section{Conclusion}
     85 
     86 @chunk[<main-module>
     87        (module main typed/racket
     88          (require (for-syntax syntax/parse
     89                               racket/syntax
     90                               #;phc-toolkit/untyped)
     91                   #;phc-toolkit/untyped)
     92          (provide lue)
     93          
     94          <lue>
     95          <scribble-macro-expansion>)]
     96 
     97 @chunk[<module-test>
     98        (module* test typed/racket
     99          (require (submod "..")
    100                   typed/rackunit)
    101          
    102          <test-foo>)]
    103 
    104 @chunk[<*>
    105        (begin
    106          <main-module>
    107          
    108          (require 'main)
    109          (provide (all-from-out 'main))
    110          
    111          <module-test>)]