Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
226 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#lang reprovide | ||
rebellion/private/custom-write |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#lang reprovide | ||
rebellion/private/custom-write-struct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#lang reprovide | ||
rebellion/private/custom-write-tuple |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#lang racket/base | ||
|
||
(require racket/contract/base) | ||
|
||
(provide | ||
(contract-out | ||
[make-struct-constructor-style-custom-write | ||
(-> struct-descriptor? custom-write-function/c)])) | ||
|
||
(require racket/struct | ||
rebellion/custom-write | ||
rebellion/struct-descriptor) | ||
|
||
;@------------------------------------------------------------------------------ | ||
|
||
(define (make-struct-constructor-style-custom-write descriptor) | ||
(define type-name (struct-descriptor-name descriptor)) | ||
(define size | ||
(+ (struct-descriptor-mutable-fields descriptor) | ||
(struct-descriptor-immutable-fields descriptor) | ||
(struct-descriptor-auto-fields descriptor))) | ||
(define accessor (struct-descriptor-accessor descriptor)) | ||
(make-constructor-style-printer | ||
(λ (this) type-name) | ||
(λ (this) (build-list size (λ (pos) (accessor this pos)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#lang racket/base | ||
|
||
(require racket/contract/base) | ||
|
||
(provide | ||
(contract-out | ||
[make-tuple-constructor-style-custom-write | ||
(-> tuple-descriptor? custom-write-function/c)])) | ||
|
||
(require racket/struct | ||
rebellion/custom-write | ||
rebellion/tuple-type) | ||
|
||
;@------------------------------------------------------------------------------ | ||
|
||
(define (make-tuple-constructor-style-custom-write descriptor) | ||
(define type (tuple-descriptor-type descriptor)) | ||
(define type-name (tuple-type-name type)) | ||
(define size (tuple-type-size type)) | ||
(define accessor (tuple-descriptor-accessor descriptor)) | ||
(make-constructor-style-printer | ||
(λ (_) type-name) | ||
(λ (this) (build-list size (λ (pos) (accessor this pos)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#lang racket/base | ||
|
||
(require racket/contract/base) | ||
|
||
(provide | ||
(contract-out | ||
[custom-write-mode/c flat-contract?] | ||
[custom-write-function/c chaperone-contract?] | ||
[make-named-object-custom-write | ||
(->* (symbol?) (#:name-getter (-> any/c (or/c symbol? #f))) | ||
custom-write-function/c)] | ||
[make-constant-custom-write (-> symbol? custom-write-function/c)])) | ||
|
||
;@------------------------------------------------------------------------------ | ||
|
||
(define custom-write-mode/c (or/c boolean? 0 1)) | ||
|
||
(define custom-write-function/c | ||
(-> any/c output-port? custom-write-mode/c void?)) | ||
|
||
(define (make-named-object-custom-write type-name | ||
#:name-getter [get-name object-name]) | ||
(define type-part (string-append "#<" (symbol->string type-name))) | ||
(λ (this out mode) | ||
(parameterize ([current-output-port out]) | ||
(write-string type-part) | ||
(define name (get-name this)) | ||
(when name | ||
(write-string ":") | ||
(write-string (symbol->string name))) | ||
(write-string ">")) | ||
(void))) | ||
|
||
(define (make-constant-custom-write name) | ||
(define str (string-append "#<" (symbol->string name) ">")) | ||
(λ (this out mode) | ||
(parameterize ([current-output-port out]) | ||
(write-string str)) | ||
(void))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#lang scribble/manual | ||
|
||
@(require (for-label racket/base | ||
racket/contract/base | ||
racket/pretty | ||
racket/struct | ||
rebellion/custom-write | ||
rebellion/custom-write/struct | ||
rebellion/custom-write/tuple | ||
rebellion/struct-descriptor | ||
rebellion/tuple-type | ||
rebellion/tuple-type-definition) | ||
(submod rebellion/private/scribble-evaluator-factory doc) | ||
scribble/example) | ||
|
||
@(define make-evaluator | ||
(make-module-sharing-evaluator-factory | ||
#:public (list 'racket/pretty | ||
'rebellion/custom-write | ||
'rebellion/custom-write/struct | ||
'rebellion/custom-write/tuple | ||
'rebellion/struct-descriptor | ||
'rebellion/tuple-type | ||
'rebellion/tuple-type-definition) | ||
#:private (list 'racket/base))) | ||
|
||
@title{Custom Write Implementations} | ||
@defmodule[rebellion/custom-write] | ||
|
||
A @deftech{custom write implementation} is a function that prints values and is | ||
suitable for use with @racket[prop:custom-write]. Custom write implementations | ||
must satisfy the @racket[custom-write-function/c] contract. | ||
|
||
@defthing[custom-write-function/c chaperone-contract? | ||
#:value (-> any/c output-port? custom-write-mode/c void?)]{ | ||
A @tech{contract} describing functions suitable for use with @racket[ | ||
prop:custom-write].} | ||
|
||
@defthing[custom-write-mode/c flat-contract? | ||
#:value (or/c boolean? 0 1)]{ | ||
A @tech{contract} describing the @racket[_mode] argument to functions matching | ||
@racket[custom-write-function/c]. See @racket[gen:custom-write] for details.} | ||
|
||
@defproc[(make-named-object-custom-write | ||
[type-name symbol?] | ||
[#:name-getter get-name (-> any/c (or/c symbol? #f)) object-name]) | ||
custom-write-function/c]{ | ||
Constructs a @tech{custom write implementation} that prints values as opaque, | ||
unreadable, named objects, similar to the way functions are printed. | ||
|
||
@(examples | ||
#:eval (make-evaluator) #:once | ||
(struct person (name) | ||
#:property prop:object-name (struct-field-index name) | ||
#:property prop:custom-write (make-named-object-custom-write 'person)) | ||
|
||
(person 'alyssa) | ||
(person 'jared) | ||
(person #f))} | ||
|
||
@defproc[(make-constant-custom-write [name symbol?]) custom-write-function/c]{ | ||
Constructs a @tech{custom write implementation} that prints all values as the | ||
same opaque constant value named @racket[name], similar to the way @racket[eof] | ||
prints. | ||
|
||
@(examples | ||
#:eval (make-evaluator) #:once | ||
(struct widget () | ||
#:property prop:custom-write (make-constant-custom-write 'widget)) | ||
|
||
(widget))} | ||
|
||
@section{Struct Custom Write Implementations} | ||
@defmodule[rebellion/custom-write/struct] | ||
|
||
@defproc[(make-struct-constructor-style-custom-write | ||
[descriptor struct-descriptor?]) | ||
custom-write-function/c]{ | ||
Constructs a @tech{custom write implementation} that prints instances of the | ||
structure type described by @racket[descriptor] in a manner similar to the way | ||
that @racket[make-constructor-style-printer] prints values. | ||
|
||
@(examples | ||
#:eval (make-evaluator) #:once | ||
(define (make-props descriptor) | ||
(define custom-write | ||
(make-struct-constructor-style-custom-write descriptor)) | ||
(list (cons prop:custom-write custom-write))) | ||
|
||
(define point-descriptor | ||
(make-struct-type/descriptor #:name 'point | ||
#:immutable-fields 2 | ||
#:property-maker make-props)) | ||
(define point (struct-descriptor-constructor point-descriptor)) | ||
|
||
(point 1 2) | ||
(parameterize ([pretty-print-columns 10]) | ||
(pretty-print (point 100000000000000 200000000000000))))} | ||
|
||
@section{Tuple Custom Write Implementations} | ||
@defmodule[rebellion/custom-write/tuple] | ||
|
||
@defproc[(make-tuple-constructor-style-custom-write | ||
[descriptor tuple-descriptor?]) | ||
custom-write-function/c]{ | ||
Constructs a @tech{custom write implementation} that prints instances of the | ||
@tech{tuple type} described by @racket[descriptor] in a manner similar to the | ||
way that @racket[make-constructor-style-printer] prints values. | ||
|
||
@(examples | ||
#:eval (make-evaluator) #:once | ||
(define-tuple-type point (x y) | ||
#:property-maker | ||
(λ (descriptor) | ||
(define custom-write | ||
(make-tuple-constructor-style-custom-write descriptor)) | ||
(list (cons prop:custom-write custom-write)))) | ||
|
||
(point 1 2) | ||
(parameterize ([pretty-print-columns 10]) | ||
(pretty-print (point 100000000000000 200000000000000))))} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.