-
Notifications
You must be signed in to change notification settings - Fork 7
/
realign.rkt
75 lines (71 loc) · 3.24 KB
/
realign.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
;; A tree fix up
#lang racket/base
(provide realign)
(require racket/match
racket/function
racket/list
syntax/readerr
"common.rkt"
"read.rkt")
(define (realign xs)
(let loop ([xs xs] [just-read-sexp-comment? #f])
(match xs
['() '()]
[(cons (? newl? d) xs) (cons d (loop xs #f))]
[(cons (? atom? d) xs) (cons d (loop xs #f))]
[(cons (? line-comment? d) xs) (cons d (loop xs #f))]
[(cons (? bare-sexp-comment?) xs)
(define-values (invisibles tail) (splitf-at (loop (dropf xs newl?) #t) (negate visible?)))
(match tail
['() (raise-read-error "sexp-comment without content" #f #f #f #f #f)]
[(cons visible xs)
(match invisibles
[(cons (sexp-comment comment style? tok content) invisibles)
;; style is NOT 'disappeared because if that's the case,
;; the current fragment wouldn't be bare-sexp-comment
(append (list (sexp-comment comment style? (string-append "#;" tok) content))
invisibles
(cons visible xs))]
['()
#:when (not just-read-sexp-comment?)
(match visible
[(node comment opener closer prefix content)
(cons
(sexp-comment comment
'disappeared
""
(list (struct-copy node
visible
[inline-comment #:parent commentable #f]
[prefix (cons (cons 'breakable "#;") prefix)])))
xs)]
[_ (cons (sexp-comment (commentable-inline-comment visible)
'any
"#;"
(list (strip-comment visible)))
xs)])]
[_ (cons (sexp-comment (commentable-inline-comment visible)
'newline
"#;"
(append invisibles (list (strip-comment visible))))
xs)])])]
[(cons (bare-prefix tk) xs)
(define-values (invisibles tail) (splitf-at (loop (dropf xs newl?) #f) (negate visible?)))
(match tail
['() (raise-read-error "quote without content" #f #f #f #f #f)]
[(cons visible xs)
(append
invisibles
(cons
(match visible
;; don't create a new wrapper, just transfer content
[(wrapper _ tk* _) (struct-copy wrapper visible [tk (string-append tk tk*)])]
[(node _ _ _ prefix _)
(match tk
[(app string-length 1)
(struct-copy node visible [prefix (cons (cons 'unbreakable tk) prefix)])]
[_ (struct-copy node visible [prefix (cons (cons 'breakable tk) prefix)])])]
[_ (wrapper (commentable-inline-comment visible) tk (strip-comment visible))])
xs))])]
[(cons (node comment opener closer prefix xs*) xs)
(cons (node comment opener closer prefix (loop xs* #f)) (loop xs #f))])))