-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexamples.rkt
98 lines (89 loc) · 2.7 KB
/
examples.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#lang racket/base
(require pretty-expressive
(prefix-in r: racket/base))
(define d-traditional
(<> (text "function append(first,second,third){")
(nest 4
(let ([f (text "first +")]
[s (text "second +")]
[t (text "third")])
(<> nl (text "return ")
(group (nest 4 (<> f nl s nl t))))))
nl (text "}")))
(define d-arbitrary
(<$> (text "function append(first,second,third){")
(let ([f (text "first +")]
[s (text "second +")]
[t (text "third")]
[sp (text " ")]
[indentation (text " ")]
[ret (text "return ")])
(<+> indentation
(alt (<$> (<+> ret (text "("))
(<+> indentation (<$> f s t))
(text ")"))
(<+> ret f sp s sp t))))
(text "}")))
(define d-pretty-expressive
(<> (text "function append(first,second,third){")
(nest 4
(let ([f (text "first +")]
[s (text "second +")]
[t (text "third")])
(<> nl (text "return ")
(alt (<> (text "(")
(nest 4 (<> nl f nl s nl t))
nl
(text ")"))
(let ([sp (text " ")])
(<> f sp s sp t))))))
nl
(text "}")))
(module+ test
(require rackunit)
(define horz-layout
#<<EOF
function append(first,second,third){
return first + second + third
}
EOF
)
(define vert-layout/no-paren
#<<EOF
function append(first,second,third){
return first +
second +
third
}
EOF
)
(define vert-layout/paren
#<<EOF
function append(first,second,third){
return (
first +
second +
third
)
}
EOF
)
(check-equal? (pretty-format d-traditional #:page-width 22) vert-layout/no-paren)
(check-equal? (pretty-format d-arbitrary #:page-width 22) vert-layout/paren)
(check-equal? (pretty-format d-pretty-expressive #:page-width 22) vert-layout/paren)
(check-equal? (pretty-format d-traditional #:page-width 36) horz-layout)
(check-equal? (pretty-format d-arbitrary #:page-width 36) horz-layout)
(check-equal? (pretty-format d-pretty-expressive #:page-width 36) horz-layout))
(module+ main
(pretty-print d-traditional #:page-width 22)
(r:newline)
(pretty-print d-arbitrary #:page-width 22)
(r:newline)
(pretty-print d-pretty-expressive #:page-width 22)
(r:newline)
(pretty-print d-traditional #:page-width 36)
(r:newline)
(pretty-print d-arbitrary #:page-width 36)
(r:newline)
(pretty-print d-pretty-expressive #:page-width 36)
(r:newline))