make `codeblock' work without a #lang line

but revise docs to clarify that a #lang line is really expected
 Closes PR 11385
This commit is contained in:
Matthew Flatt 2011-01-16 19:11:13 -07:00
parent 21eee45a48
commit c8e3d45dae
2 changed files with 33 additions and 17 deletions

View File

@ -62,7 +62,13 @@
(if context (if context
(replace-context context stx) (replace-context context stx)
stx))) stx)))
(read-syntax 'prog (open-input-bytes bstr))))] (let ([p (open-input-bytes bstr)])
(let loop ()
(let ([v (read-syntax 'prog p)])
(cond
[expand v]
[(eof-object? v) null]
[else (datum->syntax #f (cons v (loop)) v v)]))))))]
[ids (let loop ([e e]) [ids (let loop ([e e])
(cond (cond
[(and (identifier? e) [(and (identifier? e)
@ -113,7 +119,8 @@
(apply append (apply append
(map loop (syntax->list #'(form ...))))] (map loop (syntax->list #'(form ...))))]
[else null]))] [else null]))]
[language (if (regexp-match? #rx"^#lang " bstr) [has-hash-lang? (regexp-match? #rx"^#lang " bstr)]
[language (if has-hash-lang?
(let ([m (regexp-match #rx"^#lang ([-a-zA-Z/._+]+)" bstr)]) (let ([m (regexp-match #rx"^#lang ([-a-zA-Z/._+]+)" bstr)])
(if m (if m
(link-mod (link-mod
@ -128,8 +135,10 @@
mods mods
language language
(filter (lambda (x) (not (eq? (car x) 'symbol))) (filter (lambda (x) (not (eq? (car x) 'symbol)))
(if has-hash-lang?
;; Drop #lang entry: ;; Drop #lang entry:
(cdr tokens))) (cdr tokens)
tokens)))
(lambda (a b) (lambda (a b)
(or (< (cadr a) (cadr b)) (or (< (cadr a) (cadr b))
(and (= (cadr a) (cadr b)) (and (= (cadr a) (cadr b))

View File

@ -31,19 +31,30 @@ includes a @racket[latex-defaults] @tech{style property}.
@section[#:tag "scribble:manual:code"]{Typesetting Code} @section[#:tag "scribble:manual:code"]{Typesetting Code}
@defform/subs[(codeblock option ... str-expr ...+) @defform/subs[(codeblock option ... str-expr ...+)
([option (code:line #:indent indent-expr) ([option (code:line #:keep-lang-line? keep-expr)
(code:line #:indent indent-expr)
(code:line #:expand expand-expr) (code:line #:expand expand-expr)
(code:line #:context context-expr) (code:line #:context context-expr)])
(code:line #:keep-lang-line? keep-expr)]) #:contracts ([keep-expr any/c]
#:contracts ([indent-expr exact-nonnegative-integer?] [indent-expr exact-nonnegative-integer?]
[expand-expr (or/c #f (syntax-object? . -> . syntax-object?))] [expand-expr (or/c #f (syntax-object? . -> . syntax-object?))]
[context-expr syntax-object?] [context-expr syntax-object?])]{
[keep-expr any/c])]{
Parses the code formed by the strings produced by the Parses the code formed by the strings produced by the
@racket[str-expr]s as a Racket module and produces a @tech{block} that @racket[str-expr]s as a Racket module (roughly) and produces a
typesets the code. The code is indented by the amount specified by @tech{block} that typesets the code. The @racket[str-expr]s should
@racket[indent-expr], which defaults to @racket[2]. normally start with @hash-lang[] to determine the reader syntax for
the module, but the resulting ``module'' need not expand or
compile---except as needed by @racket[expand-expr]. If
@racket[expand-expr] is omitted or produces false, then the input
formed by @racket[str-expr] is read until an end-of-file is
encountered, otherwise a single form is read from the input.
When @racket[keep-expr] produces a true value (the default), the first
line in the input (which is typically @hash-lang[]) is preserved in
the typeset output, otherwise the first line is dropped. The typeset
code is indented by the amount specified by @racket[indent-expr],
which defaults to @racket[2].
When @racket[expand-expr] produces @racket[#f] (which is the default), When @racket[expand-expr] produces @racket[#f] (which is the default),
identifiers in the typeset code are colored and linked based on identifiers in the typeset code are colored and linked based on
@ -55,10 +66,6 @@ When @racket[expand-expr] produces a procedure, it is used to
macro-expand the parsed program, and syntax coloring is based on the macro-expand the parsed program, and syntax coloring is based on the
parsed program. parsed program.
When @racket[keep-lang-line?-expr] produces a true value (the
default), the @hash-lang[] line in the input is preserved in the
typeset output, otherwise the first line is dropped.
For example, For example,
@codeblock[#:keep-lang-line? #f]|<|{ @codeblock[#:keep-lang-line? #f]|<|{