-
Notifications
You must be signed in to change notification settings - Fork 7
/
core.rkt
167 lines (149 loc) · 5.75 KB
/
core.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
;; This file defines the core of code formatter
#lang racket/base
(provide pretty-doc
pretty-comment
extract
match/extract
pretty
doc
define-pretty
pretty-node
try-indent)
(require racket/match
racket/string
racket/list
racket/stxparam
syntax/parse/define
(except-in pretty-expressive flatten)
"common.rkt"
"params.rkt"
"private/memoize.rkt"
(for-syntax racket/base syntax/parse/lib/function-header))
(define (extract xs extract-configs)
(let loop ([xs xs] [extract-configs extract-configs] [fits '()] [unfits '()])
(match extract-configs
['() (list (reverse fits) (filter-not newl? (reverse unfits)) xs)]
[(cons extract-config extract-configs)
(match xs
['() #f]
[(cons x xs)
(cond
[(visible? x)
(cond
[(and extract-config (commentable-inline-comment x))
(loop xs
extract-configs
(cons (strip-comment x) fits)
(cons (line-comment (commentable-inline-comment x)) unfits))]
[else (loop xs extract-configs (cons x fits) unfits)])]
[else (loop xs (cons extract-config extract-configs) fits (cons x unfits))])])])))
(define (pretty-comment comment d)
(if comment (full (<s> d (text comment))) d))
(define (big-text s)
(reset (u-concat (add-between (map text (string-split s "\n")) hard-nl))))
(define (pretty-doc xs hook)
(define/memoize (loop d)
(match d
[(newl n) (full (v-concat (make-list n empty-doc)))]
[(full-atom _ content 'string)
(full (big-text content))]
[(atom comment content type)
(pretty-comment
comment
(match type
['block-comment (big-text content)]
[_ (text content)]))]
[(line-comment comment) (full (text comment))]
[(node _ _ _ _ xs)
(match (extract xs (list #f))
[#f ((hook #f) d)]
[(list (list (atom _ content 'symbol)) _ _) ((hook content) d)]
[_ ((hook #f) d)])]
[(wrapper comment tok content)
(pretty-comment comment (<+> (text tok) (loop content)))]
[(sexp-comment comment style tok xs)
(pretty-comment comment
(match style
['newline (apply <$> (text tok) (map loop xs))]
['any
(define :x (loop (first xs)))
(alt (<$> (text tok) :x) (<+> (text tok) :x))]
['disappeared (loop (first xs))]))]))
(set-box! current-pretty loop)
(begin0 (v-concat (map loop xs))
(set-box! current-pretty #f)))
(define (pretty-node* n d #:node [the-node n] #:unfits [unfits '()] #:adjust [adjust '("(" ")")])
(match-define (node comment opener closer prefix _) the-node)
(define doc
(pretty-comment comment
(<+> (text
(match (current-adjust-paren-shape)
[#t
#:when adjust
(first adjust)]
[(or #t #f) opener]
[(cons opener _) opener]))
d
(text
(match (current-adjust-paren-shape)
[#t
#:when adjust
(second adjust)]
[(or #t #f) closer]
[(cons _ closer) closer])))))
(define doc*
(for/fold ([doc doc]) ([prefix (in-list (reverse prefix))])
(match prefix
[(cons 'breakable tk)
(alt (<$> (text tk) doc) (<+> (text tk) doc))]
[(cons 'unbreakable tk)
(<+> (text tk) doc)])))
(match unfits
['() doc*]
[_ (<$> (v-concat (map (unbox current-pretty) unfits)) doc*)]))
(define current-pretty (box #f))
(define-syntax-parameter pretty
(λ (stx) (raise-syntax-error #f "use of pretty outside its context" stx)))
(define-syntax-parameter doc (λ (stx) (raise-syntax-error #f "use of doc outside its context" stx)))
(begin-for-syntax
(define-syntax-class header
(pattern name:id)
(pattern h:function-header
#:with name #'h.name)))
(define-syntax-parse-rule (define-pretty head:header
#:type p?
{~seq #:default [from:id to]} ...
{~seq #:let [a:id b]} ...
body ...+)
#:with ooo (quote-syntax ...)
(define (head d)
(let ([pretty-proc (unbox current-pretty)])
(cond
[(p? d)
(syntax-parameterize ([pretty (make-rename-transformer #'pretty-proc)]
[doc (make-rename-transformer #'d)])
(let* ([from (or from to)] ... [a b] ...)
body ...))]
[else (raise-argument-error 'head.name (symbol->string 'p?) d)]))))
(define-syntax-parse-rule (pretty-node args ...)
(pretty-node* doc args ...))
(define/memoize (spaces n)
(text (make-string n #\space)))
(define (require-newline? d)
(or (and (commentable? d) (commentable-inline-comment d)) (line-comment? d) (newl? d) (full-atom? d)))
(define (try-indent d #:n [n 1] #:because-of xs)
(match xs
['() d]
[_ (if (require-newline? (last xs)) (<$> d (spaces n)) d)]))
(define-syntax-parser match/extract
[(_ xs #:as unfits tail [([pat req-status] ...) body ...+] . rst)
#'(let ([-xs xs])
(match (extract -xs (list req-status ...))
[(list (list pat ...) unfits tail)
body ...]
[_
(match/extract -xs #:as unfits tail
. rst)]))]
[(_ xs #:as unfits tail [#:else body ...+])
#'(let ()
body ...)])