/racket-5-0-2-bin-i386-osx-mac-dmg/collects/syntax/scribblings/struct.scrbl

http://github.com/smorin/f4f.arc · Racket · 164 lines · 133 code · 31 blank · 0 comment · 13 complexity · 0466028b6d0b1c155c7805df1a211932 MD5 · raw file

  1. #lang scribble/doc
  2. @(require "common.ss"
  3. (for-label syntax/struct))
  4. @title[#:tag "struct"]{Expanding @scheme[define-struct]-like Forms}
  5. @defmodule[syntax/struct]
  6. @defproc[(parse-define-struct [stx syntax?] [orig-stx syntax?])
  7. (values identifier?
  8. (or/c identifier? false/c)
  9. (listof identifier?)
  10. syntax?)]{
  11. Parses @scheme[stx] as a @scheme[define-struct] form, but uses
  12. @scheme[orig-stx] to report syntax errors (under the assumption that
  13. @scheme[orig-stx] is the same as @scheme[stx], or that they at least share
  14. sub-forms). The result is four values: an identifier for the struct
  15. type name, a identifier or #f for the super-name, a list of
  16. identifiers for fields, and a syntax object for the inspector
  17. expression.}
  18. @defproc[(build-struct-names [name-id identifier?]
  19. [field-ids (listof identifier?)]
  20. [#:constructor-name ctr-name (or/c identifier? #f) #f]
  21. [omit-sel? boolean?]
  22. [omit-set? boolean?]
  23. [src-stx (or/c syntax? false/c) #f])
  24. (listof identifier?)]{
  25. Generates the names bound by @scheme[define-struct] given an
  26. identifier for the struct type name and a list of identifiers for the
  27. field names. The result is a list of identifiers:
  28. @itemize[
  29. @item{@schemeidfont{struct:}@scheme[name-id]}
  30. @item{@scheme[ctr-name], or @schemeidfont{make-}@scheme[name-id] if @racket[ctr-name] is @racket[#f]}
  31. @item{@scheme[name-id]@schemeidfont{?}}
  32. @item{@scheme[name-id]@schemeidfont{-}@scheme[_field], for each
  33. @scheme[_field] in @scheme[field-ids].}
  34. @item{@schemeidfont{set-}@scheme[name-id]@schemeidfont{-}@scheme[_field]@schemeidfont{!}
  35. (getter and setter names alternate).}
  36. @item{....}]
  37. If @scheme[omit-sel?] is true, then the selector names are omitted from the
  38. result list. If @scheme[omit-set?] is true, then the setter names are omitted
  39. from the result list.
  40. The default @scheme[src-stx] is @scheme[#f]; it is used to provide a
  41. source location to the generated identifiers.}
  42. @defproc[(build-struct-generation [name-id identifier?]
  43. [field-ids (listof identifier?)]
  44. [#:constructor-name ctr-name (or/c identifier? #f) #f]
  45. [omit-sel? boolean?]
  46. [omit-set? boolean?]
  47. [super-type any/c #f]
  48. [prop-value-list list? empty]
  49. [immutable-k-list list? empty])
  50. (listof identifier?)]{
  51. Takes the same arguments as @scheme[build-struct-names] and generates
  52. an S-expression for code using @scheme[make-struct-type] to generate
  53. the structure type and return values for the identifiers created by
  54. @scheme[build-struct-names]. The optional @scheme[super-type],
  55. @scheme[prop-value-list], and @scheme[immutable-k-list] parameters take
  56. S-expression values that are used as the corresponding arguments to
  57. @scheme[make-struct-type].}
  58. @defproc[(build-struct-generation* [all-name-ids (listof identifier?)]
  59. [name-id identifier?]
  60. [field-ids (listof identifier?)]
  61. [#:constructor-name ctr-name (or/c identifier? #f) #f]
  62. [omit-sel? boolean?]
  63. [omit-set? boolean?]
  64. [super-type any/c #f]
  65. [prop-value-list list? empty]
  66. [immutable-k-list list? empty])
  67. (listof identifier?)]{
  68. Like @scheme[build-struct-generation], but given the names produced by
  69. @scheme[build-struct-names], instead of re-generating them.}
  70. @defproc[(build-struct-expand-info [name-id identifier?]
  71. [field-ids (listof identifier?)]
  72. [#:omit-constructor? no-ctr? any/c #f]
  73. [#:constructor-name ctr-name (or/c identifier? #f) #f]
  74. [#:omit-struct-type? no-type? any/c #f]
  75. [omit-sel? boolean?]
  76. [omit-set? boolean?]
  77. [base-name (or/c identifier? boolean?)]
  78. [base-getters (listof (or/c identifier? false/c))]
  79. [base-setters (listof (or/c identifier? false/c))])
  80. any]{
  81. Takes mostly the same arguments as @scheme[build-struct-names], plus a parent
  82. identifier/@scheme[#t]/@scheme[#f] and a list of accessor and mutator
  83. identifiers (possibly ending in @scheme[#f]) for a parent type, and
  84. generates an S-expression for expansion-time code to be used in the
  85. binding for the structure name.
  86. If @racket[no-ctr?] is true, then the constructor name is omitted from
  87. the expansion-time information. Similarly, if @racket[no-type?] is
  88. true, then the structure-type name is omitted.
  89. A @scheme[#t] for the @scheme[base-name] means no super-type,
  90. @scheme[#f] means that the super-type (if any) is unknown, and an
  91. identifier indicates the super-type identifier.}
  92. @defproc[(struct-declaration-info? [v any/c]) boolean?]{
  93. Returns @scheme[#t] if @scheme[x] has the shape of expansion-time
  94. information for structure type declarations, @scheme[#f] otherwise.
  95. See @secref[#:doc refman]{structinfo}.}
  96. @defproc[(generate-struct-declaration [orig-stx syntax?]
  97. [name-id identifier?]
  98. [super-id-or-false (or/c identifier? false/c)]
  99. [field-id-list (listof identifier?)]
  100. [current-context any/c]
  101. [make-make-struct-type procedure?]
  102. [omit-sel? boolean? #f]
  103. [omit-set? boolean? #f])
  104. syntax?]{
  105. This procedure implements the core of a @scheme[define-struct]
  106. expansion.
  107. The @scheme[generate-struct-declaration] procedure is called by a
  108. macro expander to generate the expansion, where the @scheme[name-id],
  109. @scheme[super-id-or-false], and @scheme[field-id-list] arguments
  110. provide the main parameters. The @scheme[current-context] argument is
  111. normally the result of @scheme[syntax-local-context]. The
  112. @scheme[orig-stx] argument is used for syntax errors. The optional
  113. @scheme[omit-sel?] and @scheme[omit-set?] arguments default to
  114. @scheme[#f]; a @scheme[#t] value suppresses definitions of field
  115. selectors or mutators, respectively.
  116. The @scheme[make-struct-type] procedure is called to generate the
  117. expression to actually create the struct type. Its arguments are
  118. @scheme[orig-stx], @scheme[name-id-stx], @scheme[defined-name-stxes],
  119. and @scheme[super-info]. The first two are as provided originally to
  120. @scheme[generate-struct-declaration], the third is the set of names
  121. generated by @scheme[build-struct-names], and the last is super-struct
  122. info obtained by resolving @scheme[super-id-or-false] when it is not
  123. @scheme[#f], @scheme[#f] otherwise.
  124. The result should be an expression whose values are the same as the
  125. result of @scheme[make-struct-type]. Thus, the following is a basic
  126. @scheme[make-make-struct-type]:
  127. @SCHEMEBLOCK[
  128. (lambda (orig-stx name-stx defined-name-stxes super-info)
  129. #`(make-struct-type '#,name-stx
  130. #,(and super-info (list-ref super-info 0))
  131. #,(/ (- (length defined-name-stxes) 3) 2)
  132. 0 #f))]
  133. but an actual @scheme[make-make-struct-type] will likely do more.}