aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/control/parser/binary.lux
blob: 2912e0a526bbaeb51d4baa09b451d73ff62579f6 (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
(.module:
  [lux (#- and or nat int rev list type)
   [type (#+ :share)]
   [abstract
    [monad (#+ do)]]
   [control
    ["." try (#+ Try)]
    ["." exception (#+ exception:)]]
   [data
    ["." binary (#+ Binary)]
    [number
     ["." frac]]
    [text
     ["." encoding]
     ["%" format]]
    [collection
     ["." row (#+ Row)]]]]
  ["." // ("#@." monad)])

(type: #export Offset Nat)

(type: #export Parser
  (//.Parser [Offset Binary]))

(exception: #export (binary-was-not-fully-read {length Nat} {read Nat})
  (exception.report
   ["Binary length" (%.nat length)]
   ["Read bytes" (%.nat read)]))

(def: #export (run parser input)
  (All [a] (-> (Parser a) Binary (Try a)))
  (case (parser [0 input])
    (#try.Failure msg)
    (#try.Failure msg)
    
    (#try.Success [[end _] output])
    (let [length (binary.size input)]
      (if (n/= end length)
        (#try.Success output)
        (exception.throw ..binary-was-not-fully-read [length end])))))

(type: #export Size Nat)

(def: #export size/8 Size 1)
(def: #export size/16 Size 2)
(def: #export size/32 Size 4)
(def: #export size/64 Size 8)

(template [<name> <size> <read>]
  [(def: #export <name>
     (Parser I64)
     (function (_ [offset binary])
       (case (<read> offset binary)
         (#try.Success data)
         (#try.Success [(n/+ <size> offset) binary] data)
         
         (#try.Failure error)
         (#try.Failure error))))]

  [bits/8  ..size/8  binary.read/8]
  [bits/16 ..size/16 binary.read/16]
  [bits/32 ..size/32 binary.read/32]
  [bits/64 ..size/64 binary.read/64]
  )

(template [<name> <type>]
  [(def: #export <name> (Parser <type>) ..bits/64)]

  [nat Nat]
  [int Int]
  [rev Rev]
  )

(def: #export frac
  (Parser Frac)
  (//@map frac.from-bits ..bits/64))

(exception: #export (invalid-tag {range Nat} {byte Nat})
  (exception.report
   ["Range" (%.nat range)]
   ["Byte" (%.nat byte)]))

(def: #export (or left right)
  (All [l r] (-> (Parser l) (Parser r) (Parser (| l r))))
  (do //.monad
    [flag ..nat]
    (case flag
      0 (:: @ map (|>> #.Left) left)
      1 (:: @ map (|>> #.Right) right)
      _ (//.lift (exception.throw ..invalid-tag [2 flag])))))

(def: #export (rec body)
  (All [a] (-> (-> (Parser a) (Parser a)) (Parser a)))
  (function (_ input)
    (let [parser (body (rec body))]
      (parser input))))

(def: #export any
  (Parser Any)
  (//@wrap []))

(def: #export bit
  (Parser Bit)
  (do //.monad
    [choice (..or ..any ..any)]
    (wrap (case choice
            (#.Left _) #0
            (#.Right _) #1))))

(template [<name> <bits> <size>]
  [(def: #export <name>
     (Parser Binary)
     (do //.monad
       [size (//@map .nat <bits>)]
       (function (_ [offset binary])
         (do try.monad
           [#let [end (n/+ size offset)]
            output (binary.slice offset (.dec end) binary)]
           (wrap [[end binary] output])))))]

  [binary/8  ..bits/8  ..size/8]
  [binary/16 ..bits/16 ..size/16]
  [binary/32 ..bits/32 ..size/32]
  [binary/64 ..bits/64 ..size/64]
  )

(template [<name> <binary>]
  [(def: #export <name>
     (Parser Text)
     (do //.monad
       [utf8 <binary>]
       (//.lift (encoding.from-utf8 utf8))))]

  [utf8/8  ..binary/8]
  [utf8/16 ..binary/16]
  [utf8/32 ..binary/32]
  [utf8/64 ..binary/64]
  )

(def: #export text ..utf8/64)

(template [<name> <bits> <size>]
  [(def: #export (<name> valueP)
     (All [v] (-> (Parser v) (Parser (Row v))))
     (do //.monad
       [count (//@map .nat <bits>)]
       (loop [index 0
              output (:share [v]
                             {(Parser v)
                              valueP}
                             {(Row v)
                              row.empty})]
         (if (n/< count index)
           (do //.monad
             [value valueP]
             (recur (.inc index)
                    (row.add value output)))
           (//@wrap output)))))]

  [row/8  ..bits/8  ..size/8]
  [row/16 ..bits/16 ..size/16]
  [row/32 ..bits/32 ..size/32]
  [row/64 ..bits/64 ..size/64]
  )

(def: #export maybe
  (All [a] (-> (Parser a) (Parser (Maybe a))))
  (..or ..any))

(def: #export (list value)
  (All [a] (-> (Parser a) (Parser (List a))))
  (..rec
   (function (_ recur)
     (..or ..any
           (//.and value recur)))))

(def: #export name
  (Parser Name)
  (//.and ..text ..text))

(def: #export type
  (Parser Type)
  (..rec
   (function (_ type)
     (let [pair (//.and type type)
           indexed ..nat
           quantified (//.and (..list type) type)]
       ($_ ..or
           ## #Primitive
           (//.and ..text (..list type))
           ## #Sum
           pair
           ## #Product
           pair
           ## #Function
           pair
           ## #Parameter
           indexed
           ## #Var
           indexed
           ## #Ex
           indexed
           ## #UnivQ
           quantified
           ## #ExQ
           quantified
           ## #Apply
           pair
           ## #Named
           (//.and ..name type)
           )))))

(def: #export cursor
  (Parser Cursor)
  ($_ //.and ..text ..nat ..nat))

(def: #export code
  (Parser Code)
  (..rec
   (function (_ code)
     (let [sequence (..list code)
           code' ($_ ..or
                     ## #Bit
                     ..bit
                     ## #Nat
                     ..nat
                     ## #Int
                     ..int
                     ## #Rev
                     ..rev
                     ## #Frac
                     ..frac
                     ## #Text
                     ..text
                     ## #Identifier
                     ..name
                     ## #Tag
                     ..name
                     ## #Form
                     sequence
                     ## #Tuple
                     sequence
                     ## #Record
                     (..list (//.and code code)))]
       (//.and ..cursor code')))))