refine and document new scheme grammar forms

svn: r6551
This commit is contained in:
Matthew Flatt 2007-06-09 01:39:47 +00:00
parent e057ee856a
commit 39a98d4266
3 changed files with 68 additions and 37 deletions

View File

@ -263,7 +263,10 @@
(*defthing 'id 'result (lambda () (list desc ...)))]))
(define-syntax schemegrammar
(syntax-rules ()
[(_ id clause ...) (*schemegrammar (scheme id) (schemeblock0 clause) ...)]))
[(_ #:literals (lit ...) id clause ...) (*schemegrammar '(lit ...)
'(id clause ...)
(lambda () (list (scheme id) (schemeblock0 clause) ...)))]
[(_ id clause ...) (schemegrammar #:literals () id clause ...)]))
(define-syntax var
(syntax-rules ()
[(_ id) (*var 'id)]))
@ -511,7 +514,7 @@
append
(map (lambda (sub)
(list (list (make-flow (list (make-paragraph (list (tt 'nbsp))))))
(list (make-flow (list (apply *schemegrammar
(list (make-flow (list (apply *schemerawgrammar
(map (lambda (f) (f)) sub)))))))
sub-procs))))
(content-thunk)))))
@ -540,7 +543,7 @@
(make-paragraph (list (to-element form)))))))))
(flow-paragraphs (decode-flow (content-thunk)))))))
(define (*schemegrammar nonterm clause1 . clauses)
(define (*schemerawgrammar nonterm clause1 . clauses)
(make-table
'((valignment baseline baseline baseline baseline baseline)
(alignment left left center left left))
@ -560,6 +563,18 @@
(make-flow (list clause))))
clauses)))))
(define (*schemegrammar lits s-expr clauses-thunk)
(parameterize ([current-variable-list
(let loop ([form s-expr])
(cond
[(symbol? form) (if (memq form lits)
null
(list form))]
[(pair? form) (append (loop (car form))
(loop (cdr form)))]
[else null]))])
(apply *schemerawgrammar (clauses-thunk))))
(define (*var id)
(to-element (*var-sym id)))

View File

@ -371,9 +371,9 @@ list, and also works with multiple-valued sequences:
Ideally, a @scheme[for] iteration should run as fast as a loop that
your write by hand as a recursive-function invocation. A hand-written
loop, however, is normally specific to a particular kind of data, such
as lists. In that case, the hand-written loop uses @scheme[car] and
@scheme[cdr] directly, instead of handling all forms of sequences and
dispatching to an appropriate iterator.
as lists. In that case, the hand-written loop uses selectors like
@scheme[car] and @scheme[cdr] directly, instead of handling all forms
of sequences and dispatching to an appropriate iterator.
The @scheme[for] forms can provide the performance of hand-written
loops when enough information is apparent about the sequences to
@ -381,35 +381,38 @@ iterate. Specifically, the clause should have one of the following
@scheme[_fast-clause] forms:
@schemegrammar[
_fast-clause [_id _fast-seq]
[(_id) _fast-seq]
[(_id _id) _fast-indexed-seq]
[(_id ...) _fast-parallel-seq]
fast-clause [id fast-seq]
[(id) fast-seq]
[(id id) fast-indexed-seq]
[(id ...) fast-parallel-seq]
]
@schemegrammar[
_fast-seq (in-range _expr _expr)
(in-range _expr _expr _expr)
(in-naturals)
(in-naturals _expr)
(in-list _expr)
(in-vector _expr)
(in-string _expr)
(in-bytes _expr)
(stop-before _fast-seq _predicate-expr)
(stop-after _fast-seq _predicate-expr)
#:literals [in-range in-naturals in-list in-vector in-string in-bytes stop-before stop-after]
fast-seq (in-range expr expr)
(in-range expr expr expr)
(in-naturals)
(in-naturals expr)
(in-list expr)
(in-vector expr)
(in-string expr)
(in-bytes expr)
(stop-before fast-seq predicate-expr)
(stop-after fast-seq predicate-expr)
]
@schemegrammar[
_fast-indexed-seq (in-indexed _fast-seq)
(stop-before _fast-indexed-seq _predicate-expr)
(stop-after _fast-indexed-seq _predicate-expr)
#:literals [in-indexed stop-before stop-after]
fast-indexed-seq (in-indexed fast-seq)
(stop-before fast-indexed-seq predicate-expr)
(stop-after fast-indexed-seq predicate-expr)
]
@schemegrammar[
_fast-parallel-seq (in-parallel _fast-seq ...)
(stop-before _fast-parallel-seq _predicate-expr)
(stop-after _fast-parallel-seq _predicate-expr)
#:literals [in-parallel stop-before stop-after]
fast-parallel-seq (in-parallel fast-seq ...)
(stop-before fast-parallel-seq predicate-expr)
(stop-after fast-parallel-seq predicate-expr)
]
@examples[

View File

@ -226,6 +226,18 @@ procedure. In this description, a reference to any identifier in
The typesetting of @scheme[(id . datum)] preserves the source
layout, like @scheme[schemeblock], and unlike @scheme[defproc].}
@defform[(defform* [(id . datum) ..+] pre-flow ...)]{Like @scheme[defform],
but for multiple forms using the same @scheme[id].}
@defform[(defform/subs (id . datum)
([nonterm-id clause-datum ...+] ...)
pre-flow ...)]{
Like @scheme[defform], but including an auxiliary grammar of
non-terminals shown with the @scheme[id] form. Each
@scheme[nonterm-id] is specified as being any of the corresponding
@scheme[clause-datum]s, where the formatting of each
@scheme[clause-datum] is preserved.}
@defform[(specform (id . datum) pre-flow ...)]{Like @scheme[defform],
with without registering a definition, and with indenting on the left
for both the specification and the @scheme[pre-flow]s.}
@ -242,22 +254,23 @@ The @scheme[pre-flow]s list is parsed as a flow that documents the
procedure. In this description, a reference to any identifier in
@scheme[datum] is typeset as a sub-form non-terminal.}
@defform[(defthing id contract-expr-datum pre-flow ...)]{Like
@scheme[defproc], but for a non-procedure binding.}
@defform[(defstruct struct-name ([field-name contract-expr-datum] ...)
pre-flow ...)]{
@defform/subs[(defstruct struct-name ([field-name contract-expr-datum] ...)
pre-flow ...)
([struct-name id
(id super-id)])]{
Similar to @scheme[defform], but for a structure definition.
The @scheme[struct-name] can be either of the following:
@specsubform[id]{A structure type with no
specified supertype.}
@specsubform[(id super-id)]{A type with indicated supertype.}
Similar to @scheme[defform] or @scheme[defproc], but for a structure
definition.}
@defform/subs[(schemegrammar literals ? id clause-datum ...+)
([literals (code:line #:literals (literal-id ...))])]{
Creates a table to define the grammar of @scheme[id]. Each identifier mentioned
in a @scheme[clause-datum] is typeset as a non-terminal, except for the
identifiers listed as @scheme[literal-id]s, which are typeset as with
@scheme[scheme].
}
@; ------------------------------------------------------------------------