code macro improvements

svn: r775
This commit is contained in:
Matthew Flatt 2005-09-06 15:40:00 +00:00
parent efd50813a0
commit b6ba03fd18
3 changed files with 218 additions and 89 deletions

View File

@ -563,7 +563,8 @@ The "code.ss" module also exports a `code' macro generated by
> (code datum ...) - typesets the `datum' sequence to produce a pict; > (code datum ...) - typesets the `datum' sequence to produce a pict;
see `define-code' from "code.s"" in the "texpict" see `define-code' from "code.s"" in the "texpict"
collection for more information. collection for more information; `unsyntax' is
the escape identifier.
See documentation for "code.ss" in the "texpict" collection for more See documentation for "code.ss" in the "texpict" collection for more
information. information.

View File

@ -1,6 +1,7 @@
(module code mzscheme (module code mzscheme
(require "mrpict.ss" (require "mrpict.ss"
(lib "class.ss") (lib "class.ss")
(lib "list.ss")
(lib "mred.ss" "mred") (lib "mred.ss" "mred")
(lib "unitsig.ss")) (lib "unitsig.ss"))
@ -8,22 +9,26 @@
(define-syntax (define-code stx) (define-syntax (define-code stx)
(syntax-case stx () (syntax-case stx ()
[(_ code typeset-code) [(_ code typeset-code uncode)
(syntax/loc stx (syntax/loc stx
(define-syntax (code stx) (define-syntax (code stx)
(define (stx->loc-s-expr v) (define (stx->loc-s-expr v)
(cond (cond
[(syntax? v) [(syntax? v)
`(datum->syntax-object (let ([mk `(datum->syntax-object
#f #f
,(syntax-case v (unsyntax) ,(syntax-case v (uncode)
[(unsyntax e) #'e] [(uncode e) #'e]
[else (stx->loc-s-expr (syntax-e v))]) [else (stx->loc-s-expr (syntax-e v))])
(list 'code (list 'code
,(syntax-line v) ,(syntax-line v)
,(syntax-column v) ,(syntax-column v)
,(syntax-position v) ,(syntax-position v)
,(syntax-span v)))] ,(syntax-span v)))])
(let ([prop (syntax-property v 'paren-shape)])
(if prop
`(syntax-property ,mk 'paren-shape ,prop)
mk)))]
[(pair? v) `(cons ,(stx->loc-s-expr (car v)) [(pair? v) `(cons ,(stx->loc-s-expr (car v))
,(stx->loc-s-expr (cdr v)))] ,(stx->loc-s-expr (cdr v)))]
[(vector? v) `(vector ,@(map [(vector? v) `(vector ,@(map
@ -37,16 +42,19 @@
(syntax-case stx () (syntax-case stx ()
[(_ expr) #`(typeset-code #,(cvt #'expr))] [(_ expr) #`(typeset-code #,(cvt #'expr))]
[(_ expr (... ...)) [(_ expr (... ...))
#`(typeset-code #,(cvt #'(code:line expr (... ...))))])))])) #`(typeset-code #,(cvt #'(code:line expr (... ...))))])))]
[(_ code typeset-code) #'(define-code code typeset-code unsyntax)]))
(define-signature code^ (define-signature code^
(typeset-code (typeset-code
comment-color keyword-color id-color literal-color comment-color keyword-color id-color const-color literal-color
code-align current-code-tt code-align current-code-tt
current-keyword-list current-const-list code-colorize-enabled current-keyword-list current-const-list current-literal-list
code-colorize-enabled code-colorize-quote-enabled
current-comment-color current-keyword-color current-comment-color current-keyword-color
current-base-color current-id-color current-literal-color current-base-color current-id-color current-literal-color
current-open-paren current-close-paren)) current-reader-forms
mzscheme-const-list))
(define-signature code-params^ (define-signature code-params^
(current-font-size (current-font-size
@ -77,17 +85,41 @@
(define (code-align p) (define (code-align p)
(lift (inset p 0 (pict-height p) 0 0) (pict-height p))) (lift (inset p 0 (pict-height p) 0 0) (pict-height p)))
(define mzscheme-ns (let ([n (make-namespace 'empty)]
[orig (current-namespace)])
(parameterize ([current-namespace n])
(namespace-attach-module orig 'mzscheme)
(namespace-require/copy 'mzscheme))
n))
(define mzscheme-bindings (namespace-mapped-symbols mzscheme-ns))
(define mzscheme-vars (filter (lambda (n)
(not (eq? 'nope
(namespace-variable-value n #f (lambda () 'nope) mzscheme-ns))))
mzscheme-bindings))
(define current-keyword-list (define current-keyword-list
(make-parameter '("define" "cond" "define-struct" "and" "or" "else" (make-parameter
"lambda" "require" "provide" "require-for-syntax" (let ([ht (make-hash-table)])
"define-syntax" "let" "letrec" "let*" "syntax-rules" (for-each (lambda (n) (hash-table-put! ht n #f))
"syntax-case" "set!" "begin" "quote-syntax" "module"))) mzscheme-vars)
(map symbol->string
(filter (lambda (n)
(hash-table-get ht n (lambda () #t)))
mzscheme-bindings)))))
(define current-const-list (define current-const-list
(make-parameter '("null"))) (make-parameter '()))
(define current-literal-list
(make-parameter '()))
(define mzscheme-const-list
(map symbol->string mzscheme-vars))
(define code-colorize-enabled (define code-colorize-enabled
(make-parameter #t)) (make-parameter #t))
(define code-colorize-quote-enabled
(make-parameter #t))
(define (maybe-colorize p c) (define (maybe-colorize p c)
(if (code-colorize-enabled) (if (code-colorize-enabled)
(colorize p c) (colorize p c)
@ -100,49 +132,80 @@
(define current-id-color (make-parameter id-color)) (define current-id-color (make-parameter id-color))
(define literal-color (make-object color% 51 135 39)) (define literal-color (make-object color% 51 135 39))
(define current-literal-color (make-parameter literal-color)) (define current-literal-color (make-parameter literal-color))
(define const-color (make-object color% #x99 0 0))
(define current-const-color (make-parameter const-color))
(define comment-color (current-base-color)) (define comment-color (current-base-color))
(define current-comment-color (make-parameter comment-color)) (define current-comment-color (make-parameter comment-color))
(define current-open-paren (make-parameter #f)) (define current-reader-forms (make-parameter '(quote
(define current-close-paren (make-parameter #f)) quasiquote
unquote unquote-splicing
syntax
quasisyntax
unsyntax unsyntax-splicing)))
(define-computed open-paren-p (colorize (tt "(") (current-base-color))) (define-computed open-paren-p (tt "("))
(define-computed close-paren-p (colorize (tt ")") (current-base-color))) (define-computed close-paren-p (tt ")"))
(define-computed open-sq-p (colorize (tt "[") (current-base-color))) (define-computed open-sq-p (tt "["))
(define-computed close-sq-p (colorize (tt "]") (current-base-color))) (define-computed close-sq-p (tt "]"))
(define-computed quote-p (colorize (tt "'") (current-literal-color))) (define-computed open-curly-p (tt "{"))
(define-computed syntax-p (colorize (tt "#'") (current-keyword-color))) (define-computed close-curly-p (tt "}"))
(define-computed semi-p (colorize (tt "; ") (current-comment-color)))
(define-computed open-paren/lit-p (colorize (tt "(") (current-literal-color)))
(define-computed close-paren/lit-p (colorize (tt ")") (current-literal-color)))
(define-computed open-paren/tmpl-p (colorize (tt "(") (current-comment-color)))
(define-computed close-paren/tmpl-p (colorize (tt ")") (current-comment-color)))
(define-computed dot-p (colorize (tt " . ") (current-base-color))) (define-computed quote-p (tt "'"))
(define-computed unquote-p (tt ","))
(define-computed unquote-splicing-p (tt ",@"))
(define-computed quasiquote-p (tt "`"))
(define-computed syntax-p (tt "#'"))
(define-computed unsyntax-p (tt "#,"))
(define-computed unsyntax-splicing-p (tt "#,@"))
(define-computed quasisyntax-p (tt "#`"))
(define-computed semi-p (tt "; "))
(define (get-close mode) (define (comment-mode? mode)
(case mode (eq? mode 'comment))
[(literal) close-paren/lit-p]
[(template comment) close-paren/tmpl-p]
[(cond template-cond local) close-sq-p]
[else (or (current-close-paren)
close-paren-p)]))
(define (get-open mode) (define-computed dot-p (tt " . "))
(case mode
[(literal) open-paren/lit-p] (define (mode-colorize mode type p)
[(template comment) open-paren/tmpl-p] (maybe-colorize
[(contract line) (blank)] p
[(cond template-cond local) open-sq-p] (case mode
[else (or (current-open-paren) [(literal) (current-literal-color)]
open-paren-p)])) [(comment) (current-comment-color)]
[else (cond
[(number? mode) (current-literal-color)]
[(eq? type 'keyword) (current-keyword-color)]
[(eq? type 'literal) (current-literal-color)]
[(eq? type 'const) (current-const-color)]
[(eq? type 'id) (current-id-color)]
[else (current-base-color)])])))
(define (get-open mode stx)
(if (memq mode '(contract line))
(blank)
(mode-colorize
mode #f
(case (syntax-property stx 'paren-shape)
[(#\[) open-sq-p]
[(#\{) open-curly-p]
[else open-paren-p]))))
(define (get-close mode stx)
(if (memq mode '(contract line))
(blank)
(mode-colorize
mode #f
(case (syntax-property stx 'paren-shape)
[(#\[) close-sq-p]
[(#\{) close-curly-p]
[else close-paren-p]))))
(define (add-close p closes) (define (add-close p closes)
(cond (cond
[(null? closes) p] [(null? closes) p]
[(memq (car closes) '(contract line)) [(memq (caar closes) '(contract line))
(add-close p (cdr closes))] (add-close p (cdr closes))]
[else [else
(add-close (hbl-append p (get-close (car closes))) (add-close (hbl-append p (get-close (caar closes) (cdar closes)))
(cdr closes))])) (cdr closes))]))
(define (pad-left space p) (define (pad-left space p)
@ -160,8 +223,9 @@
[(and ((string-length str) . > . 1) [(and ((string-length str) . > . 1)
(char=? #\_ (string-ref str 0)) (char=? #\_ (string-ref str 0))
(not (char=? #\_ (string-ref str 1)))) (not (char=? #\_ (string-ref str 1))))
(maybe-colorize (text (substring str 1) `(bold italic . modern) (current-font-size)) (mode-colorize
(current-id-color))] mode 'id
(text (substring str 1) `(bold italic . modern) (current-font-size)))]
[(regexp-match #rx"^(.+)_([0-9a-z]+)\\^([0-9a-z]+)$" str) [(regexp-match #rx"^(.+)_([0-9a-z]+)\\^([0-9a-z]+)$" str)
=> (lambda (m) => (lambda (m)
(hbl-append (colorize-id (cadr m) mode) (hbl-append (colorize-id (cadr m) mode)
@ -183,14 +247,14 @@
(hbl-append (colorize-id (cadr m) mode) (hbl-append (colorize-id (cadr m) mode)
(text (caddr m) `(subscript bold . modern) (current-font-size))))] (text (caddr m) `(subscript bold . modern) (current-font-size))))]
[else [else
(maybe-colorize (mode-colorize
(tt str) mode
(cond (cond
[(eq? mode 'literal) (current-literal-color)] [(member str (current-keyword-list)) 'keyword]
[(memq mode '(comment template)) (current-comment-color)] [(member str (current-const-list)) 'const]
[(member str (current-keyword-list)) (current-keyword-color)] [(member str (current-literal-list)) 'literal]
[(member str (current-const-list)) (current-literal-color)] [else 'id])
[else (current-id-color)]))])) (tt str))]))
(define (sub-mode mode) (define (sub-mode mode)
(case mode (case mode
@ -210,26 +274,73 @@
[code:blank 1] [code:blank 1]
[_ (or (syntax-span stx) 1)])) [_ (or (syntax-span stx) 1)]))
(define (color-semi-p)
(mode-colorize 'comment #f semi-p))
(define (add-semis p) (define (add-semis p)
(let loop ([p p] [semis semi-p]) (let loop ([p p] [semis (color-semi-p)])
(if ((pict-height p) . > . (+ (pict-height semis) 1)) (if ((pict-height p) . > . (+ (pict-height semis) 1))
(loop p (vl-append line-sep semi-p semis)) (loop p (vl-append line-sep (color-semi-p) semis))
(htl-append semis p)))) (htl-append semis p))))
(define (add-unquote unquote-p loop x closes mode)
(let ([mode (cond
[(number? mode) (if (zero? mode)
#f
(sub1 mode))]
[else mode])])
(htl-append (mode-colorize mode 'keyword unquote-p)
(loop x closes mode))))
(define (typeset-code stx) (define (typeset-code stx)
(let loop ([stx stx][closes null][mode #f]) (let loop ([stx stx][closes null][mode #f])
(syntax-case* stx (quote syntax-unquote syntax (syntax-case* stx (quote unquote unquote-splicing quasiquote
syntax-unquote syntax unsyntax
unsyntax-splicing quasisyntax
code:contract code:comment code:line code:contract code:comment code:line
code:template code:blank $) code:template code:blank $)
(lambda (a b) (eq? (syntax-e a) (syntax-e b))) (lambda (a b) (eq? (syntax-e a) (syntax-e b)))
[() (add-close (htl-append (get-open mode) (get-close mode)) [() (add-close (htl-append (get-open mode stx) (get-close mode stx))
closes)] closes)]
[code:blank (tt " ")] [code:blank (tt " ")]
[$ (colorize-id "|" closes)] [$ (colorize-id "|" closes)]
[(quote x) [(quote x)
(htl-append quote-p (loop #'x closes 'literal))] (memq 'quote (current-reader-forms))
(htl-append (mode-colorize mode 'literal quote-p)
(loop #'x closes (if (or (not (code-colorize-quote-enabled))
(comment-mode? mode))
mode
'literal)))]
[(unquote x)
(memq 'unquote (current-reader-forms))
(add-unquote unquote-p loop #'x closes mode)]
[(unquote-splicing x)
(memq 'unquote-splicing (current-reader-forms))
(add-unquote unquote-splicing-p loop #'x closes mode)]
[(quasiquote x)
(memq 'quasiquote (current-reader-forms))
(htl-append (mode-colorize mode 'keyword quasiquote-p)
(loop #'x closes (cond
[(not (code-colorize-quote-enabled)) mode]
[(comment-mode? mode) mode]
[(number? mode) (add1 mode)]
[else 0])))]
[(syntax x) [(syntax x)
(htl-append syntax-p (loop #'x closes mode))] (memq 'syntax (current-reader-forms))
(htl-append (mode-colorize mode 'literal syntax-p)
(loop #'x closes mode))]
[(unsyntax x)
(memq 'unsyntax (current-reader-forms))
(htl-append (mode-colorize mode 'literal unsyntax-p)
(loop #'x closes mode))]
[(unsyntax-splicing x)
(memq 'unsyntax-splicing (current-reader-forms))
(htl-append (mode-colorize mode 'literal unsyntax-splicing-p)
(loop #'x closes mode))]
[(quasisyntax x)
(memq 'unsyntax-splicing (current-reader-forms))
(htl-append (mode-colorize mode 'literal quasisyntax-p)
(loop #'x closes mode))]
[(code:contract i ...) [(code:contract i ...)
(add-semis (loop (datum->syntax-object #f (syntax->list #'(i ...))) (add-semis (loop (datum->syntax-object #f (syntax->list #'(i ...)))
closes 'contract))] closes 'contract))]
@ -238,20 +349,20 @@
closes 'line)] closes 'line)]
[(code:comment s ...) [(code:comment s ...)
(apply htl-append (apply htl-append
semi-p (color-semi-p)
(map (lambda (s) (map (lambda (s)
(if (pict? (syntax-e s)) (if (pict? (syntax-e s))
(syntax-e s) (syntax-e s)
(maybe-colorize (tt (syntax-e s)) (current-comment-color)))) (maybe-colorize (tt (syntax-e s)) (current-comment-color))))
(syntax->list #'(s ...))))] (syntax->list #'(s ...))))]
[(code:template i) [(code:template i ...)
(add-semis (loop #'i closes 'template))] (add-semis (loop #'(code:line i ...) closes 'template))]
[(i ...) [(i ...)
(let ([is (syntax->list #'(i ...))]) (let ([is (syntax->list #'(i ...))])
;; Convert each i to a picture, include close paren in last item: ;; Convert each i to a picture, include close paren in last item:
(let ([ips (let iloop ([is is][sub-mode (sub-mode mode)]) (let ([ips (let iloop ([is is][sub-mode (sub-mode mode)])
(cond (cond
[(null? (cdr is)) (list (loop (car is) (cons mode closes) sub-mode))] [(null? (cdr is)) (list (loop (car is) (cons (cons mode stx) closes) sub-mode))]
[else (cons (loop (car is) null sub-mode) [else (cons (loop (car is) null sub-mode)
(iloop (cdr is) (cond (iloop (cdr is) (cond
[(cond? (car is)) [(cond? (car is))
@ -268,7 +379,7 @@
(let ([left (or (syntax-column stx) +inf.0)]) (let ([left (or (syntax-column stx) +inf.0)])
(let loop ([stxs is] (let loop ([stxs is]
[ps ips] [ps ips]
[line-so-far (get-open mode)] [line-so-far (get-open mode stx)]
[col (+ left 1)] [col (+ left 1)]
[line (syntax-line stx)] [line (syntax-line stx)]
[always-space? #f] [always-space? #f]
@ -326,12 +437,14 @@
(identifier? stx) (identifier? stx)
(add-close (colorize-id (symbol->string (syntax-e stx)) mode) closes)] (add-close (colorize-id (symbol->string (syntax-e stx)) mode) closes)]
[(a . b) [(a . b)
(hbl-append open-paren-p (loop #'a null mode) (hbl-append (mode-colorize mode #f open-paren-p) (loop #'a null mode)
dot-p (loop #'b (cons mode closes) mode))] (mode-colorize mode #f dot-p)
(loop #'b (cons (cons mode stx) closes) mode))]
[else [else
(add-close (if (pict? (syntax-e stx)) (add-close (if (pict? (syntax-e stx))
(syntax-e stx) (syntax-e stx)
(maybe-colorize (tt (format "~s" (syntax-e stx))) (current-literal-color))) (mode-colorize mode 'literal
(tt (format "~s" (syntax-e stx))) ))
closes)]))) closes)])))
))) )))

View File

@ -1120,9 +1120,15 @@ The _code^_ unit supplies the following
> current-keyword-color - parameter for a string or color% for keywords > current-keyword-color - parameter for a string or color% for keywords
> current-id-color - parameter for a string or color% for identifiers > current-id-color - parameter for a string or color% for identifiers
> current-literal-color - parameter for a string or color% for literals > current-literal-color - parameter for a string or color% for literals
> current-const-color - parameter for a string or color% for constants
> current-base-color - parameter for a string or color% for everything else > current-base-color - parameter for a string or color% for everything else
> current-open-paren - #f or a pict to use for plain open parens
> current-close-paren - #f or a pict to use for plain close parens > current-reader-forms - parameter for a list of symbols indicating
which built-in reader forms should be used;
the default is '(quote quasiquote unquote
unquote-splicing syntax quasisyntax unsyntax
unsyntax-splicing); remove a symbol to suppress
the corresponding reader output
For backward compatibility: For backward compatibility:
> comment-color - default string/color% for comments > comment-color - default string/color% for comments
@ -1137,18 +1143,26 @@ For backward compatibility:
> current-keyword-list - a list of identifiers to color as keywords; the > current-keyword-list - a list of identifiers to color as keywords; the
default includes the MzScheme syntactic form names default includes the MzScheme syntactic form names
> current-const-list - a list of identifiers to color ad literals; the > current-const-list - a list of identifiers to color as constants; the
default is 'empty default is empty
> current-literal-list - a list of identifiers to color as literals; the
default is empty
> code-colorize-enabled - a parameter to enable or disable all code > code-colorize-enabled - a parameter to enable or disable all code
coloring coloring; the default is #t
> code-colorize-quote-enabled - a parameter to control whether everything
under a quote is colorized as a literal (like DrScheme's Check Syntax,
and unlike DrScheme's interactive text coloring); the default is #t
In addition to the `code@' unit, the "code.ss" module exports a In addition to the `code@' unit, the "code.ss" module exports a
`define-code' macro for defining other macros: `define-code' macro for defining other macros:
> (define-code code-id typeset-code-id) - defines `code-id' as a macro > (define-code code-id typeset-code-id)
that uses `typeset-code-id', which is a function with the same input > (define-code code-id typeset-code-id escape-id)
as `typeset-code'. - defines `code-id' as a macro that uses `typeset-code-id', which is
a function with the same input as `typeset-code'. The `escape-id'
form defaults to `unsyntax'.
The `code-id' syntactic form takes a sequence of `datum's: The `code-id' syntactic form takes a sequence of `datum's:
@ -1156,12 +1170,13 @@ In addition to the `code@' unit, the "code.ss" module exports a
It produces a pict that typesets the sequence. Source-location It produces a pict that typesets the sequence. Source-location
information for the `datum' determine the layout of code in the information for the `datum' determine the layout of code in the
resulting pict. resulting pict. A pict datum is drawn as itself.
If a `datum' contains `(unsyntax expr)' (perhaps as #,expr), then If a `datum' contains `(escape-id expr)' (perhaps as #,expr when
the `expr' is evaluated and the result datum is spliced in place of `escape-id' is `unsyntax'), then the `expr' is evaluated and the
the `unsyntax' form in `datum'. If the result is not a syntax object, result datum is spliced in place of the `unsyntax' form in
it is given the source location of the `(unsyntax expr)'. `datum'. If the result is not a syntax object, it is given the
source location of the `(unsyntax expr)'.
The `code' syntactic form expands to a use of `typeset-code'. The The `code' syntactic form expands to a use of `typeset-code'. The
`datum's are not merely syntax-quoted, because then the compiled `datum's are not merely syntax-quoted, because then the compiled