aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/text/escape.lux
blob: 2ce6021a6c824dafde16ecfdff409f9ec1ab0070 (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]]
   [control
    ["<>" parser]
    ["[0]" maybe]
    ["[0]" try (.only Try)]
    ["[0]" exception (.only exception)]]
   [math
    [number (.only hex)
     ["n" nat]]]
   ["[0]" meta (.only)
    ["[0]" code (.only)
     ["<[1]>" \\parser]]
    [macro
     [syntax (.only syntax)]
     ["^" pattern]]]]]
 ["[0]" // (.only Char)
  ["%" \\format (.only format)]])

(def sigil "\")

(with_template [<char> <sigil>]
  [(def <char>
     (|> <sigil> (//.char 0) maybe.trusted))]

  [sigil_char ..sigil]
  [\u_sigil "u"]
  )

(with_template [<literal> <sigil> <escaped>]
  [(def <sigil>
     (|> <literal> (//.char 0) maybe.trusted))
   
   (def <escaped>
     (format ..sigil <literal>))]

  ["0" \0_sigil escaped_\0]
  ["a" \a_sigil escaped_\a]
  ["b" \b_sigil escaped_\b]
  ["t" \t_sigil escaped_\t]
  ["n" \n_sigil escaped_\n]
  ["v" \v_sigil escaped_\v]
  ["f" \f_sigil escaped_\f]
  ["r" \r_sigil escaped_\r]
  [//.\'' \''_sigil escaped_\'']
  [..sigil \\_sigil escaped_\\]
  )

(with_template [<char> <text>]
  [(def <char>
     (|> <text> (//.char 0) maybe.trusted))]

  [\0 //.\0]
  [\a //.\a]
  [\b //.\b]
  [\t //.\t]
  [\n //.\n]
  [\v //.\v]
  [\f //.\f]
  [\r //.\r]
  [\'' //.\'']
  [\\ ..sigil]
  )

(def ascii_bottom (hex "20"))
(def ascii_top (hex "7E"))

(def .public (escapable? char)
  (-> Char Bit)
  (or (n.< ..ascii_bottom char)
      (n.> ..ascii_top char)
      (when char
        (^.with_template [<char>]
          [<char>
           true])
        ([..\0] [..\a] [..\b] [..\t]
         [..\n] [..\v] [..\f] [..\r]
         [..\''] [..\\])

        _
        false)))

(def (ascii_escaped replacement pre_offset pre_limit previous current)
  (-> Text Nat Nat Text Text [Text Text Nat])
  (let [post_offset (++ pre_offset)
        post_limit (n.- post_offset pre_limit)]
    [(format previous
             ("lux text clip" 0 pre_offset current)
             replacement)
     ("lux text clip" post_offset post_limit current)
     post_limit]))

(def (unicode_escaped char pre_offset pre_limit previous current)
  (-> Char Nat Nat Text Text [Text Text Nat])
  (let [code (at n.hex encoded char)
        replacement (format ..sigil "u"
                            (when ("lux text size" code)
                              1 (format "000" code)
                              2 (format "00" code)
                              3 (format "0" code)
                              _ code))
        post_offset (++ pre_offset)
        post_limit (n.- post_offset pre_limit)]
    [(format previous
             ("lux text clip" 0 pre_offset current)
             replacement)
     ("lux text clip" post_offset post_limit current)
     post_limit]))

(def .public (escaped text)
  (-> Text Text)
  (loop (again [offset 0
                previous ""
                current text
                limit ("lux text size" text)])
    (if (n.< limit offset)
      (when ("lux text char" offset current)
        (^.with_template [<char> <replacement>]
          [<char>
           (let [[previous' current' limit'] (ascii_escaped <replacement> offset limit previous current)]
             (again 0 previous' current' limit'))])
        ([..\0 ..escaped_\0]
         [..\a ..escaped_\a]
         [..\b ..escaped_\b]
         [..\t ..escaped_\t]
         [..\n ..escaped_\n]
         [..\v ..escaped_\v]
         [..\f ..escaped_\f]
         [..\r ..escaped_\r]
         [..\'' ..escaped_\'']
         [..\\ ..escaped_\\])

        char
        (if (or (n.< ..ascii_bottom char)
                (n.> ..ascii_top char))
          (let [[previous' current' limit'] (unicode_escaped char offset limit previous current)]
            (again 0 previous' current' limit'))
          (again (++ offset) previous current limit)))
      (format previous current))))

(exception .public (dangling_escape [text Text])
  (exception.report
   (list ["In" (%.text text)]
         ["At" (%.nat (-- (//.size text)))])))

(exception .public (invalid_escape [text Text
                                    offset Nat
                                    sigil Char])
  (exception.report
   (list ["In" (%.text text)]
         ["At" (%.nat offset)]
         ["Name" (%.text (//.of_char sigil))])))

(exception .public (invalid_unicode_escape [text Text
                                            offset Nat])
  (exception.report
   (list ["In" (%.text text)]
         ["At" (%.nat offset)])))

(def code_size
  4)

(def ascii_escape_offset
  2)

(def unicode_escape_offset
  (n.+ ..ascii_escape_offset ..code_size))

(def (ascii_un_escaped replacement offset previous current limit)
  (-> Text Nat Text Text Nat [Text Text Nat])
  (let [limit' (|> limit (n.- offset) (n.- ..ascii_escape_offset))]
    [(format previous
             ("lux text clip" 0 offset current)
             replacement)
     ("lux text clip" (n.+ ..ascii_escape_offset offset) limit' current)
     limit']))

(def (unicode_un_escaped offset previous current limit)
  (-> Nat Text Text Nat (Try [Text Text Nat]))
  (when (|> current
            ("lux text clip" (n.+ ..ascii_escape_offset offset) ..code_size)
            (at n.hex decoded))
    {try.#Success char}
    (let [limit' (|> limit (n.- offset) (n.- ..unicode_escape_offset))]
      {try.#Success [(format previous
                             ("lux text clip" 0 offset current)
                             (//.of_char char))
                     ("lux text clip" (n.+ ..unicode_escape_offset offset) limit' current)
                     limit']})
    
    {try.#Failure error}
    (exception.except ..invalid_unicode_escape [current offset])))

(def .public (un_escaped text)
  (-> Text (Try Text))
  (loop (again [offset 0
                previous ""
                current text
                limit ("lux text size" text)])
    (if (n.< limit offset)
      (when ("lux text char" offset current)
        ..sigil_char
        (let [@sigil (++ offset)]
          (if (n.< limit @sigil)
            (when ("lux text char" @sigil current)
              (^.with_template [<sigil> <un_escaped>]
                [<sigil>
                 (let [[previous' current' limit'] (..ascii_un_escaped <un_escaped> offset previous current limit)]
                   (again 0 previous' current' limit'))])
              ([..\0_sigil //.\0]
               [..\a_sigil //.\a]
               [..\b_sigil //.\b]
               [..\t_sigil //.\t]
               [..\n_sigil //.\n]
               [..\v_sigil //.\v]
               [..\f_sigil //.\f]
               [..\r_sigil //.\r]
               [..\''_sigil //.\'']
               [..\\_sigil ..sigil])

              ..\u_sigil
              (let [@unicode (n.+ code_size @sigil)]
                (if (n.< limit @unicode)
                  (do try.monad
                    [[previous' current' limit'] (..unicode_un_escaped offset previous current limit)]
                    (again 0 previous' current' limit'))
                  (exception.except ..invalid_unicode_escape [text offset])))

              invalid_sigil
              (exception.except ..invalid_escape [text offset invalid_sigil]))
            (exception.except ..dangling_escape [text])))

        _
        (again (++ offset) previous current limit))
      {try.#Success (when previous
                      "" current
                      _ (format previous current))})))

(def .public literal
  (syntax (_ [literal <code>.text])
    (when (..un_escaped literal)
      {try.#Success un_escaped}
      (in (list (code.text un_escaped)))
      
      {try.#Failure error}
      (meta.failure error))))