-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
struct.rkt
174 lines (134 loc) · 4.85 KB
/
struct.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
168
169
170
171
172
173
174
#lang racket/base
;; internal structs ;;
(require racket/contract
racket/match
(for-syntax racket/base
syntax/parse))
(provide
(contract-out
[struct Decl ([filename any/c]
[id any/c]
[left exact-nonnegative-integer?]
[right exact-nonnegative-integer?])]))
(struct Decl (filename id left right) #:transparent)
;; optional arguments
(define undef-object (gensym 'undef))
(define (undef? x)
(eq? x undef-object))
(define (undef/c pred?)
(λ (x)
(or/c (undef? x) (pred? x))))
(provide undef?
undef/c
undef-object)
;; request error
(define request-err-object (gensym 'request-error))
(define (request-err? x)
(eq? x request-err-object))
(provide request-err-object
request-err?)
;; uinteger ;;
(define uinteger-upper-limit (sub1 (expt 2 31)))
(define (uinteger? x)
(and (integer? x) (<= 0 x uinteger-upper-limit)))
(provide uinteger?)
;; Position ;;
(struct Position
(line character))
(define/contract (make-Position #:line line #:character char)
(-> #:line uinteger? #:character uinteger? Position?)
(Position line char))
(define/contract (Position->hash pos)
(-> Position? hash?)
(hash 'line (Position-line pos)
'character (Position-character pos)))
(provide Position
make-Position
Position->hash)
;; Range ;;
;; `*Range` is a workaround for existing `Range` json expander currently
(struct *Range
(start end))
(define/contract (make-Range #:start start #:end end)
(-> #:start Position? #:end Position? *Range?)
(*Range start end))
(define/contract (Range->hash range)
(-> *Range? hash?)
(hash 'start (Position->hash (*Range-start range))
'end (Position->hash (*Range-end range))))
(provide *Range
make-Range
Range->hash)
(struct FormattingOptions
(tab-size
insert-spaces
trim-trailing-whitespace
insert-final-newline
trim-final-newlines
key))
(define/contract (make-FormattingOptions #:tab-size tab-size
#:insert-spaces insert-spaces
#:trim-trailing-whitespace [trim-trailing-whitespace undef-object]
#:insert-final-newline [insert-final-newline undef-object]
#:trim-final-newlines [trim-final-newlines undef-object]
#:key [key undef-object])
(-> #:tab-size uinteger?
#:insert-spaces boolean?
#:trim-trailing-whitespace (undef/c boolean?)
#:insert-final-newline (undef/c boolean?)
#:trim-final-newlines (undef/c boolean?)
#:key (undef/c hash?)
FormattingOptions?)
(FormattingOptions tab-size
insert-spaces
trim-trailing-whitespace
insert-final-newline
trim-final-newlines
key))
(define (jsexpr->FormattingOptions jsexpr)
(with-handlers ([exn:fail? (λ (_) request-err-object)])
(make-FormattingOptions #:tab-size (hash-ref jsexpr 'tabSize)
#:insert-spaces (hash-ref jsexpr 'insertSpaces)
#:trim-trailing-whitespace (hash-ref jsexpr 'trimTrailingWhitespace undef-object)
#:insert-final-newline (hash-ref jsexpr 'insertFinalNewline undef-object)
#:trim-final-newlines (hash-ref jsexpr 'trimFinalNewlines undef-object)
#:key (hash-ref jsexpr 'key undef-object))))
(struct SemanticToken
(start end type modifiers)
#:transparent)
;; The order of this list is irrelevant.
;; The client receives this list from server ability declaration during
;; initialize handshake then use it to decode server semantic tokens messages.
;; Different order produces different encoding results of semantic tokens,
;; but does not affect client and server behavior.
;; To change the order, simply change it here, don't need to change other code.
(define *semantic-token-types*
'(variable
function
string
number
regexp))
;; The order of this list is irrelevant, similar to *semantic-token-types*.
(define *semantic-token-modifiers*
'(definition))
;; usage:
;; (jsexpr? jsexpr) ;; #t
;; (match jsexpr
;; [(as-FormattingOptions opts)
;; (FormattingOptions? opts) ;; #t
;; ])
;; It's a convenient macro to verify jsexpr and convert it
;; to struct.
(define-match-expander as-FormattingOptions
(lambda (stx)
(syntax-parse stx
[(_ name)
#'(and (? hash?)
(app jsexpr->FormattingOptions (and name (not (? request-err?)))))])))
(provide FormattingOptions
FormattingOptions-tab-size
FormattingOptions-trim-trailing-whitespace
as-FormattingOptions
(struct-out SemanticToken)
*semantic-token-types*
*semantic-token-modifiers*)